Use Java declarations instead of Google Collections
[netvirt.git] / vpnservice / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / utils / AclServiceOFFlowBuilderTest.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.aclservice.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.stream.Collectors;
19 import org.junit.Test;
20 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
21 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
22 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
23 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
24 import org.opendaylight.genius.mdsalutil.matches.MatchIcmpv4;
25 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
26 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Source;
27 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;
28 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;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
30
31
32 public class AclServiceOFFlowBuilderTest {
33
34     @Test
35     public void testProgramIpFlow_NullMatches() {
36         assertNull(AclServiceOFFlowBuilder.programIpFlow(null));
37     }
38
39     @Test
40     public void testprogramOtherProtocolFlow() {
41         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null,
42                 (short) 1);
43         Map<String, List<MatchInfoBase>> flowMatchesMap =
44                 AclServiceOFFlowBuilder.programOtherProtocolFlow(builder.build());
45         List<MatchInfoBase> flowMatches = flowMatchesMap.get("OTHER_PROTO" + "1");
46         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
47     }
48
49     @Test
50     public void testprogramIcmpFlow() {
51         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "2048",
52                 (short) 1);
53         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programIcmpFlow(builder.build());
54         List<MatchInfoBase> flowMatches = flowMatchesMap.entrySet().iterator().next().getValue();
55
56         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
57
58         int matches = 0;
59         MatchIcmpv4 check = new MatchIcmpv4((short) 1024, (short) 2048);
60         for (MatchInfoBase flowMatch : flowMatches) {
61             if (check.equals(flowMatch)) {
62                 matches++;
63             }
64         }
65         assertEquals(2, matches);
66     }
67
68     @Test
69     public void testprogramTcpFlow_NoSrcDstPortRange() {
70         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null,
71                 (short) 1);
72
73         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programTcpFlow(builder.build());
74         List<MatchInfoBase> flowMatches = flowMatchesMap.get("TCP_SOURCE_ALL_");
75
76         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
77         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchFieldType.nx_tcp_src_with_mask);
78         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchFieldType.nx_tcp_dst_with_mask);
79     }
80
81     @Test
82     public void testprogramTcpFlow_WithSrcDstPortRange() {
83         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024",
84                 (short) 1);
85
86         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programTcpFlow(builder.build());
87
88         List<MatchInfoBase> srcFlowMatches = new ArrayList<>();
89         List<MatchInfoBase> dstFlowMatches = new ArrayList<>();
90
91         for (String flowId : flowMatchesMap.keySet()) {
92             if (flowId.startsWith("TCP_SOURCE_")) {
93                 srcFlowMatches.addAll(flowMatchesMap.get(flowId));
94             }
95             if (flowId.startsWith("TCP_DESTINATION_")) {
96                 dstFlowMatches.addAll(flowMatchesMap.get(flowId));
97             }
98         }
99
100         AclServiceTestUtils.verifyGeneralFlows(srcFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
101         List<MatchInfoBase> tcpSrcMatches = srcFlowMatches.stream().filter(
102             item -> item instanceof NxMatchInfo && ((NxMatchInfo) item).getMatchField().equals(
103                         NxMatchFieldType.nx_tcp_src_with_mask)).collect(Collectors.toList());
104
105         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) tcpSrcMatches.get(0), "1024", "65535");
106
107         AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
108         List<MatchInfoBase> tcpDstMatches = dstFlowMatches.stream().filter(
109             item -> item instanceof NxMatchInfo && ((NxMatchInfo) item).getMatchField().equals(
110                         NxMatchFieldType.nx_tcp_dst_with_mask)).collect(Collectors.toList());
111
112         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) tcpDstMatches.get(0), "1024", "65535");
113     }
114
115     @Test
116     public void testProgramUdpFlow_NoSrcDstPortRange() {
117         AceIpBuilder builder = new AceIpBuilder();
118         AceIpv4Builder v4builder = new AceIpv4Builder();
119         v4builder.setSourceIpv4Network(new Ipv4Prefix("10.1.1.1/24"));
120         v4builder.setDestinationIpv4Network(new Ipv4Prefix("20.1.1.1/24"));
121         builder.setAceIpVersion(v4builder.build());
122         builder.setSourcePortRange(null);
123         builder.setDestinationPortRange(null);
124         short protocol = 1;
125         builder.setProtocol(protocol);
126
127         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
128
129         List<MatchInfoBase> flowMatches = flowMatchesMap.get("UDP_SOURCE_ALL_");
130
131         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
132         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchFieldType.nx_udp_src_with_mask);
133         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchFieldType.nx_udp_dst_with_mask);
134     }
135
136     @Test
137     public void testprogramUdpFlow_WithSrcDstPortRange() {
138         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024",
139                 (short) 1);
140
141         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
142         List<MatchInfoBase> srcFlowMatches = new ArrayList<>();
143         List<MatchInfoBase> dstFlowMatches = new ArrayList<>();
144
145         for (String flowId : flowMatchesMap.keySet()) {
146             if (flowId.startsWith("UDP_SOURCE_")) {
147                 srcFlowMatches.addAll(flowMatchesMap.get(flowId));
148             }
149             if (flowId.startsWith("UDP_DESTINATION_")) {
150                 dstFlowMatches.addAll(flowMatchesMap.get(flowId));
151             }
152         }
153
154         AclServiceTestUtils.verifyGeneralFlows(srcFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
155
156         List<MatchInfoBase> udpSrcMatches = srcFlowMatches.stream().filter(
157             item -> item instanceof NxMatchInfo && ((NxMatchInfo) item).getMatchField().equals(
158                         NxMatchFieldType.nx_udp_src_with_mask)).collect(Collectors.toList());
159         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) udpSrcMatches.get(0), "1024", "65535");
160
161         AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
162
163         List<MatchInfoBase> udpDstMatches = dstFlowMatches.stream().filter(
164             item -> item instanceof NxMatchInfo && ((NxMatchInfo) item).getMatchField().equals(
165                         NxMatchFieldType.nx_udp_dst_with_mask)).collect(Collectors.toList());
166         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) udpDstMatches.get(0), "1024", "65535");
167     }
168
169     @Test
170     public void testaddDstIpMatches_v4() {
171         AceIpBuilder builder = new AceIpBuilder();
172         AceIpv4Builder v4builder = new AceIpv4Builder();
173         v4builder.setDestinationIpv4Network(new Ipv4Prefix("10.1.1.1/24"));
174         builder.setAceIpVersion(v4builder.build());
175
176         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
177
178         assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
179         assertTrue(flowMatches.contains(new MatchIpv4Destination("10.1.1.1", "24")));
180     }
181
182     @Test
183     public void testaddDstIpMatches_v4NoDstNetwork() {
184         AceIpBuilder builder = new AceIpBuilder();
185         AceIpv4Builder v4builder = new AceIpv4Builder();
186         v4builder.setDestinationIpv4Network(null);
187         builder.setAceIpVersion(v4builder.build());
188
189         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
190
191         assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
192         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchIpv4Destination.class);
193     }
194
195     @Test
196     public void testaddSrcIpMatches_v4() {
197         AceIpBuilder builder = new AceIpBuilder();
198         AceIpv4Builder v4builder = new AceIpv4Builder();
199         v4builder.setSourceIpv4Network(new Ipv4Prefix("10.1.1.1/24"));
200         builder.setAceIpVersion(v4builder.build());
201
202         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
203
204         assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
205         assertTrue(flowMatches.contains(new MatchIpv4Source("10.1.1.1", "24")));
206     }
207
208     @Test
209     public void testaddSrcIpMatches_v4NoSrcNetwork() {
210         AceIpBuilder builder = new AceIpBuilder();
211         AceIpv4Builder v4builder = new AceIpv4Builder();
212         v4builder.setSourceIpv4Network(null);
213         builder.setAceIpVersion(v4builder.build());
214
215         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
216         assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
217         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchIpv4Source.class);
218     }
219
220     @Test
221     public void testgetLayer4MaskForRange_SinglePort() {
222         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1111, 1111);
223         assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
224     }
225
226     @Test
227     public void testgetLayer4MaskForRange_MultiplePorts() {
228         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1024, 2048);
229         assertEquals("port L4 mask missing", 2, layer4MaskForRange.size());
230     }
231
232     @Test
233     public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMin() {
234         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(0, 1);
235
236         assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
237     }
238
239     @Test
240     public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMax() {
241         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1, 65536);
242         assertEquals("Illegal ports range", 0, layer4MaskForRange.size());
243     }
244
245     @Test
246     public void testgetLayer4MaskForRange_IllegalPortRange_MinGreaterThanMax() {
247         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(8192, 4096);
248         assertEquals("Illegal ports range", 0, layer4MaskForRange.size());
249     }
250 }