2 * Copyright © 2017 Ericsson, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netvirt.sfc.classifier.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
15 import org.junit.Test;
16 import org.opendaylight.genius.mdsalutil.NwConstants;
17 import org.opendaylight.genius.mdsalutil.packet.IPProtocols;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.MatchesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEthBuilder;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4Builder;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6Builder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRangeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
38 public class AclMatchesTest {
40 private static final String MAC_SRC_STR = "11:22:33:44:55:66";
41 private static final String MAC_DST_STR = "66:55:44:33:22:11";
42 private static final String IPV4_DST_STR = "10.1.2.0/24";
43 private static final String IPV4_SRC_STR = "10.1.2.3/32";
44 private static final String IPV6_DST_STR = "2001:DB8:AC10:FE01::/64";
45 private static final String IPV6_SRC_STR = "2001:db8:85a3:7334::/64";
46 private static final int TCP_SRC_LOWER_PORT = 1234;
47 private static final int TCP_SRC_UPPER_PORT = 2345;
48 private static final int TCP_DST_LOWER_PORT = 80;
49 private static final int TCP_DST_UPPER_PORT = 800;
50 private static final int UDP_SRC_LOWER_PORT = 90;
51 private static final int UDP_SRC_UPPER_PORT = 900;
52 private static final int UDP_DST_LOWER_PORT = 90;
53 private static final int UDP_DST_UPPER_PORT = 900;
54 private static final short DSCP_VALUE = (short) 42;
58 public void buildEthMatchTest() {
59 AceEthBuilder aceEthBuilder = new AceEthBuilder();
60 aceEthBuilder.setDestinationMacAddress(new MacAddress(MAC_DST_STR));
61 aceEthBuilder.setSourceMacAddress(new MacAddress(MAC_SRC_STR));
63 MatchesBuilder matchesBuilder = new MatchesBuilder();
64 matchesBuilder.setAceType(aceEthBuilder.build());
66 // Create the aclMatches that is the object to be tested
67 AclMatches aclMatches = new AclMatches(matchesBuilder.build());
68 MatchBuilder matchBuilder = aclMatches.buildMatch();
70 // The ethernet match should be there with src/dst values
71 EthernetMatch ethMatch = matchBuilder.getEthernetMatch();
72 assertNotNull(ethMatch);
73 assertEquals(ethMatch.getEthernetSource().getAddress().getValue(), MAC_SRC_STR);
74 assertEquals(ethMatch.getEthernetDestination().getAddress().getValue(), MAC_DST_STR);
76 // The rest should be null
77 assertNull(matchBuilder.getIpMatch());
78 assertNull(matchBuilder.getLayer3Match());
79 assertNull(matchBuilder.getLayer4Match());
83 public void buildIpv4MatchTest() {
84 AceIpv4Builder aceIpv4 = new AceIpv4Builder();
85 aceIpv4.setDestinationIpv4Network(new Ipv4Prefix(IPV4_DST_STR));
86 aceIpv4.setSourceIpv4Network(new Ipv4Prefix(IPV4_SRC_STR));
88 AceIpBuilder aceIpBuilder = new AceIpBuilder();
89 aceIpBuilder.setAceIpVersion(aceIpv4.build());
91 MatchesBuilder matchesBuilder = new MatchesBuilder();
92 matchesBuilder.setAceType(aceIpBuilder.build());
94 // Create the aclMatches that is the object to be tested
95 AclMatches aclMatches = new AclMatches(matchesBuilder.build());
96 MatchBuilder matchBuilder = aclMatches.buildMatch();
98 // The layer3 match should be there with src/dst values
99 Ipv4Match l3 = (Ipv4Match) matchBuilder.getLayer3Match();
101 assertEquals(l3.getIpv4Destination().getValue().toString(), IPV4_DST_STR);
102 assertEquals(l3.getIpv4Source().getValue().toString(), IPV4_SRC_STR);
104 // There should be an IPv4 etherType set
105 EthernetMatch ethMatch = matchBuilder.getEthernetMatch();
106 assertNotNull(ethMatch);
107 assertEquals(ethMatch.getEthernetType().getType().getValue(), Long.valueOf(NwConstants.ETHTYPE_IPV4));
109 // The rest should be null
110 assertNull(matchBuilder.getIpMatch());
111 assertNull(matchBuilder.getLayer4Match());
115 public void buildIpv4TcpMatchTest() {
116 AceIpBuilder aceIpBuilder = new AceIpBuilder();
117 aceIpBuilder.setAceIpVersion(new AceIpv4Builder().build());
118 aceIpBuilder.setProtocol(IPProtocols.TCP.shortValue());
120 SourcePortRangeBuilder srcPortRange = new SourcePortRangeBuilder();
121 srcPortRange.setLowerPort(new PortNumber(TCP_SRC_LOWER_PORT));
122 srcPortRange.setUpperPort(new PortNumber(TCP_SRC_UPPER_PORT));
123 aceIpBuilder.setSourcePortRange(srcPortRange.build());
125 DestinationPortRangeBuilder dstPortRange = new DestinationPortRangeBuilder();
126 dstPortRange.setLowerPort(new PortNumber(TCP_DST_LOWER_PORT));
127 dstPortRange.setUpperPort(new PortNumber(TCP_DST_UPPER_PORT));
128 aceIpBuilder.setDestinationPortRange(dstPortRange.build());
130 MatchesBuilder matchesBuilder = new MatchesBuilder();
131 matchesBuilder.setAceType(aceIpBuilder.build());
133 // Create the aclMatches that is the object to be tested
134 AclMatches aclMatches = new AclMatches(matchesBuilder.build());
135 MatchBuilder matchBuilder = aclMatches.buildMatch();
137 // There should be an IPv4 etherType set
138 EthernetMatch ethMatch = matchBuilder.getEthernetMatch();
139 assertNotNull(ethMatch);
140 assertEquals(ethMatch.getEthernetType().getType().getValue(), Long.valueOf(NwConstants.ETHTYPE_IPV4));
143 IpMatch ipMatch = matchBuilder.getIpMatch();
144 assertNotNull(ipMatch);
145 assertEquals(ipMatch.getIpProtocol(), Short.valueOf(IPProtocols.TCP.shortValue()));
147 // Currently ranges arent supported, only the lower port is used
148 TcpMatch tcpMatch = (TcpMatch) matchBuilder.getLayer4Match();
149 assertEquals(tcpMatch.getTcpSourcePort().getValue(), Integer.valueOf(TCP_SRC_LOWER_PORT));
150 assertEquals(tcpMatch.getTcpDestinationPort().getValue(), Integer.valueOf(TCP_DST_LOWER_PORT));
152 // The layer3 match should be null
153 assertNull(matchBuilder.getLayer3Match());
157 public void buildIpv4UdpMatchTest() {
158 AceIpBuilder aceIpBuilder = new AceIpBuilder();
159 aceIpBuilder.setAceIpVersion(new AceIpv4Builder().build());
160 aceIpBuilder.setProtocol(IPProtocols.UDP.shortValue());
162 SourcePortRangeBuilder srcPortRange = new SourcePortRangeBuilder();
163 srcPortRange.setLowerPort(new PortNumber(UDP_SRC_LOWER_PORT));
164 srcPortRange.setUpperPort(new PortNumber(UDP_SRC_UPPER_PORT));
165 aceIpBuilder.setSourcePortRange(srcPortRange.build());
167 DestinationPortRangeBuilder dstPortRange = new DestinationPortRangeBuilder();
168 dstPortRange.setLowerPort(new PortNumber(UDP_DST_LOWER_PORT));
169 dstPortRange.setUpperPort(new PortNumber(UDP_DST_UPPER_PORT));
170 aceIpBuilder.setDestinationPortRange(dstPortRange.build());
172 MatchesBuilder matchesBuilder = new MatchesBuilder();
173 matchesBuilder.setAceType(aceIpBuilder.build());
175 // Create the aclMatches that is the object to be tested
176 AclMatches aclMatches = new AclMatches(matchesBuilder.build());
177 MatchBuilder matchBuilder = aclMatches.buildMatch();
179 // There should be an IPv4 etherType set
180 EthernetMatch ethMatch = matchBuilder.getEthernetMatch();
181 assertNotNull(ethMatch);
182 assertEquals(ethMatch.getEthernetType().getType().getValue(), Long.valueOf(NwConstants.ETHTYPE_IPV4));
185 IpMatch ipMatch = matchBuilder.getIpMatch();
186 assertNotNull(ipMatch);
187 assertEquals(ipMatch.getIpProtocol(), Short.valueOf(IPProtocols.UDP.shortValue()));
189 // Currently ranges arent supported, only the lower port is used
190 UdpMatch udpMatch = (UdpMatch) matchBuilder.getLayer4Match();
191 assertEquals(udpMatch.getUdpSourcePort().getValue(), Integer.valueOf(UDP_SRC_LOWER_PORT));
192 assertEquals(udpMatch.getUdpDestinationPort().getValue(), Integer.valueOf(UDP_DST_LOWER_PORT));
194 // The layer3 match should be null
195 assertNull(matchBuilder.getLayer3Match());
199 public void buildIpv4DscpMatchTest() {
200 AceIpBuilder aceIpBuilder = new AceIpBuilder();
201 aceIpBuilder.setAceIpVersion(new AceIpv4Builder().build());
202 aceIpBuilder.setDscp(new Dscp(DSCP_VALUE));
204 MatchesBuilder matchesBuilder = new MatchesBuilder();
205 matchesBuilder.setAceType(aceIpBuilder.build());
207 // Create the aclMatches that is the object to be tested
208 AclMatches aclMatches = new AclMatches(matchesBuilder.build());
209 MatchBuilder matchBuilder = aclMatches.buildMatch();
211 // There should be an IPv4 etherType set
212 EthernetMatch ethMatch = matchBuilder.getEthernetMatch();
213 assertNotNull(ethMatch);
214 assertEquals(ethMatch.getEthernetType().getType().getValue(), Long.valueOf(NwConstants.ETHTYPE_IPV4));
216 // Check the DSCP value
217 IpMatch ipMatch = matchBuilder.getIpMatch();
218 assertNotNull(ipMatch);
219 assertEquals(ipMatch.getIpDscp().getValue(), Short.valueOf(DSCP_VALUE));
221 // The rest should be null
222 assertNull(matchBuilder.getLayer3Match());
223 assertNull(matchBuilder.getLayer4Match());
227 public void buildIpv6MatchTest() {
228 AceIpv6Builder aceIpv6 = new AceIpv6Builder();
229 aceIpv6.setDestinationIpv6Network(new Ipv6Prefix(IPV6_DST_STR));
230 aceIpv6.setSourceIpv6Network(new Ipv6Prefix(IPV6_SRC_STR));
232 AceIpBuilder aceIpBuilder = new AceIpBuilder();
233 aceIpBuilder.setAceIpVersion(aceIpv6.build());
235 MatchesBuilder matchesBuilder = new MatchesBuilder();
236 matchesBuilder.setAceType(aceIpBuilder.build());
238 // Create the aclMatches that is the object to be tested
239 AclMatches aclMatches = new AclMatches(matchesBuilder.build());
240 MatchBuilder matchBuilder = aclMatches.buildMatch();
242 // The layer3 match should be there with src/dst values
243 Ipv6Match l3 = (Ipv6Match) matchBuilder.getLayer3Match();
245 assertEquals(l3.getIpv6Destination().getValue().toString(), IPV6_DST_STR);
246 assertEquals(l3.getIpv6Source().getValue().toString(), IPV6_SRC_STR);
248 // There should be an IPv6 etherType set
249 EthernetMatch ethMatch = matchBuilder.getEthernetMatch();
250 assertNotNull(ethMatch);
251 assertEquals(ethMatch.getEthernetType().getType().getValue(), Long.valueOf(NwConstants.ETHTYPE_IPV6));
253 // The rest should be null
254 assertNull(matchBuilder.getIpMatch());
255 assertNull(matchBuilder.getLayer4Match());