blob: c748ab6416366bbedfc6ee0282aaa2c1f2753998 [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;
54import org.onosproject.net.pi.runtime.PiValidFieldMatch;
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");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070074 final TpPort tpPort = TpPort.tpPort(40000);
Yafit Hadar5796d972015-10-15 13:16:11 +030075 final int tributaryPortNumber = 11;
76 final int tributarySlotLen = 80;
77 final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4};
78
Ray Milkey9ee62f52015-02-06 13:47:35 -080079
80 /**
81 * Sets up for each test. Creates a context and fetches the criterion
82 * codec.
83 */
84 @Before
85 public void setUp() {
86 context = new MockCodecContext();
87 criterionCodec = context.codec(Criterion.class);
88 assertThat(criterionCodec, notNullValue());
89 }
90
91
Ray Milkey73ba84a2015-02-04 17:08:41 -080092 /**
93 * Checks that all criterion types are covered by the codec.
94 */
95 @Test
96 public void checkCriterionTypes() throws Exception {
Ray Milkey6d7968e2015-07-06 14:30:02 -070097 EncodeCriterionCodecHelper encoder = new EncodeCriterionCodecHelper(
Ray Milkeyd43fe452015-05-29 09:35:12 -070098 Criteria.dummy(), context);
Ray Milkey9ee62f52015-02-06 13:47:35 -080099 EnumMap<Criterion.Type, Object> formatMap =
Ray Milkeyd43fe452015-05-29 09:35:12 -0700100 getField(encoder, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -0800101 assertThat(formatMap, notNullValue());
102
103 for (Criterion.Type type : Criterion.Type.values()) {
104 assertThat("Entry not found for " + type.toString(),
105 formatMap.get(type), notNullValue());
106 }
107 }
Ray Milkey9ee62f52015-02-06 13:47:35 -0800108
109 /**
110 * Tests in port criterion.
111 */
112 @Test
113 public void matchInPortTest() {
114 Criterion criterion = Criteria.matchInPort(port);
115 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800116 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800117 }
118
119 /**
120 * Tests in physical port criterion.
121 */
122 @Test
123 public void matchInPhyPortTest() {
124 Criterion criterion = Criteria.matchInPhyPort(port);
125 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800126 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800127 }
128
129 /**
130 * Tests metadata criterion.
131 */
132 @Test
133 public void matchMetadataTest() {
134 Criterion criterion = Criteria.matchMetadata(0xabcdL);
135 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800136 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800137 }
138
139 /**
140 * Tests ethernet destination criterion.
141 */
142 @Test
143 public void matchEthDstTest() {
144 Criterion criterion = Criteria.matchEthDst(mac1);
145 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800146 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800147 }
148
149 /**
150 * Tests ethernet source criterion.
151 */
152 @Test
153 public void matchEthSrcTest() {
154 Criterion criterion = Criteria.matchEthSrc(mac1);
155 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800156 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800157 }
158
159 /**
160 * Tests ethernet type criterion.
161 */
162 @Test
163 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800164 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800165 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800166 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800167 }
168
169 /**
170 * Tests VLAN Id criterion.
171 */
172 @Test
173 public void matchVlanIdTest() {
174 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
175 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800176 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800177 }
178
179 /**
180 * Tests VLAN PCP criterion.
181 */
182 @Test
183 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800184 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800185 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800186 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800187 }
188
189 /**
190 * Tests IP DSCP criterion.
191 */
192 @Test
193 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800194 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800195 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800196 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800197 }
198
199 /**
200 * Tests IP ECN criterion.
201 */
202 @Test
203 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800204 Criterion criterion = Criteria.matchIPEcn((byte) 3);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800205 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800206 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800207 }
208
209 /**
210 * Tests IP protocol criterion.
211 */
212 @Test
213 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800214 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800215 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800216 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800217 }
218
219 /**
220 * Tests IP source criterion.
221 */
222 @Test
223 public void matchIPSrcTest() {
224 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
225 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800226 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800227 }
228
229 /**
230 * Tests IP destination criterion.
231 */
232 @Test
233 public void matchIPDstTest() {
234 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
235 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800236 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800237 }
238
239 /**
240 * Tests source TCP port criterion.
241 */
242 @Test
243 public void matchTcpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700244 Criterion criterion = Criteria.matchTcpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800245 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800246 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800247 }
248
249 /**
250 * Tests destination TCP port criterion.
251 */
252 @Test
253 public void matchTcpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700254 Criterion criterion = Criteria.matchTcpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800255 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800256 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800257 }
258
259 /**
260 * Tests source UDP port criterion.
261 */
262 @Test
263 public void matchUdpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700264 Criterion criterion = Criteria.matchUdpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800265 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800266 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800267 }
268
269 /**
270 * Tests destination UDP criterion.
271 */
272 @Test
273 public void matchUdpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700274 Criterion criterion = Criteria.matchUdpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800275 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800276 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800277 }
278
279 /**
280 * Tests source SCTP criterion.
281 */
282 @Test
283 public void matchSctpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700284 Criterion criterion = Criteria.matchSctpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800285 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800286 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800287 }
288
289 /**
290 * Tests destination SCTP criterion.
291 */
292 @Test
293 public void matchSctpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700294 Criterion criterion = Criteria.matchSctpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800295 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800296 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800297 }
298
299 /**
300 * Tests ICMP type criterion.
301 */
302 @Test
303 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800304 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800305 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800306 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800307 }
308
309 /**
310 * Tests ICMP code criterion.
311 */
312 @Test
313 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800314 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800315 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800316 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800317 }
318
319 /**
320 * Tests IPv6 source criterion.
321 */
322 @Test
323 public void matchIPv6SrcTest() {
324 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
325 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800326 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800327 }
328
329 /**
330 * Tests IPv6 destination criterion.
331 */
332 @Test
333 public void matchIPv6DstTest() {
334 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
335 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800336 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800337 }
338
339 /**
340 * Tests IPv6 flow label criterion.
341 */
342 @Test
343 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800344 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800345 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800346 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800347 }
348
349 /**
350 * Tests ICMP v6 type criterion.
351 */
352 @Test
353 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800354 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800355 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800356 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800357 }
358
359 /**
360 * Tests ICMP v6 code criterion.
361 */
362 @Test
363 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800364 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800365 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800366 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800367 }
368
369 /**
370 * Tests IPV6 target address criterion.
371 */
372 @Test
373 public void matchIPv6NDTargetAddressTest() {
374 Criterion criterion =
375 Criteria.matchIPv6NDTargetAddress(
376 Ip6Address.valueOf("1111:2222::"));
377 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 SLL criterion.
383 */
384 @Test
385 public void matchIPv6NDSourceLinkLayerAddressTest() {
386 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
387 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800388 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800389 }
390
391 /**
392 * Tests IPV6 TLL criterion.
393 */
394 @Test
395 public void matchIPv6NDTargetLinkLayerAddressTest() {
396 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
397 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800398 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800399 }
400
401 /**
402 * Tests MPLS label criterion.
403 */
404 @Test
405 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100406 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800407 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800408 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800409 }
410
411 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800412 * Tests IPv6 Extension Header pseudo-field flags criterion.
413 */
414 @Test
415 public void matchIPv6ExthdrFlagsTest() {
416 int exthdrFlags =
417 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
418 Criterion.IPv6ExthdrFlags.ESP.getValue() |
419 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
420 Criterion.IPv6ExthdrFlags.DEST.getValue() |
421 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
422 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
423 Criterion.IPv6ExthdrFlags.HOP.getValue() |
424 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
425 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
426 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
427 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800428
429 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800430 }
431
432 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800433 * Tests lambda criterion.
434 */
435 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700436 public void matchOchSignal() {
437 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
438 Criterion criterion = Criteria.matchLambda(ochSignal);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800439 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800440 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800441 }
442
443 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700444 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800445 */
446 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700447 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700448 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800449 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800450 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800451 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300452
453 /**
454 * Tests Odu Signal ID criterion.
455 */
456 @Test
457 public void matchOduSignalIdTest() {
458
459 OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
460
461 Criterion criterion = Criteria.matchOduSignalId(oduSignalId);
462 ObjectNode result = criterionCodec.encode(criterion, context);
463 assertThat(result, matchesCriterion(criterion));
464 }
465
466 /**
467 * Tests Odu Signal Type criterion.
468 */
469 @Test
470 public void matchOduSignalTypeTest() {
471
472 OduSignalType signalType = OduSignalType.ODU2;
473
474 Criterion criterion = Criteria.matchOduSignalType(signalType);
475 ObjectNode result = criterionCodec.encode(criterion, context);
476 assertThat(result, matchesCriterion(criterion));
477 }
478
Frank Wang74ce2c12018-04-11 20:26:45 +0800479 /**
480 * Tests protocol-independent type criterion encoding.
481 */
482 @Test
483 public void matchPiTypeEncodingTest() {
484
485 PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType");
486 byte[] matchExactBytes1 = {0x08, 0x00};
487 Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build();
488 ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context);
489 assertThat(exactResult, matchesCriterion(exactBytesCriterion));
490
491 PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr");
492 int mask = 0x00ffffff;
493 byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
494 Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build();
495 ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context);
496 assertThat(lpmResult, matchesCriterion(lpmBytesCriterion));
497
498 byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
499 byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
500 Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1,
501 matchTernaryMaskBytes).build();
502 ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context);
503 assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion));
504
505 byte[] matchRangeBytes1 = {0x10};
506 byte[] matchRangeHighBytes = {0x30};
507 Criterion rangeBytesCriterion = PiCriterion.builder()
508 .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
509 ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context);
510 assertThat(rangeResult, matchesCriterion(rangeBytesCriterion));
511
512 Criterion validCriterion = PiCriterion.builder().matchValid(ipv4MatchFieldId, false).build();
513 ObjectNode validResult = criterionCodec.encode(validCriterion, context);
514 assertThat(validResult, matchesCriterion(validCriterion));
515 }
516
517 /**
518 * Tests protocol-independent type criterion decoding.
519 */
520 @Test
521 public void matchPiTypeDecodingTest() throws IOException {
522 Criterion criterion = getCriterion("PiCriterion.json");
523 Assert.assertThat(criterion.type(), is(Criterion.Type.PROTOCOL_INDEPENDENT));
524 Collection<PiFieldMatch> piFieldMatches = ((PiCriterion) criterion).fieldMatches();
525 for (PiFieldMatch piFieldMatch : piFieldMatches) {
526 switch (piFieldMatch.type()) {
527 case EXACT:
528 Assert.assertThat(piFieldMatch.fieldId().id(), is("ingress_port"));
529 Assert.assertThat(((PiExactFieldMatch) piFieldMatch).value(), is(
530 copyFrom((byte) 0x10)));
531 break;
532 case LPM:
533 Assert.assertThat(piFieldMatch.fieldId().id(), is("src_addr"));
534 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).value(),
535 is(copyFrom(0xa010101)));
536 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).prefixLength(), is(24));
537 break;
538 case TERNARY:
539 Assert.assertThat(piFieldMatch.fieldId().id(), is("dst_addr"));
540 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).value(),
541 is(copyFrom(0xa010101)));
542 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).mask(),
543 is(copyFrom(0xfffffff)));
544 break;
545 case RANGE:
546 Assert.assertThat(piFieldMatch.fieldId().id(), is("egress_port"));
547 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).highValue(),
548 is(copyFrom((byte) 0x20)));
549 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).lowValue(),
550 is(copyFrom((byte) 0x10)));
551 break;
552 case VALID:
553 Assert.assertThat(piFieldMatch.fieldId().id(), is("ethernet_t.etherType"));
554 Assert.assertThat(((PiValidFieldMatch) piFieldMatch).isValid(), is(true));
555 break;
556 default:
557 Assert.assertTrue(false);
558 }
559 }
560 }
561
562 /**
563 * Reads in a criterion from the given resource and decodes it.
564 *
565 * @param resourceName resource to use to read the JSON for the rule
566 * @return decoded criterion
567 * @throws IOException if processing the resource fails
568 */
569 private Criterion getCriterion(String resourceName) throws IOException {
570 InputStream jsonStream = CriterionCodecTest.class.getResourceAsStream(resourceName);
571 JsonNode json = context.mapper().readTree(jsonStream);
572 MatcherAssert.assertThat(json, notNullValue());
573 Criterion criterion = criterionCodec.decode((ObjectNode) json, context);
574 Assert.assertThat(criterion, notNullValue());
575 return criterion;
576 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800577}