08d52df0350768b01ce2135916d8d79ebd190b26
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / L4ClassifierTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
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;
26
27 import com.google.common.collect.ImmutableMap;
28
29 public class L4ClassifierTest {
30
31     List<MatchBuilder> matches;
32     Map<String, ParameterValue> params;
33
34     @Rule
35     public ExpectedException thrown = ExpectedException.none();
36
37     @Before
38     public void setUp() {
39         params = new HashMap<>();
40         matches = new ArrayList<>();
41     }
42
43     @Test
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, ClassifierTestUtils.TCP));
49         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SPORT, 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);
55     }
56
57     @Test
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, ClassifierTestUtils.TCP));
63         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DPORT, 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);
69     }
70
71     @Test
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, ClassifierTestUtils.TCP));
79         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SPORT, srcPort));
80         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, 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()));
86             assertEquals(true,
87                     Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue())
88                             .equals(srcPort));
89             dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort().getValue()
90                     .longValue());
91         }
92         for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
93             assertEquals(true, dstPorts.contains((i)));
94         }
95     }
96
97     @Test
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, ClassifierTestUtils.UDP));
104         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DPORT, 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);
110     }
111
112     @Test
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, ClassifierTestUtils.UDP));
120         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SPORT_RANGE, 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()
128                     .getValue()
129                     .longValue() == 80);
130             srcPorts.add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort().getValue()
131                     .longValue());
132         }
133         for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
134             assertEquals(true, srcPorts.contains((i)));
135         }
136     }
137
138     @Test
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, ClassifierTestUtils.UDP));
147         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DPORT, dPort));
148         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SPORT_RANGE, 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()));
154             assertEquals(
155                     true,
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()));
160         }
161         for (Long i = srcRangeStart; i <= srcRangeEnd; i++) {
162             assertEquals(true, srcPorts.contains((i)));
163         }
164     }
165
166     @Test
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, ClassifierTestUtils.SCTP));
173         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SPORT, 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);
179     }
180
181     @Test
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, ClassifierTestUtils.SCTP));
189         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, 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()
195                     .intValue() == 80);
196             dstPorts.add(Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort()
197                     .getValue().longValue()));
198         }
199         for (Long i = dstRangeStart; i <= dstRangeEnd; i++) {
200             assertEquals(true, dstPorts.contains((i)));
201         }
202     }
203
204     @Test
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, ClassifierTestUtils.SCTP));
213         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SPORT_RANGE, srcRangeStart, srcRangeEnd));
214         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, 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));
224         }
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)));
228             }
229         }
230     }
231
232     @Test
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, ClassifierTestUtils.SCTP));
239         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.SPORT, srcPort));
240         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.SPORT_RANGE, srcRangeStart, srcRangeEnd));
241         thrown.expect(IllegalArgumentException.class);
242         thrown.expectMessage("Illegal source port parameters");
243         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
244     }
245
246     @Test
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, ClassifierTestUtils.UDP));
253         params.putAll(ClassifierTestUtils.createIntValueParam(L4Classifier.DPORT, dstPort));
254         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, dstRangeStart, dstRangeEnd));
255         thrown.expect(IllegalArgumentException.class);
256         thrown.expectMessage("Illegal destination port parameters");
257         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
258     }
259
260     @Test
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, ClassifierTestUtils.TCP));
266         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, dstRangeStart, dstRangeEnd));
267         thrown.expect(IllegalArgumentException.class);
268         thrown.expectMessage("Range value mismatch");
269         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
270     }
271
272     @Test
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, 136));
278         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, dstRangeStart, dstRangeEnd));
279         thrown.expect(IllegalArgumentException.class);
280         thrown.expectMessage("Unsupported L4 protocol.");
281         matches = Classifier.L4_CL.update(matches, params);
282     }
283
284     @Test
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, ClassifierTestUtils.UDP));
291         params.putAll(ClassifierTestUtils.createRangeValueParam(L4Classifier.DPORT_RANGE, dstRangeStart, dstRangeEnd));
292         thrown.expect(IllegalArgumentException.class);
293         thrown.expectMessage("Classification conflict");
294         matches = Classifier.L4_CL.update(matches, params);
295     }
296
297     @Test
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.DPORT_RANGE, dstRangeStart, dstRangeEnd));
303         thrown.expect(IllegalArgumentException.class);
304         thrown.expectMessage("L4 protocol is null.");
305         matches = Classifier.L4_CL.update(matches, params);
306     }
307
308     @Test
309     public void checkPresenceOfRequiredParameters1Test() {
310         params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.SPORT,
311                 new ParameterValueBuilder().build()));
312         thrown.expect(IllegalArgumentException.class);
313         thrown.expectMessage("Value of sourceport parameter is not present");
314         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
315     }
316
317     @Test
318     public void checkPresenceOfRequiredParameters2Test() {
319         params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.DPORT,
320                 new ParameterValueBuilder().build()));
321         thrown.expect(IllegalArgumentException.class);
322         thrown.expectMessage("Value of destport parameter is not present");
323         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
324     }
325
326     @Test
327     public void checkPresenceOfRequiredParameters3Test() {
328         params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.SPORT_RANGE,
329                 new ParameterValueBuilder().build()));
330         thrown.expect(IllegalArgumentException.class);
331         thrown.expectMessage("Range value is not present");
332         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
333     }
334
335     @Test
336     public void checkPresenceOfRequiredParameters4Test() {
337         params.putAll(ImmutableMap.<String, ParameterValue> of(L4Classifier.DPORT_RANGE,
338                 new ParameterValueBuilder().build()));
339         thrown.expect(IllegalArgumentException.class);
340         thrown.expectMessage("Range value is not present");
341         Classifier.L4_CL.checkPresenceOfRequiredParams(params);
342     }
343
344 }