diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-10-10 15:37:04 +0200 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2016-10-17 08:16:30 +0000 |
commit | 957461dcfd741fc3290e4317c2297c5618b593b5 (patch) | |
tree | 81606cfe4aa746bcde2948f80bbf5666cc1f1d30 | |
parent | 74d5f495d67f4e53d88142e9773559b15e0d9807 (diff) |
HONEYCOMB-249 Add External IP pool management
Change-Id: I3e57b09733b8c7060445ed1af7a5edb3348dd4e1
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
12 files changed, 281 insertions, 19 deletions
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 index 37c456133..1c5b20392 100644 --- 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 @@ -48,6 +48,9 @@ public final class NatInitializer extends AbstractDataTreeConverter<NatState, Na .setNatInstance(operationalData.getNatInstances().getNatInstance().stream() .map(operNatInstance -> new NatInstanceBuilder() .setId(operNatInstance.getId()) + // Ext IP pools + .setExternalIpAddressPool(operNatInstance.getNatCurrentConfig().getExternalIpAddressPool()) + // Mapping entries .setMappingTable(new MappingTableBuilder() .setMappingEntry( operNatInstance.getMappingTable().getMappingEntry().stream() @@ -58,6 +61,7 @@ public final class NatInitializer extends AbstractDataTreeConverter<NatState, Na .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/read/ExternalIpPoolCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ExternalIpPoolCustomizer.java new file mode 100644 index 000000000..206b15e63 --- /dev/null +++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ExternalIpPoolCustomizer.java @@ -0,0 +1,134 @@ +/* + * 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.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.SnatAddressDetails; +import io.fd.vpp.jvpp.snat.dto.SnatAddressDetailsReplyDump; +import io.fd.vpp.jvpp.snat.dto.SnatAddressDump; +import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.LongStream; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.parameters.ExternalIpAddressPool; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.parameters.ExternalIpAddressPoolBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.parameters.ExternalIpAddressPoolKey; +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.NatInstanceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.NatCurrentConfigBuilder; +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; + +final class ExternalIpPoolCustomizer implements + ListReaderCustomizer<ExternalIpAddressPool, ExternalIpAddressPoolKey, ExternalIpAddressPoolBuilder>, + JvppReplyConsumer, Ipv4Translator { + + private static final Logger LOG = LoggerFactory.getLogger(ExternalIpPoolCustomizer.class); + + private final DumpCacheManager<SnatAddressDetailsReplyDump, Void> dumpMgr; + + ExternalIpPoolCustomizer(final DumpCacheManager<SnatAddressDetailsReplyDump, Void> dumpMgr) { + this.dumpMgr = dumpMgr; + } + + @Nonnull + @Override + public ExternalIpAddressPoolBuilder getBuilder(@Nonnull final InstanceIdentifier<ExternalIpAddressPool> id) { + return new ExternalIpAddressPoolBuilder(); + } + + @Override + public void readCurrentAttributes(@Nonnull final InstanceIdentifier<ExternalIpAddressPool> id, + @Nonnull final ExternalIpAddressPoolBuilder builder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + LOG.trace("Reading current attributes for external IP pool: {}", id); + + final Long poolId = id.firstKeyOf(ExternalIpAddressPool.class).getPoolId(); + final SnatAddressDetails details = + dumpMgr.getDump(id, getClass().getName(), ctx.getModificationCache(), null) + .or(new SnatAddressDetailsReplyDump()).snatAddressDetails.get(Math.toIntExact(poolId)); + + builder.setExternalIpPool(new Ipv4Prefix(arrayToIpv4AddressNoZoneReversed(details.ipAddress).getValue() + "/32")); + builder.setPoolId(poolId); + + LOG.trace("External IP pool: {}. Read as: {}", id, builder); + } + + @Nonnull + @Override + public List<ExternalIpAddressPoolKey> getAllIds(@Nonnull final InstanceIdentifier<ExternalIpAddressPool> id, + @Nonnull final ReadContext ctx) throws ReadFailedException { + final NatInstanceKey natKey = id.firstKeyOf(NatInstance.class); + if (!natKey.equals(NatInstanceCustomizer.DEFAULT_VRF_ID)) { + // IP Pools are not vrf aware ... so they are only visible under default vrf (nat-instance) + return Collections.emptyList(); + } + + LOG.trace("Listing IDs for all external IP pools within nat-instance(vrf):{}", natKey); + + // Since VPP returns every single (unordered) address instead of address range, + // there is no way to determine what the original ranges were when writing the data into VPP. + + // That's why the write and read is not symmetrical in terms of data structure, instead, + // this customizer also returns every single address as a 32 prefix and assigns an artificial key to them + + final long addressCount = dumpMgr.getDump(id, getClass().getName(), ctx.getModificationCache(), null) + .or(new SnatAddressDetailsReplyDump()).snatAddressDetails.stream() + .count(); + + final List<ExternalIpAddressPoolKey> ids = LongStream.range(0, addressCount) + .mapToObj(ExternalIpAddressPoolKey::new) + .collect(Collectors.toList()); + + LOG.trace("List of external IP pool ids: {}", ids); + return ids; + } + + @Override + public void merge(@Nonnull final Builder<? extends DataObject> builder, + @Nonnull final List<ExternalIpAddressPool> readData) { + ((NatCurrentConfigBuilder) builder).setExternalIpAddressPool(readData); + } + + static final class AddressRangeDumpExecutor implements EntityDumpExecutor<SnatAddressDetailsReplyDump, Void>, + JvppReplyConsumer { + private final FutureJVppSnatFacade jvppSnat; + + AddressRangeDumpExecutor(final FutureJVppSnatFacade jvppSnat) { + this.jvppSnat = jvppSnat; + } + + @Nonnull + @Override + public SnatAddressDetailsReplyDump executeDump(final InstanceIdentifier<?> identifier, final Void params) + throws ReadFailedException { + return getReplyForRead(jvppSnat.snatAddressDump(new SnatAddressDump()).toCompletableFuture(), identifier); + } + } +} 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 index 469d361ee..c0f70452b 100644 --- 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 @@ -47,7 +47,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class MappingEntryCustomizer implements Ipv4Translator, +final class MappingEntryCustomizer implements Ipv4Translator, ListReaderCustomizer<MappingEntry, MappingEntryKey, MappingEntryBuilder> { private static final Logger LOG = LoggerFactory.getLogger(MappingEntryCustomizer.class); @@ -55,8 +55,8 @@ public class MappingEntryCustomizer implements Ipv4Translator, private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager; private final MappingEntryContext mappingEntryContext; - public MappingEntryCustomizer(final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager, - final MappingEntryContext mappingEntryContext) { + MappingEntryCustomizer(final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager, + final MappingEntryContext mappingEntryContext) { this.dumpCacheManager = dumpCacheManager; this.mappingEntryContext = mappingEntryContext; } 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 index cb639b962..43f4f1e0c 100644 --- 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 @@ -40,10 +40,11 @@ import org.slf4j.LoggerFactory; final class NatInstanceCustomizer implements ListReaderCustomizer<NatInstance, NatInstanceKey, NatInstanceBuilder> { private static final Logger LOG = LoggerFactory.getLogger(NatInstanceCustomizer.class); + static final NatInstanceKey DEFAULT_VRF_ID = new NatInstanceKey(0L); private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager; - public NatInstanceCustomizer(final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager) { + NatInstanceCustomizer(final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> dumpCacheManager) { this.dumpCacheManager = dumpCacheManager; } @@ -66,6 +67,8 @@ final class NatInstanceCustomizer implements ListReaderCustomizer<NatInstance, N public List<NatInstanceKey> getAllIds(@Nonnull final InstanceIdentifier<NatInstance> id, @Nonnull final ReadContext context) throws ReadFailedException { LOG.trace("Listing IDs for all nat-instances"); + + // Find the nat instance IDs (vrf-ids) by listing all static mappings and their VRF assignment final List<NatInstanceKey> vrfIds = dumpCacheManager.getDump(id, getClass().getName(), context.getModificationCache(), null) .or(new SnatStaticMappingDetailsReplyDump()).snatStaticMappingDetails.stream() @@ -73,6 +76,11 @@ final class NatInstanceCustomizer implements ListReaderCustomizer<NatInstance, N .map(vrfId -> new NatInstanceKey((long)vrfId)) .collect(Collectors.toList()); + // Add default vrf id if not present + if (!vrfIds.contains(DEFAULT_VRF_ID)) { + vrfIds.add(0, DEFAULT_VRF_ID); + } + LOG.debug("List of nat-instance keys (vrf-ids): {}", vrfIds); return vrfIds; } 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 index 949009c62..9ae0e12f9 100644 --- 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 @@ -23,6 +23,7 @@ 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.SnatAddressDetailsReplyDump; import io.fd.vpp.jvpp.snat.dto.SnatStaticMappingDetailsReplyDump; import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade; import javax.annotation.Nonnull; @@ -30,11 +31,14 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev1509 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.parameters.ExternalIpAddressPool; 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.NatCurrentConfig; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.state.nat.instances.nat.instance.NatCurrentConfigBuilder; 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; @@ -43,33 +47,39 @@ 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<NatCurrentConfig> CURRENT_CONFIG = NAT_INSTANCE_ID.child(NatCurrentConfig.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; + private final DumpCacheManager<SnatStaticMappingDetailsReplyDump, Void> mapEntryDumpMgr; + private final DumpCacheManager<SnatAddressDetailsReplyDump, Void> addressRangeDumpMgr; @Inject public NatReaderFactory(final FutureJVppSnatFacade jvppSnat, final MappingEntryContext mappingEntryContext) { this.mappingEntryContext = mappingEntryContext; - this.dumpCacheManager = new DumpCacheManager.DumpCacheManagerBuilder<SnatStaticMappingDetailsReplyDump, Void>() + this.mapEntryDumpMgr = new DumpCacheManager.DumpCacheManagerBuilder<SnatStaticMappingDetailsReplyDump, Void>() .withExecutor(new MappingEntryCustomizer.MappingEntryDumpExecutor(jvppSnat)) .build(); + this.addressRangeDumpMgr = new DumpCacheManager.DumpCacheManagerBuilder<SnatAddressDetailsReplyDump, Void>() + .withExecutor(new ExternalIpPoolCustomizer.AddressRangeDumpExecutor(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.add(new GenericListReader<>(NAT_INSTANCE_ID, new NatInstanceCustomizer(mapEntryDumpMgr))); 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 + new MappingEntryCustomizer(mapEntryDumpMgr, mappingEntryContext))); + registry.addStructuralReader(CURRENT_CONFIG, NatCurrentConfigBuilder.class); + registry.add(new GenericListReader<>(CURRENT_CONFIG.child(ExternalIpAddressPool.class), + new ExternalIpPoolCustomizer(addressRangeDumpMgr))); } } 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 index e1ebdd6d8..5be35ce16 100644 --- 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 @@ -29,7 +29,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public final class InterfaceInboundNatCustomizer implements ReaderCustomizer<Inbound, InboundBuilder> { +final class InterfaceInboundNatCustomizer implements ReaderCustomizer<Inbound, InboundBuilder> { private static final Logger LOG = LoggerFactory.getLogger(InterfaceInboundNatCustomizer.class); 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 index fe28584ff..cb0103111 100644 --- 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 @@ -29,7 +29,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public final class InterfaceOutboundNatCustomizer implements ReaderCustomizer<Outbound, OutboundBuilder> { +final class InterfaceOutboundNatCustomizer implements ReaderCustomizer<Outbound, OutboundBuilder> { private static final Logger LOG = LoggerFactory.getLogger(InterfaceOutboundNatCustomizer.class); diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ExternalIpPoolCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ExternalIpPoolCustomizer.java new file mode 100644 index 000000000..856bc1355 --- /dev/null +++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/write/ExternalIpPoolCustomizer.java @@ -0,0 +1,92 @@ +/* + * 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 io.fd.honeycomb.translate.spi.write.ListWriterCustomizer; +import io.fd.honeycomb.translate.vpp.util.Ipv4AddressRange; +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.SnatAddAddressRange; +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.inet.types.rev130715.Ipv4Prefix; +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.parameters.ExternalIpAddressPool; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.parameters.ExternalIpAddressPoolKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class ExternalIpPoolCustomizer implements ListWriterCustomizer<ExternalIpAddressPool, ExternalIpAddressPoolKey>, + JvppReplyConsumer, Ipv4Translator { + + private static final Logger LOG = LoggerFactory.getLogger(ExternalIpPoolCustomizer.class); + + private final FutureJVppSnatFacade jvppSnat; + + ExternalIpPoolCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat) { + this.jvppSnat = jvppSnat; + } + + @Override + public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ExternalIpAddressPool> id, + @Nonnull final ExternalIpAddressPool dataAfter, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + checkArgument(id.firstKeyOf(NatInstance.class).getId() == 0, + "External IP pools are only assignable for nat instance(vrf-id) with ID 0"); + LOG.trace("Adding address range:{}, as: {}", id, dataAfter); + // TODO check overlaps ? VPP-478 maybe no necessary, depending on how VPP handles them + getReplyForCreate(jvppSnat.snatAddAddressRange( + getRequest(dataAfter.getExternalIpPool(), true)).toCompletableFuture(), id, dataAfter); + LOG.debug("Address range: {} added successfully", id); + } + + @Override + public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ExternalIpAddressPool> id, + @Nonnull final ExternalIpAddressPool dataBefore, + @Nonnull final ExternalIpAddressPool dataAfter, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, + new UnsupportedOperationException("Address range update is not supported")); + } + + @Override + public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ExternalIpAddressPool> id, + @Nonnull final ExternalIpAddressPool dataBefore, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + LOG.trace("Deleting address range:{}, as: {}", id, dataBefore); + getReplyForDelete(jvppSnat.snatAddAddressRange( + getRequest(dataBefore.getExternalIpPool(), false)).toCompletableFuture(), id); + LOG.debug("Deleting range: {} added successfully", id); + } + + private SnatAddAddressRange getRequest(final Ipv4Prefix externalIpPool, boolean isAdd) { + SnatAddAddressRange request = new SnatAddAddressRange(); + // SNAT supports only IPv4 now, so does the model + final Ipv4AddressRange range = Ipv4AddressRange.fromPrefix(externalIpPool); + LOG.trace("Handling address range: {}", range); + request.isIp4 = 1; + request.isAdd = (byte) (isAdd ? 1 : 0); + request.firstIpAddress = ipv4AddressNoZoneToArray(range.getStart()); + request.lastIpAddress = ipv4AddressNoZoneToArray(range.getEnd()); + return request; + } +} 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 index ecc886bf0..7887d463a 100644 --- 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 @@ -31,6 +31,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev1509 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev150908.nat.parameters.ExternalIpAddressPool; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** @@ -62,5 +63,11 @@ public final class NatWriterFactory implements WriterFactory { 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))); + + // External address pool has to be executed before mapping entry. Because adding mapping entries requires to + // already have an IP range predefined ... in some cases + registry.addBefore(new GenericListWriter<>(NAT_INSTANCE_ID.child(ExternalIpAddressPool.class), + new ExternalIpPoolCustomizer(jvppSnat)), + MAP_ENTRY_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 index 8ab1c284c..e72e12ada 100644 --- 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 @@ -28,7 +28,7 @@ final class InterfaceInboundNatCustomizer extends AbstractInterfaceNatCustomizer private static final Logger LOG = LoggerFactory.getLogger(InterfaceInboundNatCustomizer.class); InterfaceInboundNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat, - @Nonnull final NamingContext ifcContext) { + @Nonnull final NamingContext ifcContext) { super(jvppSnat, ifcContext); } 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 index fdd174eba..488edc73c 100644 --- 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 @@ -28,7 +28,7 @@ final class InterfaceOutboundNatCustomizer extends AbstractInterfaceNatCustomize private static final Logger LOG = LoggerFactory.getLogger(InterfaceOutboundNatCustomizer.class); InterfaceOutboundNatCustomizer(@Nonnull final FutureJVppSnatFacade jvppSnat, - @Nonnull final NamingContext ifcContext) { + @Nonnull final NamingContext ifcContext) { super(jvppSnat, ifcContext); } diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/vpp/util/Ipv4AddressRange.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/vpp/util/Ipv4AddressRange.java index 8408e79f4..7528c5464 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/vpp/util/Ipv4AddressRange.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/vpp/util/Ipv4AddressRange.java @@ -20,16 +20,22 @@ import java.util.Objects; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * IPv4 address range representation. */ public final class Ipv4AddressRange { + private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressRange.class); + private final Ipv4AddressNoZone start; private final Ipv4AddressNoZone end; - private Ipv4AddressRange(@Nonnull final Ipv4AddressNoZone start, @Nonnull final Ipv4AddressNoZone end) { + private Ipv4AddressRange( + @Nonnull final Ipv4AddressNoZone start, + @Nonnull final Ipv4AddressNoZone end) { this.start = start; this.end = end; } @@ -71,9 +77,9 @@ public final class Ipv4AddressRange { /** * Create address range from prefix. */ - public static Ipv4AddressRange fromPrefix(final Ipv4Prefix externalIpPool) { - final String addressString = externalIpPool.getValue().split("/")[0]; - byte prefixLength = Ipv4Translator.INSTANCE.extractPrefix(externalIpPool); + public static Ipv4AddressRange fromPrefix(@Nonnull final Ipv4Prefix prefix) { + final String addressString = prefix.getValue().split("/")[0]; + byte prefixLength = Ipv4Translator.INSTANCE.extractPrefix(prefix); if (prefixLength == 32) { // 32 Prefix can be handled instantly @@ -108,7 +114,8 @@ public final class Ipv4AddressRange { prefixAddrBytesF[i] = (byte) 255; } - return new Ipv4AddressRange(Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytes0), + return new Ipv4AddressRange( + Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytes0), Ipv4Translator.INSTANCE.arrayToIpv4AddressNoZoneReversed(prefixAddrBytesF)); } } |