blob: 731f03e5be29a0ae0404f6ebcd5a9e073f97930e [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;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey73ba84a2015-02-04 17:08:41 -080031import org.onosproject.net.flow.criteria.Criterion;
32
Ray Milkey9ee62f52015-02-06 13:47:35 -080033import com.fasterxml.jackson.databind.node.ObjectNode;
34
Ray Milkey73ba84a2015-02-04 17:08:41 -080035import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkey0cc189e2015-02-09 11:08:01 -080036import static org.hamcrest.Matchers.is;
37import static org.hamcrest.Matchers.notNullValue;
38import static org.onlab.junit.TestUtils.getField;
39import static org.onlab.junit.TestUtils.setField;
Ray Milkey46670a82015-02-08 17:57:17 -080040import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080041
42/**
43 * Unit tests for criterion codec.
44 */
45public class CriterionCodecTest {
46
Ray Milkey9ee62f52015-02-06 13:47:35 -080047 CodecContext context;
48 JsonCodec<Criterion> criterionCodec;
49 final PortNumber port = PortNumber.portNumber(1);
50 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
51 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
52 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
53
54 /**
55 * Sets up for each test. Creates a context and fetches the criterion
56 * codec.
57 */
58 @Before
59 public void setUp() {
60 context = new MockCodecContext();
61 criterionCodec = context.codec(Criterion.class);
62 assertThat(criterionCodec, notNullValue());
63 }
64
65
Ray Milkey73ba84a2015-02-04 17:08:41 -080066 /**
67 * Checks that all criterion types are covered by the codec.
68 */
69 @Test
70 public void checkCriterionTypes() throws Exception {
Ray Milkey9ee62f52015-02-06 13:47:35 -080071 EnumMap<Criterion.Type, Object> formatMap =
72 getField(criterionCodec, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -080073 assertThat(formatMap, notNullValue());
74
75 for (Criterion.Type type : Criterion.Type.values()) {
76 assertThat("Entry not found for " + type.toString(),
77 formatMap.get(type), notNullValue());
78 }
79 }
Ray Milkey9ee62f52015-02-06 13:47:35 -080080
81 /**
82 * Tests in port criterion.
83 */
84 @Test
85 public void matchInPortTest() {
86 Criterion criterion = Criteria.matchInPort(port);
87 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080088 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080089 }
90
91 /**
92 * Tests in physical port criterion.
93 */
94 @Test
95 public void matchInPhyPortTest() {
96 Criterion criterion = Criteria.matchInPhyPort(port);
97 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -080098 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -080099 }
100
101 /**
102 * Tests metadata criterion.
103 */
104 @Test
105 public void matchMetadataTest() {
106 Criterion criterion = Criteria.matchMetadata(0xabcdL);
107 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800108 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800109 }
110
111 /**
112 * Tests ethernet destination criterion.
113 */
114 @Test
115 public void matchEthDstTest() {
116 Criterion criterion = Criteria.matchEthDst(mac1);
117 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800118 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800119 }
120
121 /**
122 * Tests ethernet source criterion.
123 */
124 @Test
125 public void matchEthSrcTest() {
126 Criterion criterion = Criteria.matchEthSrc(mac1);
127 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800128 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800129 }
130
131 /**
132 * Tests ethernet type criterion.
133 */
134 @Test
135 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800136 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800137 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800138 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800139 }
140
141 /**
142 * Tests VLAN Id criterion.
143 */
144 @Test
145 public void matchVlanIdTest() {
146 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
147 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800148 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800149 }
150
151 /**
152 * Tests VLAN PCP criterion.
153 */
154 @Test
155 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800156 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800157 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800158 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800159 }
160
161 /**
162 * Tests IP DSCP criterion.
163 */
164 @Test
165 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800166 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800167 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800168 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800169 }
170
171 /**
172 * Tests IP ECN criterion.
173 */
174 @Test
175 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800176 Criterion criterion = Criteria.matchIPEcn((byte) 3);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800177 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800178 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800179 }
180
181 /**
182 * Tests IP protocol criterion.
183 */
184 @Test
185 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800186 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800187 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800188 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800189 }
190
191 /**
192 * Tests IP source criterion.
193 */
194 @Test
195 public void matchIPSrcTest() {
196 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
197 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800198 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800199 }
200
201 /**
202 * Tests IP destination criterion.
203 */
204 @Test
205 public void matchIPDstTest() {
206 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
207 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800208 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800209 }
210
211 /**
212 * Tests source TCP port criterion.
213 */
214 @Test
215 public void matchTcpSrcTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800216 Criterion criterion = Criteria.matchTcpSrc((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800217 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800218 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800219 }
220
221 /**
222 * Tests destination TCP port criterion.
223 */
224 @Test
225 public void matchTcpDstTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800226 Criterion criterion = Criteria.matchTcpDst((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800227 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800228 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800229 }
230
231 /**
232 * Tests source UDP port criterion.
233 */
234 @Test
235 public void matchUdpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800236 Criterion criterion = Criteria.matchUdpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800237 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800238 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800239 }
240
241 /**
242 * Tests destination UDP criterion.
243 */
244 @Test
245 public void matchUdpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800246 Criterion criterion = Criteria.matchUdpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800247 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800248 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800249 }
250
251 /**
252 * Tests source SCTP criterion.
253 */
254 @Test
255 public void matchSctpSrcTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800256 Criterion criterion = Criteria.matchSctpSrc(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800257 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800258 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800259 }
260
261 /**
262 * Tests destination SCTP criterion.
263 */
264 @Test
265 public void matchSctpDstTest() {
Ray Milkey46670a82015-02-08 17:57:17 -0800266 Criterion criterion = Criteria.matchSctpDst(40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800267 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800268 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800269 }
270
271 /**
272 * Tests ICMP type criterion.
273 */
274 @Test
275 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800276 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800277 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800278 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800279 }
280
281 /**
282 * Tests ICMP code criterion.
283 */
284 @Test
285 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800286 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800287 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800288 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800289 }
290
291 /**
292 * Tests IPv6 source criterion.
293 */
294 @Test
295 public void matchIPv6SrcTest() {
296 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
297 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800298 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800299 }
300
301 /**
302 * Tests IPv6 destination criterion.
303 */
304 @Test
305 public void matchIPv6DstTest() {
306 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
307 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800308 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800309 }
310
311 /**
312 * Tests IPv6 flow label criterion.
313 */
314 @Test
315 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800316 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800317 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800318 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800319 }
320
321 /**
322 * Tests ICMP v6 type criterion.
323 */
324 @Test
325 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800326 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800327 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800328 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800329 }
330
331 /**
332 * Tests ICMP v6 code criterion.
333 */
334 @Test
335 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800336 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800337 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800338 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800339 }
340
341 /**
342 * Tests IPV6 target address criterion.
343 */
344 @Test
345 public void matchIPv6NDTargetAddressTest() {
346 Criterion criterion =
347 Criteria.matchIPv6NDTargetAddress(
348 Ip6Address.valueOf("1111:2222::"));
349 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800350 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800351 }
352
353 /**
354 * Tests IPV6 SLL criterion.
355 */
356 @Test
357 public void matchIPv6NDSourceLinkLayerAddressTest() {
358 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
359 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800360 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800361 }
362
363 /**
364 * Tests IPV6 TLL criterion.
365 */
366 @Test
367 public void matchIPv6NDTargetLinkLayerAddressTest() {
368 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
369 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800370 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800371 }
372
373 /**
374 * Tests MPLS label criterion.
375 */
376 @Test
377 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100378 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800379 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800380 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800381 }
382
383 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800384 * Tests IPv6 Extension Header pseudo-field flags criterion.
385 */
386 @Test
387 public void matchIPv6ExthdrFlagsTest() {
388 int exthdrFlags =
389 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
390 Criterion.IPv6ExthdrFlags.ESP.getValue() |
391 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
392 Criterion.IPv6ExthdrFlags.DEST.getValue() |
393 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
394 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
395 Criterion.IPv6ExthdrFlags.HOP.getValue() |
396 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
397 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
398 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
399 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800400
401 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800402 }
403
404 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800405 * Tests lambda criterion.
406 */
407 @Test
408 public void matchLambdaTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800409 Criterion criterion = Criteria.matchLambda((short) 40000);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800410 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800411 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800412 }
413
414 /**
415 * Tests optical signal type criterion.
416 */
417 @Test
418 public void matchOpticalSignalTypeTest() {
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800419 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800420 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800421 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800422 }
423
Ray Milkey0cc189e2015-02-09 11:08:01 -0800424 /**
425 * Tests that an unimplemented criterion type only returns the type and
426 * no other data.
427 */
428 @Test
429 public void matchUnknownTypeTest() throws Exception {
430 Criterion criterion = Criteria.matchOpticalSignalType((byte) 250);
431 setField(criterion, "type", Criterion.Type.UNASSIGNED_40);
432 ObjectNode result = criterionCodec.encode(criterion, context);
433 assertThat(result.get("type").textValue(), is(criterion.type().toString()));
434 assertThat(result.size(), is(1));
435 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800436}