blob: 2628827d18743a9e65b9edea7efabe44d07b4467 [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;
52import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
53import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Ray Milkey9ee62f52015-02-06 13:47:35 -080054
Ray Milkey73ba84a2015-02-04 17:08:41 -080055import static org.hamcrest.MatcherAssert.assertThat;
Frank Wang74ce2c12018-04-11 20:26:45 +080056import static org.hamcrest.Matchers.is;
Ray Milkey0cc189e2015-02-09 11:08:01 -080057import static org.hamcrest.Matchers.notNullValue;
58import static org.onlab.junit.TestUtils.getField;
Frank Wang74ce2c12018-04-11 20:26:45 +080059import static org.onlab.util.ImmutableByteSequence.copyFrom;
Ray Milkey46670a82015-02-08 17:57:17 -080060import static org.onosproject.codec.impl.CriterionJsonMatcher.matchesCriterion;
Ray Milkey73ba84a2015-02-04 17:08:41 -080061
62/**
63 * Unit tests for criterion codec.
64 */
65public class CriterionCodecTest {
66
Ray Milkey9ee62f52015-02-06 13:47:35 -080067 CodecContext context;
68 JsonCodec<Criterion> criterionCodec;
69 final PortNumber port = PortNumber.portNumber(1);
70 final IpPrefix ipPrefix4 = IpPrefix.valueOf("10.1.1.0/24");
71 final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
72 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
Seyeon Jeong37a851c2020-02-26 12:51:03 -080073 final MacAddress mcastMac = MacAddress.valueOf("01:00:5E:00:00:01");
74 final MacAddress mcastMacMask = MacAddress.valueOf("FF:FF:FF:80:00:00");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070075 final TpPort tpPort = TpPort.tpPort(40000);
Yafit Hadar5796d972015-10-15 13:16:11 +030076 final int tributaryPortNumber = 11;
77 final int tributarySlotLen = 80;
78 final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4};
79
Ray Milkey9ee62f52015-02-06 13:47:35 -080080
81 /**
82 * Sets up for each test. Creates a context and fetches the criterion
83 * codec.
84 */
85 @Before
86 public void setUp() {
87 context = new MockCodecContext();
88 criterionCodec = context.codec(Criterion.class);
89 assertThat(criterionCodec, notNullValue());
90 }
91
92
Ray Milkey73ba84a2015-02-04 17:08:41 -080093 /**
94 * Checks that all criterion types are covered by the codec.
95 */
96 @Test
97 public void checkCriterionTypes() throws Exception {
Ray Milkey6d7968e2015-07-06 14:30:02 -070098 EncodeCriterionCodecHelper encoder = new EncodeCriterionCodecHelper(
Ray Milkeyd43fe452015-05-29 09:35:12 -070099 Criteria.dummy(), context);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800100 EnumMap<Criterion.Type, Object> formatMap =
Ray Milkeyd43fe452015-05-29 09:35:12 -0700101 getField(encoder, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -0800102 assertThat(formatMap, notNullValue());
103
104 for (Criterion.Type type : Criterion.Type.values()) {
105 assertThat("Entry not found for " + type.toString(),
106 formatMap.get(type), notNullValue());
107 }
108 }
Ray Milkey9ee62f52015-02-06 13:47:35 -0800109
110 /**
111 * Tests in port criterion.
112 */
113 @Test
114 public void matchInPortTest() {
115 Criterion criterion = Criteria.matchInPort(port);
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 in physical port criterion.
122 */
123 @Test
124 public void matchInPhyPortTest() {
125 Criterion criterion = Criteria.matchInPhyPort(port);
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 metadata criterion.
132 */
133 @Test
134 public void matchMetadataTest() {
135 Criterion criterion = Criteria.matchMetadata(0xabcdL);
136 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 ethernet destination criterion.
142 */
143 @Test
144 public void matchEthDstTest() {
145 Criterion criterion = Criteria.matchEthDst(mac1);
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 /**
Seyeon Jeong37a851c2020-02-26 12:51:03 -0800151 * Tests masked ethernet destination criterion (Criterion.Type.ETH_DST_MASKED).
152 */
153 @Test
154 public void matchEthDstMaskTest() {
155 Criterion criterion = Criteria.matchEthDstMasked(mcastMac, mcastMacMask);
156 ObjectNode result = criterionCodec.encode(criterion, context);
157 assertThat(result, matchesCriterion(criterion));
158 }
159
160 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800161 * Tests ethernet source criterion.
162 */
163 @Test
164 public void matchEthSrcTest() {
165 Criterion criterion = Criteria.matchEthSrc(mac1);
166 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 ethernet type criterion.
172 */
173 @Test
174 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800175 Criterion criterion = Criteria.matchEthType((short) 0x8844);
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 VLAN Id criterion.
182 */
183 @Test
184 public void matchVlanIdTest() {
185 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
186 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 VLAN PCP criterion.
192 */
193 @Test
194 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800195 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800196 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 DSCP criterion.
202 */
203 @Test
204 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800205 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800206 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 IP ECN criterion.
212 */
213 @Test
214 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800215 Criterion criterion = Criteria.matchIPEcn((byte) 3);
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 IP protocol criterion.
222 */
223 @Test
224 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800225 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
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 IP source criterion.
232 */
233 @Test
234 public void matchIPSrcTest() {
235 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
236 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 IP destination criterion.
242 */
243 @Test
244 public void matchIPDstTest() {
245 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
246 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 TCP port criterion.
252 */
253 @Test
254 public void matchTcpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700255 Criterion criterion = Criteria.matchTcpSrc(tpPort);
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 TCP port criterion.
262 */
263 @Test
264 public void matchTcpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700265 Criterion criterion = Criteria.matchTcpDst(tpPort);
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 source UDP port criterion.
272 */
273 @Test
274 public void matchUdpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700275 Criterion criterion = Criteria.matchUdpSrc(tpPort);
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 destination UDP criterion.
282 */
283 @Test
284 public void matchUdpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700285 Criterion criterion = Criteria.matchUdpDst(tpPort);
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 source SCTP criterion.
292 */
293 @Test
294 public void matchSctpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700295 Criterion criterion = Criteria.matchSctpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800296 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 destination SCTP criterion.
302 */
303 @Test
304 public void matchSctpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700305 Criterion criterion = Criteria.matchSctpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800306 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 ICMP type criterion.
312 */
313 @Test
314 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800315 Criterion criterion = Criteria.matchIcmpType((byte) 250);
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 code criterion.
322 */
323 @Test
324 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800325 Criterion criterion = Criteria.matchIcmpCode((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 IPv6 source criterion.
332 */
333 @Test
334 public void matchIPv6SrcTest() {
335 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
336 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 destination criterion.
342 */
343 @Test
344 public void matchIPv6DstTest() {
345 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
346 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800347 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800348 }
349
350 /**
351 * Tests IPv6 flow label criterion.
352 */
353 @Test
354 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800355 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800356 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800357 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800358 }
359
360 /**
361 * Tests ICMP v6 type criterion.
362 */
363 @Test
364 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800365 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800366 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800367 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800368 }
369
370 /**
371 * Tests ICMP v6 code criterion.
372 */
373 @Test
374 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800375 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800376 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800377 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800378 }
379
380 /**
381 * Tests IPV6 target address criterion.
382 */
383 @Test
384 public void matchIPv6NDTargetAddressTest() {
385 Criterion criterion =
386 Criteria.matchIPv6NDTargetAddress(
387 Ip6Address.valueOf("1111:2222::"));
388 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 IPV6 SLL criterion.
394 */
395 @Test
396 public void matchIPv6NDSourceLinkLayerAddressTest() {
397 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
398 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 TLL criterion.
404 */
405 @Test
406 public void matchIPv6NDTargetLinkLayerAddressTest() {
407 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
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 MPLS label criterion.
414 */
415 @Test
416 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100417 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800418 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 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800423 * Tests IPv6 Extension Header pseudo-field flags criterion.
424 */
425 @Test
426 public void matchIPv6ExthdrFlagsTest() {
427 int exthdrFlags =
428 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
429 Criterion.IPv6ExthdrFlags.ESP.getValue() |
430 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
431 Criterion.IPv6ExthdrFlags.DEST.getValue() |
432 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
433 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
434 Criterion.IPv6ExthdrFlags.HOP.getValue() |
435 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
436 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
437 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
438 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800439
440 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800441 }
442
443 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800444 * Tests lambda criterion.
445 */
446 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700447 public void matchOchSignal() {
448 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
449 Criterion criterion = Criteria.matchLambda(ochSignal);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800450 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800451 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800452 }
453
454 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700455 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800456 */
457 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700458 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700459 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800460 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 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300463
464 /**
465 * Tests Odu Signal ID criterion.
466 */
467 @Test
468 public void matchOduSignalIdTest() {
469
470 OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
471
472 Criterion criterion = Criteria.matchOduSignalId(oduSignalId);
473 ObjectNode result = criterionCodec.encode(criterion, context);
474 assertThat(result, matchesCriterion(criterion));
475 }
476
477 /**
478 * Tests Odu Signal Type criterion.
479 */
480 @Test
481 public void matchOduSignalTypeTest() {
482
483 OduSignalType signalType = OduSignalType.ODU2;
484
485 Criterion criterion = Criteria.matchOduSignalType(signalType);
486 ObjectNode result = criterionCodec.encode(criterion, context);
487 assertThat(result, matchesCriterion(criterion));
488 }
489
Frank Wang74ce2c12018-04-11 20:26:45 +0800490 /**
491 * Tests protocol-independent type criterion encoding.
492 */
493 @Test
494 public void matchPiTypeEncodingTest() {
495
496 PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType");
497 byte[] matchExactBytes1 = {0x08, 0x00};
498 Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build();
499 ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context);
500 assertThat(exactResult, matchesCriterion(exactBytesCriterion));
501
502 PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr");
503 int mask = 0x00ffffff;
504 byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
505 Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build();
506 ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context);
507 assertThat(lpmResult, matchesCriterion(lpmBytesCriterion));
508
509 byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
510 byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
511 Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1,
512 matchTernaryMaskBytes).build();
513 ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context);
514 assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion));
515
516 byte[] matchRangeBytes1 = {0x10};
517 byte[] matchRangeHighBytes = {0x30};
518 Criterion rangeBytesCriterion = PiCriterion.builder()
519 .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
520 ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context);
521 assertThat(rangeResult, matchesCriterion(rangeBytesCriterion));
Frank Wang74ce2c12018-04-11 20:26:45 +0800522 }
523
524 /**
525 * Tests protocol-independent type criterion decoding.
526 */
527 @Test
528 public void matchPiTypeDecodingTest() throws IOException {
529 Criterion criterion = getCriterion("PiCriterion.json");
530 Assert.assertThat(criterion.type(), is(Criterion.Type.PROTOCOL_INDEPENDENT));
531 Collection<PiFieldMatch> piFieldMatches = ((PiCriterion) criterion).fieldMatches();
532 for (PiFieldMatch piFieldMatch : piFieldMatches) {
533 switch (piFieldMatch.type()) {
534 case EXACT:
535 Assert.assertThat(piFieldMatch.fieldId().id(), is("ingress_port"));
536 Assert.assertThat(((PiExactFieldMatch) piFieldMatch).value(), is(
537 copyFrom((byte) 0x10)));
538 break;
539 case LPM:
540 Assert.assertThat(piFieldMatch.fieldId().id(), is("src_addr"));
541 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).value(),
542 is(copyFrom(0xa010101)));
543 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).prefixLength(), is(24));
544 break;
545 case TERNARY:
546 Assert.assertThat(piFieldMatch.fieldId().id(), is("dst_addr"));
547 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).value(),
548 is(copyFrom(0xa010101)));
549 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).mask(),
550 is(copyFrom(0xfffffff)));
551 break;
552 case RANGE:
553 Assert.assertThat(piFieldMatch.fieldId().id(), is("egress_port"));
554 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).highValue(),
555 is(copyFrom((byte) 0x20)));
556 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).lowValue(),
557 is(copyFrom((byte) 0x10)));
558 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800559 default:
Carmelo Cascone6af4e172018-06-15 16:01:30 +0200560 Assert.fail();
Frank Wang74ce2c12018-04-11 20:26:45 +0800561 }
562 }
563 }
564
565 /**
566 * Reads in a criterion from the given resource and decodes it.
567 *
568 * @param resourceName resource to use to read the JSON for the rule
569 * @return decoded criterion
570 * @throws IOException if processing the resource fails
571 */
572 private Criterion getCriterion(String resourceName) throws IOException {
573 InputStream jsonStream = CriterionCodecTest.class.getResourceAsStream(resourceName);
574 JsonNode json = context.mapper().readTree(jsonStream);
575 MatcherAssert.assertThat(json, notNullValue());
576 Criterion criterion = criterionCodec.decode((ObjectNode) json, context);
577 Assert.assertThat(criterion, notNullValue());
578 return criterion;
579 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800580}