diff options
author | Jan Srnicek <jsrnicek@cisco.com> | 2017-01-11 18:27:43 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-01-16 11:15:21 +0000 |
commit | 3d4bfd545b1ea1f64b5aa32af80061002176f5d0 (patch) | |
tree | 94326a0fc73e80cefec12e334d0e558a3b3f9ce3 | |
parent | c4fb60c6d78c2b686650733488690d164e3b0e15 (diff) |
HC2VPP-38 / HONEYCOMB-336 - removed mandatory from ace-ip-version
Removed mandatory statements
Fixed multiple NPE
Change-Id: I30af417b0594d9634a9352f7721285257fbcee1e
Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
9 files changed, 700 insertions, 49 deletions
diff --git a/acl/acl-api/src/main/yang/vpp-acl.yang b/acl/acl-api/src/main/yang/vpp-acl.yang index 79dac9322..53818139d 100644 --- a/acl/acl-api/src/main/yang/vpp-acl.yang +++ b/acl/acl-api/src/main/yang/vpp-acl.yang @@ -163,7 +163,6 @@ module vpp-acl { choice ace-ip-version { description "IP version used in this Access List Entry."; - mandatory true; case ace-ipv4 { uses packet-fields:acl-ipv4-header-fields; } @@ -228,7 +227,6 @@ module vpp-acl { choice ace-ip-version { description "IP version used in this Access List Entry."; - mandatory true; case ace-ipv4 { uses vpp-macip-ace-ipv4-header-fields; } diff --git a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractor.java b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractor.java index b98daa5ce..503ff47dc 100644 --- a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractor.java +++ b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractor.java @@ -16,76 +16,116 @@ package io.fd.hc2vpp.acl.util.ace.extractor; + import io.fd.hc2vpp.common.translate.util.MacTranslator; import io.fd.vpp.jvpp.acl.types.MacipAclRule; +import java.util.Optional; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.Matches; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.PacketHandling; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.Deny; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.Permit; +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.yang.types.rev130715.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.VppMacipAceEthHeaderFields; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.VppMacipAceIpv4HeaderFields; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.VppMacipAceIpv6HeaderFields; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.VppMacipAce; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv6; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.VppMacipAceNodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.AceIpVersion; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv4Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv6; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv6Builder; public interface MacIpAceDataExtractor extends AddressExtractor, MacTranslator { + String DEFAULT_MAC = "00:00:00:00:00:00"; + String DEFAULT_MAC_MASK = "00:00:00:00:00:00"; + byte[] DEFAULT_MAC_BYTES = {0, 0, 0, 0, 0, 0}; + byte[] DEFAULT_MAC_MASK_BYTES = {0, 0, 0, 0, 0, 0}; + default VppMacipAce fromMacIpAce(@Nonnull final Ace ace) { - return VppMacipAce.class.cast(ace.getMatches().getAceType()); + return Optional.ofNullable(ace.getMatches()) + .map(Matches::getAceType) + .map(VppMacipAce.class::cast) + .orElseThrow( + () -> new IllegalArgumentException(String.format("Unable to create VppMacipAce from %s", ace))); } default boolean macIpIsIpv6(@Nonnull final VppMacipAce ace) { - return ace.getVppMacipAceNodes().getAceIpVersion() instanceof AceIpv6; + return Optional.ofNullable(ace.getVppMacipAceNodes()) + .map(VppMacipAceNodes::getAceIpVersion) + .map(aceIpVersion -> aceIpVersion instanceof AceIpv6) + .orElse(false); } default byte[] sourceMacAsBytes(@Nonnull final VppMacipAce ace) { - return macToByteArray(ace.getVppMacipAceNodes().getSourceMacAddress().getValue()); + return macToByteArray(Optional.ofNullable(ace.getVppMacipAceNodes()) + .map(VppMacipAceEthHeaderFields::getSourceMacAddress) + .map(MacAddress::getValue) + .orElse(DEFAULT_MAC)); } default byte[] sourceMacMaskAsBytes(@Nonnull final VppMacipAce ace) { - return macToByteArray(ace.getVppMacipAceNodes().getSourceMacAddressMask().getValue()); + return macToByteArray(Optional.ofNullable(ace.getVppMacipAceNodes()) + .map(VppMacipAceEthHeaderFields::getSourceMacAddressMask) + .map(MacAddress::getValue) + .orElse(DEFAULT_MAC_MASK)); } default byte[] ipv4Address(@Nonnull final VppMacipAce ace) { - return extractIp4Address( - VppMacipAceIpv4HeaderFields.class.cast(ace.getVppMacipAceNodes().getAceIpVersion()).getSourceIpv4Network()); + return extractIp4Address(extractV4NetworkAddressOrNull(ace)); } default byte ipv4AddressPrefix(@Nonnull final VppMacipAce ace) { - return extractIp4AddressPrefix( - VppMacipAceIpv4HeaderFields.class.cast(ace.getVppMacipAceNodes().getAceIpVersion()).getSourceIpv4Network()); + return extractIp4AddressPrefix(extractV4NetworkAddressOrNull(ace)); + } + + static Ipv4Prefix extractV4NetworkAddressOrNull(final @Nonnull VppMacipAce ace) { + return Optional.ofNullable(ace.getVppMacipAceNodes()) + .map(VppMacipAceNodes::getAceIpVersion) + .map(VppMacipAceIpv4HeaderFields.class::cast) + .map(VppMacipAceIpv4HeaderFields::getSourceIpv4Network) + .orElse(null); } default byte[] ipv6Address(@Nonnull final VppMacipAce ace) { - return extractIp6Address( - VppMacipAceIpv6HeaderFields.class.cast(ace.getVppMacipAceNodes().getAceIpVersion()).getSourceIpv6Network()); + return extractIp6Address(extractV6NetworkAddressOrNull(ace)); } default byte ipv6AddressPrefix(@Nonnull final VppMacipAce ace) { - return extractIp6AddressPrefix( - VppMacipAceIpv6HeaderFields.class.cast(ace.getVppMacipAceNodes().getAceIpVersion()).getSourceIpv6Network()); + return extractIp6AddressPrefix(extractV6NetworkAddressOrNull(ace)); + } + + default Ipv6Prefix extractV6NetworkAddressOrNull(@Nonnull final VppMacipAce ace) { + return Optional.ofNullable(ace.getVppMacipAceNodes()) + .map(VppMacipAceNodes::getAceIpVersion) + .map(VppMacipAceIpv6HeaderFields.class::cast) + .map(VppMacipAceIpv6HeaderFields::getSourceIpv6Network) + .orElse(null); } /** * Only 0 and 1 are allowed for mac-ip */ default byte macIpAction(@Nonnull final Ace ace) { - final PacketHandling action = ace.getActions().getPacketHandling(); + // action choice itself has default, but nothing stops us from not defining actions container itself + final PacketHandling action = Optional.ofNullable(ace.getActions()).orElseThrow( + () -> new IllegalArgumentException(String.format("Unable to extract Action from %s", ace))) + .getPacketHandling(); if (action instanceof Permit) { return 1; } else if (action instanceof Deny) { return 0; } else { throw new IllegalArgumentException( - String.format("Unsupported packet-handling action %s for ACE %s", action, ace)); + String.format("Unsupported packet-handling action %s for ACE %s", action, ace)); } } - default AceIpVersion ipVersion(final MacipAclRule rule) { + default AceIpVersion ipVersion(@Nonnull final MacipAclRule rule) { if (rule.isIpv6 == 0) { return ip4Ace(rule); } else { @@ -93,7 +133,7 @@ public interface MacIpAceDataExtractor extends AddressExtractor, MacTranslator { } } - default AceIpVersion ip4Ace(MacipAclRule rule) { + default AceIpVersion ip4Ace(@Nonnull final MacipAclRule rule) { final AceIpv4Builder ipVersion = new AceIpv4Builder(); if (rule.srcIpAddr != null && rule.srcIpAddr.length != 0) { ipVersion.setSourceIpv4Network(toIpv4Prefix(truncateIp4Array(rule.srcIpAddr), rule.srcIpPrefixLen)); @@ -101,7 +141,7 @@ public interface MacIpAceDataExtractor extends AddressExtractor, MacTranslator { return ipVersion.build(); } - default AceIpVersion ip6Ace(MacipAclRule rule) { + default AceIpVersion ip6Ace(@Nonnull final MacipAclRule rule) { final AceIpv6Builder ipVersion = new AceIpv6Builder(); if (rule.srcIpAddr != null && rule.srcIpAddr.length != 0) { ipVersion.setSourceIpv6Network(toIpv6Prefix(rule.srcIpAddr, rule.srcIpPrefixLen)); @@ -109,13 +149,15 @@ public interface MacIpAceDataExtractor extends AddressExtractor, MacTranslator { return ipVersion.build(); } - default MacAddress sourceMac(final MacipAclRule rule) { - return new MacAddress(byteArrayToMacSeparated(rule.srcMac)); + default MacAddress sourceMac(@Nonnull final MacipAclRule rule) { + return new MacAddress(byteArrayToMacSeparated(rule.srcMac != null + ? rule.srcMac + : DEFAULT_MAC_BYTES)); } - default MacAddress sourceMacMask(final MacipAclRule rule) { - return new MacAddress(byteArrayToMacSeparated(rule.srcMacMask)); + default MacAddress sourceMacMask(@Nonnull final MacipAclRule rule) { + return new MacAddress(byteArrayToMacSeparated(rule.srcMacMask != null + ? rule.srcMacMask + : DEFAULT_MAC_MASK_BYTES)); } - - } diff --git a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractor.java b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractor.java index 664023947..49587ec7f 100644 --- a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractor.java +++ b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractor.java @@ -21,22 +21,26 @@ import io.fd.hc2vpp.acl.util.protocol.IpProtocolReader; import io.fd.hc2vpp.acl.util.protocol.ProtoPreBindRuleProducer; import io.fd.vpp.jvpp.acl.types.AclRule; import java.util.Map; +import java.util.Optional; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.Actions; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.ActionsBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.Matches; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.PacketHandling; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.Deny; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.DenyBuilder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.Permit; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.PermitBuilder; +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.packet.fields.rev160708.AclIpv4HeaderFields; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.AclIpv6HeaderFields; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.actions.packet.handling.Stateful; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.actions.packet.handling.StatefulBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.VppAce; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.VppAceNodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.AceIpVersion; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv4; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv4Builder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv6; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv6Builder; @@ -50,55 +54,94 @@ public interface StandardAceDataExtractor extends AddressExtractor, ProtoPreBind Stateful.class, 2); default VppAce fromStandardAce(@Nonnull final Ace ace) { - return VppAce.class.cast(ace.getMatches().getAceType()); + return Optional.ofNullable(ace.getMatches()) + .map(Matches::getAceType) + .map(VppAce.class::cast) + .orElseThrow(() -> new IllegalArgumentException(String.format("Unable to create VppAce from %s", ace))); } default boolean standardIsIpv6(@Nonnull final Ace ace) { - return VppAce.class.cast(ace.getMatches().getAceType()).getVppAceNodes().getAceIpVersion() instanceof AceIpv6; + return Optional.ofNullable(ace.getMatches()) + .map(Matches::getAceType) + .map(VppAce.class::cast) + .map(VppAce::getVppAceNodes) + .map(VppAceNodes::getAceIpVersion) + .map(aceIpVersion -> aceIpVersion instanceof AceIpv6) + .orElse(false); } default byte[] ipv4SourceAddress(@Nonnull final VppAce ace) { return extractIp4Address( - AclIpv4HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getSourceIpv4Network()); + extractV4SourceAddressOrNull(ace)); } default byte ipv4SourceAddressPrefix(@Nonnull final VppAce ace) { return extractIp4AddressPrefix( - AclIpv4HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getSourceIpv4Network()); + extractV4SourceAddressOrNull(ace)); } + static Ipv4Prefix extractV4SourceAddressOrNull(@Nonnull final VppAce ace) { + return Optional.ofNullable(ace.getVppAceNodes()) + .map(VppAceNodes::getAceIpVersion) + .map(AclIpv4HeaderFields.class::cast) + .map(AclIpv4HeaderFields::getSourceIpv4Network) + .orElse(null); + } + + default byte[] ipv4DestinationAddress(@Nonnull final VppAce ace) { - return extractIp4Address( - AclIpv4HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getDestinationIpv4Network()); + return extractIp4Address(extractV4DestinationAddressOrNull(ace)); } default byte ipv4DestinationAddressPrefix(@Nonnull final VppAce ace) { - return extractIp4AddressPrefix(AceIpv4.class.cast(ace.getVppAceNodes().getAceIpVersion()).getDestinationIpv4Network()); + return extractIp4AddressPrefix(extractV4DestinationAddressOrNull(ace)); } + static Ipv4Prefix extractV4DestinationAddressOrNull(@Nonnull final VppAce ace) { + return Optional.ofNullable(ace.getVppAceNodes()) + .map(VppAceNodes::getAceIpVersion) + .map(AclIpv4HeaderFields.class::cast) + .map(AclIpv4HeaderFields::getDestinationIpv4Network) + .orElse(null); + } default byte[] ipv6SourceAddress(@Nonnull final VppAce ace) { - return extractIp6Address( - AclIpv6HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getSourceIpv6Network()); + return extractIp6Address(extractV6SourceAddressOrNull(ace)); } default byte ipv6SourceAddressPrefix(@Nonnull final VppAce ace) { - return extractIp6AddressPrefix( - AclIpv6HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getSourceIpv6Network()); + return extractIp6AddressPrefix(extractV6SourceAddressOrNull(ace)); + } + + static Ipv6Prefix extractV6SourceAddressOrNull(@Nonnull final VppAce ace) { + return Optional.ofNullable(ace.getVppAceNodes()) + .map(VppAceNodes::getAceIpVersion) + .map(AclIpv6HeaderFields.class::cast) + .map(AclIpv6HeaderFields::getSourceIpv6Network) + .orElse(null); } default byte[] ipv6DestinationAddress(@Nonnull final VppAce ace) { - return extractIp6Address( - AclIpv6HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getDestinationIpv6Network()); + return extractIp6Address(extractV6DestinationAddressOrNull(ace)); } default byte ipv6DestinationAddressPrefix(@Nonnull final VppAce ace) { - return extractIp6AddressPrefix( - AclIpv6HeaderFields.class.cast(ace.getVppAceNodes().getAceIpVersion()).getDestinationIpv6Network()); + return extractIp6AddressPrefix(extractV6DestinationAddressOrNull(ace)); + } + + static Ipv6Prefix extractV6DestinationAddressOrNull(@Nonnull final VppAce ace) { + return Optional.ofNullable(ace.getVppAceNodes()) + .map(VppAceNodes::getAceIpVersion) + .map(AclIpv6HeaderFields.class::cast) + .map(AclIpv6HeaderFields::getDestinationIpv6Network) + .orElse(null); } default byte standardAction(@Nonnull final Ace ace) { - final PacketHandling action = ace.getActions().getPacketHandling(); + // default == deny + final PacketHandling action = Optional.ofNullable(ace.getActions()) + .orElseThrow(() -> new IllegalArgumentException(String.format("Unable to extract Action from %s", ace))) + .getPacketHandling(); return ACTION_VALUE_PAIRS.get(ACTION_VALUE_PAIRS.keySet().stream() .filter(aClass -> aClass.isInstance(action)) .findAny() @@ -107,7 +150,7 @@ public interface StandardAceDataExtractor extends AddressExtractor, ProtoPreBind ace.getRuleName())))).byteValue(); } - default AceIpVersion ipVersion(final AclRule rule) { + default AceIpVersion ipVersion(@Nonnull final AclRule rule) { if (rule.isIpv6 == 0) { return ip4Ace(rule); } else { @@ -115,7 +158,7 @@ public interface StandardAceDataExtractor extends AddressExtractor, ProtoPreBind } } - default AceIpVersion ip4Ace(final AclRule rule) { + default AceIpVersion ip4Ace(@Nonnull final AclRule rule) { final AceIpv4Builder ipVersion = new AceIpv4Builder(); if (rule.srcIpAddr != null && rule.srcIpAddr.length != 0) { ipVersion.setSourceIpv4Network(toIpv4Prefix(truncateIp4Array(rule.srcIpAddr), rule.srcIpPrefixLen)); @@ -126,7 +169,7 @@ public interface StandardAceDataExtractor extends AddressExtractor, ProtoPreBind return ipVersion.build(); } - default AceIpVersion ip6Ace(final AclRule rule) { + default AceIpVersion ip6Ace(@Nonnull final AclRule rule) { final AceIpv6Builder ipVersion = new AceIpv6Builder(); if (rule.srcIpAddr != null && rule.srcIpAddr.length != 0) { ipVersion.setSourceIpv6Network(toIpv6Prefix(rule.srcIpAddr, rule.srcIpPrefixLen)); diff --git a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/acl/AclDataExtractor.java b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/acl/AclDataExtractor.java index 77e58fe0c..8c6cdcfef 100644 --- a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/acl/AclDataExtractor.java +++ b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/acl/AclDataExtractor.java @@ -18,6 +18,7 @@ package io.fd.hc2vpp.acl.util.acl; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Optional; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace; @@ -56,13 +57,16 @@ public interface AclDataExtractor { } default List<Ace> getAces(@Nonnull final Acl acl) { - return acl.getAccessListEntries().getAce(); + return Optional.ofNullable(acl.getAccessListEntries()).orElseThrow(() -> + new IllegalArgumentException(String.format("Unable to extract aces from %s", acl))).getAce(); } /** * Convert {@link Acl} name to byte array as UTF_8 */ default byte[] getAclNameAsBytes(@Nonnull final Acl acl) { - return acl.getAclName().getBytes(StandardCharsets.UTF_8); + return Optional.ofNullable(acl.getAclName()) + .orElseThrow(() -> new IllegalArgumentException("Unable to extract bytes for null")) + .getBytes(StandardCharsets.UTF_8); } } diff --git a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/write/InterfaceAclCustomizer.java b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/write/InterfaceAclCustomizer.java index f83808205..43360c629 100644 --- a/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/write/InterfaceAclCustomizer.java +++ b/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/write/InterfaceAclCustomizer.java @@ -87,7 +87,7 @@ public class InterfaceAclCustomizer extends FutureJVppAclCustomizer implements W .executeAsDelete(getjVppAclFacade()); } - private static List<String> getAclNames(@Nonnull final VppAclsBaseAttributes acls) { + private static List<String> getAclNames(final VppAclsBaseAttributes acls) { if (acls == null || acls.getVppAcls() == null) { return Collections.emptyList(); } else { diff --git a/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/AceDataExtractorTestCase.java b/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/AceDataExtractorTestCase.java new file mode 100644 index 000000000..fc925c992 --- /dev/null +++ b/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/AceDataExtractorTestCase.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2017 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.hc2vpp.acl.util.ace.extractor; + +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.yang.types.rev130715.MacAddress; + +abstract class AceDataExtractorTestCase { + + protected static final byte[] DEFAULT_MAC_ADDRESS_BYTES = new byte[]{0, 0, 0, 0, 0, 0}; + protected static final MacAddress DEFAULT_MAC_ADDRESS = new MacAddress("00:00:00:00:00:00"); + protected static final MacAddress DEFAULT_MAC_MASK_ADDRESS = new MacAddress("00:00:00:00:00:00"); + protected static final MacAddress MAC_ADDRESS = new MacAddress("00:11:11:00:11:11"); + protected static final MacAddress MAC_ADDRESS_MASK = new MacAddress("00:11:00:11:11:11"); + protected static final byte[] MAC_ADDRESS_BYTES = {0, 17, 17, 0, 17, 17}; + protected static final byte[] MAC_ADDRESS_MASK_BYTES = {0, 17, 0, 17, 17, 17}; + + protected static final byte[] DEFAULT_IPV4_ADDRESS_BYTES = new byte[4]; + protected static final Ipv4Prefix IPV4_PREFIX = new Ipv4Prefix("192.168.2.1/32"); + protected static final Ipv4Prefix IPV4_2_PREFIX = new Ipv4Prefix("192.168.2.2/24"); + protected static final byte[] IPV4_PREFIX_BYTES = {-64, -88, 2, 1}; + protected static final byte[] IPV4_2_PREFIX_BYTES = {-64, -88, 2, 2}; + protected static final byte IPV4_PREFIX_VALUE = (byte) 32; + protected static final byte IPV4_2_PREFIX_VALUE = (byte) 24; + protected static final int DEFAULT_IPV4_PREFIX_VALUE = 0; + + protected static final byte[] DEFAULT_IPV6_ADDRESS_BYTES = new byte[16]; + protected static final Ipv6Prefix IPV6_PREFIX = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:1/64"); + protected static final Ipv6Prefix IPV6_2_PREFIX = new Ipv6Prefix("2001:db8:a0b:12f0:0:0:0:2/48"); + protected static final byte[] IPV6_PREFIX_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1}; + protected static final byte[] IPV6_2_PREFIX_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 2}; + protected static final byte IPV6_PREFIX_VALUE = (byte) 64; + protected static final byte IPV6_2_PREFIX_VALUE = (byte) 48; + protected static final int DEFAULT_IPV6_PREFIX_VALUE = 0; +} diff --git a/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractorTest.java b/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractorTest.java new file mode 100644 index 000000000..5eb733c1e --- /dev/null +++ b/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/MacIpAceDataExtractorTest.java @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2017 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.hc2vpp.acl.util.ace.extractor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import io.fd.hc2vpp.common.test.util.CommonTests; +import io.fd.vpp.jvpp.acl.types.MacipAclRule; +import java.util.Arrays; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.AceBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.ActionsBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.MatchesBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.DenyBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.PermitBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.VppMacipAce; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.VppMacipAceBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.VppMacipAceNodesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.AceIpVersion; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv4; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv4Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv6; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.macip.ace.vpp.macip.ace.nodes.ace.ip.version.AceIpv6Builder; + +public class MacIpAceDataExtractorTest extends AceDataExtractorTestCase implements MacIpAceDataExtractor, CommonTests { + + @Test + public void testFromMacIpAce() { + verifyExceptionalCase(() -> fromMacIpAce(new AceBuilder().build()), IllegalArgumentException.class); + verifyExceptionalCase(() -> fromMacIpAce(new AceBuilder().setMatches(new MatchesBuilder().build()).build()), + IllegalArgumentException.class); + + final VppMacipAce macipAce = new VppMacipAceBuilder().build(); + assertEquals(macipAce, fromMacIpAce(new AceBuilder().setMatches(new MatchesBuilder() + .setAceType(macipAce).build()).build())); + } + + @Test + public void testMacIpIsIpv6() { + assertFalse(macIpIsIpv6(new VppMacipAceBuilder().build())); + assertFalse(macIpIsIpv6( + new VppMacipAceBuilder().setVppMacipAceNodes(new VppMacipAceNodesBuilder().build()).build())); + assertFalse(macIpIsIpv6(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build())); + assertTrue(macIpIsIpv6(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build())); + } + + @Test + public void testSourceMacAsBytes() { + assertTrue(Arrays.equals(DEFAULT_MAC_ADDRESS_BYTES, sourceMacAsBytes(new VppMacipAceBuilder().build()))); + assertTrue( + Arrays.equals(DEFAULT_MAC_ADDRESS_BYTES, sourceMacAsBytes(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(MAC_ADDRESS_BYTES, + sourceMacAsBytes(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setSourceMacAddress(MAC_ADDRESS).build()) + .build()))); + } + + @Test + public void sourceMacMaskAsBytes() { + assertTrue(Arrays.equals(DEFAULT_MAC_ADDRESS_BYTES, sourceMacMaskAsBytes(new VppMacipAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_MAC_ADDRESS_BYTES, + sourceMacMaskAsBytes(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(MAC_ADDRESS_BYTES, + sourceMacMaskAsBytes(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setSourceMacAddressMask(MAC_ADDRESS).build()) + .build()))); + } + + @Test + public void testIpv4Address() { + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4Address(new VppMacipAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4Address( + new VppMacipAceBuilder().setVppMacipAceNodes(new VppMacipAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4Address(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build()))); + assertTrue(Arrays.equals(IPV4_PREFIX_BYTES, ipv4Address(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv4Builder() + .setSourceIpv4Network(IPV4_PREFIX).build()).build()).build()))); + } + + @Test + public void testIpv4AddressPrefix() { + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4AddressPrefix(new VppMacipAceBuilder().build())); + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4AddressPrefix( + new VppMacipAceBuilder().setVppMacipAceNodes(new VppMacipAceNodesBuilder().build()).build())); + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4AddressPrefix(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build())); + assertEquals(IPV4_PREFIX_VALUE, ipv4AddressPrefix(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv4Builder() + .setSourceIpv4Network(IPV4_PREFIX).build()).build()).build())); + } + + @Test + public void testIpv6Address() { + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6Address(new VppMacipAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6Address( + new VppMacipAceBuilder().setVppMacipAceNodes(new VppMacipAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6Address(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build()))); + assertTrue(Arrays.equals(IPV6_PREFIX_BYTES, ipv6Address(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv6Builder() + .setSourceIpv6Network(IPV6_PREFIX).build()).build()).build()))); + } + + @Test + public void testIpv6AddressPrefix() { + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6AddressPrefix(new VppMacipAceBuilder().build())); + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6AddressPrefix( + new VppMacipAceBuilder().setVppMacipAceNodes(new VppMacipAceNodesBuilder().build()).build())); + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6AddressPrefix(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build())); + assertEquals(IPV6_PREFIX_VALUE, ipv6AddressPrefix(new VppMacipAceBuilder().setVppMacipAceNodes( + new VppMacipAceNodesBuilder().setAceIpVersion(new AceIpv6Builder() + .setSourceIpv6Network(IPV6_PREFIX).build()).build()).build())); + } + + @Test + public void testMacIpAction() { + verifyExceptionalCase(() -> macIpAction(new AceBuilder().build()), IllegalArgumentException.class); + verifyExceptionalCase(() -> macIpAction(new AceBuilder().setActions(new ActionsBuilder().build()).build()), + IllegalArgumentException.class); + // this one must pass even if deny is not fully set, because of default value definition + assertEquals((byte) 0, macIpAction(new AceBuilder().setActions(new ActionsBuilder().setPacketHandling( + new DenyBuilder().build()).build()).build())); + + assertEquals((byte) 1, macIpAction(new AceBuilder().setActions(new ActionsBuilder().setPacketHandling( + new PermitBuilder().setPermit(true).build()).build()).build())); + } + + @Test + public void testIpVersionV4Defined() { + MacipAclRule rule = new MacipAclRule(); + + rule.isIpv6 = 0; + rule.srcIpAddr = IPV4_PREFIX_BYTES; + rule.srcIpPrefixLen = IPV4_PREFIX_VALUE; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv4); + assertEquals(IPV4_PREFIX, AceIpv4.class.cast(result).getSourceIpv4Network()); + } + + @Test + public void testIpVersionV4Undefined() { + MacipAclRule rule = new MacipAclRule(); + + rule.isIpv6 = 0; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv4); + assertNull(AceIpv4.class.cast(result).getSourceIpv4Network()); + } + + @Test + public void testIpVersionV6Defined() { + MacipAclRule rule = new MacipAclRule(); + + rule.isIpv6 = 1; + rule.srcIpAddr = IPV6_PREFIX_BYTES; + rule.srcIpPrefixLen = IPV6_PREFIX_VALUE; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv6); + assertEquals(IPV6_PREFIX, AceIpv6.class.cast(result).getSourceIpv6Network()); + } + + @Test + public void testIpVersionV6Undefined() { + MacipAclRule rule = new MacipAclRule(); + + rule.isIpv6 = 1; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv6); + assertNull(AceIpv6.class.cast(result).getSourceIpv6Network()); + } + + @Test + public void testSourceMac() { + assertEquals(DEFAULT_MAC_ADDRESS, sourceMac(new MacipAclRule())); + + + MacipAclRule rule = new MacipAclRule(); + rule.srcMac = MAC_ADDRESS_BYTES; + assertEquals(MAC_ADDRESS, sourceMac(rule)); + } + + @Test + public void testSourceMacMask() { + assertEquals(DEFAULT_MAC_MASK_ADDRESS, sourceMacMask(new MacipAclRule())); + + + MacipAclRule rule = new MacipAclRule(); + rule.srcMac = MAC_ADDRESS_MASK_BYTES; + assertEquals(MAC_ADDRESS_MASK, sourceMac(rule)); + } +}
\ No newline at end of file diff --git a/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractorTest.java b/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractorTest.java new file mode 100644 index 000000000..35add44a2 --- /dev/null +++ b/acl/acl-impl/src/test/java/io/fd/hc2vpp/acl/util/ace/extractor/StandardAceDataExtractorTest.java @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2017 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.hc2vpp.acl.util.ace.extractor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import io.fd.hc2vpp.common.test.util.CommonTests; +import io.fd.vpp.jvpp.acl.types.AclRule; +import java.util.Arrays; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.AceBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.ActionsBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.MatchesBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.Deny; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.DenyBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.Permit; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.actions.packet.handling.Stateful; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.actions.packet.handling.StatefulBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.VppAce; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.VppAceBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.VppAceNodesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.AceIpVersion; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv4; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv4Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv6; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.vpp.ace.vpp.ace.nodes.ace.ip.version.AceIpv6Builder; + + +public class StandardAceDataExtractorTest extends AceDataExtractorTestCase implements StandardAceDataExtractor, + CommonTests { + + @Test + public void testFromStandardAce() { + verifyExceptionalCase(() -> fromStandardAce(new AceBuilder().build()), IllegalArgumentException.class); + verifyExceptionalCase(() -> fromStandardAce(new AceBuilder().setMatches(new MatchesBuilder().build()).build()), + IllegalArgumentException.class); + + final VppAce ace = new VppAceBuilder().build(); + assertEquals(ace, fromStandardAce(new AceBuilder().setMatches(new MatchesBuilder() + .setAceType(ace).build()).build())); + } + + @Test + public void testStandardIsIpv6() { + assertFalse(standardIsIpv6(new AceBuilder().build())); + assertFalse(standardIsIpv6(new AceBuilder().setMatches(new MatchesBuilder().build()).build())); + assertFalse(standardIsIpv6( + new AceBuilder().setMatches(new MatchesBuilder().setAceType(new VppAceBuilder().build()).build()) + .build())); + assertFalse(standardIsIpv6(new AceBuilder().setMatches(new MatchesBuilder() + .setAceType(new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build()).build()) + .build())); + assertTrue(standardIsIpv6(new AceBuilder().setMatches(new MatchesBuilder().setAceType(new VppAceBuilder() + .setVppAceNodes(new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build()) + .build()).build())); + } + + @Test + public void testIpv4SourceAddress() { + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4SourceAddress(new VppAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4SourceAddress( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4SourceAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build()))); + assertTrue(Arrays.equals(IPV4_PREFIX_BYTES, ipv4SourceAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder() + .setSourceIpv4Network(IPV4_PREFIX).build()).build()).build()))); + } + + @Test + public void testIpv4SourceAddressPrefix() { + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4SourceAddressPrefix(new VppAceBuilder().build())); + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4SourceAddressPrefix( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build())); + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4SourceAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build())); + assertEquals(IPV4_PREFIX_VALUE, ipv4SourceAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder() + .setSourceIpv4Network(IPV4_PREFIX).build()).build()).build())); + } + + @Test + public void testIpv4DestinationAddress() { + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4DestinationAddress(new VppAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4DestinationAddress( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(DEFAULT_IPV4_ADDRESS_BYTES, ipv4DestinationAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build()))); + assertTrue(Arrays.equals(IPV4_PREFIX_BYTES, ipv4DestinationAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder() + .setDestinationIpv4Network(IPV4_PREFIX).build()).build()).build()))); + } + + @Test + public void testIpv4DestinationAddressPrefix() { + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4DestinationAddressPrefix(new VppAceBuilder().build())); + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4DestinationAddressPrefix( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build())); + assertEquals(DEFAULT_IPV4_PREFIX_VALUE, ipv4DestinationAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder().build()).build()).build())); + assertEquals(IPV4_PREFIX_VALUE, ipv4DestinationAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv4Builder() + .setDestinationIpv4Network(IPV4_PREFIX).build()).build()).build())); + } + + @Test + public void testIpv6SourceAddress() { + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6SourceAddress(new VppAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6SourceAddress( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6SourceAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build()))); + assertTrue(Arrays.equals(IPV6_PREFIX_BYTES, ipv6SourceAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder() + .setSourceIpv6Network(IPV6_PREFIX).build()).build()).build()))); + } + + @Test + public void ipv6SourceAddressPrefix() { + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6SourceAddressPrefix(new VppAceBuilder().build())); + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6SourceAddressPrefix( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build())); + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6SourceAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build())); + assertEquals(IPV6_PREFIX_VALUE, ipv6SourceAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder() + .setSourceIpv6Network(IPV6_PREFIX).build()).build()).build())); + } + + @Test + public void ipv6DestinationAddress() { + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6DestinationAddress(new VppAceBuilder().build()))); + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6DestinationAddress( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build()))); + assertTrue(Arrays.equals(DEFAULT_IPV6_ADDRESS_BYTES, ipv6DestinationAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build()))); + assertTrue(Arrays.equals(IPV6_PREFIX_BYTES, ipv6DestinationAddress(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder() + .setDestinationIpv6Network(IPV6_PREFIX).build()).build()).build()))); + } + + @Test + public void ipv6DestinationAddressPrefix() { + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6DestinationAddressPrefix(new VppAceBuilder().build())); + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6DestinationAddressPrefix( + new VppAceBuilder().setVppAceNodes(new VppAceNodesBuilder().build()).build())); + assertEquals(DEFAULT_IPV6_PREFIX_VALUE, ipv6DestinationAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder().build()).build()).build())); + assertEquals(IPV6_PREFIX_VALUE, ipv6DestinationAddressPrefix(new VppAceBuilder().setVppAceNodes( + new VppAceNodesBuilder().setAceIpVersion(new AceIpv6Builder() + .setDestinationIpv6Network(IPV6_PREFIX).build()).build()).build())); + } + + @Test + public void testStandardAction() { + verifyExceptionalCase(() -> standardAction(new AceBuilder().build()), IllegalArgumentException.class); + verifyExceptionalCase(() -> standardAction(new AceBuilder().setActions(new ActionsBuilder().build()).build()), + IllegalArgumentException.class); + + // this one should pass because of default value + assertEquals(0, standardAction( + new AceBuilder().setActions(new ActionsBuilder().setPacketHandling(new DenyBuilder().build()).build()) + .build())); + + assertEquals(2, standardAction(new AceBuilder().setActions( + new ActionsBuilder().setPacketHandling(new StatefulBuilder().setPermit(true).build()).build()) + .build())); + } + + @Test + public void testIpVersionV4Defined() { + AclRule rule = new AclRule(); + + rule.isIpv6 = 0; + rule.srcIpAddr = IPV4_PREFIX_BYTES; + rule.srcIpPrefixLen = IPV4_PREFIX_VALUE; + rule.dstIpAddr = IPV4_2_PREFIX_BYTES; + rule.dstIpPrefixLen = IPV4_2_PREFIX_VALUE; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv4); + assertEquals(IPV4_PREFIX, AceIpv4.class.cast(result).getSourceIpv4Network()); + assertEquals(IPV4_2_PREFIX, AceIpv4.class.cast(result).getDestinationIpv4Network()); + } + + @Test + public void testIpVersionV4Undefined() { + AclRule rule = new AclRule(); + + rule.isIpv6 = 0; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv4); + assertNull(AceIpv4.class.cast(result).getSourceIpv4Network()); + assertNull(AceIpv4.class.cast(result).getDestinationIpv4Network()); + } + + @Test + public void testIpVersionV6Defined() { + AclRule rule = new AclRule(); + + rule.isIpv6 = 1; + rule.srcIpAddr = IPV6_PREFIX_BYTES; + rule.srcIpPrefixLen = IPV6_PREFIX_VALUE; + rule.dstIpAddr = IPV6_2_PREFIX_BYTES; + rule.dstIpPrefixLen = IPV6_2_PREFIX_VALUE; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv6); + assertEquals(IPV6_PREFIX, AceIpv6.class.cast(result).getSourceIpv6Network()); + assertEquals(IPV6_2_PREFIX, AceIpv6.class.cast(result).getDestinationIpv6Network()); + } + + @Test + public void testIpVersionV6Undefined() { + AclRule rule = new AclRule(); + + rule.isIpv6 = 1; + + final AceIpVersion result = ipVersion(rule); + assertTrue(result instanceof AceIpv6); + assertNull(AceIpv6.class.cast(result).getSourceIpv6Network()); + assertNull(AceIpv6.class.cast(result).getDestinationIpv6Network()); + } + + + @Test + public void testActions() { + verifyExceptionalCase(() -> actions((byte) -1), IllegalArgumentException.class); + assertTrue(actions((byte) 0).getPacketHandling() instanceof Deny); + assertTrue(actions((byte) 1).getPacketHandling() instanceof Permit); + assertTrue(actions((byte) 2).getPacketHandling() instanceof Stateful); + } + +}
\ No newline at end of file diff --git a/vpp-common/vpp-translate-test/src/main/java/io/fd/hc2vpp/common/test/util/CommonTests.java b/vpp-common/vpp-translate-test/src/main/java/io/fd/hc2vpp/common/test/util/CommonTests.java new file mode 100644 index 000000000..53d84f5f8 --- /dev/null +++ b/vpp-common/vpp-translate-test/src/main/java/io/fd/hc2vpp/common/test/util/CommonTests.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017 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.hc2vpp.common.test.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import javax.annotation.Nonnull; + +public interface CommonTests { + + default void verifyExceptionalCase(@Nonnull final Performer negativeCase, + @Nonnull final Class<? extends Exception> expectedExceptionType) { + try { + negativeCase.perform(); + } catch (Exception e) { + assertEquals(e.getLocalizedMessage(), e.getClass(), expectedExceptionType); + return; + } + fail("Test should have thrown exception"); + } + + /** + * Used to just perform test, without consuming or supplying anything + */ + @FunctionalInterface + interface Performer { + void perform(); + } +} |