blob: abd77add01653b11b03d76bb26e00210050a703a [file] [log] [blame]
Ray Milkey73ba84a2015-02-04 17:08:41 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
18import java.util.EnumMap;
19
Ray Milkey9ee62f52015-02-06 13:47:35 -080020import org.junit.Before;
Ray Milkey73ba84a2015-02-04 17:08:41 -080021import org.junit.Test;
Ray Milkey9ee62f52015-02-06 13:47:35 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010025import org.onlab.packet.MplsLabel;
Ray Milkey9ee62f52015-02-06 13:47:35 -080026import org.onlab.packet.VlanId;
27import org.onosproject.codec.CodecContext;
28import org.onosproject.codec.JsonCodec;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070029import org.onosproject.net.ChannelSpacing;
30import org.onosproject.net.GridType;
31import org.onosproject.net.Lambda;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070032import org.onosproject.net.OchSignalType;
Ray Milkey9ee62f52015-02-06 13:47:35 -080033import org.onosproject.net.PortNumber;
34import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey73ba84a2015-02-04 17:08:41 -080035import org.onosproject.net.flow.criteria.Criterion;
36
Ray Milkey9ee62f52015-02-06 13:47:35 -080037import com.fasterxml.jackson.databind.node.ObjectNode;
38
Ray Milkey73ba84a2015-02-04 17:08:41 -080039import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey0cc189e2015-02-09 11:08:01 -080040import static org.hamcrest.Matchers.is;
41import static org.hamcrest.Matchers.notNullValue;
42import static org.onlab.junit.TestUtils.getField;
43import static org.onlab.junit.TestUtils.setField;
Ray Milkey46670a82015-02-08 17:57:17 -080044import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080045
46/**
47 * Unit tests for criterion codec.
48 */
49public class CriterionCodecTest {
50
Ray Milkey9ee62f52015-02-06 13:47:35 -080051 CodecContext context;
52 JsonCodec<Criterion> criterionCodec;
53 final PortNumber port = PortNumber.portNumber(1);
54 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
55 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
56 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
57
58 /**
59 * Sets up for each test. Creates a context and fetches the criterion
60 * codec.
61 */
62 @Before
63 public void setUp() {
64 context = new MockCodecContext();
65 criterionCodec = context.codec(Criterion.class);
66 assertThat(criterionCodec, notNullValue());
67 }
68
69
Ray Milkey73ba84a2015-02-04 17:08:41 -080070 /**
71 * Checks that all criterion types are covered by the codec.
72 */
73 @Test
74 public void checkCriterionTypes() throws Exception {
Ray Milkeyd43fe452015-05-29 09:35:12 -070075 EncodeCriterionCodec encoder = new EncodeCriterionCodec(
76 Criteria.dummy(), context);
Ray Milkey9ee62f52015-02-06 13:47:35 -080077 EnumMap<Criterion.Type, Object> formatMap =
Ray Milkeyd43fe452015-05-29 09:35:12 -070078 getField(encoder, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080079 assertThat(formatMap, notNullValue());
80
81 for (Criterion.Type type : Criterion.Type.values()) {
82 assertThat("Entry not found for " + type.toString(),
83 formatMap.get(type), notNullValue());
84 }
85 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080086
87 /**
88 * Tests in port criterion.
89 */
90 @Test
91 public void matchInPortTest() {
92 Criterion criterion = Criteria.matchInPort(port);
93 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080094 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080095 }
96
97 /**
98 * Tests in physical port criterion.
99 */
100 @Test
101 public void matchInPhyPortTest() {
102 Criterion criterion = Criteria.matchInPhyPort(port);
103 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800104 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800105 }
106
107 /**
108 * Tests metadata criterion.
109 */
110 @Test
111 public void matchMetadataTest() {
112 Criterion criterion = Criteria.matchMetadata(0xabcdL);
113 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800114 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800115 }
116
117 /**
118 * Tests ethernet destination criterion.
119 */
120 @Test
121 public void matchEthDstTest() {
122 Criterion criterion = Criteria.matchEthDst(mac1);
123 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800124 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800125 }
126
127 /**
128 * Tests ethernet source criterion.
129 */
130 @Test
131 public void matchEthSrcTest() {
132 Criterion criterion = Criteria.matchEthSrc(mac1);
133 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800134 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800135 }
136
137 /**
138 * Tests ethernet type criterion.
139 */
140 @Test
141 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800142 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800143 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800144 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800145 }
146
147 /**
148 * Tests VLAN Id criterion.
149 */
150 @Test
151 public void matchVlanIdTest() {
152 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
153 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800154 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800155 }
156
157 /**
158 * Tests VLAN PCP criterion.
159 */
160 @Test
161 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800162 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800163 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800164 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800165 }
166
167 /**
168 * Tests IP DSCP criterion.
169 */
170 @Test
171 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800172 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800173 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800174 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800175 }
176
177 /**
178 * Tests IP ECN criterion.
179 */
180 @Test
181 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800182 Criterion criterion = Criteria.matchIPEcn((byte) 3);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800183 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800184 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800185 }
186
187 /**
188 * Tests IP protocol criterion.
189 */
190 @Test
191 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800192 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800193 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800194 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800195 }
196
197 /**
198 * Tests IP source criterion.
199 */
200 @Test
201 public void matchIPSrcTest() {
202 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
203 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800204 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800205 }
206
207 /**
208 * Tests IP destination criterion.
209 */
210 @Test
211 public void matchIPDstTest() {
212 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
213 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800214 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800215 }
216
217 /**
218 * Tests source TCP port criterion.
219 */
220 @Test
221 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800222 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800223 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800224 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800225 }
226
227 /**
228 * Tests destination TCP port criterion.
229 */
230 @Test
231 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800232 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800233 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800234 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800235 }
236
237 /**
238 * Tests source UDP port criterion.
239 */
240 @Test
241 public void matchUdpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800242 Criterion criterion = Criteria.matchUdpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800243 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800244 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800245 }
246
247 /**
248 * Tests destination UDP criterion.
249 */
250 @Test
251 public void matchUdpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800252 Criterion criterion = Criteria.matchUdpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800253 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800254 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800255 }
256
257 /**
258 * Tests source SCTP criterion.
259 */
260 @Test
261 public void matchSctpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800262 Criterion criterion = Criteria.matchSctpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800263 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800264 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800265 }
266
267 /**
268 * Tests destination SCTP criterion.
269 */
270 @Test
271 public void matchSctpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800272 Criterion criterion = Criteria.matchSctpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800273 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800274 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800275 }
276
277 /**
278 * Tests ICMP type criterion.
279 */
280 @Test
281 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800282 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800283 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800284 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800285 }
286
287 /**
288 * Tests ICMP code criterion.
289 */
290 @Test
291 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800292 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800293 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800294 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800295 }
296
297 /**
298 * Tests IPv6 source criterion.
299 */
300 @Test
301 public void matchIPv6SrcTest() {
302 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
303 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800304 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800305 }
306
307 /**
308 * Tests IPv6 destination criterion.
309 */
310 @Test
311 public void matchIPv6DstTest() {
312 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
313 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800314 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800315 }
316
317 /**
318 * Tests IPv6 flow label criterion.
319 */
320 @Test
321 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800322 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800323 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800324 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800325 }
326
327 /**
328 * Tests ICMP v6 type criterion.
329 */
330 @Test
331 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800332 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800333 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800334 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800335 }
336
337 /**
338 * Tests ICMP v6 code criterion.
339 */
340 @Test
341 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800342 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800343 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800344 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800345 }
346
347 /**
348 * Tests IPV6 target address criterion.
349 */
350 @Test
351 public void matchIPv6NDTargetAddressTest() {
352 Criterion criterion =
353 Criteria.matchIPv6NDTargetAddress(
354 Ip6Address.valueOf("1111:2222::"));
355 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800356 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800357 }
358
359 /**
360 * Tests IPV6 SLL criterion.
361 */
362 @Test
363 public void matchIPv6NDSourceLinkLayerAddressTest() {
364 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
365 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800366 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800367 }
368
369 /**
370 * Tests IPV6 TLL criterion.
371 */
372 @Test
373 public void matchIPv6NDTargetLinkLayerAddressTest() {
374 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
375 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800376 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800377 }
378
379 /**
380 * Tests MPLS label criterion.
381 */
382 @Test
383 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100384 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800385 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800386 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800387 }
388
389 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800390 * Tests IPv6 Extension Header pseudo-field flags criterion.
391 */
392 @Test
393 public void matchIPv6ExthdrFlagsTest() {
394 int exthdrFlags =
395 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
396 Criterion.IPv6ExthdrFlags.ESP.getValue() |
397 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
398 Criterion.IPv6ExthdrFlags.DEST.getValue() |
399 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
400 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
401 Criterion.IPv6ExthdrFlags.HOP.getValue() |
402 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
403 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
404 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
405 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800406
407 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800408 }
409
410 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800411 * Tests lambda criterion.
412 */
413 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700414 public void matchOchSignal() {
415 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
416 Criterion criterion = Criteria.matchLambda(ochSignal);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800417 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800418 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800419 }
420
421 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700422 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800423 */
424 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700425 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700426 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800427 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800428 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800429 }
430
Ray Milkey0cc189e2015-02-09 11:08:01 -0800431 /**
432 * Tests that an unimplemented criterion type only returns the type and
433 * no other data.
434 */
435 @Test
436 public void matchUnknownTypeTest() throws Exception {
437 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
438 setField(criterion, "type", Criterion.Type.UNASSIGNED_40);
439 ObjectNode result = criterionCodec.encode(criterion, context);
440 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
441 assertThat(result.size(), is(1));
442 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800443}