blob: 1542f0dbb0087de3b7d721c4863b7041c79c091e [file] [log] [blame]
Ray Milkey73ba84a2015-02-04 17:08:41 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey73ba84a2015-02-04 17:08:41 -08003 *
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
Frank Wang74ce2c12018-04-11 20:26:45 +080018import java.io.IOException;
19import java.io.InputStream;
20import java.util.Collection;
Ray Milkey73ba84a2015-02-04 17:08:41 -080021import java.util.EnumMap;
22
Frank Wang74ce2c12018-04-11 20:26:45 +080023import com.fasterxml.jackson.databind.JsonNode;
24import org.hamcrest.MatcherAssert;
25import org.junit.Assert;
Ray Milkey9ee62f52015-02-06 13:47:35 -080026import org.junit.Before;
Ray Milkey73ba84a2015-02-04 17:08:41 -080027import org.junit.Test;
Ray Milkey9ee62f52015-02-06 13:47:35 -080028import org.onlab.packet.Ip6Address;
29import org.onlab.packet.IpPrefix;
30import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010031import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070032import org.onlab.packet.TpPort;
Ray Milkey9ee62f52015-02-06 13:47:35 -080033import org.onlab.packet.VlanId;
34import org.onosproject.codec.CodecContext;
35import org.onosproject.codec.JsonCodec;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070036import org.onosproject.net.ChannelSpacing;
37import org.onosproject.net.GridType;
38import org.onosproject.net.Lambda;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070039import org.onosproject.net.OchSignalType;
Yafit Hadar5796d972015-10-15 13:16:11 +030040import org.onosproject.net.OduSignalId;
41import org.onosproject.net.OduSignalType;
Ray Milkey9ee62f52015-02-06 13:47:35 -080042import org.onosproject.net.PortNumber;
43import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey73ba84a2015-02-04 17:08:41 -080044import org.onosproject.net.flow.criteria.Criterion;
45
Ray Milkey9ee62f52015-02-06 13:47:35 -080046import com.fasterxml.jackson.databind.node.ObjectNode;
Frank Wang74ce2c12018-04-11 20:26:45 +080047import org.onosproject.net.flow.criteria.PiCriterion;
48import org.onosproject.net.pi.model.PiMatchFieldId;
49import org.onosproject.net.pi.runtime.PiExactFieldMatch;
50import org.onosproject.net.pi.runtime.PiFieldMatch;
51import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
Daniele Moroc6f2f7f2020-12-18 10:55:57 +010052import org.onosproject.net.pi.runtime.PiOptionalFieldMatch;
Frank Wang74ce2c12018-04-11 20:26:45 +080053import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
54import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Ray Milkey9ee62f52015-02-06 13:47:35 -080055
Ray Milkey73ba84a2015-02-04 17:08:41 -080056import static org.hamcrest.MatcherAssert.assertThat;
Frank Wang74ce2c12018-04-11 20:26:45 +080057import static org.hamcrest.Matchers.is;
Ray Milkey0cc189e2015-02-09 11:08:01 -080058import static org.hamcrest.Matchers.notNullValue;
59import static org.onlab.junit.TestUtils.getField;
Frank Wang74ce2c12018-04-11 20:26:45 +080060import static org.onlab.util.ImmutableByteSequence.copyFrom;
Ray Milkey46670a82015-02-08 17:57:17 -080061import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080062
63/**
64 * Unit tests for criterion codec.
65 */
66public class CriterionCodecTest {
67
Ray Milkey9ee62f52015-02-06 13:47:35 -080068 CodecContext context;
69 JsonCodec<Criterion> criterionCodec;
70 final PortNumber port = PortNumber.portNumber(1);
71 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
72 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
73 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
Seyeon Jeong8f014142020-02-26 12:51:03 -080074 final MacAddress mcastMac = MacAddress.valueOf("01:00:5E:00:00:01");
75 final MacAddress mcastMacMask = MacAddress.valueOf("FF:FF:FF:80:00:00");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070076 final TpPort tpPort = TpPort.tpPort(40000);
David Glantz6210c4c2021-09-21 15:39:19 -050077 final TpPort tpPortMask = TpPort.tpPort(65472);
Yafit Hadar5796d972015-10-15 13:16:11 +030078 final int tributaryPortNumber = 11;
79 final int tributarySlotLen = 80;
80 final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4};
81
Ray Milkey9ee62f52015-02-06 13:47:35 -080082
83 /**
84 * Sets up for each test. Creates a context and fetches the criterion
85 * codec.
86 */
87 @Before
88 public void setUp() {
89 context = new MockCodecContext();
90 criterionCodec = context.codec(Criterion.class);
91 assertThat(criterionCodec, notNullValue());
92 }
93
94
Ray Milkey73ba84a2015-02-04 17:08:41 -080095 /**
96 * Checks that all criterion types are covered by the codec.
97 */
98 @Test
99 public void checkCriterionTypes() throws Exception {
Ray Milkey6d7968e2015-07-06 14:30:02 -0700100 EncodeCriterionCodecHelper encoder = new EncodeCriterionCodecHelper(
Ray Milkeyd43fe452015-05-29 09:35:12 -0700101 Criteria.dummy(), context);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800102 EnumMap<Criterion.Type, Object> formatMap =
Ray Milkeyd43fe452015-05-29 09:35:12 -0700103 getField(encoder, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -0800104 assertThat(formatMap, notNullValue());
105
106 for (Criterion.Type type : Criterion.Type.values()) {
107 assertThat("Entry not found for " + type.toString(),
108 formatMap.get(type), notNullValue());
109 }
110 }
Ray Milkey9ee62f52015-02-06 13:47:35 -0800111
112 /**
113 * Tests in port criterion.
114 */
115 @Test
116 public void matchInPortTest() {
117 Criterion criterion = Criteria.matchInPort(port);
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 in physical port criterion.
124 */
125 @Test
126 public void matchInPhyPortTest() {
127 Criterion criterion = Criteria.matchInPhyPort(port);
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 metadata criterion.
134 */
135 @Test
136 public void matchMetadataTest() {
137 Criterion criterion = Criteria.matchMetadata(0xabcdL);
138 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 ethernet destination criterion.
144 */
145 @Test
146 public void matchEthDstTest() {
147 Criterion criterion = Criteria.matchEthDst(mac1);
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 /**
Seyeon Jeong8f014142020-02-26 12:51:03 -0800153 * Tests masked ethernet destination criterion (Criterion.Type.ETH_DST_MASKED).
154 */
155 @Test
156 public void matchEthDstMaskTest() {
157 Criterion criterion = Criteria.matchEthDstMasked(mcastMac, mcastMacMask);
158 ObjectNode result = criterionCodec.encode(criterion, context);
159 assertThat(result, matchesCriterion(criterion));
160 }
161
162 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800163 * Tests ethernet source criterion.
164 */
165 @Test
166 public void matchEthSrcTest() {
167 Criterion criterion = Criteria.matchEthSrc(mac1);
168 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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500173 * Tests masked ethernet source criterion (Criterion.Type.ETH_SRC_MASKED).
174 */
175 @Test
176 public void matchEthSrcMaskTest() {
177 Criterion criterion = Criteria.matchEthSrcMasked(mcastMac, mcastMacMask);
178 ObjectNode result = criterionCodec.encode(criterion, context);
179 assertThat(result, matchesCriterion(criterion));
180 }
181
182 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800183 * Tests ethernet type criterion.
184 */
185 @Test
186 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800187 Criterion criterion = Criteria.matchEthType((short) 0x8844);
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 VLAN Id criterion.
194 */
195 @Test
196 public void matchVlanIdTest() {
197 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
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 VLAN PCP criterion.
204 */
205 @Test
206 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800207 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800208 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 IP DSCP criterion.
214 */
215 @Test
216 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800217 Criterion criterion = Criteria.matchIPDscp((byte) 63);
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 IP ECN criterion.
224 */
225 @Test
226 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800227 Criterion criterion = Criteria.matchIPEcn((byte) 3);
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 IP protocol criterion.
234 */
235 @Test
236 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800237 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
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 IP source criterion.
244 */
245 @Test
246 public void matchIPSrcTest() {
247 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
248 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 IP destination criterion.
254 */
255 @Test
256 public void matchIPDstTest() {
257 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
258 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 source TCP port criterion.
264 */
265 @Test
266 public void matchTcpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700267 Criterion criterion = Criteria.matchTcpSrc(tpPort);
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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500273 * Tests source TCP port masked criterion.
274 */
275 @Test
276 public void matchTcpSrcMaskedTest() {
277 Criterion criterion = Criteria.matchTcpSrcMasked(tpPort, tpPortMask);
278 ObjectNode result = criterionCodec.encode(criterion, context);
279 assertThat(result, matchesCriterion(criterion));
280 }
281
282 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800283 * Tests destination TCP port criterion.
284 */
285 @Test
286 public void matchTcpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700287 Criterion criterion = Criteria.matchTcpDst(tpPort);
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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500293 * Tests destination TCP port masked criterion.
294 */
295 @Test
296 public void matchTcpDstMaskedTest() {
297 Criterion criterion = Criteria.matchTcpDstMasked(tpPort, tpPortMask);
298 ObjectNode result = criterionCodec.encode(criterion, context);
299 assertThat(result, matchesCriterion(criterion));
300 }
301
302 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800303 * Tests source UDP port criterion.
304 */
305 @Test
306 public void matchUdpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700307 Criterion criterion = Criteria.matchUdpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800308 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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500313 * Tests source UDP port masked criterion.
314 */
315 @Test
316 public void matchUdpSrcMaskedTest() {
317 Criterion criterion = Criteria.matchUdpSrcMasked(tpPort, tpPortMask);
318 ObjectNode result = criterionCodec.encode(criterion, context);
319 assertThat(result, matchesCriterion(criterion));
320 }
321
322 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800323 * Tests destination UDP criterion.
324 */
325 @Test
326 public void matchUdpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700327 Criterion criterion = Criteria.matchUdpDst(tpPort);
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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500333 * Tests destination UDP port masked criterion.
334 */
335 @Test
336 public void matchUdpDstMaskedTest() {
337 Criterion criterion = Criteria.matchUdpDstMasked(tpPort, tpPortMask);
338 ObjectNode result = criterionCodec.encode(criterion, context);
339 assertThat(result, matchesCriterion(criterion));
340 }
341
342 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800343 * Tests source SCTP criterion.
344 */
345 @Test
346 public void matchSctpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700347 Criterion criterion = Criteria.matchSctpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800348 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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500353 * Tests source SCTP port masked criterion.
354 */
355 @Test
356 public void matchSctpSrcMaskedTest() {
357 Criterion criterion = Criteria.matchSctpSrcMasked(tpPort, tpPortMask);
358 ObjectNode result = criterionCodec.encode(criterion, context);
359 assertThat(result, matchesCriterion(criterion));
360 }
361
362 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800363 * Tests destination SCTP criterion.
364 */
365 @Test
366 public void matchSctpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700367 Criterion criterion = Criteria.matchSctpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800368 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 /**
David Glantz6210c4c2021-09-21 15:39:19 -0500373 * Tests destination SCTP port masked criterion.
374 */
375 @Test
376 public void matchSctpDstMaskedTest() {
377 Criterion criterion = Criteria.matchSctpDstMasked(tpPort, tpPortMask);
378 ObjectNode result = criterionCodec.encode(criterion, context);
379 assertThat(result, matchesCriterion(criterion));
380 }
381
382 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800383 * Tests ICMP type criterion.
384 */
385 @Test
386 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800387 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800388 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800389 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800390 }
391
392 /**
393 * Tests ICMP code criterion.
394 */
395 @Test
396 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800397 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800398 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800399 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800400 }
401
402 /**
403 * Tests IPv6 source criterion.
404 */
405 @Test
406 public void matchIPv6SrcTest() {
407 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
408 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800409 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800410 }
411
412 /**
413 * Tests IPv6 destination criterion.
414 */
415 @Test
416 public void matchIPv6DstTest() {
417 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
418 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800419 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800420 }
421
422 /**
423 * Tests IPv6 flow label criterion.
424 */
425 @Test
426 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800427 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800428 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800429 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800430 }
431
432 /**
433 * Tests ICMP v6 type criterion.
434 */
435 @Test
436 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800437 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800438 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800439 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800440 }
441
442 /**
443 * Tests ICMP v6 code criterion.
444 */
445 @Test
446 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800447 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800448 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800449 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800450 }
451
452 /**
453 * Tests IPV6 target address criterion.
454 */
455 @Test
456 public void matchIPv6NDTargetAddressTest() {
457 Criterion criterion =
458 Criteria.matchIPv6NDTargetAddress(
459 Ip6Address.valueOf("1111:2222::"));
460 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800461 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800462 }
463
464 /**
465 * Tests IPV6 SLL criterion.
466 */
467 @Test
468 public void matchIPv6NDSourceLinkLayerAddressTest() {
469 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
470 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800471 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800472 }
473
474 /**
475 * Tests IPV6 TLL criterion.
476 */
477 @Test
478 public void matchIPv6NDTargetLinkLayerAddressTest() {
479 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
480 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800481 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800482 }
483
484 /**
485 * Tests MPLS label criterion.
486 */
487 @Test
488 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100489 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800490 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800491 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800492 }
493
494 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800495 * Tests IPv6 Extension Header pseudo-field flags criterion.
496 */
497 @Test
498 public void matchIPv6ExthdrFlagsTest() {
499 int exthdrFlags =
500 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
501 Criterion.IPv6ExthdrFlags.ESP.getValue() |
502 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
503 Criterion.IPv6ExthdrFlags.DEST.getValue() |
504 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
505 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
506 Criterion.IPv6ExthdrFlags.HOP.getValue() |
507 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
508 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
509 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
510 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800511
512 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800513 }
514
515 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800516 * Tests lambda criterion.
517 */
518 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700519 public void matchOchSignal() {
520 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
521 Criterion criterion = Criteria.matchLambda(ochSignal);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800522 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800523 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800524 }
525
526 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700527 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800528 */
529 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700530 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700531 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800532 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800533 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800534 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300535
536 /**
537 * Tests Odu Signal ID criterion.
538 */
539 @Test
540 public void matchOduSignalIdTest() {
541
542 OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
543
544 Criterion criterion = Criteria.matchOduSignalId(oduSignalId);
545 ObjectNode result = criterionCodec.encode(criterion, context);
546 assertThat(result, matchesCriterion(criterion));
547 }
548
549 /**
550 * Tests Odu Signal Type criterion.
551 */
552 @Test
553 public void matchOduSignalTypeTest() {
554
555 OduSignalType signalType = OduSignalType.ODU2;
556
557 Criterion criterion = Criteria.matchOduSignalType(signalType);
558 ObjectNode result = criterionCodec.encode(criterion, context);
559 assertThat(result, matchesCriterion(criterion));
560 }
561
Frank Wang74ce2c12018-04-11 20:26:45 +0800562 /**
563 * Tests protocol-independent type criterion encoding.
564 */
565 @Test
566 public void matchPiTypeEncodingTest() {
567
568 PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType");
569 byte[] matchExactBytes1 = {0x08, 0x00};
570 Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build();
571 ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context);
572 assertThat(exactResult, matchesCriterion(exactBytesCriterion));
573
574 PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr");
575 int mask = 0x00ffffff;
576 byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
577 Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build();
578 ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context);
579 assertThat(lpmResult, matchesCriterion(lpmBytesCriterion));
580
581 byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
582 byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
583 Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1,
584 matchTernaryMaskBytes).build();
585 ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context);
586 assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion));
587
588 byte[] matchRangeBytes1 = {0x10};
589 byte[] matchRangeHighBytes = {0x30};
590 Criterion rangeBytesCriterion = PiCriterion.builder()
591 .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
592 ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context);
593 assertThat(rangeResult, matchesCriterion(rangeBytesCriterion));
Daniele Moroc6f2f7f2020-12-18 10:55:57 +0100594
595 byte[] matchOptionalBytes = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
596 Criterion optionalBytesCriterion = PiCriterion.builder().matchOptional(ethMatchFieldId,
597 matchOptionalBytes).build();
598 ObjectNode optionalResult = criterionCodec.encode(optionalBytesCriterion, context);
599 assertThat(optionalResult, matchesCriterion(optionalBytesCriterion));
Frank Wang74ce2c12018-04-11 20:26:45 +0800600 }
601
602 /**
603 * Tests protocol-independent type criterion decoding.
604 */
605 @Test
606 public void matchPiTypeDecodingTest() throws IOException {
607 Criterion criterion = getCriterion("PiCriterion.json");
608 Assert.assertThat(criterion.type(), is(Criterion.Type.PROTOCOL_INDEPENDENT));
609 Collection<PiFieldMatch> piFieldMatches = ((PiCriterion) criterion).fieldMatches();
610 for (PiFieldMatch piFieldMatch : piFieldMatches) {
611 switch (piFieldMatch.type()) {
612 case EXACT:
613 Assert.assertThat(piFieldMatch.fieldId().id(), is("ingress_port"));
614 Assert.assertThat(((PiExactFieldMatch) piFieldMatch).value(), is(
615 copyFrom((byte) 0x10)));
616 break;
617 case LPM:
618 Assert.assertThat(piFieldMatch.fieldId().id(), is("src_addr"));
619 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).value(),
620 is(copyFrom(0xa010101)));
621 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).prefixLength(), is(24));
622 break;
623 case TERNARY:
624 Assert.assertThat(piFieldMatch.fieldId().id(), is("dst_addr"));
625 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).value(),
626 is(copyFrom(0xa010101)));
627 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).mask(),
628 is(copyFrom(0xfffffff)));
629 break;
630 case RANGE:
631 Assert.assertThat(piFieldMatch.fieldId().id(), is("egress_port"));
632 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).highValue(),
633 is(copyFrom((byte) 0x20)));
634 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).lowValue(),
635 is(copyFrom((byte) 0x10)));
636 break;
Daniele Moroc6f2f7f2020-12-18 10:55:57 +0100637 case OPTIONAL:
638 Assert.assertThat(piFieldMatch.fieldId().id(), is("eth_dst"));
639 Assert.assertThat(((PiOptionalFieldMatch) piFieldMatch).value(),
640 is(copyFrom(new byte[]{0x00, 0x11, 0x22, 0x33, 0x44, 0x55})));
641 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800642 default:
Carmelo Cascone6af4e172018-06-15 16:01:30 +0200643 Assert.fail();
Frank Wang74ce2c12018-04-11 20:26:45 +0800644 }
645 }
646 }
647
648 /**
649 * Reads in a criterion from the given resource and decodes it.
650 *
651 * @param resourceName resource to use to read the JSON for the rule
652 * @return decoded criterion
653 * @throws IOException if processing the resource fails
654 */
655 private Criterion getCriterion(String resourceName) throws IOException {
656 InputStream jsonStream = CriterionCodecTest.class.getResourceAsStream(resourceName);
657 JsonNode json = context.mapper().readTree(jsonStream);
658 MatcherAssert.assertThat(json, notNullValue());
659 Criterion criterion = criterionCodec.decode((ObjectNode) json, context);
660 Assert.assertThat(criterion, notNullValue());
661 return criterion;
662 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800663}