1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
3 import static org.junit.Assert.assertEquals;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
12 import org.apache.commons.lang3.tuple.Pair;
13 import org.junit.Before;
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.rules.ExpectedException;
17 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
18 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
29 import com.google.common.collect.ImmutableMap;
31 public class L4ClassifierTest {
33 List<MatchBuilder> matches;
35 Map<String, ParameterValue> params;
38 public ExpectedException thrown = ExpectedException.none();
42 params = new HashMap<>();
43 matches = new ArrayList<>();
47 public void setTcpSrcPortTest() {
48 matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
49 Long sPort = Long.valueOf(80);
50 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
51 ClassifierTestUtils.TCP));
52 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort));
53 matches = Classifier.L4_CL.update(matches, params);
55 ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
56 TcpMatch match = new TcpMatchBuilder((TcpMatch) matches.get(0).getLayer4Match()).build();
57 assertEquals(true, sPort.equals(match.getTcpSourcePort().getValue().longValue()));
58 assertEquals(true, match.getTcpDestinationPort() == null);
62 public void setTcpDstPortTest() {
63 Long dPort = Long.valueOf(80);
64 matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
65 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
66 ClassifierTestUtils.TCP));
67 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
68 matches = Classifier.L4_CL.update(matches, params);
70 ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
71 TcpMatch match = new TcpMatchBuilder((TcpMatch) matches.get(0).getLayer4Match()).build();
72 assertEquals(true, dPort.equals(match.getTcpDestinationPort().getValue().longValue()));
73 assertEquals(true, match.getTcpSourcePort() == null);
77 public void setTcpSrcPortDstPortRangeTest() {
78 matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
79 Long dstRangeStart = Long.valueOf(8079);
80 Long dstRangeEnd = Long.valueOf(8081);
81 Long srcPort = Long.valueOf(80);
82 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
83 ClassifierTestUtils.TCP));
84 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort));
85 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
86 dstRangeStart, dstRangeEnd));
87 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
88 matches = Classifier.L4_CL.update(matches, params);
89 Set<Long> dstPorts = new HashSet<>();
90 for (MatchBuilder match : matches) {
91 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
93 Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue())
95 dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort()
99 for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
100 assertEquals(true, dstPorts.contains((i)));
105 public void overrideDstPortWithTheSameValueTest() {
106 Long dPort = Long.valueOf(80);
107 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(80)).setEthernetMatch(
108 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
109 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
110 ClassifierTestUtils.UDP));
111 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
112 matches = Classifier.L4_CL.update(matches, params);
114 ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
115 UdpMatch match = new UdpMatchBuilder((UdpMatch) matches.get(0).getLayer4Match()).build();
116 assertEquals(true, dPort.equals(match.getUdpDestinationPort().getValue().longValue()));
117 assertEquals(true, match.getUdpSourcePort() == null);
121 public void addUdpSrcPortRangeTest() {
122 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(80)).setEthernetMatch(
123 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
124 Long srcRangeStart = Long.valueOf(8079);
125 Long srcRangeEnd = Long.valueOf(8081);
126 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
127 ClassifierTestUtils.UDP));
128 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
129 srcRangeStart, srcRangeEnd));
130 matches = Classifier.L4_CL.update(matches, params);
131 Set<Long> srcPorts = new HashSet<>();
132 for (MatchBuilder match : matches) {
133 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
134 assertEquals(true, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
137 assertEquals(true, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
140 srcPorts.add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
144 for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
145 assertEquals(true, srcPorts.contains((i)));
150 public void setUdpSrcPortRangeDstPortTest() {
151 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(80)).setEthernetMatch(
152 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
153 Long dPort = Long.valueOf(80);
154 Long srcRangeStart = Long.valueOf(8079);
155 Long srcRangeEnd = Long.valueOf(8081);
156 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
157 ClassifierTestUtils.UDP));
158 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort));
159 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
160 srcRangeStart, srcRangeEnd));
161 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
162 matches = Classifier.L4_CL.update(matches, params);
163 Set<Long> srcPorts = new HashSet<>();
164 for (MatchBuilder match : matches) {
165 assertEquals(true, ClassifierTestUtils.IPV6_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
168 dPort.equals(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
171 srcPorts.add(Long.valueOf(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
175 for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
176 assertEquals(true, srcPorts.contains((i)));
181 public void overrideSrcPortWithTheSameValueTest() {
182 Long sPort = Long.valueOf(80);
183 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(sPort.intValue()))
184 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
185 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
186 ClassifierTestUtils.SCTP));
187 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort));
188 matches = Classifier.L4_CL.update(matches, params);
190 ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
191 SctpMatch match = new SctpMatchBuilder((SctpMatch) matches.get(0).getLayer4Match()).build();
192 assertEquals(true, sPort.equals(match.getSctpSourcePort().getValue().longValue()));
193 assertEquals(true, match.getSctpDestinationPort() == null);
197 public void addSctpDstPortRangeTest() {
198 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(80)).setEthernetMatch(
199 ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
200 Long dstRangeStart = Long.valueOf(8079);
201 Long dstRangeEnd = Long.valueOf(8081);
202 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
203 ClassifierTestUtils.SCTP));
204 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
205 dstRangeStart, dstRangeEnd));
206 matches = Classifier.L4_CL.update(matches, params);
207 Set<Long> dstPorts = new HashSet<>();
208 for (MatchBuilder match : matches) {
209 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
210 assertEquals(true, new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort()
213 dstPorts.add(Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
217 for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
218 assertEquals(true, dstPorts.contains((i)));
223 public void setSctpSrcPortRangeDstPortRangeTest() {
224 matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
225 Long srcRangeStart = Long.valueOf(79);
226 Long srcRangeEnd = Long.valueOf(81);
227 Long dstRangeStart = Long.valueOf(8079);
228 Long dstRangeEnd = Long.valueOf(8081);
229 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
230 ClassifierTestUtils.SCTP));
231 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
232 srcRangeStart, srcRangeEnd));
233 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
234 dstRangeStart, dstRangeEnd));
235 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
236 matches = Classifier.L4_CL.update(matches, params);
237 Set<Pair<Long, Long>> set = new HashSet<>();
238 for (MatchBuilder match : matches) {
239 Long srcPort = Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort()
242 Long dstPort = Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
245 set.add(Pair.of(srcPort, dstPort));
247 for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
248 for (Long j = dstRangeStart; j <= dstRangeEnd; j++) {
249 assertEquals(true, set.contains(Pair.of(i, j)));
255 public void srcPortSrtPortRangeMutualExclusionTest() {
256 matches.add(new MatchBuilder());
257 Long srcRangeStart = Long.valueOf(8079);
258 Long srcRangeEnd = Long.valueOf(8081);
259 Long srcPort = Long.valueOf(80);
260 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
261 ClassifierTestUtils.SCTP));
262 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort));
263 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
264 srcRangeStart, srcRangeEnd));
265 thrown.expect(IllegalArgumentException.class);
266 thrown.expectMessage("mutually exclusive");
267 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
271 public void dstPortSrtPortRangeMutualExclusionTest() {
272 matches.add(new MatchBuilder());
273 Long dstRangeStart = Long.valueOf(8079);
274 Long dstRangeEnd = Long.valueOf(8081);
275 Long dstPort = Long.valueOf(80);
276 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
277 ClassifierTestUtils.UDP));
278 params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dstPort));
279 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
280 dstRangeStart, dstRangeEnd));
281 thrown.expect(IllegalArgumentException.class);
282 thrown.expectMessage("mutually exclusive");
283 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
287 public void rangeValueMismatchTest() {
288 matches.add(new MatchBuilder());
289 Long dstRangeStart = Long.valueOf(8081);
290 Long dstRangeEnd = Long.valueOf(8079);
291 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
292 ClassifierTestUtils.TCP));
293 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
294 dstRangeStart, dstRangeEnd));
295 thrown.expect(IllegalArgumentException.class);
296 thrown.expectMessage("Range value mismatch");
297 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
301 public void unsupportedProtocolTest() {
302 matches.add(new MatchBuilder());
303 Long dstRangeStart = Long.valueOf(8079);
304 Long dstRangeEnd = Long.valueOf(8081);
305 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, 136));
306 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
307 dstRangeStart, dstRangeEnd));
308 thrown.expect(IllegalArgumentException.class);
309 thrown.expectMessage("not supported");
310 matches = Classifier.L4_CL.update(matches, params);
314 public void classificationConflictTest() {
315 matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpDstPort(80)));
316 Long dstRangeStart = Long.valueOf(8079);
317 Long dstRangeEnd = Long.valueOf(8081);
318 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
319 ClassifierTestUtils.UDP));
320 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
321 dstRangeStart, dstRangeEnd));
322 thrown.expect(IllegalArgumentException.class);
323 thrown.expectMessage("Classification conflict");
324 matches = Classifier.L4_CL.update(matches, params);
328 public void noProtoTest() {
329 matches.add(new MatchBuilder());
330 Long dstRangeStart = Long.valueOf(8079);
331 Long dstRangeEnd = Long.valueOf(8081);
332 params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
333 dstRangeStart, dstRangeEnd));
334 thrown.expect(IllegalArgumentException.class);
335 thrown.expectMessage(IpProtoClassifierDefinition.PROTO_PARAM + " is missing");
336 matches = Classifier.L4_CL.update(matches, params);
340 public void checkPresenceOfRequiredParameters1Test() {
341 params.putAll(ImmutableMap.<String, ParameterValue>of(L4ClassifierDefinition.SRC_PORT_PARAM,
342 new ParameterValueBuilder().build()));
343 thrown.expect(IllegalArgumentException.class);
344 thrown.expectMessage("not specified");
345 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
349 public void checkPresenceOfRequiredParameters2Test() {
350 params.putAll(ImmutableMap.<String, ParameterValue>of(L4ClassifierDefinition.DST_PORT_PARAM,
351 new ParameterValueBuilder().build()));
352 thrown.expect(IllegalArgumentException.class);
353 thrown.expectMessage("not specified");
354 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
358 public void checkPresenceOfRequiredParameters3Test() {
359 params.putAll(ImmutableMap.<String, ParameterValue>of(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM,
360 new ParameterValueBuilder().build()));
361 thrown.expect(IllegalArgumentException.class);
362 thrown.expectMessage("not present");
363 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
367 public void checkPresenceOfRequiredParameters4Test() {
368 params.putAll(ImmutableMap.<String, ParameterValue>of(L4ClassifierDefinition.DST_PORT_RANGE_PARAM,
369 new ParameterValueBuilder().build()));
370 thrown.expect(IllegalArgumentException.class);
371 thrown.expectMessage("not present");
372 Classifier.L4_CL.checkPresenceOfRequiredParams(params);