Added Port Range and Ipv6 matches
[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 com.google.common.collect.Iterables.filter;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14
15 import com.google.common.collect.Iterables;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.junit.Test;
22 import org.opendaylight.genius.mdsalutil.MatchFieldType;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
25 import org.opendaylight.genius.mdsalutil.NwConstants;
26 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
27 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
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;
29 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;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
32
33
34 public class AclServiceOFFlowBuilderTest {
35
36     @Test
37     public void testProgramIpFlow_NullMatches() {
38         Matches matches = null;
39         Map<String, List<MatchInfoBase>> flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
40         assertNull(flowMap);
41     }
42
43     @Test
44     public void testprogramOtherProtocolFlow() {
45         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null,
46                 (short) 1);
47         Map<String, List<MatchInfoBase>> flowMatchesMap =
48                 AclServiceOFFlowBuilder.programOtherProtocolFlow(builder.build());
49         List<MatchInfoBase> flowMatches = flowMatchesMap.get("OTHER_PROTO" + "1");
50         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
51     }
52
53     @Test
54     public void testprogramIcmpFlow() {
55         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "2048",
56                 (short) 1);
57         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programIcmpFlow(builder.build());
58         List<MatchInfoBase> flowMatches = flowMatchesMap.entrySet().iterator().next().getValue();
59
60         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
61
62         Iterable<MatchInfoBase> icmpv4Matches = filter(flowMatches,
63                 (item -> ((MatchInfo) item).getMatchField().equals(MatchFieldType.icmp_v4)));
64         AclServiceTestUtils.verifyMatchValues((MatchInfo) Iterables.get(icmpv4Matches, 0), "1024", "2048");
65         AclServiceTestUtils.verifyMatchValues((MatchInfo) Iterables.get(icmpv4Matches, 1), "4096", "8192");
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<MatchInfoBase>();
89         List<MatchInfoBase> dstFlowMatches = new ArrayList<MatchInfoBase>();
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         Iterable<MatchInfoBase> nxSrcMatches = filter(srcFlowMatches,
102             (item -> item instanceof NxMatchInfo) );
103         Iterable<MatchInfoBase> tcpSrcMatches = filter(nxSrcMatches,
104                 (item -> ((NxMatchInfo) item).getMatchField().equals(NxMatchFieldType.nx_tcp_src_with_mask)));
105
106         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) Iterables.getFirst(tcpSrcMatches, null), "1024", "65535");
107
108         AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
109         Iterable<MatchInfoBase> nxDstMatches = filter(dstFlowMatches,
110             (item -> item instanceof NxMatchInfo) );
111         Iterable<MatchInfoBase> tcpDstMatches = filter(nxDstMatches,
112                 (item -> ((NxMatchInfo) item).getMatchField().equals(NxMatchFieldType.nx_tcp_dst_with_mask)));
113
114         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) Iterables.getFirst(tcpDstMatches, null), "1024", "65535");
115     }
116
117     @Test
118     public void testProgramUdpFlow_NoSrcDstPortRange() {
119         AceIpBuilder builder = new AceIpBuilder();
120         AceIpv4Builder v4builder = new AceIpv4Builder();
121         v4builder.setSourceIpv4Network(new Ipv4Prefix("10.1.1.1/24"));
122         v4builder.setDestinationIpv4Network(new Ipv4Prefix("20.1.1.1/24"));
123         builder.setAceIpVersion(v4builder.build());
124         builder.setSourcePortRange(null);
125         builder.setDestinationPortRange(null);
126         short protocol = 1;
127         builder.setProtocol(protocol);
128
129         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
130
131         List<MatchInfoBase> flowMatches = flowMatchesMap.get("UDP_SOURCE_ALL_");
132
133         AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
134         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchFieldType.nx_udp_src_with_mask);
135         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchFieldType.nx_udp_dst_with_mask);
136     }
137
138     @Test
139     public void testprogramUdpFlow_WithSrcDstPortRange() {
140         AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024",
141                 (short) 1);
142
143         Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
144         List<MatchInfoBase> srcFlowMatches = new ArrayList<MatchInfoBase>();
145         List<MatchInfoBase> dstFlowMatches = new ArrayList<MatchInfoBase>();
146
147         for (String flowId : flowMatchesMap.keySet()) {
148             if (flowId.startsWith("UDP_SOURCE_")) {
149                 srcFlowMatches.addAll(flowMatchesMap.get(flowId));
150             }
151             if (flowId.startsWith("UDP_DESTINATION_")) {
152                 dstFlowMatches.addAll(flowMatchesMap.get(flowId));
153             }
154         }
155
156         AclServiceTestUtils.verifyGeneralFlows(srcFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
157
158         Iterable<MatchInfoBase> nxSrcMatches = filter(srcFlowMatches,
159             (item -> item instanceof NxMatchInfo) );
160         Iterable<MatchInfoBase> udpSrcMatches = filter(nxSrcMatches,
161                 (item -> ((NxMatchInfo) item).getMatchField().equals(NxMatchFieldType.nx_udp_src_with_mask)));
162         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) Iterables.getFirst(udpSrcMatches, null), "1024", "65535");
163
164         AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
165
166         Iterable<MatchInfoBase> nxDstMatches = filter(dstFlowMatches,
167             (item -> item instanceof NxMatchInfo) );
168         Iterable<MatchInfoBase> udpDstMatches = filter(nxDstMatches,
169                 (item -> ((NxMatchInfo) item).getMatchField().equals(NxMatchFieldType.nx_udp_dst_with_mask)));
170         AclServiceTestUtils.verifyMatchValues((NxMatchInfo) Iterables.getFirst(udpDstMatches, null), "1024", "65535");
171     }
172
173     @Test
174     public void testaddDstIpMatches_v4() {
175         AceIpBuilder builder = new AceIpBuilder();
176         AceIpv4Builder v4builder = new AceIpv4Builder();
177         v4builder.setDestinationIpv4Network(new Ipv4Prefix("10.1.1.1/24"));
178         builder.setAceIpVersion(v4builder.build());
179
180         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
181
182         AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.eth_type,
183                 Integer.toString(NwConstants.ETHTYPE_IPV4));
184         AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.ipv4_destination, "10.1.1.1", "24");
185     }
186
187     @Test
188     public void testaddDstIpMatches_v4NoDstNetwork() {
189         AceIpBuilder builder = new AceIpBuilder();
190         AceIpv4Builder v4builder = new AceIpv4Builder();
191         v4builder.setDestinationIpv4Network(null);
192         builder.setAceIpVersion(v4builder.build());
193
194         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
195
196         AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.eth_type,
197                 Integer.toString(NwConstants.ETHTYPE_IPV4));
198         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchFieldType.ipv4_destination);
199     }
200
201     @Test
202     public void testaddSrcIpMatches_v4() {
203         AceIpBuilder builder = new AceIpBuilder();
204         AceIpv4Builder v4builder = new AceIpv4Builder();
205         v4builder.setSourceIpv4Network(new Ipv4Prefix("10.1.1.1/24"));
206         builder.setAceIpVersion(v4builder.build());
207
208         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
209
210         AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.eth_type,
211                 Integer.toString(NwConstants.ETHTYPE_IPV4));
212         AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.ipv4_source, "10.1.1.1", "24");
213     }
214
215     @Test
216     public void testaddSrcIpMatches_v4NoSrcNetwork() {
217         AceIpBuilder builder = new AceIpBuilder();
218         AceIpv4Builder v4builder = new AceIpv4Builder();
219         v4builder.setSourceIpv4Network(null);
220         builder.setAceIpVersion(v4builder.build());
221
222         List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
223         AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.eth_type,
224                 Integer.toString(NwConstants.ETHTYPE_IPV4));
225         AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchFieldType.ipv4_source);
226     }
227
228     @Test
229     public void testgetLayer4MaskForRange_SinglePort() {
230         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1111, 1111);
231         assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
232     }
233
234     @Test
235     public void testgetLayer4MaskForRange_MultiplePorts() {
236         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1024, 2048);
237         assertEquals("port L4 mask missing", 2, layer4MaskForRange.size());
238     }
239
240     @Test
241     public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMin() {
242         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(0, 1);
243
244         assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
245     }
246
247     @Test
248     public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMax() {
249         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1, 65536);
250         assertEquals("Illegal ports range", 0, layer4MaskForRange.size());
251     }
252
253     @Test
254     public void testgetLayer4MaskForRange_IllegalPortRange_MinGreaterThanMax() {
255         Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(8192, 4096);
256         assertEquals("Illegal ports range", 0, layer4MaskForRange.size());
257     }
258 }