blob: b19ee53e70c87110a46559698ddae076f6440f75 [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeydb358082015-01-13 16:34:38 -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.util.Collection;
Yafit Hadar5796d972015-10-15 13:16:11 +030019import java.util.Objects;
20
Ray Milkeydb358082015-01-13 16:34:38 -080021import org.hamcrest.Description;
22import org.hamcrest.TypeSafeDiagnosingMatcher;
Yafit Hadar5796d972015-10-15 13:16:11 +030023import org.onlab.util.HexString;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070024import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030025import org.onosproject.net.OduSignalId;
Ray Milkeydb358082015-01-13 16:34:38 -080026import org.onosproject.net.flow.criteria.Criterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070027import org.onosproject.net.flow.criteria.EthCriterion;
28import org.onosproject.net.flow.criteria.EthTypeCriterion;
29import org.onosproject.net.flow.criteria.IPCriterion;
30import org.onosproject.net.flow.criteria.IPDscpCriterion;
31import org.onosproject.net.flow.criteria.IPEcnCriterion;
32import org.onosproject.net.flow.criteria.IPProtocolCriterion;
33import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
34import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
35import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
36import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
37import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
38import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
39import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
40import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070041import org.onosproject.net.flow.criteria.MetadataCriterion;
42import org.onosproject.net.flow.criteria.MplsCriterion;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070043import org.onosproject.net.flow.criteria.OchSignalCriterion;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070044import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
Yafit Hadar5796d972015-10-15 13:16:11 +030045import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
46import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
Frank Wang74ce2c12018-04-11 20:26:45 +080047import org.onosproject.net.flow.criteria.PiCriterion;
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -070048import org.onosproject.net.flow.criteria.PortCriterion;
49import org.onosproject.net.flow.criteria.SctpPortCriterion;
50import org.onosproject.net.flow.criteria.TcpPortCriterion;
51import org.onosproject.net.flow.criteria.UdpPortCriterion;
52import org.onosproject.net.flow.criteria.VlanIdCriterion;
53import org.onosproject.net.flow.criteria.VlanPcpCriterion;
Ray Milkeydb358082015-01-13 16:34:38 -080054
Yafit Hadar5796d972015-10-15 13:16:11 +030055import com.fasterxml.jackson.databind.JsonNode;
56import com.google.common.base.Joiner;
Frank Wang74ce2c12018-04-11 20:26:45 +080057import org.onosproject.net.pi.runtime.PiExactFieldMatch;
58import org.onosproject.net.pi.runtime.PiFieldMatch;
59import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
60import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
61import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
Frank Wang74ce2c12018-04-11 20:26:45 +080062
63import static org.onlab.util.ImmutableByteSequence.copyFrom;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070064
Ray Milkeydb358082015-01-13 16:34:38 -080065/**
66 * Hamcrest matcher for criterion objects.
67 */
Ray Milkey46670a82015-02-08 17:57:17 -080068public final class CriterionJsonMatcher extends
69 TypeSafeDiagnosingMatcher<JsonNode> {
Ray Milkeydb358082015-01-13 16:34:38 -080070
71 final Criterion criterion;
Ray Milkey46670a82015-02-08 17:57:17 -080072 Description description;
73 JsonNode jsonCriterion;
Ray Milkeydb358082015-01-13 16:34:38 -080074
Ray Milkey46670a82015-02-08 17:57:17 -080075 /**
76 * Constructs a matcher object.
77 *
78 * @param criterionValue criterion to match
79 */
Ray Milkeydb358082015-01-13 16:34:38 -080080 private CriterionJsonMatcher(Criterion criterionValue) {
81 criterion = criterionValue;
82 }
83
Ray Milkey46670a82015-02-08 17:57:17 -080084 /**
85 * Factory to allocate an criterion matcher.
86 *
87 * @param criterion criterion object we are looking for
88 * @return matcher
89 */
90 public static CriterionJsonMatcher matchesCriterion(Criterion criterion) {
91 return new CriterionJsonMatcher(criterion);
92 }
93
94 /**
95 * Matches a port criterion object.
96 *
97 * @param criterion criterion to match
98 * @return true if the JSON matches the criterion, false otherwise.
99 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700100 private boolean matchCriterion(PortCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800101 final long port = criterion.port().toLong();
102 final long jsonPort = jsonCriterion.get("port").asLong();
103 if (port != jsonPort) {
104 description.appendText("port was " + Long.toString(jsonPort));
105 return false;
106 }
107 return true;
108 }
109
110 /**
111 * Matches a metadata criterion object.
112 *
113 * @param criterion criterion to match
114 * @return true if the JSON matches the criterion, false otherwise.
115 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700116 private boolean matchCriterion(MetadataCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800117 final long metadata = criterion.metadata();
118 final long jsonMetadata = jsonCriterion.get("metadata").asLong();
119 if (metadata != jsonMetadata) {
120 description.appendText("metadata was "
121 + Long.toString(jsonMetadata));
122 return false;
123 }
124 return true;
125 }
126
127 /**
128 * Matches an eth criterion object.
129 *
130 * @param criterion criterion to match
131 * @return true if the JSON matches the criterion, false otherwise.
132 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700133 private boolean matchCriterion(EthCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800134 final String mac = criterion.mac().toString();
135 final String jsonMac = jsonCriterion.get("mac").textValue();
136 if (!mac.equals(jsonMac)) {
137 description.appendText("mac was " + jsonMac);
138 return false;
139 }
Seyeon Jeong37a851c2020-02-26 12:51:03 -0800140 if (criterion.type() == Criterion.Type.ETH_DST_MASKED) {
141 final String macMask = criterion.mask().toString();
142 final String jsonMacMask = jsonCriterion.get("macMask").textValue();
143 if (!macMask.equals(jsonMacMask)) {
144 description.appendText("macMask was " + jsonMacMask);
145 return false;
146 }
147 }
Ray Milkey46670a82015-02-08 17:57:17 -0800148 return true;
149 }
150
151 /**
152 * Matches an eth type criterion object.
153 *
154 * @param criterion criterion to match
155 * @return true if the JSON matches the criterion, false otherwise.
156 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700157 private boolean matchCriterion(EthTypeCriterion criterion) {
andread35f89c2015-11-23 10:02:07 -0800158 final int ethType = criterion.ethType().toShort() & 0xffff;
159 final int jsonEthType = Integer.decode(jsonCriterion.get("ethType").textValue()) & 0xffff;
Ray Milkey46670a82015-02-08 17:57:17 -0800160 if (ethType != jsonEthType) {
161 description.appendText("ethType was "
162 + Integer.toString(jsonEthType));
163 return false;
164 }
165 return true;
166 }
167
168 /**
169 * Matches a VLAN ID criterion object.
170 *
171 * @param criterion criterion to match
172 * @return true if the JSON matches the criterion, false otherwise.
173 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700174 private boolean matchCriterion(VlanIdCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800175 final short vlanId = criterion.vlanId().toShort();
176 final short jsonVlanId = jsonCriterion.get("vlanId").shortValue();
177 if (vlanId != jsonVlanId) {
178 description.appendText("vlanId was " + Short.toString(jsonVlanId));
179 return false;
180 }
181 return true;
182 }
183
184 /**
185 * Matches a VLAN PCP criterion object.
186 *
187 * @param criterion criterion to match
188 * @return true if the JSON matches the criterion, false otherwise.
189 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700190 private boolean matchCriterion(VlanPcpCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800191 final byte priority = criterion.priority();
192 final byte jsonPriority =
193 (byte) jsonCriterion.get("priority").shortValue();
194 if (priority != jsonPriority) {
195 description.appendText("priority was " + Byte.toString(jsonPriority));
196 return false;
197 }
198 return true;
199 }
200
201 /**
202 * Matches an IP DSCP criterion object.
203 *
204 * @param criterion criterion to match
205 * @return true if the JSON matches the criterion, false otherwise.
206 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700207 private boolean matchCriterion(IPDscpCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800208 final byte ipDscp = criterion.ipDscp();
209 final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue();
210 if (ipDscp != jsonIpDscp) {
211 description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp));
212 return false;
213 }
214 return true;
215 }
216
217 /**
218 * Matches an IP ECN criterion object.
219 *
220 * @param criterion criterion to match
221 * @return true if the JSON matches the criterion, false otherwise.
222 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700223 private boolean matchCriterion(IPEcnCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800224 final byte ipEcn = criterion.ipEcn();
225 final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue();
226 if (ipEcn != jsonIpEcn) {
227 description.appendText("IP ECN was " + Byte.toString(jsonIpEcn));
228 return false;
229 }
230 return true;
231 }
232
233 /**
234 * Matches an IP protocol criterion object.
235 *
236 * @param criterion criterion to match
237 * @return true if the JSON matches the criterion, false otherwise.
238 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700239 private boolean matchCriterion(IPProtocolCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800240 final short protocol = criterion.protocol();
241 final short jsonProtocol = jsonCriterion.get("protocol").shortValue();
242 if (protocol != jsonProtocol) {
243 description.appendText("protocol was "
244 + Short.toString(jsonProtocol));
245 return false;
246 }
247 return true;
248 }
249
250 /**
251 * Matches an IP address criterion object.
252 *
253 * @param criterion criterion to match
254 * @return true if the JSON matches the criterion, false otherwise.
255 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700256 private boolean matchCriterion(IPCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800257 final String ip = criterion.ip().toString();
258 final String jsonIp = jsonCriterion.get("ip").textValue();
259 if (!ip.equals(jsonIp)) {
260 description.appendText("ip was " + jsonIp);
261 return false;
262 }
263 return true;
264 }
265
266 /**
267 * Matches a TCP port criterion object.
268 *
269 * @param criterion criterion to match
270 * @return true if the JSON matches the criterion, false otherwise.
271 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700272 private boolean matchCriterion(TcpPortCriterion criterion) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700273 final int tcpPort = criterion.tcpPort().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800274 final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue();
275 if (tcpPort != jsonTcpPort) {
276 description.appendText("tcp port was "
277 + Integer.toString(jsonTcpPort));
278 return false;
279 }
280 return true;
281 }
282
283 /**
284 * Matches a UDP port criterion object.
285 *
286 * @param criterion criterion to match
287 * @return true if the JSON matches the criterion, false otherwise.
288 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700289 private boolean matchCriterion(UdpPortCriterion criterion) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700290 final int udpPort = criterion.udpPort().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800291 final int jsonUdpPort = jsonCriterion.get("udpPort").intValue();
292 if (udpPort != jsonUdpPort) {
293 description.appendText("udp port was "
294 + Integer.toString(jsonUdpPort));
295 return false;
296 }
297 return true;
298 }
299
300 /**
301 * Matches an SCTP port criterion object.
302 *
303 * @param criterion criterion to match
304 * @return true if the JSON matches the criterion, false otherwise.
305 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700306 private boolean matchCriterion(SctpPortCriterion criterion) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700307 final int sctpPort = criterion.sctpPort().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800308 final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue();
309 if (sctpPort != jsonSctpPort) {
310 description.appendText("sctp port was "
311 + Integer.toString(jsonSctpPort));
312 return false;
313 }
314 return true;
315 }
316
317 /**
318 * Matches an ICMP type criterion object.
319 *
320 * @param criterion criterion to match
321 * @return true if the JSON matches the criterion, false otherwise.
322 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700323 private boolean matchCriterion(IcmpTypeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800324 final short icmpType = criterion.icmpType();
325 final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue();
326 if (icmpType != jsonIcmpType) {
327 description.appendText("icmp type was "
328 + Short.toString(jsonIcmpType));
329 return false;
330 }
331 return true;
332 }
333
334 /**
335 * Matches an ICMP code criterion object.
336 *
337 * @param criterion criterion to match
338 * @return true if the JSON matches the criterion, false otherwise.
339 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700340 private boolean matchCriterion(IcmpCodeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800341 final short icmpCode = criterion.icmpCode();
342 final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue();
343 if (icmpCode != jsonIcmpCode) {
344 description.appendText("icmp code was "
345 + Short.toString(jsonIcmpCode));
346 return false;
347 }
348 return true;
349 }
350
351 /**
352 * Matches an IPV6 flow label criterion object.
353 *
354 * @param criterion criterion to match
355 * @return true if the JSON matches the criterion, false otherwise.
356 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700357 private boolean matchCriterion(IPv6FlowLabelCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800358 final int flowLabel = criterion.flowLabel();
359 final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue();
360 if (flowLabel != jsonFlowLabel) {
361 description.appendText("IPv6 flow label was "
362 + Integer.toString(jsonFlowLabel));
363 return false;
364 }
365 return true;
366 }
367
368 /**
369 * Matches an ICMP V6 type criterion object.
370 *
371 * @param criterion criterion to match
372 * @return true if the JSON matches the criterion, false otherwise.
373 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700374 private boolean matchCriterion(Icmpv6TypeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800375 final short icmpv6Type = criterion.icmpv6Type();
376 final short jsonIcmpv6Type =
377 jsonCriterion.get("icmpv6Type").shortValue();
378 if (icmpv6Type != jsonIcmpv6Type) {
379 description.appendText("icmpv6 type was "
380 + Short.toString(jsonIcmpv6Type));
381 return false;
382 }
383 return true;
384 }
385
386 /**
387 * Matches an IPV6 code criterion object.
388 *
389 * @param criterion criterion to match
390 * @return true if the JSON matches the criterion, false otherwise.
391 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700392 private boolean matchCriterion(Icmpv6CodeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800393 final short icmpv6Code = criterion.icmpv6Code();
394 final short jsonIcmpv6Code =
395 jsonCriterion.get("icmpv6Code").shortValue();
396 if (icmpv6Code != jsonIcmpv6Code) {
397 description.appendText("icmpv6 code was "
398 + Short.toString(jsonIcmpv6Code));
399 return false;
400 }
401 return true;
402 }
403
404 /**
405 * Matches an IPV6 ND target criterion object.
406 *
407 * @param criterion criterion to match
408 * @return true if the JSON matches the criterion, false otherwise.
409 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700410 private boolean matchCriterion(IPv6NDTargetAddressCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800411 final String targetAddress =
412 criterion.targetAddress().toString();
413 final String jsonTargetAddress =
414 jsonCriterion.get("targetAddress").textValue();
415 if (!targetAddress.equals(jsonTargetAddress)) {
416 description.appendText("target address was " +
417 jsonTargetAddress);
418 return false;
419 }
420 return true;
421 }
422
423 /**
424 * Matches an IPV6 ND link layer criterion object.
425 *
426 * @param criterion criterion to match
427 * @return true if the JSON matches the criterion, false otherwise.
428 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700429 private boolean matchCriterion(IPv6NDLinkLayerAddressCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800430 final String llAddress =
431 criterion.mac().toString();
432 final String jsonLlAddress =
433 jsonCriterion.get("mac").textValue();
434 if (!llAddress.equals(jsonLlAddress)) {
435 description.appendText("mac was " + jsonLlAddress);
436 return false;
437 }
438 return true;
439 }
440
441 /**
442 * Matches an MPLS label criterion object.
443 *
444 * @param criterion criterion to match
445 * @return true if the JSON matches the criterion, false otherwise.
446 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700447 private boolean matchCriterion(MplsCriterion criterion) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100448 final int label = criterion.label().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800449 final int jsonLabel = jsonCriterion.get("label").intValue();
450 if (label != jsonLabel) {
451 description.appendText("label was " + Integer.toString(jsonLabel));
452 return false;
453 }
454 return true;
455 }
456
457 /**
458 * Matches an IPV6 exthdr criterion object.
459 *
460 * @param criterion criterion to match
461 * @return true if the JSON matches the criterion, false otherwise.
462 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700463 private boolean matchCriterion(IPv6ExthdrFlagsCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800464 final int exthdrFlags = criterion.exthdrFlags();
465 final int jsonExthdrFlags =
466 jsonCriterion.get("exthdrFlags").intValue();
467 if (exthdrFlags != jsonExthdrFlags) {
468 description.appendText("exthdrFlags was "
469 + Long.toHexString(jsonExthdrFlags));
470 return false;
471 }
472 return true;
473 }
474
475 /**
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700476 * Matches an Och signal criterion object.
Ray Milkey46670a82015-02-08 17:57:17 -0800477 *
478 * @param criterion criterion to match
479 * @return true if the JSON matches the criterion, false otherwise.
480 */
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700481 private boolean matchCriterion(OchSignalCriterion criterion) {
482 final OchSignal ochSignal = criterion.lambda();
483 final JsonNode jsonOchSignal = jsonCriterion.get("ochSignalId");
484 String jsonGridType = jsonOchSignal.get("gridType").textValue();
485 String jsonChannelSpacing = jsonOchSignal.get("channelSpacing").textValue();
486 int jsonSpacingMultiplier = jsonOchSignal.get("spacingMultiplier").intValue();
487 int jsonSlotGranularity = jsonOchSignal.get("slotGranularity").intValue();
488
489 boolean equality = Objects.equals(ochSignal.gridType().name(), jsonGridType)
490 && Objects.equals(ochSignal.channelSpacing().name(), jsonChannelSpacing)
491 && Objects.equals(ochSignal.spacingMultiplier(), jsonSpacingMultiplier)
492 && Objects.equals(ochSignal.slotGranularity(), jsonSlotGranularity);
493
494 if (!equality) {
495 String joined = Joiner.on(", ")
496 .join(jsonGridType, jsonChannelSpacing, jsonSpacingMultiplier, jsonSlotGranularity);
497
498 description.appendText("och signal id was " + joined);
Ray Milkey46670a82015-02-08 17:57:17 -0800499 return false;
500 }
501 return true;
502 }
503
504 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700505 * Matches an Och signal type criterion object.
Ray Milkey46670a82015-02-08 17:57:17 -0800506 *
507 * @param criterion criterion to match
508 * @return true if the JSON matches the criterion, false otherwise.
509 */
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700510 private boolean matchCriterion(OchSignalTypeCriterion criterion) {
511 final String signalType = criterion.signalType().name();
512 final String jsonSignalType = jsonCriterion.get("ochSignalType").textValue();
513 if (!signalType.equals(jsonSignalType)) {
Sho SHIMIZUc06966e2015-06-01 12:14:37 -0700514 description.appendText("signal type was " + jsonSignalType);
Ray Milkey46670a82015-02-08 17:57:17 -0800515 return false;
516 }
517 return true;
518 }
519
Yafit Hadar5796d972015-10-15 13:16:11 +0300520 /**
521 * Matches an ODU signal ID criterion object.
522 *
523 * @param criterion criterion to match
524 * @return true if the JSON matches the criterion, false otherwise.
525 */
526 private boolean matchCriterion(OduSignalIdCriterion criterion) {
527 final OduSignalId oduSignal = criterion.oduSignalId();
528 final JsonNode jsonOduSignal = jsonCriterion.get(CriterionCodec.ODU_SIGNAL_ID);
529 int jsonTpn = jsonOduSignal.get(CriterionCodec.TRIBUTARY_PORT_NUMBER).intValue();
530 int jsonTsLen = jsonOduSignal.get(CriterionCodec.TRIBUTARY_SLOT_LEN).intValue();
531 byte[] jsonTributaryBitMap = HexString.fromHexString(
532 jsonOduSignal.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP).asText());
533 OduSignalId jsonOduSignalId = OduSignalId.oduSignalId(jsonTpn, jsonTsLen, jsonTributaryBitMap);
534 if (!oduSignal.equals(jsonOduSignalId)) {
535 description.appendText("oduSignalId was " + criterion);
536 return false;
537 }
538 return true;
539 }
540
541 /**
542 * Matches an ODU signal Type criterion object.
543 *
544 * @param criterion criterion to match
545 * @return true if the JSON matches the criterion, false otherwise.
546 */
547 private boolean matchCriterion(OduSignalTypeCriterion criterion) {
548 final String signalType = criterion.signalType().name();
549 final String jsonOduSignalType = jsonCriterion.get("oduSignalType").textValue();
550 if (!signalType.equals(jsonOduSignalType)) {
551 description.appendText("signalType was " + signalType);
552 return false;
553 }
554 return true;
555 }
556
Frank Wang74ce2c12018-04-11 20:26:45 +0800557 /**
558 * Matches a protocol-independent Type criterion object.
559 *
560 * @param criterion criterion to match
561 * @return true if the JSON matches the criterion, false otherwise.
562 */
563 private boolean matchCriterion(PiCriterion criterion) {
564 Collection<PiFieldMatch> piFieldMatches = criterion.fieldMatches();
565 JsonNode jsonMathes = jsonCriterion.get("matches");
566 if (!jsonMathes.isArray()) {
567 return false;
568 }
569 for (JsonNode matchNode : jsonMathes) {
570 for (PiFieldMatch fieldMatch : piFieldMatches) {
571
572 if (!Objects.equals(matchNode.get("field").textValue(), fieldMatch.fieldId().id())) {
573 description.appendText("match field was " + fieldMatch.fieldId().id());
574 return false;
575 }
576
577 if (!Objects.equals(matchNode.get("match").textValue(),
578 fieldMatch.type().name().toLowerCase())) {
579 description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
580 return false;
581 }
582
583 switch (fieldMatch.type()) {
584 case EXACT:
585 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value")
586 .textValue(), null)),
587 ((PiExactFieldMatch) fieldMatch).value())) {
588 description.appendText("match value was " + ((PiExactFieldMatch) fieldMatch).value());
589 return false;
590 }
591 break;
592 case LPM:
593 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value")
594 .textValue(), null)),
595 ((PiLpmFieldMatch) fieldMatch).value())) {
596 description.appendText("match value was " + ((PiLpmFieldMatch) fieldMatch).value());
597 return false;
598 }
599 if (!Objects.equals(matchNode.get("prefixLength").intValue(),
600 ((PiLpmFieldMatch) fieldMatch).prefixLength())) {
601 description.appendText("match prefix was " +
602 ((PiLpmFieldMatch) fieldMatch).prefixLength());
603 return false;
604 }
605 break;
606 case TERNARY:
607 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value")
608 .textValue(), null)),
609 ((PiTernaryFieldMatch) fieldMatch).value())) {
610 description.appendText("match value was " + ((PiTernaryFieldMatch) fieldMatch).value());
611 return false;
612 }
613 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("mask")
614 .textValue(), null)),
615 ((PiTernaryFieldMatch) fieldMatch).mask())) {
616 description.appendText("match mask was " + ((PiTernaryFieldMatch) fieldMatch).mask());
617 return false;
618 }
619 break;
620 case RANGE:
621 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("highValue")
622 .textValue(), null)),
623 ((PiRangeFieldMatch) fieldMatch).highValue())) {
624 description.appendText("match high value was " +
625 ((PiRangeFieldMatch) fieldMatch).highValue());
626 return false;
627 }
628 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("lowValue")
629 .textValue(), null)),
630 ((PiRangeFieldMatch) fieldMatch).lowValue())) {
631 description.appendText("match low value was " +
632 ((PiRangeFieldMatch) fieldMatch).lowValue());
633 return false;
634 }
635 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800636 default:
637 description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
638 return false;
639 }
640 }
641 }
642
643 return true;
644 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300645
Ray Milkeydb358082015-01-13 16:34:38 -0800646 @Override
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800647 public boolean matchesSafely(JsonNode jsonCriterion,
648 Description description) {
Ray Milkey46670a82015-02-08 17:57:17 -0800649 this.description = description;
650 this.jsonCriterion = jsonCriterion;
Ray Milkeydb358082015-01-13 16:34:38 -0800651 final String type = criterion.type().name();
652 final String jsonType = jsonCriterion.get("type").asText();
653 if (!type.equals(jsonType)) {
654 description.appendText("type was " + type);
655 return false;
656 }
657
658 switch (criterion.type()) {
659
660 case IN_PORT:
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800661 case IN_PHY_PORT:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700662 return matchCriterion((PortCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800663
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800664 case METADATA:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700665 return matchCriterion((MetadataCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800666
Ray Milkeydb358082015-01-13 16:34:38 -0800667 case ETH_DST:
Seyeon Jeong37a851c2020-02-26 12:51:03 -0800668 case ETH_DST_MASKED:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800669 case ETH_SRC:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700670 return matchCriterion((EthCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800671
672 case ETH_TYPE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700673 return matchCriterion((EthTypeCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800674
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800675 case VLAN_VID:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700676 return matchCriterion((VlanIdCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800677
678 case VLAN_PCP:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700679 return matchCriterion((VlanPcpCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800680
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800681 case IP_DSCP:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700682 return matchCriterion((IPDscpCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800683
684 case IP_ECN:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700685 return matchCriterion((IPEcnCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800686
Ray Milkeydb358082015-01-13 16:34:38 -0800687 case IP_PROTO:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700688 return matchCriterion((IPProtocolCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800689
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800690 case IPV4_SRC:
691 case IPV4_DST:
692 case IPV6_SRC:
693 case IPV6_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700694 return matchCriterion((IPCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800695
696 case TCP_SRC:
697 case TCP_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700698 return matchCriterion((TcpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800699
700 case UDP_SRC:
701 case UDP_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700702 return matchCriterion((UdpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800703
704 case SCTP_SRC:
705 case SCTP_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700706 return matchCriterion((SctpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800707
708 case ICMPV4_TYPE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700709 return matchCriterion((IcmpTypeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800710
711 case ICMPV4_CODE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700712 return matchCriterion((IcmpCodeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800713
714 case IPV6_FLABEL:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700715 return matchCriterion((IPv6FlowLabelCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800716
717 case ICMPV6_TYPE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700718 return matchCriterion((Icmpv6TypeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800719
720 case ICMPV6_CODE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700721 return matchCriterion((Icmpv6CodeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800722
723 case IPV6_ND_TARGET:
Ray Milkey46670a82015-02-08 17:57:17 -0800724 return matchCriterion(
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700725 (IPv6NDTargetAddressCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800726
727 case IPV6_ND_SLL:
728 case IPV6_ND_TLL:
Ray Milkey46670a82015-02-08 17:57:17 -0800729 return matchCriterion(
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700730 (IPv6NDLinkLayerAddressCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800731
732 case MPLS_LABEL:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700733 return matchCriterion((MplsCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800734
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800735 case IPV6_EXTHDR:
Ray Milkey46670a82015-02-08 17:57:17 -0800736 return matchCriterion(
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700737 (IPv6ExthdrFlagsCriterion) criterion);
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800738
Ray Milkeydb358082015-01-13 16:34:38 -0800739 case OCH_SIGID:
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700740 return matchCriterion((OchSignalCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800741
742 case OCH_SIGTYPE:
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700743 return matchCriterion((OchSignalTypeCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800744
Yafit Hadar5796d972015-10-15 13:16:11 +0300745 case ODU_SIGID:
746 return matchCriterion((OduSignalIdCriterion) criterion);
747
748 case ODU_SIGTYPE:
749 return matchCriterion((OduSignalTypeCriterion) criterion);
Frank Wang74ce2c12018-04-11 20:26:45 +0800750 case PROTOCOL_INDEPENDENT:
751 return matchCriterion((PiCriterion) criterion);
Yafit Hadar5796d972015-10-15 13:16:11 +0300752
Ray Milkeydb358082015-01-13 16:34:38 -0800753 default:
754 // Don't know how to format this type
755 description.appendText("unknown criterion type " +
756 criterion.type());
757 return false;
758 }
Ray Milkeydb358082015-01-13 16:34:38 -0800759 }
760
761 @Override
762 public void describeTo(Description description) {
763 description.appendText(criterion.toString());
764 }
Ray Milkeydb358082015-01-13 16:34:38 -0800765}