diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2018-05-30 13:45:41 +0200 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2018-05-31 09:09:49 +0000 |
commit | 7d961bf199719410b2e4111c830fbf35b41c83c2 (patch) | |
tree | 8f5a5dbd0502b7e9046f3b672aa21173c392296f /nat/nat2vpp/src/main/java/io | |
parent | a626d4ad0333762de57eb3298b9c7b3f9f19f82e (diff) |
HC2VPP-325: use ip-prefix instead of ip-address in ietf-nat@2018-02-23.yang
Only /32 prefix for IPv4 and /128 for IPv6 are supported.
Change-Id: I875b1eb6c2cb43da54de0a731aeeb9757b39c39c
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'nat/nat2vpp/src/main/java/io')
3 files changed, 46 insertions, 38 deletions
diff --git a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/MappingEntryCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/MappingEntryCustomizer.java index 43dba092a..15f73e66f 100644 --- a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/MappingEntryCustomizer.java +++ b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/MappingEntryCustomizer.java @@ -37,7 +37,7 @@ import java.util.List; import java.util.Optional; 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.IpPrefix; 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.rev180223.mapping.entry.ExternalSrcPortBuilder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.mapping.entry.InternalSrcPortBuilder; @@ -114,9 +114,8 @@ final class MappingEntryCustomizer implements Ipv4Translator, Ipv6Translator, builder.setIndex((long) index); builder.setType( org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.MappingEntry.Type.Static); - builder.setExternalSrcAddress(arrayToIpv4AddressNoZone(detail.externalIpAddress)); - builder.setInternalSrcAddress( - new IpAddress(arrayToIpv4AddressNoZone(detail.localIpAddress))); + builder.setExternalSrcAddress(new IpPrefix(toIpv4Prefix(detail.externalIpAddress, 32))); + builder.setInternalSrcAddress(new IpPrefix(toIpv4Prefix(detail.localIpAddress, 32))); if (detail.addrOnly == 0) { builder.setExternalSrcPort(new ExternalSrcPortBuilder() @@ -136,9 +135,8 @@ final class MappingEntryCustomizer implements Ipv4Translator, Ipv6Translator, builder.setType( org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.MappingEntry.Type.DynamicImplicit); } - builder.setExternalSrcAddress(arrayToIpv4AddressNoZone(detail.oAddr)); - builder.setInternalSrcAddress( - new IpAddress(arrayToIpv6AddressNoZone(detail.iAddr))); + builder.setExternalSrcAddress(new IpPrefix(toIpv4Prefix(detail.oAddr,32 ))); + builder.setInternalSrcAddress(new IpPrefix(toIpv6Prefix(detail.iAddr, 128))); builder.setExternalSrcPort(new ExternalSrcPortBuilder() .setStartPortNumber(new PortNumber(Short.toUnsignedInt(detail.oPort))).build()); diff --git a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/util/MappingEntryContext.java b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/util/MappingEntryContext.java index 0f7220988..d136764d0 100644 --- a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/util/MappingEntryContext.java +++ b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/util/MappingEntryContext.java @@ -40,8 +40,11 @@ import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.nat.context 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.IpPrefix; 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.inet.types.rev130715.Ipv4Prefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -94,32 +97,30 @@ public class MappingEntryContext implements Ipv4Translator, Ipv6Translator { static MappingEntryKey entryToKey( final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.mapping.table.MappingEntry entry) { // Only IPv4 - return new MappingEntryKey(new IpAddress(entry.getExternalSrcAddress()), entry.getInternalSrcAddress()); + return new MappingEntryKey(entry.getExternalSrcAddress(), entry.getInternalSrcAddress()); } private MappingEntryKey entryToKey(final Nat44StaticMappingDetails entry) { // Only IPv4 return new MappingEntryKey( - new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZone(entry.externalIpAddress))), - new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZone(entry.localIpAddress)))); + new IpPrefix(new Ipv4Prefix(toIpv4Prefix(entry.externalIpAddress,32))), + new IpPrefix(new Ipv4Prefix(toIpv4Prefix(entry.localIpAddress, 32)))); } private MappingEntryKey entryToKey(final Nat64BibDetails entry) { return new MappingEntryKey( - new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZone(entry.oAddr))), - new IpAddress(new Ipv6Address(arrayToIpv6AddressNoZone(entry.iAddr)))); + new IpPrefix(new Ipv4Prefix(toIpv4Prefix(entry.oAddr, 32))), + new IpPrefix(new Ipv6Prefix(toIpv6Prefix(entry.iAddr, 128)))); } private boolean equalEntries(final Nat44StaticMappingDetails detail, final MappingEntry ctxMappingEntry) { - final IpAddress internalAddrFromDetails = - new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZone(detail.localIpAddress))); // Only IPv4 + final IpPrefix internalAddrFromDetails = new IpPrefix(toIpv4Prefix(detail.localIpAddress, 32)); if (!ctxMappingEntry.getInternal().equals(internalAddrFromDetails)) { return false; } // Only IPv4 - final IpAddress externalAddrFromDetails = - new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZone(detail.externalIpAddress))); + final IpPrefix externalAddrFromDetails = new IpPrefix(toIpv4Prefix(detail.externalIpAddress, 32)); if (!ctxMappingEntry.getExternal().equals(externalAddrFromDetails)) { return false; } @@ -127,15 +128,13 @@ public class MappingEntryContext implements Ipv4Translator, Ipv6Translator { } private boolean equalEntries(final Nat64BibDetails detail, final MappingEntry ctxMappingEntry) { - final IpAddress internalAddrFromDetails = - new IpAddress(new Ipv6Address(arrayToIpv6AddressNoZone(detail.iAddr))); // Only IPv6 + final IpPrefix internalAddrFromDetails = new IpPrefix(toIpv6Prefix(detail.iAddr, 128)); if (!ctxMappingEntry.getInternal().equals(internalAddrFromDetails)) { return false; } // Only IPv4 - final IpAddress externalAddrFromDetails = - new IpAddress(new Ipv4Address(arrayToIpv4AddressNoZone(detail.oAddr))); + final IpPrefix externalAddrFromDetails = new IpPrefix(toIpv4Prefix(detail.oAddr, 32)); if (!ctxMappingEntry.getExternal().equals(externalAddrFromDetails)) { return false; } diff --git a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/write/MappingEntryCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/write/MappingEntryCustomizer.java index 6aca1af9e..74160683b 100644 --- a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/write/MappingEntryCustomizer.java +++ b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/write/MappingEntryCustomizer.java @@ -32,9 +32,9 @@ import io.fd.vpp.jvpp.nat.dto.Nat44AddDelStaticMapping; import io.fd.vpp.jvpp.nat.dto.Nat64AddDelStaticBib; import io.fd.vpp.jvpp.nat.future.FutureJVppNatFacade; 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.Ipv4Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; +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.inet.types.rev130715.Ipv6Prefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.PortNumber; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.Instance; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.mapping.table.MappingEntry; @@ -85,15 +85,15 @@ final class MappingEntryCustomizer implements ListWriterCustomizer<MappingEntry, @Nonnull final MappingEntry entry, @Nonnull final Long natInstanceId, final boolean isAdd) throws WriteFailedException { - final IpAddress internalSrcAddress = entry.getInternalSrcAddress(); - final Ipv4Address internalV4SrcAddress = internalSrcAddress.getIpv4Address(); - final Ipv6Address internalV6SrcAddress = internalSrcAddress.getIpv6Address(); - if (internalV4SrcAddress != null) { + final IpPrefix internalSrcPrefix = entry.getInternalSrcAddress(); + final Ipv4Prefix internalV4SrcPrefix = internalSrcPrefix.getIpv4Prefix(); + final Ipv6Prefix internalV6SrcPrefix = internalSrcPrefix.getIpv6Prefix(); + if (internalV4SrcPrefix != null) { final Nat44AddDelStaticMapping request = getNat44Request(id, entry, natInstanceId, isAdd); getReplyForWrite(jvppNat.nat44AddDelStaticMapping(request).toCompletableFuture(), id); } else { - checkState(internalV6SrcAddress != null, - "internalSrcAddress.getIpv6Address() should not return null if v4 address is not given"); + checkState(internalV6SrcPrefix != null, + "internalSrcPrefix.getIpv4Prefix() should not return null if v4 prefix is not given"); final Nat64AddDelStaticBib request = getNat64Request(id, entry, natInstanceId, isAdd); getReplyForWrite(jvppNat.nat64AddDelStaticBib(request).toCompletableFuture(), id); } @@ -147,14 +147,15 @@ final class MappingEntryCustomizer implements ListWriterCustomizer<MappingEntry, // VPP uses int, model long request.vrfId = natInstanceId.intValue(); - final Ipv4Address internalAddress = mappingEntry.getInternalSrcAddress().getIpv4Address(); + final Ipv4Prefix internalAddress = mappingEntry.getInternalSrcAddress().getIpv4Prefix(); checkArgument(internalAddress != null, "No Ipv4 present in internal-src-address %s", - mappingEntry.getInternalSrcAddress()); + mappingEntry.getInternalSrcAddress()); + checkArgument(extractPrefix(internalAddress) == 32, + "Only /32 prefix in internal-src-address is supported, but was %s", internalAddress); request.addrOnly = 1; - request.localIpAddress = - ipv4AddressNoZoneToArray(mappingEntry.getInternalSrcAddress().getIpv4Address().getValue()); - request.externalIpAddress = ipv4AddressNoZoneToArray(mappingEntry.getExternalSrcAddress().getValue()); + request.localIpAddress = ipv4AddressPrefixToArray(internalAddress); + request.externalIpAddress = ipv4AddressPrefixToArray(getExternalAddress(mappingEntry)); request.externalSwIfIndex = -1; // external ip address is ignored if externalSwIfIndex is given request.protocol = -1; final Short protocol = mappingEntry.getTransportProtocol(); @@ -185,13 +186,14 @@ final class MappingEntryCustomizer implements ListWriterCustomizer<MappingEntry, // VPP uses int, model long request.vrfId = natInstanceId.intValue(); - final Ipv6Address internalAddress = mappingEntry.getInternalSrcAddress().getIpv6Address(); + final Ipv6Prefix internalAddress = mappingEntry.getInternalSrcAddress().getIpv6Prefix(); checkArgument(internalAddress != null, "No Ipv6 present in internal-src-address %s", mappingEntry.getInternalSrcAddress()); + checkArgument(extractPrefix(internalAddress) == (byte)128, + "Only /128 prefix in internal-src-address is supported, but was %s", internalAddress); - - request.iAddr = ipv6AddressNoZoneToArray(internalAddress); - request.oAddr = ipv4AddressNoZoneToArray(mappingEntry.getExternalSrcAddress().getValue()); + request.iAddr = ipv6AddressPrefixToArray(internalAddress); + request.oAddr = ipv4AddressPrefixToArray(getExternalAddress(mappingEntry)); request.proto = -1; final Short protocol = mappingEntry.getTransportProtocol(); if (protocol != null) { @@ -210,6 +212,15 @@ final class MappingEntryCustomizer implements ListWriterCustomizer<MappingEntry, return request; } + private Ipv4Prefix getExternalAddress(final MappingEntry mappingEntry) { + final Ipv4Prefix externalAddress = mappingEntry.getExternalSrcAddress().getIpv4Prefix(); + checkArgument(externalAddress != null, "No Ipv4 present in external-src-address %s", + mappingEntry.getExternalSrcAddress()); + checkArgument(extractPrefix(externalAddress) == 32, + "Only /32 prefix in external-src-address is supported, but was %s", externalAddress); + return externalAddress; + } + private Short getPortNumber(final InstanceIdentifier<MappingEntry> id, final PortNumber portNumber) { if (portNumber != null) { if (portNumber.getStartPortNumber() != null && portNumber.getEndPortNumber() == null) { |