blob: 6b3d40a992d029235189a843de2d4e5d2668e7dc [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 SHIMIZU6c70f642015-05-29 17:27:22 -070029import org.onosproject.net.OchSignalType;
Ray Milkey9ee62f52015-02-06 13:47:35 -080030import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey73ba84a2015-02-04 17:08:41 -080032import org.onosproject.net.flow.criteria.Criterion;
33
Ray Milkey9ee62f52015-02-06 13:47:35 -080034import com.fasterxml.jackson.databind.node.ObjectNode;
35
Ray Milkey73ba84a2015-02-04 17:08:41 -080036import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey0cc189e2015-02-09 11:08:01 -080037import static org.hamcrest.Matchers.is;
38import static org.hamcrest.Matchers.notNullValue;
39import static org.onlab.junit.TestUtils.getField;
40import static org.onlab.junit.TestUtils.setField;
Ray Milkey46670a82015-02-08 17:57:17 -080041import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080042
43/**
44 * Unit tests for criterion codec.
45 */
46public class CriterionCodecTest {
47
Ray Milkey9ee62f52015-02-06 13:47:35 -080048 CodecContext context;
49 JsonCodec<Criterion> criterionCodec;
50 final PortNumber port = PortNumber.portNumber(1);
51 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
52 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
53 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
54
55 /**
56 * Sets up for each test. Creates a context and fetches the criterion
57 * codec.
58 */
59 @Before
60 public void setUp() {
61 context = new MockCodecContext();
62 criterionCodec = context.codec(Criterion.class);
63 assertThat(criterionCodec, notNullValue());
64 }
65
66
Ray Milkey73ba84a2015-02-04 17:08:41 -080067 /**
68 * Checks that all criterion types are covered by the codec.
69 */
70 @Test
71 public void checkCriterionTypes() throws Exception {
Ray Milkey9ee62f52015-02-06 13:47:35 -080072 EnumMap<Criterion.Type, Object> formatMap =
73 getField(criterionCodec, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080074 assertThat(formatMap, notNullValue());
75
76 for (Criterion.Type type : Criterion.Type.values()) {
77 assertThat("Entry not found for " + type.toString(),
78 formatMap.get(type), notNullValue());
79 }
80 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080081
82 /**
83 * Tests in port criterion.
84 */
85 @Test
86 public void matchInPortTest() {
87 Criterion criterion = Criteria.matchInPort(port);
88 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080089 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080090 }
91
92 /**
93 * Tests in physical port criterion.
94 */
95 @Test
96 public void matchInPhyPortTest() {
97 Criterion criterion = Criteria.matchInPhyPort(port);
98 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080099 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800100 }
101
102 /**
103 * Tests metadata criterion.
104 */
105 @Test
106 public void matchMetadataTest() {
107 Criterion criterion = Criteria.matchMetadata(0xabcdL);
108 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800109 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800110 }
111
112 /**
113 * Tests ethernet destination criterion.
114 */
115 @Test
116 public void matchEthDstTest() {
117 Criterion criterion = Criteria.matchEthDst(mac1);
118 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800119 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800120 }
121
122 /**
123 * Tests ethernet source criterion.
124 */
125 @Test
126 public void matchEthSrcTest() {
127 Criterion criterion = Criteria.matchEthSrc(mac1);
128 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800129 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800130 }
131
132 /**
133 * Tests ethernet type criterion.
134 */
135 @Test
136 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800137 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800138 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800139 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800140 }
141
142 /**
143 * Tests VLAN Id criterion.
144 */
145 @Test
146 public void matchVlanIdTest() {
147 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
148 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800149 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800150 }
151
152 /**
153 * Tests VLAN PCP criterion.
154 */
155 @Test
156 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800157 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800158 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800159 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800160 }
161
162 /**
163 * Tests IP DSCP criterion.
164 */
165 @Test
166 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800167 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800168 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800169 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800170 }
171
172 /**
173 * Tests IP ECN criterion.
174 */
175 @Test
176 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800177 Criterion criterion = Criteria.matchIPEcn((byte) 3);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800178 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800179 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800180 }
181
182 /**
183 * Tests IP protocol criterion.
184 */
185 @Test
186 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800187 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800188 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800189 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800190 }
191
192 /**
193 * Tests IP source criterion.
194 */
195 @Test
196 public void matchIPSrcTest() {
197 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
198 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800199 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800200 }
201
202 /**
203 * Tests IP destination criterion.
204 */
205 @Test
206 public void matchIPDstTest() {
207 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
208 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800209 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800210 }
211
212 /**
213 * Tests source TCP port criterion.
214 */
215 @Test
216 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800217 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800218 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800219 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800220 }
221
222 /**
223 * Tests destination TCP port criterion.
224 */
225 @Test
226 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800227 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800228 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800229 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800230 }
231
232 /**
233 * Tests source UDP port criterion.
234 */
235 @Test
236 public void matchUdpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800237 Criterion criterion = Criteria.matchUdpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800238 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800239 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800240 }
241
242 /**
243 * Tests destination UDP criterion.
244 */
245 @Test
246 public void matchUdpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800247 Criterion criterion = Criteria.matchUdpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800248 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800249 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800250 }
251
252 /**
253 * Tests source SCTP criterion.
254 */
255 @Test
256 public void matchSctpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800257 Criterion criterion = Criteria.matchSctpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800258 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800259 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800260 }
261
262 /**
263 * Tests destination SCTP criterion.
264 */
265 @Test
266 public void matchSctpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800267 Criterion criterion = Criteria.matchSctpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800268 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800269 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800270 }
271
272 /**
273 * Tests ICMP type criterion.
274 */
275 @Test
276 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800277 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800278 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800279 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800280 }
281
282 /**
283 * Tests ICMP code criterion.
284 */
285 @Test
286 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800287 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800288 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800289 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800290 }
291
292 /**
293 * Tests IPv6 source criterion.
294 */
295 @Test
296 public void matchIPv6SrcTest() {
297 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
298 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800299 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800300 }
301
302 /**
303 * Tests IPv6 destination criterion.
304 */
305 @Test
306 public void matchIPv6DstTest() {
307 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
308 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800309 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800310 }
311
312 /**
313 * Tests IPv6 flow label criterion.
314 */
315 @Test
316 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800317 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800318 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800319 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800320 }
321
322 /**
323 * Tests ICMP v6 type criterion.
324 */
325 @Test
326 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800327 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800328 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800329 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800330 }
331
332 /**
333 * Tests ICMP v6 code criterion.
334 */
335 @Test
336 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800337 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800338 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800339 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800340 }
341
342 /**
343 * Tests IPV6 target address criterion.
344 */
345 @Test
346 public void matchIPv6NDTargetAddressTest() {
347 Criterion criterion =
348 Criteria.matchIPv6NDTargetAddress(
349 Ip6Address.valueOf("1111:2222::"));
350 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800351 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800352 }
353
354 /**
355 * Tests IPV6 SLL criterion.
356 */
357 @Test
358 public void matchIPv6NDSourceLinkLayerAddressTest() {
359 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
360 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800361 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800362 }
363
364 /**
365 * Tests IPV6 TLL criterion.
366 */
367 @Test
368 public void matchIPv6NDTargetLinkLayerAddressTest() {
369 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
370 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800371 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800372 }
373
374 /**
375 * Tests MPLS label criterion.
376 */
377 @Test
378 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100379 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800380 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800381 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800382 }
383
384 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800385 * Tests IPv6 Extension Header pseudo-field flags criterion.
386 */
387 @Test
388 public void matchIPv6ExthdrFlagsTest() {
389 int exthdrFlags =
390 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
391 Criterion.IPv6ExthdrFlags.ESP.getValue() |
392 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
393 Criterion.IPv6ExthdrFlags.DEST.getValue() |
394 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
395 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
396 Criterion.IPv6ExthdrFlags.HOP.getValue() |
397 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
398 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
399 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
400 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800401
402 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800403 }
404
405 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800406 * Tests lambda criterion.
407 */
408 @Test
409 public void matchLambdaTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800410 Criterion criterion = Criteria.matchLambda((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800411 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800412 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800413 }
414
415 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700416 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800417 */
418 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700419 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700420 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800421 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800422 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800423 }
424
Ray Milkey0cc189e2015-02-09 11:08:01 -0800425 /**
426 * Tests that an unimplemented criterion type only returns the type and
427 * no other data.
428 */
429 @Test
430 public void matchUnknownTypeTest() throws Exception {
431 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
432 setField(criterion, "type", Criterion.Type.UNASSIGNED_40);
433 ObjectNode result = criterionCodec.encode(criterion, context);
434 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
435 assertThat(result.size(), is(1));
436 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800437}