blob: 919719cc305ee1d587ec720e28876a21274390b5 [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 Milkey9ee62f52015-02-06 13:47:35 -080075 EnumMap<Criterion.Type, Object> formatMap =
76 getField(criterionCodec, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080077 assertThat(formatMap, notNullValue());
78
79 for (Criterion.Type type : Criterion.Type.values()) {
80 assertThat("Entry not found for " + type.toString(),
81 formatMap.get(type), notNullValue());
82 }
83 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080084
85 /**
86 * Tests in port criterion.
87 */
88 @Test
89 public void matchInPortTest() {
90 Criterion criterion = Criteria.matchInPort(port);
91 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080092 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080093 }
94
95 /**
96 * Tests in physical port criterion.
97 */
98 @Test
99 public void matchInPhyPortTest() {
100 Criterion criterion = Criteria.matchInPhyPort(port);
101 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800102 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800103 }
104
105 /**
106 * Tests metadata criterion.
107 */
108 @Test
109 public void matchMetadataTest() {
110 Criterion criterion = Criteria.matchMetadata(0xabcdL);
111 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800112 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800113 }
114
115 /**
116 * Tests ethernet destination criterion.
117 */
118 @Test
119 public void matchEthDstTest() {
120 Criterion criterion = Criteria.matchEthDst(mac1);
121 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800122 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800123 }
124
125 /**
126 * Tests ethernet source criterion.
127 */
128 @Test
129 public void matchEthSrcTest() {
130 Criterion criterion = Criteria.matchEthSrc(mac1);
131 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800132 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800133 }
134
135 /**
136 * Tests ethernet type criterion.
137 */
138 @Test
139 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800140 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800141 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800142 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800143 }
144
145 /**
146 * Tests VLAN Id criterion.
147 */
148 @Test
149 public void matchVlanIdTest() {
150 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
151 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800152 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800153 }
154
155 /**
156 * Tests VLAN PCP criterion.
157 */
158 @Test
159 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800160 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800161 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800162 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800163 }
164
165 /**
166 * Tests IP DSCP criterion.
167 */
168 @Test
169 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800170 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800171 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800172 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800173 }
174
175 /**
176 * Tests IP ECN criterion.
177 */
178 @Test
179 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800180 Criterion criterion = Criteria.matchIPEcn((byte) 3);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800181 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800182 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800183 }
184
185 /**
186 * Tests IP protocol criterion.
187 */
188 @Test
189 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800190 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800191 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800192 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800193 }
194
195 /**
196 * Tests IP source criterion.
197 */
198 @Test
199 public void matchIPSrcTest() {
200 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
201 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800202 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800203 }
204
205 /**
206 * Tests IP destination criterion.
207 */
208 @Test
209 public void matchIPDstTest() {
210 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
211 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800212 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800213 }
214
215 /**
216 * Tests source TCP port criterion.
217 */
218 @Test
219 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800220 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800221 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800222 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800223 }
224
225 /**
226 * Tests destination TCP port criterion.
227 */
228 @Test
229 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800230 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800231 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800232 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800233 }
234
235 /**
236 * Tests source UDP port criterion.
237 */
238 @Test
239 public void matchUdpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800240 Criterion criterion = Criteria.matchUdpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800241 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800242 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800243 }
244
245 /**
246 * Tests destination UDP criterion.
247 */
248 @Test
249 public void matchUdpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800250 Criterion criterion = Criteria.matchUdpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800251 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800252 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800253 }
254
255 /**
256 * Tests source SCTP criterion.
257 */
258 @Test
259 public void matchSctpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800260 Criterion criterion = Criteria.matchSctpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800261 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800262 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800263 }
264
265 /**
266 * Tests destination SCTP criterion.
267 */
268 @Test
269 public void matchSctpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800270 Criterion criterion = Criteria.matchSctpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800271 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800272 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800273 }
274
275 /**
276 * Tests ICMP type criterion.
277 */
278 @Test
279 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800280 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800281 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800282 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800283 }
284
285 /**
286 * Tests ICMP code criterion.
287 */
288 @Test
289 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800290 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800291 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800292 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800293 }
294
295 /**
296 * Tests IPv6 source criterion.
297 */
298 @Test
299 public void matchIPv6SrcTest() {
300 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
301 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800302 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800303 }
304
305 /**
306 * Tests IPv6 destination criterion.
307 */
308 @Test
309 public void matchIPv6DstTest() {
310 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
311 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800312 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800313 }
314
315 /**
316 * Tests IPv6 flow label criterion.
317 */
318 @Test
319 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800320 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800321 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800322 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800323 }
324
325 /**
326 * Tests ICMP v6 type criterion.
327 */
328 @Test
329 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800330 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800331 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800332 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800333 }
334
335 /**
336 * Tests ICMP v6 code criterion.
337 */
338 @Test
339 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800340 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800341 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800342 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800343 }
344
345 /**
346 * Tests IPV6 target address criterion.
347 */
348 @Test
349 public void matchIPv6NDTargetAddressTest() {
350 Criterion criterion =
351 Criteria.matchIPv6NDTargetAddress(
352 Ip6Address.valueOf("1111:2222::"));
353 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800354 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800355 }
356
357 /**
358 * Tests IPV6 SLL criterion.
359 */
360 @Test
361 public void matchIPv6NDSourceLinkLayerAddressTest() {
362 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
363 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800364 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800365 }
366
367 /**
368 * Tests IPV6 TLL criterion.
369 */
370 @Test
371 public void matchIPv6NDTargetLinkLayerAddressTest() {
372 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
373 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800374 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800375 }
376
377 /**
378 * Tests MPLS label criterion.
379 */
380 @Test
381 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100382 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800383 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800384 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800385 }
386
387 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800388 * Tests IPv6 Extension Header pseudo-field flags criterion.
389 */
390 @Test
391 public void matchIPv6ExthdrFlagsTest() {
392 int exthdrFlags =
393 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
394 Criterion.IPv6ExthdrFlags.ESP.getValue() |
395 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
396 Criterion.IPv6ExthdrFlags.DEST.getValue() |
397 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
398 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
399 Criterion.IPv6ExthdrFlags.HOP.getValue() |
400 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
401 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
402 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
403 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800404
405 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800406 }
407
408 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800409 * Tests lambda criterion.
410 */
411 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700412 public void matchOchSignal() {
413 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
414 Criterion criterion = Criteria.matchLambda(ochSignal);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800415 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800416 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800417 }
418
419 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700420 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800421 */
422 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700423 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700424 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800425 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800426 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800427 }
428
Ray Milkey0cc189e2015-02-09 11:08:01 -0800429 /**
430 * Tests that an unimplemented criterion type only returns the type and
431 * no other data.
432 */
433 @Test
434 public void matchUnknownTypeTest() throws Exception {
435 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
436 setField(criterion, "type", Criterion.Type.UNASSIGNED_40);
437 ObjectNode result = criterionCodec.encode(criterion, context);
438 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
439 assertThat(result.size(), is(1));
440 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800441}