summaryrefslogtreecommitdiffstats
path: root/nat/nat2vpp/src
diff options
context:
space:
mode:
Diffstat (limited to 'nat/nat2vpp/src')
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/NatModule.java77
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/init/NatInitializer.java63
-rwxr-xr-xnat/nat2vpp/src/main/java/io/fd/honeycomb/nat/jvpp/JVppSnatProvider.java60
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/MappingEntryCustomizer.java151
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatInstanceCustomizer.java84
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatReaderFactory.java75
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/IfcNatReaderFactory.java55
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceInboundNatCustomizer.java55
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java54
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/util/MappingEntryContext.java210
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/MappingEntryCustomizer.java182
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatInstaceCustomizer.java53
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatWriterFactory.java66
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/AbstractInterfaceNatCustomizer.java105
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/IfcNatWriterFactory.java62
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceInboundNatCustomizer.java44
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceOutboundNatCustomizer.java44
-rw-r--r--nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java123
-rw-r--r--nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/util/MappingEntryContextTest.java198
-rw-r--r--nat/nat2vpp/src/test/resources/nat.json3
20 files changed, 1764 insertions, 0 deletions
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/NatModule.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/NatModule.java
new file mode 100644
index 000000000..b33f900e5
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/NatModule.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.inject.AbstractModule;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+import com.google.inject.multibindings.Multibinder;
+import io.fd.honeycomb.data.init.DataTreeInitializer;
+import io.fd.honeycomb.nat.init.NatInitializer;
+import io.fd.honeycomb.nat.jvpp.JVppSnatProvider;
+import io.fd.honeycomb.nat.read.NatReaderFactory;
+import io.fd.honeycomb.nat.read.ifc.IfcNatReaderFactory;
+import io.fd.honeycomb.nat.util.MappingEntryContext;
+import io.fd.honeycomb.nat.write.NatWriterFactory;
+import io.fd.honeycomb.nat.write.ifc.IfcNatWriterFactory;
+import io.fd.honeycomb.translate.read.ReaderFactory;
+import io.fd.honeycomb.translate.write.WriterFactory;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Module class instantiating nat plugin components.
+ */
+public final class NatModule extends AbstractModule {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NatModule.class);
+ private final Class<? extends Provider<FutureJVppSnatFacade>> jvppSnatProviderClass;
+
+ public NatModule() {
+ this(JVppSnatProvider.class);
+ }
+
+ @VisibleForTesting
+ NatModule(Class<? extends Provider<FutureJVppSnatFacade>> jvppSnatProvider) {
+ this.jvppSnatProviderClass = jvppSnatProvider;
+ }
+
+ @Override
+ protected void configure() {
+ // Mapping entry context util
+ bind(MappingEntryContext.class).toInstance(new MappingEntryContext());
+
+ LOG.debug("Installing NAT module");
+
+ // Bind to Plugin's JVPP
+ bind(FutureJVppSnatFacade.class).toProvider(jvppSnatProviderClass).in(Singleton.class);
+
+ final Multibinder<ReaderFactory> readBinder = Multibinder.newSetBinder(binder(), ReaderFactory.class);
+ readBinder.addBinding().to(IfcNatReaderFactory.class).in(Singleton.class);
+ readBinder.addBinding().to(NatReaderFactory.class).in(Singleton.class);
+
+ final Multibinder<WriterFactory> writeBinder = Multibinder.newSetBinder(binder(), WriterFactory.class);
+ writeBinder.addBinding().to(IfcNatWriterFactory.class).in(Singleton.class);
+ writeBinder.addBinding().to(NatWriterFactory.class).in(Singleton.class);
+
+ Multibinder.newSetBinder(binder(), DataTreeInitializer.class)
+ .addBinding().to(NatInitializer.class).in(Singleton.class);
+ LOG.info("Module NAT successfully configured");
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/init/NatInitializer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/init/NatInitializer.java
new file mode 100644
index 000000000..37c456133
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/init/NatInitializer.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.init;
+
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+import io.fd.honeycomb.data.init.AbstractDataTreeConverter;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatConfig;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatConfigBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.NatInstancesBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.MappingTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntryBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Initialize nat-config from nat-state.
+ */
+public final class NatInitializer extends AbstractDataTreeConverter<NatState, NatConfig> {
+
+ @Inject
+ public NatInitializer(@Named("honeycomb-initializer") @Nonnull final DataBroker bindingDataBroker) {
+ super(bindingDataBroker, InstanceIdentifier.create(NatState.class), InstanceIdentifier.create(NatConfig.class));
+ }
+
+ @Override
+ public NatConfig convert(final NatState operationalData) {
+ return new NatConfigBuilder()
+ .setNatInstances(new NatInstancesBuilder()
+ .setNatInstance(operationalData.getNatInstances().getNatInstance().stream()
+ .map(operNatInstance -> new NatInstanceBuilder()
+ .setId(operNatInstance.getId())
+ .setMappingTable(new MappingTableBuilder()
+ .setMappingEntry(
+ operNatInstance.getMappingTable().getMappingEntry().stream()
+ .map(operEntry -> new MappingEntryBuilder(operEntry).build())
+ .collect(Collectors.toList()))
+ .build())
+ .build())
+ .collect(Collectors.toList()))
+ .build())
+ .build();
+ // TODO implement initialization for nat inbound/outbound NAT feature after VPP-459
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/jvpp/JVppSnatProvider.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/jvpp/JVppSnatProvider.java
new file mode 100755
index 000000000..b83665679
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/jvpp/JVppSnatProvider.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.jvpp;
+
+import com.google.inject.Inject;
+import io.fd.honeycomb.infra.distro.ProviderTrait;
+import io.fd.vpp.jvpp.JVppRegistry;
+import io.fd.vpp.jvpp.snat.JVppSnatImpl;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import java.io.IOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides future API for jvpp-nsh plugin. Must be a singleton due to shutdown hook usage.
+ * Registers shutdown hook to free plugin's resources on shutdown.
+ */
+public final class JVppSnatProvider extends ProviderTrait<FutureJVppSnatFacade> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JVppSnatProvider.class);
+
+ @Inject
+ private JVppRegistry registry;
+
+ @Override
+ protected FutureJVppSnatFacade create() {
+ try {
+ final JVppSnatImpl jvppSnat = new JVppSnatImpl();
+ // Free jvpp-nsh plugin's resources on shutdown
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ LOG.info("Unloading jvpp-snat plugin");
+ jvppSnat.close();
+ LOG.info("Successfully unloaded jvpp-snat plugin");
+ }
+ });
+
+ LOG.info("Successfully loaded jvpp-snat plugin");
+ return new FutureJVppSnatFacade(registry, jvppSnat);
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to open VPP management connection", e);
+ }
+ }
+}
+
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/MappingEntryCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/MappingEntryCustomizer.java
new file mode 100644
index 000000000..469d361ee
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/MappingEntryCustomizer.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.read;
+
+import io.fd.honeycomb.nat.util.MappingEntryContext;
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
+import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
+import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
+import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
+import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetails;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetailsReplyDump;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDump;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import java.util.List;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.mapping.entry.ExternalSrcPortBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.mapping.entry.InternalSrcPortBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.MappingTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.mapping.table.MappingEntry;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.mapping.table.MappingEntryBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.mapping.table.MappingEntryKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.port.number.port.type.SinglePortNumberBuilder;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MappingEntryCustomizer implements Ipv4Translator,
+ ListReaderCustomizer<MappingEntry, MappingEntryKey, MappingEntryBuilder> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MappingEntryCustomizer.class);
+
+ private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager;
+ private final MappingEntryContext mappingEntryContext;
+
+ public MappingEntryCustomizer(final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager,
+ final MappingEntryContext mappingEntryContext) {
+ this.dumpCacheManager = dumpCacheManager;
+ this.mappingEntryContext = mappingEntryContext;
+ }
+
+ @Nonnull
+ @Override
+ public MappingEntryBuilder getBuilder(@Nonnull final InstanceIdentifier<MappingEntry> id) {
+ return new MappingEntryBuilder();
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<MappingEntry> id,
+ @Nonnull final MappingEntryBuilder builder, @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ LOG.trace("Reading current attributes for mapping-entry: {}", id);
+
+ final int idx = id.firstKeyOf(MappingEntry.class).getIndex().intValue();
+ final int natInstanceId = id.firstKeyOf(NatInstance.class).getId().intValue();
+ final List<SnatStaticMappingDetails> details =
+ dumpCacheManager.getDump(id, getClass().getName(), ctx.getModificationCache(), null)
+ .or(new SnatStaticMappingDetailsReplyDump()).snatStaticMappingDetails;
+ final SnatStaticMappingDetails snatStaticMappingDetails =
+ mappingEntryContext.findDetails(details, natInstanceId, idx, ctx.getMappingContext());
+
+ builder.setIndex((long) idx);
+ builder.setType(
+ org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.MappingEntry.Type.Static);
+ // Snat only supports ipv4 for now
+ builder.setExternalSrcAddress(arrayToIpv4AddressNoZoneReversed(snatStaticMappingDetails.externalIpAddress));
+ builder.setInternalSrcAddress(
+ new IpAddress(arrayToIpv4AddressNoZoneReversed(snatStaticMappingDetails.localIpAddress)));
+
+ if (snatStaticMappingDetails.addrOnly == 0) {
+ builder.setExternalSrcPort(new ExternalSrcPortBuilder()
+ .setPortType(new SinglePortNumberBuilder().setSinglePortNumber(new PortNumber(
+ (int) snatStaticMappingDetails.externalPort))
+ .build())
+ .build());
+ builder.setInternalSrcPort(new InternalSrcPortBuilder()
+ .setPortType(new SinglePortNumberBuilder().setSinglePortNumber(new PortNumber(
+ (int) snatStaticMappingDetails.localPort))
+ .build())
+ .build());
+ }
+
+ LOG.trace("Mapping-entry read as: {}", builder);
+ }
+
+ @Nonnull
+ @Override
+ public List<MappingEntryKey> getAllIds(@Nonnull final InstanceIdentifier<MappingEntry> id,
+ @Nonnull final ReadContext context) throws ReadFailedException {
+ final Long natInstanceId = id.firstKeyOf(NatInstance.class).getId();
+ LOG.trace("Listing IDs for all mapping-entries within nat-instance(vrf):{}", natInstanceId);
+
+ final List<MappingEntryKey> entryKeys =
+ dumpCacheManager.getDump(id, getClass().getName(), context.getModificationCache(), null)
+ .or(new SnatStaticMappingDetailsReplyDump()).snatStaticMappingDetails.stream()
+ .filter(detail -> natInstanceId == detail.vrfId)
+ .map(detail -> mappingEntryContext
+ .getStoredOrArtificialIndex(natInstanceId, detail, context.getMappingContext()))
+ .map(MappingEntryKey::new)
+ .collect(Collectors.toList());
+ LOG.debug("List of mapping-entry keys within nat-instance(vrf):{} : {}", natInstanceId, entryKeys);
+
+ return entryKeys;
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> builder,
+ @Nonnull final List<MappingEntry> readData) {
+ ((MappingTableBuilder) builder).setMappingEntry(readData);
+ }
+
+ static final class MappingEntryDumpExecutor
+ implements EntityDumpExecutor<SnatStaticMappingDetailsReplyDump, Void>, JvppReplyConsumer {
+
+ private final FutureJVppSnatFacade jvppSnat;
+
+ MappingEntryDumpExecutor(final FutureJVppSnatFacade jvppSnat) {
+ this.jvppSnat = jvppSnat;
+ }
+
+ @Nonnull
+ @Override
+ public SnatStaticMappingDetailsReplyDump executeDump(final InstanceIdentifier<?> identifier, final Void params)
+ throws ReadFailedException {
+ return getReplyForRead(jvppSnat.snatStaticMappingDump(new SnatStaticMappingDump()).toCompletableFuture(),
+ identifier);
+ }
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatInstanceCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatInstanceCustomizer.java
new file mode 100644
index 000000000..cb639b962
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatInstanceCustomizer.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.read;
+
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
+import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetailsReplyDump;
+import java.util.List;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.NatInstancesBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstanceKey;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Nat instance ID is mapped to VRF-ID in VPP.
+ */
+final class NatInstanceCustomizer implements ListReaderCustomizer<NatInstance, NatInstanceKey, NatInstanceBuilder> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NatInstanceCustomizer.class);
+
+ private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager;
+
+ public NatInstanceCustomizer(final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager) {
+ this.dumpCacheManager = dumpCacheManager;
+ }
+
+ @Nonnull
+ @Override
+ public NatInstanceBuilder getBuilder(@Nonnull final InstanceIdentifier<NatInstance> id) {
+ return new NatInstanceBuilder();
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<NatInstance> id,
+ @Nonnull final NatInstanceBuilder builder, @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ LOG.trace("Reading current attributes for nat-instance: {}", id);
+ builder.setId(id.firstKeyOf(NatInstance.class).getId());
+ }
+
+ @Nonnull
+ @Override
+ public List<NatInstanceKey> getAllIds(@Nonnull final InstanceIdentifier<NatInstance> id,
+ @Nonnull final ReadContext context) throws ReadFailedException {
+ LOG.trace("Listing IDs for all nat-instances");
+ final List<NatInstanceKey> vrfIds =
+ dumpCacheManager.getDump(id, getClass().getName(), context.getModificationCache(), null)
+ .or(new SnatStaticMappingDetailsReplyDump()).snatStaticMappingDetails.stream()
+ .map(detail -> detail.vrfId)
+ .map(vrfId -> new NatInstanceKey((long)vrfId))
+ .collect(Collectors.toList());
+
+ LOG.debug("List of nat-instance keys (vrf-ids): {}", vrfIds);
+ return vrfIds;
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<NatInstance> readData) {
+ ((NatInstancesBuilder) builder).setNatInstance(readData);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatReaderFactory.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatReaderFactory.java
new file mode 100644
index 000000000..949009c62
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/NatReaderFactory.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.read;
+
+import com.google.common.collect.Sets;
+import com.google.inject.Inject;
+import io.fd.honeycomb.nat.util.MappingEntryContext;
+import io.fd.honeycomb.translate.impl.read.GenericListReader;
+import io.fd.honeycomb.translate.read.ReaderFactory;
+import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
+import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetailsReplyDump;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.mapping.entry.ExternalSrcPort;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.mapping.entry.InternalSrcPort;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.NatInstances;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.NatInstancesBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.MappingTable;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.MappingTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.mapping.table.MappingEntry;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class NatReaderFactory implements ReaderFactory {
+
+ private static final InstanceIdentifier<NatState> NAT_OPER_ID = InstanceIdentifier.create(NatState.class);
+ private static final InstanceIdentifier<NatInstances> NAT_INSTANCES_ID = NAT_OPER_ID.child(NatInstances.class);
+ private static final InstanceIdentifier<NatInstance> NAT_INSTANCE_ID = NAT_INSTANCES_ID.child(NatInstance.class);
+ private static final InstanceIdentifier<MappingTable> MAP_TABLE_ID = NAT_INSTANCE_ID.child(MappingTable.class);
+ private static final InstanceIdentifier<MappingEntry> MAP_ENTRY_ID = MAP_TABLE_ID.child(MappingEntry.class);
+
+ private final MappingEntryContext mappingEntryContext;
+ private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager;
+
+ @Inject
+ public NatReaderFactory(final FutureJVppSnatFacade jvppSnat,
+ final MappingEntryContext mappingEntryContext) {
+ this.mappingEntryContext = mappingEntryContext;
+ this.dumpCacheManager = new DumpCacheManager.DumpCacheManagerBuilder<SnatStaticMappingDetailsReplyDump, Void>()
+ .withExecutor(new MappingEntryCustomizer.MappingEntryDumpExecutor(jvppSnat))
+ .build();
+ }
+
+ @Override
+ public void init(@Nonnull final ModifiableReaderRegistryBuilder registry) {
+ registry.addStructuralReader(NAT_OPER_ID, NatStateBuilder.class);
+ registry.addStructuralReader(NAT_INSTANCES_ID, NatInstancesBuilder.class);
+ registry.add(new GenericListReader<>(NAT_INSTANCE_ID, new NatInstanceCustomizer(dumpCacheManager)));
+ registry.addStructuralReader(MAP_TABLE_ID, MappingTableBuilder.class);
+ registry.subtreeAdd(Sets.newHashSet(InstanceIdentifier.create(MappingEntry.class).child(ExternalSrcPort.class),
+ InstanceIdentifier.create(MappingEntry.class).child(InternalSrcPort.class)),
+ new GenericListReader<>(MAP_ENTRY_ID,
+ new MappingEntryCustomizer(dumpCacheManager, mappingEntryContext)));
+
+ // TODO VPP-453 Implement address range read
+
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/IfcNatReaderFactory.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/IfcNatReaderFactory.java
new file mode 100644
index 000000000..293a3dfd9
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/IfcNatReaderFactory.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.read.ifc;
+
+
+import io.fd.honeycomb.translate.impl.read.GenericReader;
+import io.fd.honeycomb.translate.read.ReaderFactory;
+import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceStateAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceStateAugmentationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.Nat;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Outbound;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Factory producing readers for nat plugin's data.
+ */
+public final class IfcNatReaderFactory implements ReaderFactory {
+
+ private static final InstanceIdentifier<Interface>
+ IFC_ID = InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
+ private static final InstanceIdentifier<NatInterfaceStateAugmentation> NAT_AUG_ID =
+ IFC_ID.augmentation(NatInterfaceStateAugmentation.class);
+ private static final InstanceIdentifier<Nat> NAT_AUG_CONTAINER_ID = NAT_AUG_ID.child(Nat.class);
+
+ @Override
+ public void init(@Nonnull final ModifiableReaderRegistryBuilder registry) {
+ registry.addStructuralReader(NAT_AUG_ID, NatInterfaceStateAugmentationBuilder.class);
+ registry.addStructuralReader(NAT_AUG_CONTAINER_ID, NatBuilder.class);
+
+ registry.addAfter(
+ new GenericReader<>(NAT_AUG_CONTAINER_ID.child(Inbound.class), new InterfaceInboundNatCustomizer()), IFC_ID);
+ registry.addAfter(
+ new GenericReader<>(NAT_AUG_CONTAINER_ID.child(Outbound.class), new InterfaceOutboundNatCustomizer()), IFC_ID);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceInboundNatCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceInboundNatCustomizer.java
new file mode 100644
index 000000000..e1ebdd6d8
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceInboundNatCustomizer.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.read.ifc;
+
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.InboundBuilder;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class InterfaceInboundNatCustomizer implements ReaderCustomizer<Inbound, InboundBuilder> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(InterfaceInboundNatCustomizer.class);
+
+ @Nonnull
+ @Override
+ public InboundBuilder getBuilder(@Nonnull final InstanceIdentifier<Inbound> id) {
+ return new InboundBuilder();
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Inbound> id,
+ @Nonnull final InboundBuilder builder,
+ @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ // FIXME HONEYCOMB-248 VPP-459 Implement when read is available in VPP/Snat
+ LOG.debug("Unable to read Inbound NAT feature state for an interface");
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Inbound readValue) {
+ ((NatBuilder) parentBuilder).setInbound(readValue);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java
new file mode 100644
index 000000000..fe28584ff
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.read.ifc;
+
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Outbound;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.OutboundBuilder;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class InterfaceOutboundNatCustomizer implements ReaderCustomizer<Outbound, OutboundBuilder> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(InterfaceOutboundNatCustomizer.class);
+
+ @Nonnull
+ @Override
+ public OutboundBuilder getBuilder(@Nonnull final InstanceIdentifier<Outbound> id) {
+ return new OutboundBuilder();
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Outbound> id,
+ @Nonnull final OutboundBuilder builder, @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ // FIXME HONEYCOMB-248 VPP-459 Implement when read is available in VPP/Snat
+ LOG.debug("Unable to read Outbound NAT feature state for an interface");
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Outbound readValue) {
+ ((NatBuilder) parentBuilder).setOutbound(readValue);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/util/MappingEntryContext.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/util/MappingEntryContext.java
new file mode 100644
index 000000000..0f2df7e1d
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/util/MappingEntryContext.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.util;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
+import io.fd.honeycomb.translate.MappingContext;
+import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetails;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.ThreadSafe;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.NatMappingEntryCtxAugmentation;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.NatMappingEntryContext;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.NatInstanceKey;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.nat.instance.MappingTable;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.nat.instance.mapping.table.MappingEntry;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.nat.instance.mapping.table.MappingEntryBuilder;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.nat.instance.mapping.table.MappingEntryKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Context tracker for Nat Mapping entries.
+ */
+@ThreadSafe
+public class MappingEntryContext implements Ipv4Translator {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MappingEntryContext.class);
+
+ /**
+ * Add mapping entry to index mapping to context.
+ */
+ public synchronized void addEntry(final long natInstanceId,
+ final long entryId,
+ @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry entry,
+ @Nonnull final MappingContext mappingContext) {
+ final InstanceIdentifier<MappingEntry> id = getId(natInstanceId, entryToKey(entry));
+ checkArgument(!containsEntry(natInstanceId, entry, mappingContext), "Mapping for %s already present", id);
+ mappingContext.put(id, toCtxMapEntry(entry, entryId));
+ }
+
+ /**
+ * Check whether mapping entry to index mapping already exists in context.
+ */
+ public synchronized boolean containsEntry(final long natInstanceId,
+ @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry entry,
+ @Nonnull final MappingContext mappingContext) {
+ final InstanceIdentifier<MappingEntry> id = getId(natInstanceId, entryToKey(entry));
+ return mappingContext.read(id).isPresent();
+ }
+
+ @VisibleForTesting
+ static InstanceIdentifier<MappingEntry> getId(final Long natInstanceId, final MappingEntryKey key) {
+ return getTableId(natInstanceId).child(MappingEntry.class, key);
+ }
+
+ @VisibleForTesting
+ static InstanceIdentifier<MappingTable> getTableId(final long natInstanceId) {
+ return InstanceIdentifier.create(Contexts.class)
+ .augmentation(NatMappingEntryCtxAugmentation.class)
+ .child(NatMappingEntryContext.class)
+ .child(NatInstance.class, new NatInstanceKey(natInstanceId))
+ .child(MappingTable.class);
+ }
+
+ @VisibleForTesting
+ static MappingEntryKey entryToKey(
+ final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry entry) {
+ // Only IPv4
+ return new MappingEntryKey(new IpAddress(entry.getExternalSrcAddress()), entry.getInternalSrcAddress());
+ }
+
+ private MappingEntryKey entryToKey(final SnatStaticMappingDetails entry) {
+ // Only IPv4
+ return new MappingEntryKey(
+ new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZoneReversed(entry.externalIpAddress))),
+ new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZoneReversed(entry.localIpAddress))));
+ }
+
+ private boolean equalEntries(final SnatStaticMappingDetails detail, final MappingEntry ctxMappingEntry) {
+ final IpAddress internalAddrFromDetails =
+ new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZoneReversed(detail.localIpAddress)));
+ // Only IPv4
+ if (!ctxMappingEntry.getInternal().equals(internalAddrFromDetails)) {
+ return false;
+ }
+ // Only IPv4
+ final IpAddress externalAddrFromDetails =
+ new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZoneReversed(detail.externalIpAddress)));
+ if (!ctxMappingEntry.getExternal().equals(externalAddrFromDetails)) {
+ return false;
+ }
+ return true;
+ }
+
+ @VisibleForTesting
+ static MappingEntry toCtxMapEntry(
+ @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry entry,
+ final long entryId) {
+ return new MappingEntryBuilder()
+ .setKey(entryToKey(entry))
+ .setIndex(entryId)
+ .build();
+ }
+
+ private MappingEntry toCtxMapEntry(@Nonnull final SnatStaticMappingDetails details, final long entryId) {
+ return new MappingEntryBuilder()
+ .setKey(entryToKey(details))
+ .setIndex(entryId)
+ .build();
+ }
+
+ /**
+ * Delete mapping of mapping entry to index from context.
+ */
+ public synchronized void removeEntry(final long natInstanceId,
+ @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry entry,
+ @Nonnull final MappingContext mappingContext) {
+ mappingContext.delete(getId(natInstanceId, entryToKey(entry)));
+ }
+
+ /**
+ * Find specific details in provided collection identified with provided index.
+ */
+ public synchronized SnatStaticMappingDetails findDetails(@Nonnull final List<SnatStaticMappingDetails> details,
+ final long natInstanceId, final long idx,
+ @Nonnull final MappingContext mappingContext) {
+ // Find mapping entry for Index
+ final MappingEntry ctxMappingEntry = mappingContext.read(getTableId(natInstanceId))
+ .transform(MappingTable::getMappingEntry)
+ .or(Collections.emptyList())
+ .stream()
+ .filter(entry -> entry.getIndex() == idx)
+ .findFirst()
+ .orElseThrow(() -> new IllegalStateException("Unable to find context mapping for nat-instance: "
+ + natInstanceId + " and ID: " + idx));
+
+ // Find which details matches the context stored entry under index
+ return details.stream()
+ .filter(detail -> equalEntries(detail, ctxMappingEntry))
+ .findFirst()
+ .orElseThrow(() -> new IllegalStateException("Unable to match mapping for nat-instance: "
+ + natInstanceId + " and match: " + ctxMappingEntry + " in: " + details));
+ }
+
+ /**
+ * Get index for a mapping entry details or create an artificial one.
+ */
+ public synchronized long getStoredOrArtificialIndex(final Long natInstanceId,
+ @Nonnull final SnatStaticMappingDetails details,
+ @Nonnull final MappingContext mappingContext) {
+ return mappingContext.read(getId(natInstanceId, entryToKey(details)))
+ .transform(MappingEntry::getIndex)
+ .or(() -> getArtificialId(details, natInstanceId, mappingContext));
+ }
+
+ /**
+ * Get index for a stored mapping entry.
+ */
+ public synchronized Optional<Long> getStoredIndex(final long natInstanceId,
+ @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry entry,
+ @Nonnull final MappingContext mappingContext) {
+ return mappingContext.read(getId(natInstanceId, entryToKey(entry)))
+ .transform(MappingEntry::getIndex);
+ }
+
+ private long getArtificialId(final SnatStaticMappingDetails details, final Long natInstanceId,
+ final MappingContext mappingContext) {
+ LOG.trace("Assigning artificial ID for {}", details);
+ final long artificialIdx = findFreeIndex(natInstanceId, mappingContext);
+ LOG.debug("Artificial ID for {} assigned as: {}", details, artificialIdx);
+ mappingContext.put(getId(natInstanceId, entryToKey(details)), toCtxMapEntry(details, artificialIdx));
+ return artificialIdx;
+ }
+
+ private long findFreeIndex(final long natInstanceId, final MappingContext mappingContext) {
+ return mappingContext.read(getTableId(natInstanceId))
+ .transform(MappingTable::getMappingEntry)
+ .or(Collections.emptyList())
+ .stream()
+ .map(MappingEntry::getIndex)
+ .max(Comparator.naturalOrder())
+ .map(i -> i + 1)
+ .orElse(0L);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/MappingEntryCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/MappingEntryCustomizer.java
new file mode 100644
index 000000000..785606942
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/MappingEntryCustomizer.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.base.Optional;
+import io.fd.honeycomb.nat.util.MappingEntryContext;
+import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
+import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
+import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
+import io.fd.honeycomb.translate.write.WriteContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import io.fd.vpp.jvpp.snat.dto.SnatAddStaticMapping;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.PortNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntryKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.port.number.PortType;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.port.number.port.type.SinglePortNumber;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class MappingEntryCustomizer implements ListWriterCustomizer<MappingEntry, MappingEntryKey>,
+ JvppReplyConsumer, Ipv4Translator {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MappingEntryCustomizer.class);
+
+ private final FutureJVppSnatFacade jvppSnat;
+ private final MappingEntryContext mappingEntryContext;
+
+ MappingEntryCustomizer(final FutureJVppSnatFacade jvppSnat, final MappingEntryContext mappingEntryContext) {
+ this.jvppSnat = jvppSnat;
+ this.mappingEntryContext = mappingEntryContext;
+ }
+
+ @Override
+ public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<MappingEntry> id,
+ @Nonnull final MappingEntry dataAfter,
+ @Nonnull final WriteContext writeContext)
+ throws WriteFailedException {
+ // Only static mapping supported by SNAT for now
+ checkArgument(dataAfter.getType() ==
+ org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.MappingEntry.Type.Static,
+ "Only static NAT entries are supported currently. Trying to write: %s entry", dataAfter.getType());
+ final Long natInstanceId = id.firstKeyOf(NatInstance.class).getId();
+ final Long mappingEntryId = id.firstKeyOf(MappingEntry.class).getIndex();
+ LOG.debug("Writing mapping entry: {} for nat-instance(vrf): {}", natInstanceId, mappingEntryId);
+
+ final SnatAddStaticMapping request = getRequest(id, dataAfter, natInstanceId, true);
+ getReplyForWrite(jvppSnat.snatAddStaticMapping(request).toCompletableFuture(), id);
+
+ // Store context mapping only if not already present under the same exact mapping
+ synchronized (mappingEntryContext) {
+ if (shouldStoreContextMapping(natInstanceId, mappingEntryId, dataAfter, writeContext)) {
+ mappingEntryContext
+ .addEntry(natInstanceId, mappingEntryId, dataAfter, writeContext.getMappingContext());
+ }
+ }
+ LOG.trace("Mapping entry: {} for nat-instance(vrf): {} written successfully", request.vrfId, id);
+ }
+
+ /**
+ * Check whether entry is already stored in context under the same index.
+ *
+ * @return true if it's not yet stored under same index, false otherwise.
+ */
+ private boolean shouldStoreContextMapping(final long natInstanceId, final long mappingEntryId,
+ final MappingEntry dataAfter,
+ final WriteContext writeCtx) {
+ if (!mappingEntryContext.containsEntry(natInstanceId, dataAfter, writeCtx.getMappingContext())) {
+ return true;
+ }
+
+ final Optional<Long> storedIndex =
+ mappingEntryContext.getStoredIndex(natInstanceId, dataAfter, writeCtx.getMappingContext());
+ if (!storedIndex.isPresent()) {
+ return true;
+ }
+
+ if (storedIndex.get() != mappingEntryId) {
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<MappingEntry> id,
+ @Nonnull final MappingEntry dataBefore,
+ @Nonnull final MappingEntry dataAfter,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
+ new UnsupportedOperationException("Mapping entry update not supported"));
+ }
+
+ @Override
+ public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<MappingEntry> id,
+ @Nonnull final MappingEntry dataBefore,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ final long natInstanceId = id.firstKeyOf(NatInstance.class).getId();
+ final MappingEntryKey mappingEntryKey = id.firstKeyOf(MappingEntry.class);
+ LOG.debug("Deleting mapping entry: {} for nat-instance(vrf): {}", natInstanceId, mappingEntryKey);
+
+ final SnatAddStaticMapping request = getRequest(id, dataBefore, natInstanceId, false);
+ getReplyForWrite(jvppSnat.snatAddStaticMapping(request).toCompletableFuture(), id);
+ mappingEntryContext.removeEntry(natInstanceId, dataBefore, writeContext.getMappingContext());
+ LOG.trace("Mapping entry: {} for nat-instance(vrf): {} deleted successfully", request.vrfId, id);
+ }
+
+ private SnatAddStaticMapping getRequest(final InstanceIdentifier<MappingEntry> id,
+ final MappingEntry dataAfter,
+ final Long natInstanceId,
+ final boolean isAdd)
+ throws WriteFailedException.CreateFailedException {
+ final SnatAddStaticMapping request = new SnatAddStaticMapping();
+ request.isAdd = isAdd
+ ? (byte) 1
+ : 0;
+ request.isIp4 = 1;
+ // VPP uses int, model long
+ request.vrfId = natInstanceId.intValue();
+
+ // Snat supports only ipv4 now
+ if (dataAfter.getInternalSrcAddress().getIpv4Address() == null) {
+ throw new WriteFailedException.CreateFailedException(id, dataAfter,
+ new UnsupportedOperationException(
+ String.format("No Ipv4 present for in address %s. Ipv6 not supported",
+ dataAfter.getInternalSrcAddress())));
+ }
+
+ request.addrOnly = 1;
+ request.localIpAddress =
+ ipv4AddressNoZoneToArray(dataAfter.getInternalSrcAddress().getIpv4Address().getValue());
+ request.externalIpAddress = ipv4AddressNoZoneToArray(dataAfter.getExternalSrcAddress().getValue());
+
+ Optional<Short> internalPortNumber = getPortNumber(id, dataAfter,
+ (entry) -> Optional.fromNullable(entry.getInternalSrcPort()).transform(PortNumber::getPortType));
+ Optional<Short> externalPortNumber = getPortNumber(id, dataAfter,
+ (entry) -> Optional.fromNullable(entry.getExternalSrcPort()).transform(PortNumber::getPortType));
+ if (internalPortNumber.isPresent() && externalPortNumber.isPresent()) {
+ request.addrOnly = 0;
+ request.localPort = internalPortNumber.get();
+ request.externalPort = externalPortNumber.get();
+ }
+ return request;
+ }
+
+ private Optional<Short> getPortNumber(final InstanceIdentifier<MappingEntry> id, final MappingEntry dataAfter,
+ final PortGetter portGetter) {
+ return portGetter.getPortType(dataAfter).transform(port -> {
+ if (port instanceof SinglePortNumber) {
+ return ((SinglePortNumber) port).getSinglePortNumber().getValue().shortValue();
+ } else {
+ throw new IllegalArgumentException(
+ String.format("Only single port number supported. Submitted: %s for entry: %s",
+ dataAfter.getInternalSrcPort(), id));
+ }
+ });
+ }
+
+ interface PortGetter {
+ Optional<PortType> getPortType(MappingEntry entry);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatInstaceCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatInstaceCustomizer.java
new file mode 100644
index 000000000..3cc477dd0
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatInstaceCustomizer.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write;
+
+import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
+import io.fd.honeycomb.translate.write.WriteContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstanceKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class NatInstaceCustomizer implements ListWriterCustomizer<NatInstance, NatInstanceKey> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NatInstaceCustomizer.class);
+
+ @Override
+ public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<NatInstance> id,
+ @Nonnull final NatInstance dataAfter, @Nonnull final WriteContext writeContext)
+ throws WriteFailedException {
+ LOG.trace("Writing nat-instance: {}", id);
+ }
+
+ @Override
+ public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<NatInstance> id,
+ @Nonnull final NatInstance dataBefore, @Nonnull final NatInstance dataAfter,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ LOG.trace("Updating nat-instance: {}", id);
+ }
+
+ @Override
+ public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<NatInstance> id,
+ @Nonnull final NatInstance dataBefore, @Nonnull final WriteContext writeContext)
+ throws WriteFailedException {
+ LOG.trace("Deleting nat-instance: {}", id);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatWriterFactory.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatWriterFactory.java
new file mode 100644
index 000000000..ecc886bf0
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/NatWriterFactory.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write;
+
+import com.google.common.collect.Sets;
+import com.google.inject.Inject;
+import io.fd.honeycomb.nat.util.MappingEntryContext;
+import io.fd.honeycomb.translate.impl.write.GenericListWriter;
+import io.fd.honeycomb.translate.write.WriterFactory;
+import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.NatConfig;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.mapping.entry.ExternalSrcPort;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.mapping.entry.InternalSrcPort;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.NatInstances;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.NatInstance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.MappingTable;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Nat Writers registration.
+ */
+public final class NatWriterFactory implements WriterFactory {
+
+ private static final InstanceIdentifier<NatConfig> NAT_CFG_ID = InstanceIdentifier.create(NatConfig.class);
+ private static final InstanceIdentifier<NatInstance> NAT_INSTANCE_ID =
+ NAT_CFG_ID.child(NatInstances.class).child(NatInstance.class);
+ private static final InstanceIdentifier<MappingEntry> MAP_ENTRY_ID =
+ NAT_INSTANCE_ID.child(MappingTable.class).child(MappingEntry.class);
+
+ private final FutureJVppSnatFacade jvppSnat;
+ private final MappingEntryContext mappingEntryContext;
+
+ @Inject
+ public NatWriterFactory(final FutureJVppSnatFacade jvppSnat,
+ final MappingEntryContext mappingEntryContext) {
+ this.jvppSnat = jvppSnat;
+ this.mappingEntryContext = mappingEntryContext;
+ }
+
+ @Override
+ public void init(@Nonnull final ModifiableWriterRegistryBuilder registry) {
+ // Nat-instance
+ registry.add(new GenericListWriter<>(NAT_INSTANCE_ID, new NatInstaceCustomizer()));
+ // Mapping-entry
+ registry.subtreeAdd(Sets.newHashSet(InstanceIdentifier.create(MappingEntry.class).child(ExternalSrcPort.class),
+ InstanceIdentifier.create(MappingEntry.class).child(InternalSrcPort.class)),
+ new GenericListWriter<>(MAP_ENTRY_ID, new MappingEntryCustomizer(jvppSnat, mappingEntryContext)));
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/AbstractInterfaceNatCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/AbstractInterfaceNatCustomizer.java
new file mode 100644
index 000000000..64e714241
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/AbstractInterfaceNatCustomizer.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write.ifc;
+
+import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
+import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
+import io.fd.honeycomb.translate.vpp.util.NamingContext;
+import io.fd.honeycomb.translate.write.WriteContext;
+import io.fd.honeycomb.translate.write.WriteFailedException;
+import io.fd.vpp.jvpp.snat.dto.SnatInterfaceAddDelFeature;
+import io.fd.vpp.jvpp.snat.dto.SnatInterfaceAddDelFeatureReply;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import java.util.concurrent.CompletionStage;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+
+abstract class AbstractInterfaceNatCustomizer<D extends DataObject> implements JvppReplyConsumer, WriterCustomizer<D> {
+
+ private final FutureJVppSnatFacade jvppSnat;
+ private final NamingContext ifcContext;
+
+ AbstractInterfaceNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat,
+ @Nonnull final NamingContext ifcContext) {
+ this.jvppSnat = jvppSnat;
+ this.ifcContext = ifcContext;
+ }
+
+ @Override
+ public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ final String ifcName = id.firstKeyOf(Interface.class).getName();
+ getLog().debug("Enabling " + getType() + " NAT on interface: {}", ifcName);
+ getLog().debug("Enabling " + getType() + " NAT: {}", id);
+
+ final int ifcIndex = ifcContext.getIndex(ifcName, writeContext.getMappingContext());
+ final SnatInterfaceAddDelFeature request = getRequest(ifcIndex, (byte)1);
+ final CompletionStage<SnatInterfaceAddDelFeatureReply> future = jvppSnat.snatInterfaceAddDelFeature(request);
+
+ final SnatInterfaceAddDelFeatureReply reply = getReplyForWrite(future.toCompletableFuture(), id);
+ getLog().debug("NAT " + getType() + " enabled successfully on: {}, reply: {}", ifcName, reply);
+ }
+
+ @Override
+ public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final D dataBefore, @Nonnull final D dataAfter,
+ @Nonnull final WriteContext writeContext) throws WriteFailedException {
+ throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
+ new UnsupportedOperationException("Unable to update NAT feature"));
+ }
+
+ @Override
+ public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
+ @Nonnull final D dataBefore, @Nonnull final WriteContext writeContext)
+ throws WriteFailedException {
+ final String ifcName = id.firstKeyOf(Interface.class).getName();
+ getLog().debug("Disabling " + getType() + " NAT on interface: {}", ifcName);
+ getLog().debug("Disabling " + getType() + " NAT: {}", id);
+
+ final int ifcIndex = ifcContext.getIndex(ifcName, writeContext.getMappingContext());
+ final SnatInterfaceAddDelFeature request = getRequest(ifcIndex, (byte)0);
+ final CompletionStage<SnatInterfaceAddDelFeatureReply> future = jvppSnat.snatInterfaceAddDelFeature(request);
+
+ final SnatInterfaceAddDelFeatureReply reply = getReplyForWrite(future.toCompletableFuture(), id);
+ getLog().debug("NAT " + getType() + " disabled successfully on: {}, reply: {}", ifcName, reply);
+ }
+
+ enum NatType {
+ INBOUND((byte)1), OUTBOUND((byte)0);
+
+ private final byte isInside;
+
+ NatType(final byte isInside) {
+ this.isInside = isInside;
+ }
+ }
+
+ abstract NatType getType();
+ abstract Logger getLog();
+
+ private SnatInterfaceAddDelFeature getRequest(final int ifcIdx, final byte isAdd) {
+ final SnatInterfaceAddDelFeature request = new SnatInterfaceAddDelFeature();
+ request.isAdd = isAdd;
+ request.isInside = getType().isInside;
+ request.swIfIndex = ifcIdx;
+ return request;
+ }
+
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/IfcNatWriterFactory.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/IfcNatWriterFactory.java
new file mode 100644
index 000000000..bf2c600a2
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/IfcNatWriterFactory.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write.ifc;
+
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+import io.fd.honeycomb.translate.impl.write.GenericWriter;
+import io.fd.honeycomb.translate.vpp.util.NamingContext;
+import io.fd.honeycomb.translate.write.WriterFactory;
+import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.Nat;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Outbound;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Nat Writers registration.
+ */
+public final class IfcNatWriterFactory implements WriterFactory {
+
+ private static final InstanceIdentifier<Interface>
+ IFC_ID = InstanceIdentifier.create(Interfaces.class).child(Interface.class);
+ private static final InstanceIdentifier<Nat> NAT_AUG_ID =
+ IFC_ID .augmentation(NatInterfaceAugmentation.class).child(Nat.class);
+
+ private final FutureJVppSnatFacade jvppSnat;
+ private final NamingContext ifcContext;
+
+ @Inject
+ public IfcNatWriterFactory(final FutureJVppSnatFacade jvppSnat,
+ @Named("interface-context") final NamingContext ifcContext) {
+ this.jvppSnat = jvppSnat;
+ this.ifcContext = ifcContext;
+ }
+
+ @Override
+ public void init(@Nonnull final ModifiableWriterRegistryBuilder registry) {
+ registry.addAfter(new GenericWriter<>(NAT_AUG_ID.child(Inbound.class),
+ new InterfaceInboundNatCustomizer(jvppSnat, ifcContext)), IFC_ID);
+ registry.addAfter(new GenericWriter<>(NAT_AUG_ID.child(Outbound.class),
+ new InterfaceOutboundNatCustomizer(jvppSnat, ifcContext)), IFC_ID);
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceInboundNatCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceInboundNatCustomizer.java
new file mode 100644
index 000000000..8ab1c284c
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceInboundNatCustomizer.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write.ifc;
+
+import io.fd.honeycomb.translate.vpp.util.NamingContext;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class InterfaceInboundNatCustomizer extends AbstractInterfaceNatCustomizer<Inbound> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(InterfaceInboundNatCustomizer.class);
+
+ InterfaceInboundNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat,
+ @Nonnull final NamingContext ifcContext) {
+ super(jvppSnat, ifcContext);
+ }
+
+ @Override
+ NatType getType() {
+ return NatType.INBOUND;
+ }
+
+ @Override
+ Logger getLog() {
+ return LOG;
+ }
+}
diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceOutboundNatCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceOutboundNatCustomizer.java
new file mode 100644
index 000000000..fdd174eba
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ifc/InterfaceOutboundNatCustomizer.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.write.ifc;
+
+import io.fd.honeycomb.translate.vpp.util.NamingContext;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Outbound;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class InterfaceOutboundNatCustomizer extends AbstractInterfaceNatCustomizer<Outbound> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(InterfaceOutboundNatCustomizer.class);
+
+ InterfaceOutboundNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat,
+ @Nonnull final NamingContext ifcContext) {
+ super(jvppSnat, ifcContext);
+ }
+
+ @Override
+ NatType getType() {
+ return NatType.OUTBOUND;
+ }
+
+ @Override
+ Logger getLog() {
+ return LOG;
+ }
+}
diff --git a/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java b/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java
new file mode 100644
index 000000000..774dd6f2a
--- /dev/null
+++ b/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/NatModuleTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.Matchers.empty;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.multibindings.Multibinder;
+import com.google.inject.name.Named;
+import com.google.inject.testing.fieldbinder.Bind;
+import com.google.inject.testing.fieldbinder.BoundFieldModule;
+import io.fd.honeycomb.translate.read.ReaderFactory;
+import io.fd.honeycomb.translate.util.read.registry.CompositeReaderRegistryBuilder;
+import io.fd.honeycomb.translate.util.write.registry.FlatWriterRegistryBuilder;
+import io.fd.honeycomb.translate.vpp.util.NamingContext;
+import io.fd.honeycomb.translate.write.WriterFactory;
+import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
+import java.util.HashSet;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class NatModuleTest {
+
+ @Named("honeycomb-context")
+ @Bind
+ @Mock
+ private DataBroker honeycombContext;
+
+ @Bind
+ private ReaderFactory ietfIfcReaderFactory;
+
+ @Named("honeycomb-initializer")
+ @Bind
+ @Mock
+ private DataBroker honeycombInitializer;
+
+ @Named("interface-context")
+ @Bind
+ private NamingContext ifcContext;
+
+ @Inject
+ private Set<ReaderFactory> readerFactories = new HashSet<>();
+
+ @Inject
+ private Set<WriterFactory> writerFactories = new HashSet<>();
+
+ @Before
+ public void setUp() throws Exception {
+ ietfIfcReaderFactory = registry -> {
+ registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class), InterfacesStateBuilder.class);
+ registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class).child(Interface.class), InterfaceBuilder.class);
+ };
+ initMocks(this);
+ ifcContext = new NamingContext("interface-", "interface-context");
+ // Nat Module adds readers under InterfacesState/Interface and since readers for parents that do nothing need to
+ // be present, add structural readers (or add V3poModule here, but adding the full Module is not the best solution)
+ Guice.createInjector(binder -> Multibinder.newSetBinder(binder, ReaderFactory.class)
+ .addBinding().toInstance(registry -> {
+ registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class),
+ InterfacesStateBuilder.class);
+ registry.addStructuralReader(InstanceIdentifier.create(InterfacesState.class).child(Interface.class),
+ InterfaceBuilder.class);
+ }), new NatModule(MockJVppSnatProvider.class), BoundFieldModule.of(this)).injectMembers(this);
+ }
+
+ @Test
+ public void testReaderFactories() throws Exception {
+ assertThat(readerFactories, is(not(empty())));
+
+ // Test registration process (all dependencies present, topological order of readers does exist, etc.)
+ final CompositeReaderRegistryBuilder registryBuilder = new CompositeReaderRegistryBuilder();
+ readerFactories.forEach(factory -> factory.init(registryBuilder));
+ assertNotNull(registryBuilder.build());
+ }
+
+ @Test
+ public void testWriterFactories() throws Exception {
+ assertThat(writerFactories, is(not(empty())));
+
+ // Test registration process (all dependencies present, topological order of writers does exist, etc.)
+ final FlatWriterRegistryBuilder registryBuilder = new FlatWriterRegistryBuilder();
+ writerFactories.forEach(factory -> factory.init(registryBuilder));
+ assertNotNull(registryBuilder.build());
+ }
+
+ private static final class MockJVppSnatProvider implements Provider<FutureJVppSnatFacade> {
+
+ @Override
+ public FutureJVppSnatFacade get() {
+ return mock(FutureJVppSnatFacade.class);
+ }
+ }
+} \ No newline at end of file
diff --git a/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/util/MappingEntryContextTest.java b/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/util/MappingEntryContextTest.java
new file mode 100644
index 000000000..fee8def89
--- /dev/null
+++ b/nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/util/MappingEntryContextTest.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.honeycomb.nat.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+import io.fd.honeycomb.translate.MappingContext;
+import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
+import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetails;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.nat.instance.MappingTableBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntry;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntryBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.config.nat.instances.nat.instance.mapping.table.MappingEntryKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+@SuppressWarnings("unchecked")
+public class MappingEntryContextTest implements Ipv4Translator {
+
+ private MappingEntryContext ctx = new MappingEntryContext();
+ @Mock
+ private MappingContext mappingCtx;
+
+ @Before
+ public void setUp() throws Exception {
+ initMocks(this);
+ }
+
+ @Test
+ public void testAdd() throws Exception {
+ when(mappingCtx.read(any(InstanceIdentifier.class))).thenReturn(Optional.absent());
+ final long natId = 7;
+ final long entryId = 99;
+ final MappingEntry entry = getEntry(natId, "192.168.1.5", "17.14.4.6");
+
+ ctx.addEntry(natId, entryId, entry, mappingCtx);
+
+ verify(mappingCtx).put(MappingEntryContext.getId(natId, MappingEntryContext.entryToKey(entry)), MappingEntryContext.toCtxMapEntry(entry, entryId));
+ }
+
+ @Test
+ public void testRemove() throws Exception {
+ final long natId = 0;
+ final MappingEntry entry = getEntry(natId, "192.168.1.5", "17.14.4.6");
+
+ ctx.removeEntry(natId, entry, mappingCtx);
+
+ verify(mappingCtx).delete(MappingEntryContext.getId(natId, MappingEntryContext.entryToKey(entry)));
+ }
+
+ @Test
+ public void testGetExistingIndex() throws Exception {
+ final long natId = 0;
+ final long entryId = 12;
+ final MappingEntry entry = getEntry(entryId, "192.168.1.5", "17.14.4.6");
+ final SnatStaticMappingDetails details = getDetails(entryId, "192.168.1.5", "17.14.4.6");
+
+ when(mappingCtx.read(MappingEntryContext.getId(natId, MappingEntryContext.entryToKey(entry))))
+ .thenReturn(Optional.of(MappingEntryContext.toCtxMapEntry(entry, entryId)));
+
+ assertEquals(12, ctx.getStoredOrArtificialIndex(natId, details, mappingCtx));
+ verify(mappingCtx).read(MappingEntryContext.getId(natId, MappingEntryContext.entryToKey(entry)));
+ }
+
+ @Test
+ public void testFindDetails() throws Exception {
+ final long natId = 0;
+ final MappingEntry entry = getEntry(0, "192.168.1.5", "17.14.4.6");
+ final SnatStaticMappingDetails details = getDetails(0, "192.168.1.5", "17.14.4.6");
+ final MappingEntry entry2 = getEntry(1, "192.168.1.8", "17.14.4.10");
+ final SnatStaticMappingDetails details2 = getDetails(1, "192.168.1.8", "17.14.4.10");
+
+ final List<SnatStaticMappingDetails> someDetails = Lists.newArrayList(details, details2);
+
+ when(mappingCtx.read(MappingEntryContext.getTableId(natId)))
+ .thenReturn(Optional.of(new MappingTableBuilder()
+ .setMappingEntry(Lists.newArrayList(
+ MappingEntryContext.toCtxMapEntry(entry, 0),
+ MappingEntryContext.toCtxMapEntry(entry2, 1)))
+ .build()));
+
+ assertSame(details, ctx.findDetails(someDetails, natId, 0, mappingCtx));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testFindDetailsNoMappingStored() throws Exception {
+ final long natId = 0;
+ final long entryId = 12;
+ final SnatStaticMappingDetails details = getDetails(entryId, "192.168.1.5", "17.14.4.6");
+ final List<SnatStaticMappingDetails> someDetails = Lists.newArrayList(details);
+ when(mappingCtx.read(MappingEntryContext.getTableId(natId))).thenReturn(Optional.absent());
+
+ ctx.findDetails(someDetails, natId, entryId, mappingCtx);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testFindDetailsNoMappingStored2() throws Exception {
+ final long natId = 0;
+ final long entryId = 12;
+ final SnatStaticMappingDetails details = getDetails(entryId, "192.168.1.5", "17.14.4.6");
+ final List<SnatStaticMappingDetails> someDetails = Lists.newArrayList(details);
+
+ when(mappingCtx.read(MappingEntryContext.getTableId(natId)))
+ .thenReturn(Optional.of(new MappingTableBuilder().setMappingEntry(Collections.emptyList()).build()));
+
+ ctx.findDetails(someDetails, natId, entryId, mappingCtx);
+ }
+
+ @Test
+ public void testGetArtificialIndex() throws Exception {
+ final long natId = 0;
+ final long entryId = 0;
+ final MappingEntry entry = getEntry(entryId, "192.168.1.5", "17.14.4.6");
+ final long entryId2 = 55;
+ final MappingEntry entry2 = getEntry(entryId2, "192.168.1.6", "17.14.4.7");
+ final long entryId3 = 18954;
+ final MappingEntry entry3 = getEntry(entryId3, "192.168.1.7", "17.14.4.8");
+ final long entryId4 = 18955;
+ final MappingEntry entry4 = getEntry(entryId4, "192.168.1.8", "17.14.4.9");
+
+ final long newEntryId = 18956;
+ final MappingEntry newEntry = getEntry(newEntryId, "192.168.1.99", "17.14.4.99");
+ final SnatStaticMappingDetails newDetails = getDetails(newEntryId, "192.168.1.99", "17.14.4.99");
+ when(mappingCtx.read(MappingEntryContext.getId(natId, MappingEntryContext.entryToKey(newEntry))))
+ .thenReturn(Optional.absent());
+
+ when(mappingCtx.read(MappingEntryContext.getTableId(natId)))
+ .thenReturn(Optional.of(new MappingTableBuilder()
+ .setMappingEntry(Lists.newArrayList(
+ MappingEntryContext.toCtxMapEntry(entry, entryId),
+ MappingEntryContext.toCtxMapEntry(entry3, entryId3),
+ MappingEntryContext.toCtxMapEntry(entry4, entryId4),
+ MappingEntryContext.toCtxMapEntry(entry2, entryId2)))
+ .build()));
+
+ assertFalse(ctx.getStoredIndex(natId, newEntry, mappingCtx).isPresent());
+ assertEquals(newEntryId, ctx.getStoredOrArtificialIndex(natId, newDetails, mappingCtx));
+ }
+
+ private SnatStaticMappingDetails getDetails(final long vrfId, final String localIp, final String externIp) {
+ final SnatStaticMappingDetails snatStaticMappingDetails = new SnatStaticMappingDetails();
+ snatStaticMappingDetails.vrfId = (int) vrfId;
+ snatStaticMappingDetails.addrOnly = 1;
+ snatStaticMappingDetails.isIp4 = 1;
+ snatStaticMappingDetails.localIpAddress = ipv4AddressNoZoneToArray(localIp);
+ snatStaticMappingDetails.externalIpAddress = ipv4AddressNoZoneToArray(externIp);
+ return snatStaticMappingDetails;
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testAddExisting() throws Exception {
+ final long natId = 7;
+ final long entryId = 99;
+ final MappingEntry entry = getEntry(natId, "192.168.1.5", "17.14.4.6");
+ final org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context.rev161214.mapping.entry.context.attributes.nat.mapping.entry.context.nat.instance.mapping.table.MappingEntry
+ data = MappingEntryContext.toCtxMapEntry(entry, entryId);
+ when(mappingCtx.read(any(InstanceIdentifier.class))).thenReturn(Optional.of(data));
+
+ ctx.addEntry(natId, entryId, entry, mappingCtx);
+ }
+
+ private static MappingEntry getEntry(final long id, final String longernalIpv4, final String externalIpv4) {
+ return new MappingEntryBuilder()
+ .setKey(new MappingEntryKey(id))
+ .setType(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.MappingEntry.Type.Static)
+ .setInternalSrcAddress(new IpAddress(new Ipv4Address(longernalIpv4)))
+ .setExternalSrcAddress(new Ipv4Address(externalIpv4))
+ .build();
+ }
+} \ No newline at end of file
diff --git a/nat/nat2vpp/src/test/resources/nat.json b/nat/nat2vpp/src/test/resources/nat.json
new file mode 100644
index 000000000..c7d9afeb3
--- /dev/null
+++ b/nat/nat2vpp/src/test/resources/nat.json
@@ -0,0 +1,3 @@
+{
+ "enabled": "true"
+} \ No newline at end of file