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 org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.ArrayList;
16 import java.util.List;
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;
32 public class AclServiceOFFlowBuilderTest {
35 public void testProgramIpFlow_NullMatches() {
36 assertNull(AclServiceOFFlowBuilder.programIpFlow(null));
40 public void testprogramOtherProtocolFlow() {
41 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null,
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");
50 public void testprogramIcmpFlow() {
51 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "2048",
53 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programIcmpFlow(builder.build());
54 List<MatchInfoBase> flowMatches = flowMatchesMap.entrySet().iterator().next().getValue();
56 AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
59 MatchIcmpv4 check = new MatchIcmpv4((short) 1024, (short) 2048);
60 for (MatchInfoBase flowMatch : flowMatches) {
61 if (check.equals(flowMatch)) {
65 assertEquals(2, matches);
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<>();
89 List<MatchInfoBase> dstFlowMatches = new ArrayList<>();
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 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());
105 AclServiceTestUtils.verifyMatchValues((NxMatchInfo) tcpSrcMatches.get(0), "1024", "65535");
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());
112 AclServiceTestUtils.verifyMatchValues((NxMatchInfo) tcpDstMatches.get(0), "1024", "65535");
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);
125 builder.setProtocol(protocol);
127 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
129 List<MatchInfoBase> flowMatches = flowMatchesMap.get("UDP_SOURCE_ALL_");
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);
137 public void testprogramUdpFlow_WithSrcDstPortRange() {
138 AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024",
141 Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programUdpFlow(builder.build());
142 List<MatchInfoBase> srcFlowMatches = new ArrayList<>();
143 List<MatchInfoBase> dstFlowMatches = new ArrayList<>();
145 for (String flowId : flowMatchesMap.keySet()) {
146 if (flowId.startsWith("UDP_SOURCE_")) {
147 srcFlowMatches.addAll(flowMatchesMap.get(flowId));
149 if (flowId.startsWith("UDP_DESTINATION_")) {
150 dstFlowMatches.addAll(flowMatchesMap.get(flowId));
154 AclServiceTestUtils.verifyGeneralFlows(srcFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
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");
161 AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24");
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");
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());
176 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
178 assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
179 assertTrue(flowMatches.contains(new MatchIpv4Destination("10.1.1.1", "24")));
183 public void testaddDstIpMatches_v4NoDstNetwork() {
184 AceIpBuilder builder = new AceIpBuilder();
185 AceIpv4Builder v4builder = new AceIpv4Builder();
186 v4builder.setDestinationIpv4Network(null);
187 builder.setAceIpVersion(v4builder.build());
189 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addDstIpMatches(builder.build());
191 assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
192 AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchIpv4Destination.class);
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());
202 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
204 assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
205 assertTrue(flowMatches.contains(new MatchIpv4Source("10.1.1.1", "24")));
209 public void testaddSrcIpMatches_v4NoSrcNetwork() {
210 AceIpBuilder builder = new AceIpBuilder();
211 AceIpv4Builder v4builder = new AceIpv4Builder();
212 v4builder.setSourceIpv4Network(null);
213 builder.setAceIpVersion(v4builder.build());
215 List<MatchInfoBase> flowMatches = AclServiceOFFlowBuilder.addSrcIpMatches(builder.build());
216 assertTrue(flowMatches.contains(MatchEthernetType.IPV4));
217 AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, MatchIpv4Source.class);
221 public void testgetLayer4MaskForRange_SinglePort() {
222 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1111, 1111);
223 assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
227 public void testgetLayer4MaskForRange_MultiplePorts() {
228 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1024, 2048);
229 assertEquals("port L4 mask missing", 2, layer4MaskForRange.size());
233 public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMin() {
234 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(0, 1);
236 assertEquals("port L4 mask missing", 1, layer4MaskForRange.size());
240 public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMax() {
241 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1, 65536);
242 assertEquals("Illegal ports range", 0, layer4MaskForRange.size());
246 public void testgetLayer4MaskForRange_IllegalPortRange_MinGreaterThanMax() {
247 Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(8192, 4096);
248 assertEquals("Illegal ports range", 0, layer4MaskForRange.size());