move vpnservice and cleanup poms
[netvirt.git] / sfc / classifier / impl / src / test / java / org / opendaylight / netvirt / sfc / classifier / utils / AclMatchesTest.java
1 /*
2  * Copyright © 2017 Ericsson, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.sfc.classifier.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14
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;
37
38 public class AclMatchesTest {
39
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;
55
56
57     @Test
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));
62
63         MatchesBuilder matchesBuilder = new MatchesBuilder();
64         matchesBuilder.setAceType(aceEthBuilder.build());
65
66         // Create the aclMatches that is the object to be tested
67         AclMatches aclMatches = new AclMatches(matchesBuilder.build());
68         MatchBuilder matchBuilder = aclMatches.buildMatch();
69
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);
75
76         // The rest should be null
77         assertNull(matchBuilder.getIpMatch());
78         assertNull(matchBuilder.getLayer3Match());
79         assertNull(matchBuilder.getLayer4Match());
80     }
81
82     @Test
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));
87
88         AceIpBuilder aceIpBuilder = new AceIpBuilder();
89         aceIpBuilder.setAceIpVersion(aceIpv4.build());
90
91         MatchesBuilder matchesBuilder = new MatchesBuilder();
92         matchesBuilder.setAceType(aceIpBuilder.build());
93
94         // Create the aclMatches that is the object to be tested
95         AclMatches aclMatches = new AclMatches(matchesBuilder.build());
96         MatchBuilder matchBuilder = aclMatches.buildMatch();
97
98         // The layer3 match should be there with src/dst values
99         Ipv4Match l3 = (Ipv4Match) matchBuilder.getLayer3Match();
100         assertNotNull(l3);
101         assertEquals(l3.getIpv4Destination().getValue().toString(), IPV4_DST_STR);
102         assertEquals(l3.getIpv4Source().getValue().toString(), IPV4_SRC_STR);
103
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));
108
109         // The rest should be null
110         assertNull(matchBuilder.getIpMatch());
111         assertNull(matchBuilder.getLayer4Match());
112     }
113
114     @Test
115     public void buildIpv4TcpMatchTest() {
116         AceIpBuilder aceIpBuilder = new AceIpBuilder();
117         aceIpBuilder.setAceIpVersion(new AceIpv4Builder().build());
118         aceIpBuilder.setProtocol(IPProtocols.TCP.shortValue());
119
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());
124
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());
129
130         MatchesBuilder matchesBuilder = new MatchesBuilder();
131         matchesBuilder.setAceType(aceIpBuilder.build());
132
133         // Create the aclMatches that is the object to be tested
134         AclMatches aclMatches = new AclMatches(matchesBuilder.build());
135         MatchBuilder matchBuilder = aclMatches.buildMatch();
136
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));
141
142         // Make sure its TCP
143         IpMatch ipMatch = matchBuilder.getIpMatch();
144         assertNotNull(ipMatch);
145         assertEquals(ipMatch.getIpProtocol(), Short.valueOf(IPProtocols.TCP.shortValue()));
146
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));
151
152         // The layer3 match should be null
153         assertNull(matchBuilder.getLayer3Match());
154     }
155
156     @Test
157     public void buildIpv4UdpMatchTest() {
158         AceIpBuilder aceIpBuilder = new AceIpBuilder();
159         aceIpBuilder.setAceIpVersion(new AceIpv4Builder().build());
160         aceIpBuilder.setProtocol(IPProtocols.UDP.shortValue());
161
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());
166
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());
171
172         MatchesBuilder matchesBuilder = new MatchesBuilder();
173         matchesBuilder.setAceType(aceIpBuilder.build());
174
175         // Create the aclMatches that is the object to be tested
176         AclMatches aclMatches = new AclMatches(matchesBuilder.build());
177         MatchBuilder matchBuilder = aclMatches.buildMatch();
178
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));
183
184         // Make sure its UDP
185         IpMatch ipMatch = matchBuilder.getIpMatch();
186         assertNotNull(ipMatch);
187         assertEquals(ipMatch.getIpProtocol(), Short.valueOf(IPProtocols.UDP.shortValue()));
188
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));
193
194         // The layer3 match should be null
195         assertNull(matchBuilder.getLayer3Match());
196     }
197
198     @Test
199     public void buildIpv4DscpMatchTest() {
200         AceIpBuilder aceIpBuilder = new AceIpBuilder();
201         aceIpBuilder.setAceIpVersion(new AceIpv4Builder().build());
202         aceIpBuilder.setDscp(new Dscp(DSCP_VALUE));
203
204         MatchesBuilder matchesBuilder = new MatchesBuilder();
205         matchesBuilder.setAceType(aceIpBuilder.build());
206
207         // Create the aclMatches that is the object to be tested
208         AclMatches aclMatches = new AclMatches(matchesBuilder.build());
209         MatchBuilder matchBuilder = aclMatches.buildMatch();
210
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));
215
216         // Check the DSCP value
217         IpMatch ipMatch = matchBuilder.getIpMatch();
218         assertNotNull(ipMatch);
219         assertEquals(ipMatch.getIpDscp().getValue(), Short.valueOf(DSCP_VALUE));
220
221         // The rest should be null
222         assertNull(matchBuilder.getLayer3Match());
223         assertNull(matchBuilder.getLayer4Match());
224     }
225
226     @Test
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));
231
232         AceIpBuilder aceIpBuilder = new AceIpBuilder();
233         aceIpBuilder.setAceIpVersion(aceIpv6.build());
234
235         MatchesBuilder matchesBuilder = new MatchesBuilder();
236         matchesBuilder.setAceType(aceIpBuilder.build());
237
238         // Create the aclMatches that is the object to be tested
239         AclMatches aclMatches = new AclMatches(matchesBuilder.build());
240         MatchBuilder matchBuilder = aclMatches.buildMatch();
241
242         // The layer3 match should be there with src/dst values
243         Ipv6Match l3 = (Ipv6Match) matchBuilder.getLayer3Match();
244         assertNotNull(l3);
245         assertEquals(l3.getIpv6Destination().getValue().toString(), IPV6_DST_STR);
246         assertEquals(l3.getIpv6Source().getValue().toString(), IPV6_SRC_STR);
247
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));
252
253         // The rest should be null
254         assertNull(matchBuilder.getIpMatch());
255         assertNull(matchBuilder.getLayer4Match());
256     }
257
258 }