blob: f2fd5e5afb7764e3ccdb18fc41f43b49d8ae6b16 [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);
Yafit Hadar5796d972015-10-15 13:16:11 +030077 final int tributaryPortNumber = 11;
78 final int tributarySlotLen = 80;
79 final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4};
80
Ray Milkey9ee62f52015-02-06 13:47:35 -080081
82 /**
83 * Sets up for each test. Creates a context and fetches the criterion
84 * codec.
85 */
86 @Before
87 public void setUp() {
88 context = new MockCodecContext();
89 criterionCodec = context.codec(Criterion.class);
90 assertThat(criterionCodec, notNullValue());
91 }
92
93
Ray Milkey73ba84a2015-02-04 17:08:41 -080094 /**
95 * Checks that all criterion types are covered by the codec.
96 */
97 @Test
98 public void checkCriterionTypes() throws Exception {
Ray Milkey6d7968e2015-07-06 14:30:02 -070099 EncodeCriterionCodecHelper encoder = new EncodeCriterionCodecHelper(
Ray Milkeyd43fe452015-05-29 09:35:12 -0700100 Criteria.dummy(), context);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800101 EnumMap<Criterion.Type, Object> formatMap =
Ray Milkeyd43fe452015-05-29 09:35:12 -0700102 getField(encoder, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -0800103 assertThat(formatMap, notNullValue());
104
105 for (Criterion.Type type : Criterion.Type.values()) {
106 assertThat("Entry not found for " + type.toString(),
107 formatMap.get(type), notNullValue());
108 }
109 }
Ray Milkey9ee62f52015-02-06 13:47:35 -0800110
111 /**
112 * Tests in port criterion.
113 */
114 @Test
115 public void matchInPortTest() {
116 Criterion criterion = Criteria.matchInPort(port);
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 in physical port criterion.
123 */
124 @Test
125 public void matchInPhyPortTest() {
126 Criterion criterion = Criteria.matchInPhyPort(port);
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 metadata criterion.
133 */
134 @Test
135 public void matchMetadataTest() {
136 Criterion criterion = Criteria.matchMetadata(0xabcdL);
137 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 ethernet destination criterion.
143 */
144 @Test
145 public void matchEthDstTest() {
146 Criterion criterion = Criteria.matchEthDst(mac1);
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 /**
Seyeon Jeong8f014142020-02-26 12:51:03 -0800152 * Tests masked ethernet destination criterion (Criterion.Type.ETH_DST_MASKED).
153 */
154 @Test
155 public void matchEthDstMaskTest() {
156 Criterion criterion = Criteria.matchEthDstMasked(mcastMac, mcastMacMask);
157 ObjectNode result = criterionCodec.encode(criterion, context);
158 assertThat(result, matchesCriterion(criterion));
159 }
160
161 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800162 * Tests ethernet source criterion.
163 */
164 @Test
165 public void matchEthSrcTest() {
166 Criterion criterion = Criteria.matchEthSrc(mac1);
167 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 ethernet type criterion.
173 */
174 @Test
175 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800176 Criterion criterion = Criteria.matchEthType((short) 0x8844);
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 VLAN Id criterion.
183 */
184 @Test
185 public void matchVlanIdTest() {
186 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
187 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 VLAN PCP criterion.
193 */
194 @Test
195 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800196 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800197 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 DSCP criterion.
203 */
204 @Test
205 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800206 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800207 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 IP ECN criterion.
213 */
214 @Test
215 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800216 Criterion criterion = Criteria.matchIPEcn((byte) 3);
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 IP protocol criterion.
223 */
224 @Test
225 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800226 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
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 IP source criterion.
233 */
234 @Test
235 public void matchIPSrcTest() {
236 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
237 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 IP destination criterion.
243 */
244 @Test
245 public void matchIPDstTest() {
246 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
247 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 TCP port criterion.
253 */
254 @Test
255 public void matchTcpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700256 Criterion criterion = Criteria.matchTcpSrc(tpPort);
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 TCP port criterion.
263 */
264 @Test
265 public void matchTcpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700266 Criterion criterion = Criteria.matchTcpDst(tpPort);
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 source UDP port criterion.
273 */
274 @Test
275 public void matchUdpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700276 Criterion criterion = Criteria.matchUdpSrc(tpPort);
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 destination UDP criterion.
283 */
284 @Test
285 public void matchUdpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700286 Criterion criterion = Criteria.matchUdpDst(tpPort);
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 source SCTP criterion.
293 */
294 @Test
295 public void matchSctpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700296 Criterion criterion = Criteria.matchSctpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800297 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 destination SCTP criterion.
303 */
304 @Test
305 public void matchSctpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700306 Criterion criterion = Criteria.matchSctpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800307 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 ICMP type criterion.
313 */
314 @Test
315 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800316 Criterion criterion = Criteria.matchIcmpType((byte) 250);
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 code criterion.
323 */
324 @Test
325 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800326 Criterion criterion = Criteria.matchIcmpCode((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 IPv6 source criterion.
333 */
334 @Test
335 public void matchIPv6SrcTest() {
336 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
337 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 destination criterion.
343 */
344 @Test
345 public void matchIPv6DstTest() {
346 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
347 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800348 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800349 }
350
351 /**
352 * Tests IPv6 flow label criterion.
353 */
354 @Test
355 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800356 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800357 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800358 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800359 }
360
361 /**
362 * Tests ICMP v6 type criterion.
363 */
364 @Test
365 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800366 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800367 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800368 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800369 }
370
371 /**
372 * Tests ICMP v6 code criterion.
373 */
374 @Test
375 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800376 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800377 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800378 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800379 }
380
381 /**
382 * Tests IPV6 target address criterion.
383 */
384 @Test
385 public void matchIPv6NDTargetAddressTest() {
386 Criterion criterion =
387 Criteria.matchIPv6NDTargetAddress(
388 Ip6Address.valueOf("1111:2222::"));
389 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800390 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800391 }
392
393 /**
394 * Tests IPV6 SLL criterion.
395 */
396 @Test
397 public void matchIPv6NDSourceLinkLayerAddressTest() {
398 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
399 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800400 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800401 }
402
403 /**
404 * Tests IPV6 TLL criterion.
405 */
406 @Test
407 public void matchIPv6NDTargetLinkLayerAddressTest() {
408 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
409 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 MPLS label criterion.
415 */
416 @Test
417 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100418 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
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
423 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800424 * Tests IPv6 Extension Header pseudo-field flags criterion.
425 */
426 @Test
427 public void matchIPv6ExthdrFlagsTest() {
428 int exthdrFlags =
429 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
430 Criterion.IPv6ExthdrFlags.ESP.getValue() |
431 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
432 Criterion.IPv6ExthdrFlags.DEST.getValue() |
433 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
434 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
435 Criterion.IPv6ExthdrFlags.HOP.getValue() |
436 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
437 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
438 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
439 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800440
441 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800442 }
443
444 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800445 * Tests lambda criterion.
446 */
447 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700448 public void matchOchSignal() {
449 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
450 Criterion criterion = Criteria.matchLambda(ochSignal);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800451 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800452 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800453 }
454
455 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700456 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800457 */
458 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700459 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700460 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800461 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800462 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800463 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300464
465 /**
466 * Tests Odu Signal ID criterion.
467 */
468 @Test
469 public void matchOduSignalIdTest() {
470
471 OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
472
473 Criterion criterion = Criteria.matchOduSignalId(oduSignalId);
474 ObjectNode result = criterionCodec.encode(criterion, context);
475 assertThat(result, matchesCriterion(criterion));
476 }
477
478 /**
479 * Tests Odu Signal Type criterion.
480 */
481 @Test
482 public void matchOduSignalTypeTest() {
483
484 OduSignalType signalType = OduSignalType.ODU2;
485
486 Criterion criterion = Criteria.matchOduSignalType(signalType);
487 ObjectNode result = criterionCodec.encode(criterion, context);
488 assertThat(result, matchesCriterion(criterion));
489 }
490
Frank Wang74ce2c12018-04-11 20:26:45 +0800491 /**
492 * Tests protocol-independent type criterion encoding.
493 */
494 @Test
495 public void matchPiTypeEncodingTest() {
496
497 PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType");
498 byte[] matchExactBytes1 = {0x08, 0x00};
499 Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build();
500 ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context);
501 assertThat(exactResult, matchesCriterion(exactBytesCriterion));
502
503 PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr");
504 int mask = 0x00ffffff;
505 byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
506 Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build();
507 ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context);
508 assertThat(lpmResult, matchesCriterion(lpmBytesCriterion));
509
510 byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
511 byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
512 Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1,
513 matchTernaryMaskBytes).build();
514 ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context);
515 assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion));
516
517 byte[] matchRangeBytes1 = {0x10};
518 byte[] matchRangeHighBytes = {0x30};
519 Criterion rangeBytesCriterion = PiCriterion.builder()
520 .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
521 ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context);
522 assertThat(rangeResult, matchesCriterion(rangeBytesCriterion));
Daniele Moroc6f2f7f2020-12-18 10:55:57 +0100523
524 byte[] matchOptionalBytes = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
525 Criterion optionalBytesCriterion = PiCriterion.builder().matchOptional(ethMatchFieldId,
526 matchOptionalBytes).build();
527 ObjectNode optionalResult = criterionCodec.encode(optionalBytesCriterion, context);
528 assertThat(optionalResult, matchesCriterion(optionalBytesCriterion));
Frank Wang74ce2c12018-04-11 20:26:45 +0800529 }
530
531 /**
532 * Tests protocol-independent type criterion decoding.
533 */
534 @Test
535 public void matchPiTypeDecodingTest() throws IOException {
536 Criterion criterion = getCriterion("PiCriterion.json");
537 Assert.assertThat(criterion.type(), is(Criterion.Type.PROTOCOL_INDEPENDENT));
538 Collection<PiFieldMatch> piFieldMatches = ((PiCriterion) criterion).fieldMatches();
539 for (PiFieldMatch piFieldMatch : piFieldMatches) {
540 switch (piFieldMatch.type()) {
541 case EXACT:
542 Assert.assertThat(piFieldMatch.fieldId().id(), is("ingress_port"));
543 Assert.assertThat(((PiExactFieldMatch) piFieldMatch).value(), is(
544 copyFrom((byte) 0x10)));
545 break;
546 case LPM:
547 Assert.assertThat(piFieldMatch.fieldId().id(), is("src_addr"));
548 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).value(),
549 is(copyFrom(0xa010101)));
550 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).prefixLength(), is(24));
551 break;
552 case TERNARY:
553 Assert.assertThat(piFieldMatch.fieldId().id(), is("dst_addr"));
554 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).value(),
555 is(copyFrom(0xa010101)));
556 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).mask(),
557 is(copyFrom(0xfffffff)));
558 break;
559 case RANGE:
560 Assert.assertThat(piFieldMatch.fieldId().id(), is("egress_port"));
561 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).highValue(),
562 is(copyFrom((byte) 0x20)));
563 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).lowValue(),
564 is(copyFrom((byte) 0x10)));
565 break;
Daniele Moroc6f2f7f2020-12-18 10:55:57 +0100566 case OPTIONAL:
567 Assert.assertThat(piFieldMatch.fieldId().id(), is("eth_dst"));
568 Assert.assertThat(((PiOptionalFieldMatch) piFieldMatch).value(),
569 is(copyFrom(new byte[]{0x00, 0x11, 0x22, 0x33, 0x44, 0x55})));
570 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800571 default:
Carmelo Cascone6af4e172018-06-15 16:01:30 +0200572 Assert.fail();
Frank Wang74ce2c12018-04-11 20:26:45 +0800573 }
574 }
575 }
576
577 /**
578 * Reads in a criterion from the given resource and decodes it.
579 *
580 * @param resourceName resource to use to read the JSON for the rule
581 * @return decoded criterion
582 * @throws IOException if processing the resource fails
583 */
584 private Criterion getCriterion(String resourceName) throws IOException {
585 InputStream jsonStream = CriterionCodecTest.class.getResourceAsStream(resourceName);
586 JsonNode json = context.mapper().readTree(jsonStream);
587 MatcherAssert.assertThat(json, notNullValue());
588 Criterion criterion = criterionCodec.decode((ObjectNode) json, context);
589 Assert.assertThat(criterion, notNullValue());
590 return criterion;
591 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800592}