1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
3 import static org.junit.Assert.*;
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.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
27 import com.google.common.collect.ImmutableMap;
29 public class L4ClassifierTest {
31 List<MatchBuilder> matches;
32 Map<String, ParameterValue> params;
35 public ExpectedException thrown = ExpectedException.none();
39 params = new HashMap<>();
40 matches = new ArrayList<>();
44 public void setTcpSrcPortTest() {
45 matches.add(new MatchBuilder()
46 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
47 Long sPort = Long.valueOf(80);
48 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.TCP));
49 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SRC_PORT_PARAM, sPort));
50 matches = Classifier.L4_CL.update(matches, params);
51 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
52 TcpMatch match = new TcpMatchBuilder((TcpMatch) matches.get(0).getLayer4Match()).build();
53 assertEquals(true, sPort.equals(match.getTcpSourcePort().getValue().longValue()));
54 assertEquals(true, match.getTcpDestinationPort() == null);
58 public void setTcpDstPortTest() {
59 Long dPort = Long.valueOf(80);
60 matches.add(new MatchBuilder()
61 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
62 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.TCP));
63 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DST_PORT_PARAM, dPort));
64 matches = Classifier.L4_CL.update(matches, params);
65 assertEquals(true, ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
66 TcpMatch match = new TcpMatchBuilder((TcpMatch) matches.get(0).getLayer4Match()).build();
67 assertEquals(true, dPort.equals(match.getTcpDestinationPort().getValue().longValue()));
68 assertEquals(true, match.getTcpSourcePort() == null);
72 public void setTcpSrcPortDstPortRangeTest() {
73 matches.add(new MatchBuilder()
74 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
75 Long dstRangeStart = Long.valueOf(8079);
76 Long dstRangeEnd = Long.valueOf(8081);
77 Long srcPort = Long.valueOf(80);
78 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.TCP));
79 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SRC_PORT_PARAM, srcPort));
80 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
81 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
82 matches = Classifier.L4_CL.update(matches, params);
83 Set<Long> dstPorts = new HashSet<>();
84 for (MatchBuilder match : matches) {
85 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
87 Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue())
89 dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort().getValue()
92 for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
93 assertEquals(true, dstPorts.contains((i)));
98 public void overrideDstPortWithTheSameValueTest() {
99 Long dPort = Long.valueOf(80);
100 matches.add(new MatchBuilder()
101 .setLayer4Match(ClassifierTestUtils.createUdpDstPort(80))
102 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
103 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.UDP));
104 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DST_PORT_PARAM, dPort));
105 matches = Classifier.L4_CL.update(matches, params);
106 assertEquals(true, ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
107 UdpMatch match = new UdpMatchBuilder((UdpMatch) matches.get(0).getLayer4Match()).build();
108 assertEquals(true, dPort.equals(match.getUdpDestinationPort().getValue().longValue()));
109 assertEquals(true, match.getUdpSourcePort() == null);
113 public void addUdpSrcPortRangeTest() {
114 matches.add(new MatchBuilder()
115 .setLayer4Match(ClassifierTestUtils.createUdpDstPort(80))
116 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
117 Long srcRangeStart = Long.valueOf(8079);
118 Long srcRangeEnd = Long.valueOf(8081);
119 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.UDP));
120 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd));
121 matches = Classifier.L4_CL.update(matches, params);
122 Set<Long> srcPorts = new HashSet<>();
123 for (MatchBuilder match : matches) {
124 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
125 assertEquals(true, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
126 .getValue().intValue() == 80);
127 assertEquals(true, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
130 srcPorts.add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort().getValue()
133 for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
134 assertEquals(true, srcPorts.contains((i)));
139 public void setUdpSrcPortRangeDstPortTest() {
140 matches.add(new MatchBuilder()
141 .setLayer4Match(ClassifierTestUtils.createUdpDstPort(80))
142 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)));
143 Long dPort = Long.valueOf(80);
144 Long srcRangeStart = Long.valueOf(8079);
145 Long srcRangeEnd = Long.valueOf(8081);
146 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.UDP));
147 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DST_PORT_PARAM, dPort));
148 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd));
149 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
150 matches = Classifier.L4_CL.update(matches, params);
151 Set<Long> srcPorts = new HashSet<>();
152 for (MatchBuilder match : matches) {
153 assertEquals(true, ClassifierTestUtils.IPV6_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
156 dPort.equals(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort()
157 .getValue().longValue()));
158 srcPorts.add(Long.valueOf(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort()
159 .getValue().longValue()));
161 for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
162 assertEquals(true, srcPorts.contains((i)));
167 public void overrideSrcPortWithTheSameValueTest() {
168 Long sPort = Long.valueOf(80);
169 matches.add(new MatchBuilder()
170 .setLayer4Match(ClassifierTestUtils.createSctpSrcPort(sPort.intValue()))
171 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
172 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.SCTP));
173 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SRC_PORT_PARAM, sPort));
174 matches = Classifier.L4_CL.update(matches, params);
175 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
176 SctpMatch match = new SctpMatchBuilder((SctpMatch) matches.get(0).getLayer4Match()).build();
177 assertEquals(true, sPort.equals(match.getSctpSourcePort().getValue().longValue()));
178 assertEquals(true, match.getSctpDestinationPort() == null);
182 public void addSctpDstPortRangeTest() {
183 matches.add(new MatchBuilder()
184 .setLayer4Match(ClassifierTestUtils.createSctpSrcPort(80))
185 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
186 Long dstRangeStart = Long.valueOf(8079);
187 Long dstRangeEnd = Long.valueOf(8081);
188 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.SCTP));
189 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
190 matches = Classifier.L4_CL.update(matches, params);
191 Set<Long> dstPorts = new HashSet<>();
192 for (MatchBuilder match : matches) {
193 assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType()));
194 assertEquals(true, new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue()
196 dstPorts.add(Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
197 .getValue().longValue()));
199 for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
200 assertEquals(true, dstPorts.contains((i)));
205 public void setSctpSrcPortRangeDstPortRangeTest() {
206 matches.add(new MatchBuilder()
207 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
208 Long srcRangeStart = Long.valueOf(79);
209 Long srcRangeEnd = Long.valueOf(81);
210 Long dstRangeStart = Long.valueOf(8079);
211 Long dstRangeEnd = Long.valueOf(8081);
212 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.SCTP));
213 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd));
214 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
215 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
216 matches = Classifier.L4_CL.update(matches, params);
217 Set<Pair<Long, Long>> set = new HashSet<>();
218 for (MatchBuilder match : matches) {
219 Long srcPort = Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort()
220 .getValue().longValue());
221 Long dstPort = Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match())
222 .getSctpDestinationPort().getValue().longValue());
223 set.add(Pair.of(srcPort, dstPort));
225 for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
226 for (Long j = dstRangeStart; j <= dstRangeEnd; j++) {
227 assertEquals(true, set.contains(Pair.of(i, j)));
233 public void srcPortSrtPortRangeMutualExclusionTest() {
234 matches.add(new MatchBuilder());
235 Long srcRangeStart = Long.valueOf(8079);
236 Long srcRangeEnd = Long.valueOf(8081);
237 Long srcPort = Long.valueOf(80);
238 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.SCTP));
239 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SRC_PORT_PARAM, srcPort));
240 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd));
241 thrown.expect(IllegalArgumentException.class);
242 thrown.expectMessage("mutually exclusive");
243 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
247 public void dstPortSrtPortRangeMutualExclusionTest() {
248 matches.add(new MatchBuilder());
249 Long dstRangeStart = Long.valueOf(8079);
250 Long dstRangeEnd = Long.valueOf(8081);
251 Long dstPort = Long.valueOf(80);
252 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.UDP));
253 params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DST_PORT_PARAM, dstPort));
254 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
255 thrown.expect(IllegalArgumentException.class);
256 thrown.expectMessage("mutually exclusive");
257 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
261 public void rangeValueMismatchTest() {
262 matches.add(new MatchBuilder());
263 Long dstRangeStart = Long.valueOf(8081);
264 Long dstRangeEnd = Long.valueOf(8079);
265 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.TCP));
266 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
267 thrown.expect(IllegalArgumentException.class);
268 thrown.expectMessage("Range value mismatch");
269 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
273 public void unsupportedProtocolTest() {
274 matches.add(new MatchBuilder());
275 Long dstRangeStart = Long.valueOf(8079);
276 Long dstRangeEnd = Long.valueOf(8081);
277 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, 136));
278 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
279 thrown.expect(IllegalArgumentException.class);
280 thrown.expectMessage("not supported");
281 matches = Classifier.L4_CL.update(matches, params);
285 public void classificationConflictTest() {
286 matches.add(new MatchBuilder()
287 .setLayer4Match(ClassifierTestUtils.createSctpDstPort(80)));
288 Long dstRangeStart = Long.valueOf(8079);
289 Long dstRangeEnd = Long.valueOf(8081);
290 params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.UDP));
291 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
292 thrown.expect(IllegalArgumentException.class);
293 thrown.expectMessage("Classification conflict");
294 matches = Classifier.L4_CL.update(matches, params);
298 public void noProtoTest() {
299 matches.add(new MatchBuilder());
300 Long dstRangeStart = Long.valueOf(8079);
301 Long dstRangeEnd = Long.valueOf(8081);
302 params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd));
303 thrown.expect(IllegalArgumentException.class);
304 thrown.expectMessage(IpProtoClassifier.PROTO_PARAM + " is missing");
305 matches = Classifier.L4_CL.update(matches, params);
309 public void checkPresenceOfRequiredParameters1Test() {
310 params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.SRC_PORT_PARAM,
311 new ParameterValueBuilder().build()));
312 thrown.expect(IllegalArgumentException.class);
313 thrown.expectMessage("not specified");
314 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
318 public void checkPresenceOfRequiredParameters2Test() {
319 params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.DST_PORT_PARAM,
320 new ParameterValueBuilder().build()));
321 thrown.expect(IllegalArgumentException.class);
322 thrown.expectMessage("not specified");
323 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
327 public void checkPresenceOfRequiredParameters3Test() {
328 params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.SRC_PORT_RANGE_PARAM,
329 new ParameterValueBuilder().build()));
330 thrown.expect(IllegalArgumentException.class);
331 thrown.expectMessage("not present");
332 Classifier.L4_CL.checkPresenceOfRequiredParams(params);
336 public void checkPresenceOfRequiredParameters4Test() {
337 params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.DST_PORT_RANGE_PARAM,
338 new ParameterValueBuilder().build()));
339 thrown.expect(IllegalArgumentException.class);
340 thrown.expectMessage("not present");
341 Classifier.L4_CL.checkPresenceOfRequiredParams(params);