2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.aclservice.utils;
11 import static com.google.common.collect.Iterables.filter;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
15 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.List;
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;
34 public class AclServiceOFFlowBuilderTest {
37 public void testProgramIpFlow_NullMatches() {
38 Matches matches = null;
39 Map<String, List<MatchInfoBase>> flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
44 public void testprogramOtherProtocolFlow() {
45 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null,
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");
54 public void testprogramIcmpFlow() {
55 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "2048",
57 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programIcmpFlow(builder.build());
58 List<MatchInfoBase> flowMatches = flowMatchesMap.entrySet().iterator().next().getValue();
60 AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
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");
69 public void testprogramTcpFlow_NoSrcDstPortRange() {
70 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null,
73 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programTcpFlow(builder.build());
74 List<MatchInfoBase> flowMatches = flowMatchesMap.get("TCP_SOURCE_ALL_");
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);
82 public void testprogramTcpFlow_WithSrcDstPortRange() {
83 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024",
86 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programTcpFlow(builder.build());
88 List<MatchInfoBase> srcFlowMatches = new ArrayList<MatchInfoBase>();
89 List<MatchInfoBase> dstFlowMatches = new ArrayList<MatchInfoBase>();
91 for (String flowId : flowMatchesMap.keySet()) {
92 if (flowId.startsWith("TCP_SOURCE_")) {
93 srcFlowMatches.addAll(flowMatchesMap.get(flowId));
95 if (flowId.startsWith("TCP_DESTINATION_")) {
96 dstFlowMatches.addAll(flowMatchesMap.get(flowId));
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)));
106 AclServiceTestUtils.verifyMatchValues((NxMatchInfo) Iterables.getFirst(tcpSrcMatches, null), "1024", "65535");
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)));
114 AclServiceTestUtils.verifyMatchValues((NxMatchInfo) Iterables.getFirst(tcpDstMatches, null), "1024", "65535");
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);
127 builder.setProtocol(protocol);
129 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
131 List<MatchInfoBase> flowMatches = flowMatchesMap.get("UDP_SOURCE_ALL_");
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);
139 public void testprogramUdpFlow_WithSrcDstPortRange() {
140 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024",
143 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
144 List<MatchInfoBase> srcFlowMatches = new ArrayList<MatchInfoBase>();
145 List<MatchInfoBase> dstFlowMatches = new ArrayList<MatchInfoBase>();
147 for (String flowId : flowMatchesMap.keySet()) {
148 if (flowId.startsWith("UDP_SOURCE_")) {
149 srcFlowMatches.addAll(flowMatchesMap.get(flowId));
151 if (flowId.startsWith("UDP_DESTINATION_")) {
152 dstFlowMatches.addAll(flowMatchesMap.get(flowId));
156 AclServiceTestUtils.verifyGeneralFlows(srcFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
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");
164 AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
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");
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());
180 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
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");
188 public void testaddDstIpMatches_v4NoDstNetwork() {
189 AceIpBuilder builder = new AceIpBuilder();
190 AceIpv4Builder v4builder = new AceIpv4Builder();
191 v4builder.setDestinationIpv4Network(null);
192 builder.setAceIpVersion(v4builder.build());
194 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
196 AclServiceTestUtils.verifyMatchInfo(flowMatches, MatchFieldType.eth_type,
197 Integer.toString(NwConstants.ETHTYPE_IPV4));
198 AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchFieldType.ipv4_destination);
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());
208 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
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");
216 public void testaddSrcIpMatches_v4NoSrcNetwork() {
217 AceIpBuilder builder = new AceIpBuilder();
218 AceIpv4Builder v4builder = new AceIpv4Builder();
219 v4builder.setSourceIpv4Network(null);
220 builder.setAceIpVersion(v4builder.build());
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);
229 public void testgetLayer4MaskForRange_SinglePort() {
230 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1111, 1111);
231 assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
235 public void testgetLayer4MaskForRange_MultiplePorts() {
236 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1024, 2048);
237 assertEquals("port L4 mask missing", 2, layer4MaskForRange.size());
241 public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMin() {
242 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(0, 1);
244 assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
248 public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMax() {
249 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1, 65536);
250 assertEquals("Illegal ports range", 0, layer4MaskForRange.size());
254 public void testgetLayer4MaskForRange_IllegalPortRange_MinGreaterThanMax() {
255 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(8192, 4096);
256 assertEquals("Illegal ports range", 0, layer4MaskForRange.size());