2 * Copyright (c) 2015 Cisco Systems, Inc. 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
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertTrue;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
22 import com.google.common.collect.ImmutableMap;
23 import org.apache.commons.lang3.tuple.Pair;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
28 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
39 public class L4ClassifierTest {
41 private static final long LESSER_RANGE_START = 79L;
42 private static final long LESSER_RANGE_END = 81L;
43 private static final long GREATER_RANGE_START = 8079L;
44 private static final long GREATER_RANGE_END = 8081L;
45 private static final long SINGLE_PORT = 80L;
46 private static final int SINGLE_PORT_INT = 80;
49 public ExpectedException thrown = ExpectedException.none();
52 public void testUpdate_TcpSrcPort() {
53 List<MatchBuilder> matches = new ArrayList<>();
54 Map<String, ParameterValue> params = new HashMap<>();
55 matches.add(new MatchBuilder()
57 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
58 Long sPort = SINGLE_PORT;
59 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
60 ClassifierTestUtils.TCP));
61 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort));
63 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
65 assertEquals(1, updated.size());
66 MatchBuilder first = updated.get(0);
67 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType());
68 TcpMatch match = new TcpMatchBuilder((TcpMatch) first.getLayer4Match()).build();
69 assertSame(sPort, match.getTcpSourcePort().getValue().longValue());
70 assertNull(match.getTcpDestinationPort());
74 public void testUpdate_TcpDstPort() {
75 List<MatchBuilder> matches = new ArrayList<>();
76 Map<String, ParameterValue> params = new HashMap<>();
77 Long dPort = SINGLE_PORT;
78 matches.add(new MatchBuilder()
80 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
81 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
82 ClassifierTestUtils.TCP));
83 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
85 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
87 assertEquals(1, updated.size());
88 MatchBuilder first = updated.get(0);
89 assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType());
90 TcpMatch match = new TcpMatchBuilder((TcpMatch) first.getLayer4Match()).build();
91 assertSame(dPort, match.getTcpDestinationPort().getValue().longValue());
92 assertNull(match.getTcpSourcePort());
96 public void testUpdate_TcpSrcPort_DstPortRange() {
97 List<MatchBuilder> matches = new ArrayList<>();
98 Map<String, ParameterValue> params = new HashMap<>();
99 matches.add(new MatchBuilder()
101 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
102 Long dstRangeStart = GREATER_RANGE_START;
103 Long dstRangeEnd = GREATER_RANGE_END;
104 Long srcPort = SINGLE_PORT;
105 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
106 ClassifierTestUtils.TCP));
107 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort));
108 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
109 dstRangeStart, dstRangeEnd));
110 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
112 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
114 Set<Long> dstPorts = new HashSet<>();
115 for (MatchBuilder match : updated) {
116 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType());
117 assertEquals(srcPort,
118 Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue()));
119 dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort()
123 for (Long port = dstRangeStart; port <= dstRangeEnd; port++) {
124 assertTrue(dstPorts.contains((port)));
129 public void testUpdate_OverrideDstPortWithTheSameValue() {
130 List<MatchBuilder> matches = new ArrayList<>();
131 Map<String, ParameterValue> params = new HashMap<>();
132 Long dPort = SINGLE_PORT;
133 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT))
135 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
136 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
137 ClassifierTestUtils.UDP));
138 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
140 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
142 assertEquals(1, updated.size());
143 MatchBuilder first = updated.get(0);
144 assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType());
145 UdpMatch match = new UdpMatchBuilder((UdpMatch) first.getLayer4Match()).build();
146 assertSame(dPort, match.getUdpDestinationPort().getValue().longValue());
147 assertNull(match.getUdpSourcePort());
151 public void testUpdate_AddUdpSrcPortRange() {
152 List<MatchBuilder> matches = new ArrayList<>();
153 Map<String, ParameterValue> params = new HashMap<>();
154 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT))
156 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
157 Long srcRangeStart = GREATER_RANGE_START;
158 Long srcRangeEnd = GREATER_RANGE_END;
159 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
160 ClassifierTestUtils.UDP));
161 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
162 srcRangeStart, srcRangeEnd));
164 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
166 assertEquals(3, updated.size());
167 Set<Long> srcPorts = new HashSet<>();
168 for (MatchBuilder match : updated) {
169 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType());
170 assertSame(SINGLE_PORT_INT,
171 new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort().getValue());
172 assertEquals(SINGLE_PORT_INT, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
176 .add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
180 for (Long port = srcRangeStart; port <= srcRangeEnd; port++) {
181 assertTrue(srcPorts.contains((port)));
186 public void testUpdate_UdpSrcPortRange_DstPort() {
187 List<MatchBuilder> matches = new ArrayList<>();
188 Map<String, ParameterValue> params = new HashMap<>();
189 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT))
191 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
192 Long dPort = SINGLE_PORT;
193 Long srcRangeStart = GREATER_RANGE_START;
194 Long srcRangeEnd = GREATER_RANGE_END;
195 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
196 ClassifierTestUtils.UDP));
197 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
198 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
199 srcRangeStart, srcRangeEnd));
200 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
202 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
204 assertEquals(3, updated.size());
205 Set<Long> srcPorts = new HashSet<>();
206 for (MatchBuilder match : updated) {
207 assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, match.getEthernetMatch().getEthernetType());
208 assertSame(dPort, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
212 .add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
216 for (Long port = srcRangeStart; port <= srcRangeEnd; port++) {
217 assertTrue(srcPorts.contains((port)));
222 public void testUpdate_OverrideSrcPortWithTheSameValue() {
223 List<MatchBuilder> matches = new ArrayList<>();
224 Map<String, ParameterValue> params = new HashMap<>();
225 Long sPort = SINGLE_PORT;
226 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(sPort.intValue()))
227 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
228 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
229 ClassifierTestUtils.SCTP));
230 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort));
232 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
234 assertEquals(1, updated.size());
235 MatchBuilder first = updated.get(0);
236 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType());
237 SctpMatch match = new SctpMatchBuilder((SctpMatch) first.getLayer4Match()).build();
238 assertSame(sPort, match.getSctpSourcePort().getValue().longValue());
239 assertNull(match.getSctpDestinationPort());
243 public void testUpdate_AddSctpDstPortRange() {
244 List<MatchBuilder> matches = new ArrayList<>();
245 Map<String, ParameterValue> params = new HashMap<>();
246 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(SINGLE_PORT_INT))
248 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
249 Long dstRangeStart = GREATER_RANGE_START;
250 Long dstRangeEnd = GREATER_RANGE_END;
251 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
252 ClassifierTestUtils.SCTP));
253 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
254 dstRangeStart, dstRangeEnd));
256 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
258 assertEquals(3, updated.size());
259 Set<Long> dstPorts = new HashSet<>();
260 for (MatchBuilder match : updated) {
261 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType());
262 assertSame(SINGLE_PORT_INT,
263 new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue());
264 dstPorts.add(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
268 for (Long port = dstRangeStart; port <= dstRangeEnd; port++) {
269 assertTrue(dstPorts.contains((port)));
274 public void testUpdate_Sctp_SrcPortRange_DstPortRange() {
275 List<MatchBuilder> matches = new ArrayList<>();
276 Map<String, ParameterValue> params = new HashMap<>();
277 matches.add(new MatchBuilder()
279 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
280 Long srcRangeStart = LESSER_RANGE_START;
281 Long srcRangeEnd = LESSER_RANGE_END;
282 Long dstRangeStart = GREATER_RANGE_START;
283 Long dstRangeEnd = GREATER_RANGE_END;
284 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
285 ClassifierTestUtils.SCTP));
286 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
287 srcRangeStart, srcRangeEnd));
288 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
289 dstRangeStart, dstRangeEnd));
290 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
292 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
294 assertEquals(9, updated.size());
295 Set<Pair<Long, Long>> set = new HashSet<>();
296 for (MatchBuilder match : updated) {
298 new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue().longValue();
299 Long dstPort = new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
302 set.add(Pair.of(srcPort, dstPort));
304 for (Long sPort = srcRangeStart; sPort <= srcRangeEnd; sPort++) {
305 for (Long dPort = dstRangeStart; dPort <= dstRangeEnd; dPort++) {
306 assertTrue(set.contains(Pair.of(sPort, dPort)));
312 public void testCheckPresenceOfRequiredParams_SrcPort_SrtPortRange_MutualExclusion() {
313 Map<String, ParameterValue> params = new HashMap<>();
314 Long srcRangeStart = GREATER_RANGE_START;
315 Long srcRangeEnd = GREATER_RANGE_END;
316 Long srcPort = SINGLE_PORT;
317 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
318 ClassifierTestUtils.SCTP));
319 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort));
320 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
321 srcRangeStart, srcRangeEnd));
323 thrown.expect(IllegalArgumentException.class);
324 thrown.expectMessage(Classifier.MSG_MUTUALLY_EXCLUSIVE);
325 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
329 public void testCheckPresenceOfRequiredParams_DstPort_DstPortRange_MutualExclusion() {
330 Map<String, ParameterValue> params = new HashMap<>();
331 Long dstRangeStart = GREATER_RANGE_START;
332 Long dstRangeEnd = GREATER_RANGE_END;
333 Long dstPort = SINGLE_PORT;
334 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
335 ClassifierTestUtils.UDP));
336 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dstPort));
337 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
338 dstRangeStart, dstRangeEnd));
340 thrown.expect(IllegalArgumentException.class);
341 thrown.expectMessage(Classifier.MSG_MUTUALLY_EXCLUSIVE);
342 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
346 public void testCheckPresenceOfRequiredParams_RangeValueMismatch() {
347 Map<String, ParameterValue> params = new HashMap<>();
348 Long dstRangeStart = GREATER_RANGE_END;
349 Long dstRangeEnd = GREATER_RANGE_START;
350 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
351 ClassifierTestUtils.TCP));
352 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
353 dstRangeStart, dstRangeEnd));
355 thrown.expect(IllegalArgumentException.class);
356 thrown.expectMessage(Classifier.MSG_RANGE_VALUE_MISMATCH);
357 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
361 public void testUpdate_UnsupportedProtocol() {
362 List<MatchBuilder> matches = new ArrayList<>();
363 Map<String, ParameterValue> params = new HashMap<>();
364 matches.add(new MatchBuilder());
365 Long dstRangeStart = GREATER_RANGE_START;
366 Long dstRangeEnd = GREATER_RANGE_END;
367 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, 136));
368 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
369 dstRangeStart, dstRangeEnd));
371 thrown.expect(IllegalArgumentException.class);
372 thrown.expectMessage(Classifier.MSG_NOT_SUPPORTED);
373 Classifier.L4_CL.update(matches, params);
377 public void testUpdate_ClassificationConflict() {
378 List<MatchBuilder> matches = new ArrayList<>();
379 Map<String, ParameterValue> params = new HashMap<>();
380 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpDstPort(SINGLE_PORT_INT)));
381 Long dstRangeStart = GREATER_RANGE_START;
382 Long dstRangeEnd = GREATER_RANGE_END;
383 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
384 ClassifierTestUtils.UDP));
385 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
386 dstRangeStart, dstRangeEnd));
388 thrown.expect(IllegalArgumentException.class);
389 thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED);
390 Classifier.L4_CL.update(matches, params);
394 public void testUpdate_NoProto() {
395 List<MatchBuilder> matches = new ArrayList<>();
396 Map<String, ParameterValue> params = new HashMap<>();
397 matches.add(new MatchBuilder());
398 Long dstRangeStart = GREATER_RANGE_START;
399 Long dstRangeEnd = GREATER_RANGE_END;
400 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
401 dstRangeStart, dstRangeEnd));
403 thrown.expect(IllegalArgumentException.class);
404 thrown.expectMessage(Classifier.MSG_IS_MISSING);
405 Classifier.L4_CL.update(matches, params);
409 public void testCheckPresenceOfRequiredParameters_SrcPortNotSet() {
410 Map<String, ParameterValue> params = new HashMap<>();
411 params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_PARAM, new ParameterValueBuilder().build()));
413 thrown.expect(IllegalArgumentException.class);
414 thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED);
415 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
419 public void testCheckPresenceOfRequiredParameters_DstPortNotSet() {
420 Map<String, ParameterValue> params = new HashMap<>();
421 params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_PARAM, new ParameterValueBuilder().build()));
423 thrown.expect(IllegalArgumentException.class);
424 thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED);
425 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
429 public void testCheckPresenceOfRequiredParameters_SrcRangeNotSet() {
430 Map<String, ParameterValue> params = new HashMap<>();
431 params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
432 new ParameterValueBuilder().build()));
434 thrown.expect(IllegalArgumentException.class);
435 thrown.expectMessage(Classifier.MSG_NOT_PRESENT);
436 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
440 public void testCheckPresenceOfRequiredParameters_DstRangeNotSet() {
441 Map<String, ParameterValue> params = new HashMap<>();
442 params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
443 new ParameterValueBuilder().build()));
445 thrown.expect(IllegalArgumentException.class);
446 thrown.expectMessage(Classifier.MSG_NOT_PRESENT);
447 Classifier.L4_CL.checkPresenceOfRequiredParams(params);