1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertSame;
6 import static org.junit.Assert.assertTrue;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.List;
15 import com.google.common.collect.ImmutableMap;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
21 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
32 public class L4ClassifierTest {
34 private static final long LESSER_RANGE_START = 79L;
35 private static final long LESSER_RANGE_END = 81L;
36 private static final long GREATER_RANGE_START = 8079L;
37 private static final long GREATER_RANGE_END = 8081L;
38 private static final long SINGLE_PORT = 80L;
39 private static final int SINGLE_PORT_INT = 80;
42 public ExpectedException thrown = ExpectedException.none();
45 public void testUpdate_TcpSrcPort() {
46 List<MatchBuilder> matches = new ArrayList<>();
47 Map<String, ParameterValue> params = new HashMap<>();
48 matches.add(new MatchBuilder()
50 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
51 Long sPort = SINGLE_PORT;
52 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
53 ClassifierTestUtils.TCP));
54 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort));
56 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
58 assertEquals(1, updated.size());
59 MatchBuilder first = updated.get(0);
60 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType());
61 TcpMatch match = new TcpMatchBuilder((TcpMatch) first.getLayer4Match()).build();
62 assertSame(sPort, match.getTcpSourcePort().getValue().longValue());
63 assertNull(match.getTcpDestinationPort());
67 public void testUpdate_TcpDstPort() {
68 List<MatchBuilder> matches = new ArrayList<>();
69 Map<String, ParameterValue> params = new HashMap<>();
70 Long dPort = SINGLE_PORT;
71 matches.add(new MatchBuilder()
73 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
74 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
75 ClassifierTestUtils.TCP));
76 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
78 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
80 assertEquals(1, updated.size());
81 MatchBuilder first = updated.get(0);
82 assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType());
83 TcpMatch match = new TcpMatchBuilder((TcpMatch) first.getLayer4Match()).build();
84 assertSame(dPort, match.getTcpDestinationPort().getValue().longValue());
85 assertNull(match.getTcpSourcePort());
89 public void testUpdate_TcpSrcPort_DstPortRange() {
90 List<MatchBuilder> matches = new ArrayList<>();
91 Map<String, ParameterValue> params = new HashMap<>();
92 matches.add(new MatchBuilder()
94 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
95 Long dstRangeStart = GREATER_RANGE_START;
96 Long dstRangeEnd = GREATER_RANGE_END;
97 Long srcPort = SINGLE_PORT;
98 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
99 ClassifierTestUtils.TCP));
100 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort));
101 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
102 dstRangeStart, dstRangeEnd));
103 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
105 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
107 Set<Long> dstPorts = new HashSet<>();
108 for (MatchBuilder match : updated) {
109 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType());
110 assertEquals(srcPort,
111 Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue()));
112 dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort()
116 for (Long port = dstRangeStart; port <= dstRangeEnd; port++) {
117 assertTrue(dstPorts.contains((port)));
122 public void testUpdate_OverrideDstPortWithTheSameValue() {
123 List<MatchBuilder> matches = new ArrayList<>();
124 Map<String, ParameterValue> params = new HashMap<>();
125 Long dPort = SINGLE_PORT;
126 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT))
128 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
129 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
130 ClassifierTestUtils.UDP));
131 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
133 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
135 assertEquals(1, updated.size());
136 MatchBuilder first = updated.get(0);
137 assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType());
138 UdpMatch match = new UdpMatchBuilder((UdpMatch) first.getLayer4Match()).build();
139 assertSame(dPort, match.getUdpDestinationPort().getValue().longValue());
140 assertNull(match.getUdpSourcePort());
144 public void testUpdate_AddUdpSrcPortRange() {
145 List<MatchBuilder> matches = new ArrayList<>();
146 Map<String, ParameterValue> params = new HashMap<>();
147 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT))
149 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
150 Long srcRangeStart = GREATER_RANGE_START;
151 Long srcRangeEnd = GREATER_RANGE_END;
152 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
153 ClassifierTestUtils.UDP));
154 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
155 srcRangeStart, srcRangeEnd));
157 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
159 assertEquals(3, updated.size());
160 Set<Long> srcPorts = new HashSet<>();
161 for (MatchBuilder match : updated) {
162 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType());
163 assertSame(SINGLE_PORT_INT,
164 new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort().getValue());
165 assertEquals(SINGLE_PORT_INT, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
169 .add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
173 for (Long port = srcRangeStart; port <= srcRangeEnd; port++) {
174 assertTrue(srcPorts.contains((port)));
179 public void testUpdate_UdpSrcPortRange_DstPort() {
180 List<MatchBuilder> matches = new ArrayList<>();
181 Map<String, ParameterValue> params = new HashMap<>();
182 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT))
184 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
185 Long dPort = SINGLE_PORT;
186 Long srcRangeStart = GREATER_RANGE_START;
187 Long srcRangeEnd = GREATER_RANGE_END;
188 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
189 ClassifierTestUtils.UDP));
190 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
191 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
192 srcRangeStart, srcRangeEnd));
193 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
195 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
197 assertEquals(3, updated.size());
198 Set<Long> srcPorts = new HashSet<>();
199 for (MatchBuilder match : updated) {
200 assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, match.getEthernetMatch().getEthernetType());
201 assertSame(dPort, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
205 .add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
209 for (Long port = srcRangeStart; port <= srcRangeEnd; port++) {
210 assertTrue(srcPorts.contains((port)));
215 public void testUpdate_OverrideSrcPortWithTheSameValue() {
216 List<MatchBuilder> matches = new ArrayList<>();
217 Map<String, ParameterValue> params = new HashMap<>();
218 Long sPort = SINGLE_PORT;
219 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(sPort.intValue()))
220 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
221 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
222 ClassifierTestUtils.SCTP));
223 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort));
225 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
227 assertEquals(1, updated.size());
228 MatchBuilder first = updated.get(0);
229 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType());
230 SctpMatch match = new SctpMatchBuilder((SctpMatch) first.getLayer4Match()).build();
231 assertSame(sPort, match.getSctpSourcePort().getValue().longValue());
232 assertNull(match.getSctpDestinationPort());
236 public void testUpdate_AddSctpDstPortRange() {
237 List<MatchBuilder> matches = new ArrayList<>();
238 Map<String, ParameterValue> params = new HashMap<>();
239 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(SINGLE_PORT_INT))
241 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
242 Long dstRangeStart = GREATER_RANGE_START;
243 Long dstRangeEnd = GREATER_RANGE_END;
244 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
245 ClassifierTestUtils.SCTP));
246 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
247 dstRangeStart, dstRangeEnd));
249 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
251 assertEquals(3, updated.size());
252 Set<Long> dstPorts = new HashSet<>();
253 for (MatchBuilder match : updated) {
254 assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType());
255 assertSame(SINGLE_PORT_INT,
256 new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue());
257 dstPorts.add(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
261 for (Long port = dstRangeStart; port <= dstRangeEnd; port++) {
262 assertTrue(dstPorts.contains((port)));
267 public void testUpdate_Sctp_SrcPortRange_DstPortRange() {
268 List<MatchBuilder> matches = new ArrayList<>();
269 Map<String, ParameterValue> params = new HashMap<>();
270 matches.add(new MatchBuilder()
272 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
273 Long srcRangeStart = LESSER_RANGE_START;
274 Long srcRangeEnd = LESSER_RANGE_END;
275 Long dstRangeStart = GREATER_RANGE_START;
276 Long dstRangeEnd = GREATER_RANGE_END;
277 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
278 ClassifierTestUtils.SCTP));
279 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
280 srcRangeStart, srcRangeEnd));
281 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
282 dstRangeStart, dstRangeEnd));
283 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
285 List<MatchBuilder> updated = Classifier.L4_CL.update(matches, params);
287 assertEquals(9, updated.size());
288 Set<Pair<Long, Long>> set = new HashSet<>();
289 for (MatchBuilder match : updated) {
291 new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue().longValue();
292 Long dstPort = new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
295 set.add(Pair.of(srcPort, dstPort));
297 for (Long sPort = srcRangeStart; sPort <= srcRangeEnd; sPort++) {
298 for (Long dPort = dstRangeStart; dPort <= dstRangeEnd; dPort++) {
299 assertTrue(set.contains(Pair.of(sPort, dPort)));
305 public void testCheckPresenceOfRequiredParams_SrcPort_SrtPortRange_MutualExclusion() {
306 Map<String, ParameterValue> params = new HashMap<>();
307 Long srcRangeStart = GREATER_RANGE_START;
308 Long srcRangeEnd = GREATER_RANGE_END;
309 Long srcPort = SINGLE_PORT;
310 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
311 ClassifierTestUtils.SCTP));
312 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort));
313 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
314 srcRangeStart, srcRangeEnd));
316 thrown.expect(IllegalArgumentException.class);
317 thrown.expectMessage(Classifier.MSG_MUTUALLY_EXCLUSIVE);
318 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
322 public void testCheckPresenceOfRequiredParams_DstPort_DstPortRange_MutualExclusion() {
323 Map<String, ParameterValue> params = new HashMap<>();
324 Long dstRangeStart = GREATER_RANGE_START;
325 Long dstRangeEnd = GREATER_RANGE_END;
326 Long dstPort = SINGLE_PORT;
327 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
328 ClassifierTestUtils.UDP));
329 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dstPort));
330 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
331 dstRangeStart, dstRangeEnd));
333 thrown.expect(IllegalArgumentException.class);
334 thrown.expectMessage(Classifier.MSG_MUTUALLY_EXCLUSIVE);
335 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
339 public void testCheckPresenceOfRequiredParams_RangeValueMismatch() {
340 Map<String, ParameterValue> params = new HashMap<>();
341 Long dstRangeStart = GREATER_RANGE_END;
342 Long dstRangeEnd = GREATER_RANGE_START;
343 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
344 ClassifierTestUtils.TCP));
345 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
346 dstRangeStart, dstRangeEnd));
348 thrown.expect(IllegalArgumentException.class);
349 thrown.expectMessage(Classifier.MSG_RANGE_VALUE_MISMATCH);
350 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
354 public void testUpdate_UnsupportedProtocol() {
355 List<MatchBuilder> matches = new ArrayList<>();
356 Map<String, ParameterValue> params = new HashMap<>();
357 matches.add(new MatchBuilder());
358 Long dstRangeStart = GREATER_RANGE_START;
359 Long dstRangeEnd = GREATER_RANGE_END;
360 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, 136));
361 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
362 dstRangeStart, dstRangeEnd));
364 thrown.expect(IllegalArgumentException.class);
365 thrown.expectMessage(Classifier.MSG_NOT_SUPPORTED);
366 Classifier.L4_CL.update(matches, params);
370 public void testUpdate_ClassificationConflict() {
371 List<MatchBuilder> matches = new ArrayList<>();
372 Map<String, ParameterValue> params = new HashMap<>();
373 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpDstPort(SINGLE_PORT_INT)));
374 Long dstRangeStart = GREATER_RANGE_START;
375 Long dstRangeEnd = GREATER_RANGE_END;
376 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
377 ClassifierTestUtils.UDP));
378 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
379 dstRangeStart, dstRangeEnd));
381 thrown.expect(IllegalArgumentException.class);
382 thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED);
383 Classifier.L4_CL.update(matches, params);
387 public void testUpdate_NoProto() {
388 List<MatchBuilder> matches = new ArrayList<>();
389 Map<String, ParameterValue> params = new HashMap<>();
390 matches.add(new MatchBuilder());
391 Long dstRangeStart = GREATER_RANGE_START;
392 Long dstRangeEnd = GREATER_RANGE_END;
393 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
394 dstRangeStart, dstRangeEnd));
396 thrown.expect(IllegalArgumentException.class);
397 thrown.expectMessage(Classifier.MSG_IS_MISSING);
398 Classifier.L4_CL.update(matches, params);
402 public void testCheckPresenceOfRequiredParameters_SrcPortNotSet() {
403 Map<String, ParameterValue> params = new HashMap<>();
404 params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_PARAM, new ParameterValueBuilder().build()));
406 thrown.expect(IllegalArgumentException.class);
407 thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED);
408 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
412 public void testCheckPresenceOfRequiredParameters_DstPortNotSet() {
413 Map<String, ParameterValue> params = new HashMap<>();
414 params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_PARAM, new ParameterValueBuilder().build()));
416 thrown.expect(IllegalArgumentException.class);
417 thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED);
418 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
422 public void testCheckPresenceOfRequiredParameters_SrcRangeNotSet() {
423 Map<String, ParameterValue> params = new HashMap<>();
424 params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
425 new ParameterValueBuilder().build()));
427 thrown.expect(IllegalArgumentException.class);
428 thrown.expectMessage(Classifier.MSG_NOT_PRESENT);
429 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
433 public void testCheckPresenceOfRequiredParameters_DstRangeNotSet() {
434 Map<String, ParameterValue> params = new HashMap<>();
435 params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
436 new ParameterValueBuilder().build()));
438 thrown.expect(IllegalArgumentException.class);
439 thrown.expectMessage(Classifier.MSG_NOT_PRESENT);
440 Classifier.L4_CL.checkPresenceOfRequiredParams(params);