blob: 2a615c7e84f524ce7b0a0505d743adf592af2144 [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");
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070073 final TpPort tpPort = TpPort.tpPort(40000);
Yafit Hadar5796d972015-10-15 13:16:11 +030074 final int tributaryPortNumber = 11;
75 final int tributarySlotLen = 80;
76 final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4};
77
Ray Milkey9ee62f52015-02-06 13:47:35 -080078
79 /**
80 * Sets up for each test. Creates a context and fetches the criterion
81 * codec.
82 */
83 @Before
84 public void setUp() {
85 context = new MockCodecContext();
86 criterionCodec = context.codec(Criterion.class);
87 assertThat(criterionCodec, notNullValue());
88 }
89
90
Ray Milkey73ba84a2015-02-04 17:08:41 -080091 /**
92 * Checks that all criterion types are covered by the codec.
93 */
94 @Test
95 public void checkCriterionTypes() throws Exception {
Ray Milkey6d7968e2015-07-06 14:30:02 -070096 EncodeCriterionCodecHelper encoder = new EncodeCriterionCodecHelper(
Ray Milkeyd43fe452015-05-29 09:35:12 -070097 Criteria.dummy(), context);
Ray Milkey9ee62f52015-02-06 13:47:35 -080098 EnumMap<Criterion.Type, Object> formatMap =
Ray Milkeyd43fe452015-05-29 09:35:12 -070099 getField(encoder, "formatMap");
Ray Milkey73ba84a2015-02-04 17:08:41 -0800100 assertThat(formatMap, notNullValue());
101
102 for (Criterion.Type type : Criterion.Type.values()) {
103 assertThat("Entry not found for " + type.toString(),
104 formatMap.get(type), notNullValue());
105 }
106 }
Ray Milkey9ee62f52015-02-06 13:47:35 -0800107
108 /**
109 * Tests in port criterion.
110 */
111 @Test
112 public void matchInPortTest() {
113 Criterion criterion = Criteria.matchInPort(port);
114 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800115 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800116 }
117
118 /**
119 * Tests in physical port criterion.
120 */
121 @Test
122 public void matchInPhyPortTest() {
123 Criterion criterion = Criteria.matchInPhyPort(port);
124 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800125 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800126 }
127
128 /**
129 * Tests metadata criterion.
130 */
131 @Test
132 public void matchMetadataTest() {
133 Criterion criterion = Criteria.matchMetadata(0xabcdL);
134 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800135 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800136 }
137
138 /**
139 * Tests ethernet destination criterion.
140 */
141 @Test
142 public void matchEthDstTest() {
143 Criterion criterion = Criteria.matchEthDst(mac1);
144 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800145 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800146 }
147
148 /**
149 * Tests ethernet source criterion.
150 */
151 @Test
152 public void matchEthSrcTest() {
153 Criterion criterion = Criteria.matchEthSrc(mac1);
154 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800155 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800156 }
157
158 /**
159 * Tests ethernet type criterion.
160 */
161 @Test
162 public void matchEthTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800163 Criterion criterion = Criteria.matchEthType((short) 0x8844);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800164 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800165 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800166 }
167
168 /**
169 * Tests VLAN Id criterion.
170 */
171 @Test
172 public void matchVlanIdTest() {
173 Criterion criterion = Criteria.matchVlanId(VlanId.ANY);
174 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800175 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800176 }
177
178 /**
179 * Tests VLAN PCP criterion.
180 */
181 @Test
182 public void matchVlanPcpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800183 Criterion criterion = Criteria.matchVlanPcp((byte) 7);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800184 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800185 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800186 }
187
188 /**
189 * Tests IP DSCP criterion.
190 */
191 @Test
192 public void matchIPDscpTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800193 Criterion criterion = Criteria.matchIPDscp((byte) 63);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800194 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800195 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800196 }
197
198 /**
199 * Tests IP ECN criterion.
200 */
201 @Test
202 public void matchIPEcnTest() {
Pavlin Radoslavovab8553a2015-02-20 14:13:50 -0800203 Criterion criterion = Criteria.matchIPEcn((byte) 3);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800204 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800205 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800206 }
207
208 /**
209 * Tests IP protocol criterion.
210 */
211 @Test
212 public void matchIPProtocolTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800213 Criterion criterion = Criteria.matchIPProtocol((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800214 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800215 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800216 }
217
218 /**
219 * Tests IP source criterion.
220 */
221 @Test
222 public void matchIPSrcTest() {
223 Criterion criterion = Criteria.matchIPSrc(ipPrefix4);
224 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800225 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800226 }
227
228 /**
229 * Tests IP destination criterion.
230 */
231 @Test
232 public void matchIPDstTest() {
233 Criterion criterion = Criteria.matchIPDst(ipPrefix4);
234 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800235 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800236 }
237
238 /**
239 * Tests source TCP port criterion.
240 */
241 @Test
242 public void matchTcpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700243 Criterion criterion = Criteria.matchTcpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800244 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800245 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800246 }
247
248 /**
249 * Tests destination TCP port criterion.
250 */
251 @Test
252 public void matchTcpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700253 Criterion criterion = Criteria.matchTcpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800254 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800255 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800256 }
257
258 /**
259 * Tests source UDP port criterion.
260 */
261 @Test
262 public void matchUdpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700263 Criterion criterion = Criteria.matchUdpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800264 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800265 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800266 }
267
268 /**
269 * Tests destination UDP criterion.
270 */
271 @Test
272 public void matchUdpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700273 Criterion criterion = Criteria.matchUdpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800274 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800275 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800276 }
277
278 /**
279 * Tests source SCTP criterion.
280 */
281 @Test
282 public void matchSctpSrcTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700283 Criterion criterion = Criteria.matchSctpSrc(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800284 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800285 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800286 }
287
288 /**
289 * Tests destination SCTP criterion.
290 */
291 @Test
292 public void matchSctpDstTest() {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700293 Criterion criterion = Criteria.matchSctpDst(tpPort);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800294 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800295 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800296 }
297
298 /**
299 * Tests ICMP type criterion.
300 */
301 @Test
302 public void matchIcmpTypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800303 Criterion criterion = Criteria.matchIcmpType((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800304 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800305 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800306 }
307
308 /**
309 * Tests ICMP code criterion.
310 */
311 @Test
312 public void matchIcmpCodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800313 Criterion criterion = Criteria.matchIcmpCode((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800314 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800315 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800316 }
317
318 /**
319 * Tests IPv6 source criterion.
320 */
321 @Test
322 public void matchIPv6SrcTest() {
323 Criterion criterion = Criteria.matchIPv6Src(ipPrefix6);
324 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800325 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800326 }
327
328 /**
329 * Tests IPv6 destination criterion.
330 */
331 @Test
332 public void matchIPv6DstTest() {
333 Criterion criterion = Criteria.matchIPv6Dst(ipPrefix6);
334 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800335 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800336 }
337
338 /**
339 * Tests IPv6 flow label criterion.
340 */
341 @Test
342 public void matchIPv6FlowLabelTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800343 Criterion criterion = Criteria.matchIPv6FlowLabel(0xffffe);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800344 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800345 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800346 }
347
348 /**
349 * Tests ICMP v6 type criterion.
350 */
351 @Test
352 public void matchIcmpv6TypeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800353 Criterion criterion = Criteria.matchIcmpv6Type((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800354 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800355 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800356 }
357
358 /**
359 * Tests ICMP v6 code criterion.
360 */
361 @Test
362 public void matchIcmpv6CodeTest() {
Pavlin Radoslavovf3b69332015-02-06 15:47:05 -0800363 Criterion criterion = Criteria.matchIcmpv6Code((byte) 250);
Ray Milkey9ee62f52015-02-06 13:47:35 -0800364 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800365 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800366 }
367
368 /**
369 * Tests IPV6 target address criterion.
370 */
371 @Test
372 public void matchIPv6NDTargetAddressTest() {
373 Criterion criterion =
374 Criteria.matchIPv6NDTargetAddress(
375 Ip6Address.valueOf("1111:2222::"));
376 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 SLL criterion.
382 */
383 @Test
384 public void matchIPv6NDSourceLinkLayerAddressTest() {
385 Criterion criterion = Criteria.matchIPv6NDSourceLinkLayerAddress(mac1);
386 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800387 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800388 }
389
390 /**
391 * Tests IPV6 TLL criterion.
392 */
393 @Test
394 public void matchIPv6NDTargetLinkLayerAddressTest() {
395 Criterion criterion = Criteria.matchIPv6NDTargetLinkLayerAddress(mac1);
396 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800397 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800398 }
399
400 /**
401 * Tests MPLS label criterion.
402 */
403 @Test
404 public void matchMplsLabelTest() {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100405 Criterion criterion = Criteria.matchMplsLabel(MplsLabel.mplsLabel(0xffffe));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800406 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800407 assertThat(result, matchesCriterion(criterion));
Ray Milkey9ee62f52015-02-06 13:47:35 -0800408 }
409
410 /**
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800411 * Tests IPv6 Extension Header pseudo-field flags criterion.
412 */
413 @Test
414 public void matchIPv6ExthdrFlagsTest() {
415 int exthdrFlags =
416 Criterion.IPv6ExthdrFlags.NONEXT.getValue() |
417 Criterion.IPv6ExthdrFlags.ESP.getValue() |
418 Criterion.IPv6ExthdrFlags.AUTH.getValue() |
419 Criterion.IPv6ExthdrFlags.DEST.getValue() |
420 Criterion.IPv6ExthdrFlags.FRAG.getValue() |
421 Criterion.IPv6ExthdrFlags.ROUTER.getValue() |
422 Criterion.IPv6ExthdrFlags.HOP.getValue() |
423 Criterion.IPv6ExthdrFlags.UNREP.getValue() |
424 Criterion.IPv6ExthdrFlags.UNSEQ.getValue();
425 Criterion criterion = Criteria.matchIPv6ExthdrFlags(exthdrFlags);
426 ObjectNode result = criterionCodec.encode(criterion, context);
Ray Milkey46670a82015-02-08 17:57:17 -0800427
428 assertThat(result, matchesCriterion(criterion));
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800429 }
430
431 /**
Ray Milkey9ee62f52015-02-06 13:47:35 -0800432 * Tests lambda criterion.
433 */
434 @Test
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700435 public void matchOchSignal() {
436 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
437 Criterion criterion = Criteria.matchLambda(ochSignal);
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 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700443 * Tests Och signal type criterion.
Ray Milkey9ee62f52015-02-06 13:47:35 -0800444 */
445 @Test
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700446 public void matchOchSignalTypeTest() {
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700447 Criterion criterion = Criteria.matchOchSignalType(OchSignalType.FIXED_GRID);
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 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300451
452 /**
453 * Tests Odu Signal ID criterion.
454 */
455 @Test
456 public void matchOduSignalIdTest() {
457
458 OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
459
460 Criterion criterion = Criteria.matchOduSignalId(oduSignalId);
461 ObjectNode result = criterionCodec.encode(criterion, context);
462 assertThat(result, matchesCriterion(criterion));
463 }
464
465 /**
466 * Tests Odu Signal Type criterion.
467 */
468 @Test
469 public void matchOduSignalTypeTest() {
470
471 OduSignalType signalType = OduSignalType.ODU2;
472
473 Criterion criterion = Criteria.matchOduSignalType(signalType);
474 ObjectNode result = criterionCodec.encode(criterion, context);
475 assertThat(result, matchesCriterion(criterion));
476 }
477
Frank Wang74ce2c12018-04-11 20:26:45 +0800478 /**
479 * Tests protocol-independent type criterion encoding.
480 */
481 @Test
482 public void matchPiTypeEncodingTest() {
483
484 PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType");
485 byte[] matchExactBytes1 = {0x08, 0x00};
486 Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build();
487 ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context);
488 assertThat(exactResult, matchesCriterion(exactBytesCriterion));
489
490 PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr");
491 int mask = 0x00ffffff;
492 byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
493 Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build();
494 ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context);
495 assertThat(lpmResult, matchesCriterion(lpmBytesCriterion));
496
497 byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
498 byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
499 Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1,
500 matchTernaryMaskBytes).build();
501 ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context);
502 assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion));
503
504 byte[] matchRangeBytes1 = {0x10};
505 byte[] matchRangeHighBytes = {0x30};
506 Criterion rangeBytesCriterion = PiCriterion.builder()
507 .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
508 ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context);
509 assertThat(rangeResult, matchesCriterion(rangeBytesCriterion));
Frank Wang74ce2c12018-04-11 20:26:45 +0800510 }
511
512 /**
513 * Tests protocol-independent type criterion decoding.
514 */
515 @Test
516 public void matchPiTypeDecodingTest() throws IOException {
517 Criterion criterion = getCriterion("PiCriterion.json");
518 Assert.assertThat(criterion.type(), is(Criterion.Type.PROTOCOL_INDEPENDENT));
519 Collection<PiFieldMatch> piFieldMatches = ((PiCriterion) criterion).fieldMatches();
520 for (PiFieldMatch piFieldMatch : piFieldMatches) {
521 switch (piFieldMatch.type()) {
522 case EXACT:
523 Assert.assertThat(piFieldMatch.fieldId().id(), is("ingress_port"));
524 Assert.assertThat(((PiExactFieldMatch) piFieldMatch).value(), is(
525 copyFrom((byte) 0x10)));
526 break;
527 case LPM:
528 Assert.assertThat(piFieldMatch.fieldId().id(), is("src_addr"));
529 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).value(),
530 is(copyFrom(0xa010101)));
531 Assert.assertThat(((PiLpmFieldMatch) piFieldMatch).prefixLength(), is(24));
532 break;
533 case TERNARY:
534 Assert.assertThat(piFieldMatch.fieldId().id(), is("dst_addr"));
535 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).value(),
536 is(copyFrom(0xa010101)));
537 Assert.assertThat(((PiTernaryFieldMatch) piFieldMatch).mask(),
538 is(copyFrom(0xfffffff)));
539 break;
540 case RANGE:
541 Assert.assertThat(piFieldMatch.fieldId().id(), is("egress_port"));
542 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).highValue(),
543 is(copyFrom((byte) 0x20)));
544 Assert.assertThat(((PiRangeFieldMatch) piFieldMatch).lowValue(),
545 is(copyFrom((byte) 0x10)));
546 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800547 default:
Carmelo Cascone6af4e172018-06-15 16:01:30 +0200548 Assert.fail();
Frank Wang74ce2c12018-04-11 20:26:45 +0800549 }
550 }
551 }
552
553 /**
554 * Reads in a criterion from the given resource and decodes it.
555 *
556 * @param resourceName resource to use to read the JSON for the rule
557 * @return decoded criterion
558 * @throws IOException if processing the resource fails
559 */
560 private Criterion getCriterion(String resourceName) throws IOException {
561 InputStream jsonStream = CriterionCodecTest.class.getResourceAsStream(resourceName);
562 JsonNode json = context.mapper().readTree(jsonStream);
563 MatcherAssert.assertThat(json, notNullValue());
564 Criterion criterion = criterionCodec.decode((ObjectNode) json, context);
565 Assert.assertThat(criterion, notNullValue());
566 return criterion;
567 }
Ray Milkey73ba84a2015-02-04 17:08:41 -0800568}