blob: 873424bc49465ae39ec4a535d780d01098227a36 [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;
25import org.onlab.packet.VlanId;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey73ba84a2015-02-04 17:08:41 -080030import org.onosproject.net.flow.criteria.Criterion;
31
Ray Milkey9ee62f52015-02-06 13:47:35 -080032import com.fasterxml.jackson.databind.node.ObjectNode;
33
Ray Milkey73ba84a2015-02-04 17:08:41 -080034import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey0cc189e2015-02-09 11:08:01 -080035import static org.hamcrest.Matchers.is;
36import static org.hamcrest.Matchers.notNullValue;
37import static org.onlab.junit.TestUtils.getField;
38import static org.onlab.junit.TestUtils.setField;
Ray Milkey46670a82015-02-08 17:57:17 -080039import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080040
41/**
42 * Unit tests for criterion codec.
43 */
44public class CriterionCodecTest {
45
Ray Milkey9ee62f52015-02-06 13:47:35 -080046 CodecContext context;
47 JsonCodec<Criterion> criterionCodec;
48 final PortNumber port = PortNumber.portNumber(1);
49 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
50 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
51 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
52
53 /**
54 * Sets up for each test. Creates a context and fetches the criterion
55 * codec.
56 */
57 @Before
58 public void setUp() {
59 context = new MockCodecContext();
60 criterionCodec = context.codec(Criterion.class);
61 assertThat(criterionCodec, notNullValue());
62 }
63
64
Ray Milkey73ba84a2015-02-04 17:08:41 -080065 /**
66 * Checks that all criterion types are covered by the codec.
67 */
68 @Test
69 public void checkCriterionTypes() throws Exception {
Ray Milkey9ee62f52015-02-06 13:47:35 -080070 EnumMap<Criterion.Type, Object> formatMap =
71 getField(criterionCodec, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080072 assertThat(formatMap, notNullValue());
73
74 for (Criterion.Type type : Criterion.Type.values()) {
75 assertThat("Entry not found for " + type.toString(),
76 formatMap.get(type), notNullValue());
77 }
78 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080079
80 /**
81 * Tests in port criterion.
82 */
83 @Test
84 public void matchInPortTest() {
85 Criterion criterion = Criteria.matchInPort(port);
86 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080087 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080088 }
89
90 /**
91 * Tests in physical port criterion.
92 */
93 @Test
94 public void matchInPhyPortTest() {
95 Criterion criterion = Criteria.matchInPhyPort(port);
96 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080097 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080098 }
99
100 /**
101 * Tests metadata criterion.
102 */
103 @Test
104 public void matchMetadataTest() {
105 Criterion criterion = Criteria.matchMetadata(0xabcdL);
106 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800107 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800108 }
109
110 /**
111 * Tests ethernet destination criterion.
112 */
113 @Test
114 public void matchEthDstTest() {
115 Criterion criterion = Criteria.matchEthDst(mac1);
116 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800117 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800118 }
119
120 /**
121 * Tests ethernet source criterion.
122 */
123 @Test
124 public void matchEthSrcTest() {
125 Criterion criterion = Criteria.matchEthSrc(mac1);
126 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800127 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800128 }
129
130 /**
131 * Tests ethernet type criterion.
132 */
133 @Test
134 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800135 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800136 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800137 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800138 }
139
140 /**
141 * Tests VLAN Id criterion.
142 */
143 @Test
144 public void matchVlanIdTest() {
145 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
146 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800147 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800148 }
149
150 /**
151 * Tests VLAN PCP criterion.
152 */
153 @Test
154 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800155 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800156 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800157 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800158 }
159
160 /**
161 * Tests IP DSCP criterion.
162 */
163 @Test
164 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800165 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800166 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800167 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800168 }
169
170 /**
171 * Tests IP ECN criterion.
172 */
173 @Test
174 public void matchIPEcnTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800175 Criterion criterion = Criteria.matchIPEcn((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800176 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800177 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800178 }
179
180 /**
181 * Tests IP protocol criterion.
182 */
183 @Test
184 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800185 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800186 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800187 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800188 }
189
190 /**
191 * Tests IP source criterion.
192 */
193 @Test
194 public void matchIPSrcTest() {
195 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
196 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800197 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800198 }
199
200 /**
201 * Tests IP destination criterion.
202 */
203 @Test
204 public void matchIPDstTest() {
205 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
206 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800207 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800208 }
209
210 /**
211 * Tests source TCP port criterion.
212 */
213 @Test
214 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800215 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800216 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800217 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800218 }
219
220 /**
221 * Tests destination TCP port criterion.
222 */
223 @Test
224 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800225 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800226 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800227 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800228 }
229
230 /**
231 * Tests source UDP port criterion.
232 */
233 @Test
234 public void matchUdpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800235 Criterion criterion = Criteria.matchUdpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800236 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800237 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800238 }
239
240 /**
241 * Tests destination UDP criterion.
242 */
243 @Test
244 public void matchUdpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800245 Criterion criterion = Criteria.matchUdpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800246 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800247 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800248 }
249
250 /**
251 * Tests source SCTP criterion.
252 */
253 @Test
254 public void matchSctpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800255 Criterion criterion = Criteria.matchSctpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800256 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800257 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800258 }
259
260 /**
261 * Tests destination SCTP criterion.
262 */
263 @Test
264 public void matchSctpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800265 Criterion criterion = Criteria.matchSctpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800266 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800267 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800268 }
269
270 /**
271 * Tests ICMP type criterion.
272 */
273 @Test
274 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800275 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800276 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800277 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800278 }
279
280 /**
281 * Tests ICMP code criterion.
282 */
283 @Test
284 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800285 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800286 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800287 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800288 }
289
290 /**
291 * Tests IPv6 source criterion.
292 */
293 @Test
294 public void matchIPv6SrcTest() {
295 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
296 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800297 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800298 }
299
300 /**
301 * Tests IPv6 destination criterion.
302 */
303 @Test
304 public void matchIPv6DstTest() {
305 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
306 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800307 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800308 }
309
310 /**
311 * Tests IPv6 flow label criterion.
312 */
313 @Test
314 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800315 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800316 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800317 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800318 }
319
320 /**
321 * Tests ICMP v6 type criterion.
322 */
323 @Test
324 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800325 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800326 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800327 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800328 }
329
330 /**
331 * Tests ICMP v6 code criterion.
332 */
333 @Test
334 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800335 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800336 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800337 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800338 }
339
340 /**
341 * Tests IPV6 target address criterion.
342 */
343 @Test
344 public void matchIPv6NDTargetAddressTest() {
345 Criterion criterion =
346 Criteria.matchIPv6NDTargetAddress(
347 Ip6Address.valueOf("1111:2222::"));
348 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800349 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800350 }
351
352 /**
353 * Tests IPV6 SLL criterion.
354 */
355 @Test
356 public void matchIPv6NDSourceLinkLayerAddressTest() {
357 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
358 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800359 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800360 }
361
362 /**
363 * Tests IPV6 TLL criterion.
364 */
365 @Test
366 public void matchIPv6NDTargetLinkLayerAddressTest() {
367 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
368 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800369 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800370 }
371
372 /**
373 * Tests MPLS label criterion.
374 */
375 @Test
376 public void matchMplsLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800377 Criterion criterion = Criteria.matchMplsLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800378 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800379 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800380 }
381
382 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800383 * Tests IPv6 Extension Header pseudo-field flags criterion.
384 */
385 @Test
386 public void matchIPv6ExthdrFlagsTest() {
387 int exthdrFlags =
388 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
389 Criterion.IPv6ExthdrFlags.ESP.getValue() |
390 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
391 Criterion.IPv6ExthdrFlags.DEST.getValue() |
392 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
393 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
394 Criterion.IPv6ExthdrFlags.HOP.getValue() |
395 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
396 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
397 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
398 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800399
400 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800401 }
402
403 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800404 * Tests lambda criterion.
405 */
406 @Test
407 public void matchLambdaTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800408 Criterion criterion = Criteria.matchLambda((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800409 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800410 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800411 }
412
413 /**
414 * Tests optical signal type criterion.
415 */
416 @Test
417 public void matchOpticalSignalTypeTest() {
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800418 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800419 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800420 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800421 }
422
Ray Milkey0cc189e2015-02-09 11:08:01 -0800423 /**
424 * Tests that an unimplemented criterion type only returns the type and
425 * no other data.
426 */
427 @Test
428 public void matchUnknownTypeTest() throws Exception {
429 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
430 setField(criterion, "type", Criterion.Type.UNASSIGNED_40);
431 ObjectNode result = criterionCodec.encode(criterion, context);
432 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
433 assertThat(result.size(), is(1));
434 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800435}