blob: bda55153fb7ea91c6567004a079ae2fcc39123ff [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 }
140 return true;
141 }
142
143 /**
144 * Matches an eth type criterion object.
145 *
146 * @param criterion criterion to match
147 * @return true if the JSON matches the criterion, false otherwise.
148 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700149 private boolean matchCriterion(EthTypeCriterion criterion) {
andread35f89c2015-11-23 10:02:07 -0800150 final int ethType = criterion.ethType().toShort() & 0xffff;
151 final int jsonEthType = Integer.decode(jsonCriterion.get("ethType").textValue()) & 0xffff;
Ray Milkey46670a82015-02-08 17:57:17 -0800152 if (ethType != jsonEthType) {
153 description.appendText("ethType was "
154 + Integer.toString(jsonEthType));
155 return false;
156 }
157 return true;
158 }
159
160 /**
161 * Matches a VLAN ID criterion object.
162 *
163 * @param criterion criterion to match
164 * @return true if the JSON matches the criterion, false otherwise.
165 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700166 private boolean matchCriterion(VlanIdCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800167 final short vlanId = criterion.vlanId().toShort();
168 final short jsonVlanId = jsonCriterion.get("vlanId").shortValue();
169 if (vlanId != jsonVlanId) {
170 description.appendText("vlanId was " + Short.toString(jsonVlanId));
171 return false;
172 }
173 return true;
174 }
175
176 /**
177 * Matches a VLAN PCP criterion object.
178 *
179 * @param criterion criterion to match
180 * @return true if the JSON matches the criterion, false otherwise.
181 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700182 private boolean matchCriterion(VlanPcpCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800183 final byte priority = criterion.priority();
184 final byte jsonPriority =
185 (byte) jsonCriterion.get("priority").shortValue();
186 if (priority != jsonPriority) {
187 description.appendText("priority was " + Byte.toString(jsonPriority));
188 return false;
189 }
190 return true;
191 }
192
193 /**
194 * Matches an IP DSCP criterion object.
195 *
196 * @param criterion criterion to match
197 * @return true if the JSON matches the criterion, false otherwise.
198 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700199 private boolean matchCriterion(IPDscpCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800200 final byte ipDscp = criterion.ipDscp();
201 final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue();
202 if (ipDscp != jsonIpDscp) {
203 description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp));
204 return false;
205 }
206 return true;
207 }
208
209 /**
210 * Matches an IP ECN criterion object.
211 *
212 * @param criterion criterion to match
213 * @return true if the JSON matches the criterion, false otherwise.
214 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700215 private boolean matchCriterion(IPEcnCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800216 final byte ipEcn = criterion.ipEcn();
217 final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue();
218 if (ipEcn != jsonIpEcn) {
219 description.appendText("IP ECN was " + Byte.toString(jsonIpEcn));
220 return false;
221 }
222 return true;
223 }
224
225 /**
226 * Matches an IP protocol criterion object.
227 *
228 * @param criterion criterion to match
229 * @return true if the JSON matches the criterion, false otherwise.
230 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700231 private boolean matchCriterion(IPProtocolCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800232 final short protocol = criterion.protocol();
233 final short jsonProtocol = jsonCriterion.get("protocol").shortValue();
234 if (protocol != jsonProtocol) {
235 description.appendText("protocol was "
236 + Short.toString(jsonProtocol));
237 return false;
238 }
239 return true;
240 }
241
242 /**
243 * Matches an IP address criterion object.
244 *
245 * @param criterion criterion to match
246 * @return true if the JSON matches the criterion, false otherwise.
247 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700248 private boolean matchCriterion(IPCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800249 final String ip = criterion.ip().toString();
250 final String jsonIp = jsonCriterion.get("ip").textValue();
251 if (!ip.equals(jsonIp)) {
252 description.appendText("ip was " + jsonIp);
253 return false;
254 }
255 return true;
256 }
257
258 /**
259 * Matches a TCP port criterion object.
260 *
261 * @param criterion criterion to match
262 * @return true if the JSON matches the criterion, false otherwise.
263 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700264 private boolean matchCriterion(TcpPortCriterion criterion) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700265 final int tcpPort = criterion.tcpPort().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800266 final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue();
267 if (tcpPort != jsonTcpPort) {
268 description.appendText("tcp port was "
269 + Integer.toString(jsonTcpPort));
270 return false;
271 }
272 return true;
273 }
274
275 /**
276 * Matches a UDP port criterion object.
277 *
278 * @param criterion criterion to match
279 * @return true if the JSON matches the criterion, false otherwise.
280 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700281 private boolean matchCriterion(UdpPortCriterion criterion) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700282 final int udpPort = criterion.udpPort().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800283 final int jsonUdpPort = jsonCriterion.get("udpPort").intValue();
284 if (udpPort != jsonUdpPort) {
285 description.appendText("udp port was "
286 + Integer.toString(jsonUdpPort));
287 return false;
288 }
289 return true;
290 }
291
292 /**
293 * Matches an SCTP port criterion object.
294 *
295 * @param criterion criterion to match
296 * @return true if the JSON matches the criterion, false otherwise.
297 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700298 private boolean matchCriterion(SctpPortCriterion criterion) {
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700299 final int sctpPort = criterion.sctpPort().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800300 final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue();
301 if (sctpPort != jsonSctpPort) {
302 description.appendText("sctp port was "
303 + Integer.toString(jsonSctpPort));
304 return false;
305 }
306 return true;
307 }
308
309 /**
310 * Matches an ICMP type criterion object.
311 *
312 * @param criterion criterion to match
313 * @return true if the JSON matches the criterion, false otherwise.
314 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700315 private boolean matchCriterion(IcmpTypeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800316 final short icmpType = criterion.icmpType();
317 final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue();
318 if (icmpType != jsonIcmpType) {
319 description.appendText("icmp type was "
320 + Short.toString(jsonIcmpType));
321 return false;
322 }
323 return true;
324 }
325
326 /**
327 * Matches an ICMP code criterion object.
328 *
329 * @param criterion criterion to match
330 * @return true if the JSON matches the criterion, false otherwise.
331 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700332 private boolean matchCriterion(IcmpCodeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800333 final short icmpCode = criterion.icmpCode();
334 final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue();
335 if (icmpCode != jsonIcmpCode) {
336 description.appendText("icmp code was "
337 + Short.toString(jsonIcmpCode));
338 return false;
339 }
340 return true;
341 }
342
343 /**
344 * Matches an IPV6 flow label criterion object.
345 *
346 * @param criterion criterion to match
347 * @return true if the JSON matches the criterion, false otherwise.
348 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700349 private boolean matchCriterion(IPv6FlowLabelCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800350 final int flowLabel = criterion.flowLabel();
351 final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue();
352 if (flowLabel != jsonFlowLabel) {
353 description.appendText("IPv6 flow label was "
354 + Integer.toString(jsonFlowLabel));
355 return false;
356 }
357 return true;
358 }
359
360 /**
361 * Matches an ICMP V6 type criterion object.
362 *
363 * @param criterion criterion to match
364 * @return true if the JSON matches the criterion, false otherwise.
365 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700366 private boolean matchCriterion(Icmpv6TypeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800367 final short icmpv6Type = criterion.icmpv6Type();
368 final short jsonIcmpv6Type =
369 jsonCriterion.get("icmpv6Type").shortValue();
370 if (icmpv6Type != jsonIcmpv6Type) {
371 description.appendText("icmpv6 type was "
372 + Short.toString(jsonIcmpv6Type));
373 return false;
374 }
375 return true;
376 }
377
378 /**
379 * Matches an IPV6 code criterion object.
380 *
381 * @param criterion criterion to match
382 * @return true if the JSON matches the criterion, false otherwise.
383 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700384 private boolean matchCriterion(Icmpv6CodeCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800385 final short icmpv6Code = criterion.icmpv6Code();
386 final short jsonIcmpv6Code =
387 jsonCriterion.get("icmpv6Code").shortValue();
388 if (icmpv6Code != jsonIcmpv6Code) {
389 description.appendText("icmpv6 code was "
390 + Short.toString(jsonIcmpv6Code));
391 return false;
392 }
393 return true;
394 }
395
396 /**
397 * Matches an IPV6 ND target criterion object.
398 *
399 * @param criterion criterion to match
400 * @return true if the JSON matches the criterion, false otherwise.
401 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700402 private boolean matchCriterion(IPv6NDTargetAddressCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800403 final String targetAddress =
404 criterion.targetAddress().toString();
405 final String jsonTargetAddress =
406 jsonCriterion.get("targetAddress").textValue();
407 if (!targetAddress.equals(jsonTargetAddress)) {
408 description.appendText("target address was " +
409 jsonTargetAddress);
410 return false;
411 }
412 return true;
413 }
414
415 /**
416 * Matches an IPV6 ND link layer criterion object.
417 *
418 * @param criterion criterion to match
419 * @return true if the JSON matches the criterion, false otherwise.
420 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700421 private boolean matchCriterion(IPv6NDLinkLayerAddressCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800422 final String llAddress =
423 criterion.mac().toString();
424 final String jsonLlAddress =
425 jsonCriterion.get("mac").textValue();
426 if (!llAddress.equals(jsonLlAddress)) {
427 description.appendText("mac was " + jsonLlAddress);
428 return false;
429 }
430 return true;
431 }
432
433 /**
434 * Matches an MPLS label criterion object.
435 *
436 * @param criterion criterion to match
437 * @return true if the JSON matches the criterion, false otherwise.
438 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700439 private boolean matchCriterion(MplsCriterion criterion) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100440 final int label = criterion.label().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800441 final int jsonLabel = jsonCriterion.get("label").intValue();
442 if (label != jsonLabel) {
443 description.appendText("label was " + Integer.toString(jsonLabel));
444 return false;
445 }
446 return true;
447 }
448
449 /**
450 * Matches an IPV6 exthdr criterion object.
451 *
452 * @param criterion criterion to match
453 * @return true if the JSON matches the criterion, false otherwise.
454 */
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700455 private boolean matchCriterion(IPv6ExthdrFlagsCriterion criterion) {
Ray Milkey46670a82015-02-08 17:57:17 -0800456 final int exthdrFlags = criterion.exthdrFlags();
457 final int jsonExthdrFlags =
458 jsonCriterion.get("exthdrFlags").intValue();
459 if (exthdrFlags != jsonExthdrFlags) {
460 description.appendText("exthdrFlags was "
461 + Long.toHexString(jsonExthdrFlags));
462 return false;
463 }
464 return true;
465 }
466
467 /**
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700468 * Matches an Och signal criterion object.
Ray Milkey46670a82015-02-08 17:57:17 -0800469 *
470 * @param criterion criterion to match
471 * @return true if the JSON matches the criterion, false otherwise.
472 */
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700473 private boolean matchCriterion(OchSignalCriterion criterion) {
474 final OchSignal ochSignal = criterion.lambda();
475 final JsonNode jsonOchSignal = jsonCriterion.get("ochSignalId");
476 String jsonGridType = jsonOchSignal.get("gridType").textValue();
477 String jsonChannelSpacing = jsonOchSignal.get("channelSpacing").textValue();
478 int jsonSpacingMultiplier = jsonOchSignal.get("spacingMultiplier").intValue();
479 int jsonSlotGranularity = jsonOchSignal.get("slotGranularity").intValue();
480
481 boolean equality = Objects.equals(ochSignal.gridType().name(), jsonGridType)
482 && Objects.equals(ochSignal.channelSpacing().name(), jsonChannelSpacing)
483 && Objects.equals(ochSignal.spacingMultiplier(), jsonSpacingMultiplier)
484 && Objects.equals(ochSignal.slotGranularity(), jsonSlotGranularity);
485
486 if (!equality) {
487 String joined = Joiner.on(", ")
488 .join(jsonGridType, jsonChannelSpacing, jsonSpacingMultiplier, jsonSlotGranularity);
489
490 description.appendText("och signal id was " + joined);
Ray Milkey46670a82015-02-08 17:57:17 -0800491 return false;
492 }
493 return true;
494 }
495
496 /**
Sho SHIMIZU68b8c1f2015-05-29 18:42:26 -0700497 * Matches an Och signal type criterion object.
Ray Milkey46670a82015-02-08 17:57:17 -0800498 *
499 * @param criterion criterion to match
500 * @return true if the JSON matches the criterion, false otherwise.
501 */
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700502 private boolean matchCriterion(OchSignalTypeCriterion criterion) {
503 final String signalType = criterion.signalType().name();
504 final String jsonSignalType = jsonCriterion.get("ochSignalType").textValue();
505 if (!signalType.equals(jsonSignalType)) {
Sho SHIMIZUc06966e2015-06-01 12:14:37 -0700506 description.appendText("signal type was " + jsonSignalType);
Ray Milkey46670a82015-02-08 17:57:17 -0800507 return false;
508 }
509 return true;
510 }
511
Yafit Hadar5796d972015-10-15 13:16:11 +0300512 /**
513 * Matches an ODU signal ID criterion object.
514 *
515 * @param criterion criterion to match
516 * @return true if the JSON matches the criterion, false otherwise.
517 */
518 private boolean matchCriterion(OduSignalIdCriterion criterion) {
519 final OduSignalId oduSignal = criterion.oduSignalId();
520 final JsonNode jsonOduSignal = jsonCriterion.get(CriterionCodec.ODU_SIGNAL_ID);
521 int jsonTpn = jsonOduSignal.get(CriterionCodec.TRIBUTARY_PORT_NUMBER).intValue();
522 int jsonTsLen = jsonOduSignal.get(CriterionCodec.TRIBUTARY_SLOT_LEN).intValue();
523 byte[] jsonTributaryBitMap = HexString.fromHexString(
524 jsonOduSignal.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP).asText());
525 OduSignalId jsonOduSignalId = OduSignalId.oduSignalId(jsonTpn, jsonTsLen, jsonTributaryBitMap);
526 if (!oduSignal.equals(jsonOduSignalId)) {
527 description.appendText("oduSignalId was " + criterion);
528 return false;
529 }
530 return true;
531 }
532
533 /**
534 * Matches an ODU signal Type criterion object.
535 *
536 * @param criterion criterion to match
537 * @return true if the JSON matches the criterion, false otherwise.
538 */
539 private boolean matchCriterion(OduSignalTypeCriterion criterion) {
540 final String signalType = criterion.signalType().name();
541 final String jsonOduSignalType = jsonCriterion.get("oduSignalType").textValue();
542 if (!signalType.equals(jsonOduSignalType)) {
543 description.appendText("signalType was " + signalType);
544 return false;
545 }
546 return true;
547 }
548
Frank Wang74ce2c12018-04-11 20:26:45 +0800549 /**
550 * Matches a protocol-independent Type criterion object.
551 *
552 * @param criterion criterion to match
553 * @return true if the JSON matches the criterion, false otherwise.
554 */
555 private boolean matchCriterion(PiCriterion criterion) {
556 Collection<PiFieldMatch> piFieldMatches = criterion.fieldMatches();
557 JsonNode jsonMathes = jsonCriterion.get("matches");
558 if (!jsonMathes.isArray()) {
559 return false;
560 }
561 for (JsonNode matchNode : jsonMathes) {
562 for (PiFieldMatch fieldMatch : piFieldMatches) {
563
564 if (!Objects.equals(matchNode.get("field").textValue(), fieldMatch.fieldId().id())) {
565 description.appendText("match field was " + fieldMatch.fieldId().id());
566 return false;
567 }
568
569 if (!Objects.equals(matchNode.get("match").textValue(),
570 fieldMatch.type().name().toLowerCase())) {
571 description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
572 return false;
573 }
574
575 switch (fieldMatch.type()) {
576 case EXACT:
577 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value")
578 .textValue(), null)),
579 ((PiExactFieldMatch) fieldMatch).value())) {
580 description.appendText("match value was " + ((PiExactFieldMatch) fieldMatch).value());
581 return false;
582 }
583 break;
584 case LPM:
585 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value")
586 .textValue(), null)),
587 ((PiLpmFieldMatch) fieldMatch).value())) {
588 description.appendText("match value was " + ((PiLpmFieldMatch) fieldMatch).value());
589 return false;
590 }
591 if (!Objects.equals(matchNode.get("prefixLength").intValue(),
592 ((PiLpmFieldMatch) fieldMatch).prefixLength())) {
593 description.appendText("match prefix was " +
594 ((PiLpmFieldMatch) fieldMatch).prefixLength());
595 return false;
596 }
597 break;
598 case TERNARY:
599 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value")
600 .textValue(), null)),
601 ((PiTernaryFieldMatch) fieldMatch).value())) {
602 description.appendText("match value was " + ((PiTernaryFieldMatch) fieldMatch).value());
603 return false;
604 }
605 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("mask")
606 .textValue(), null)),
607 ((PiTernaryFieldMatch) fieldMatch).mask())) {
608 description.appendText("match mask was " + ((PiTernaryFieldMatch) fieldMatch).mask());
609 return false;
610 }
611 break;
612 case RANGE:
613 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("highValue")
614 .textValue(), null)),
615 ((PiRangeFieldMatch) fieldMatch).highValue())) {
616 description.appendText("match high value was " +
617 ((PiRangeFieldMatch) fieldMatch).highValue());
618 return false;
619 }
620 if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("lowValue")
621 .textValue(), null)),
622 ((PiRangeFieldMatch) fieldMatch).lowValue())) {
623 description.appendText("match low value was " +
624 ((PiRangeFieldMatch) fieldMatch).lowValue());
625 return false;
626 }
627 break;
Frank Wang74ce2c12018-04-11 20:26:45 +0800628 default:
629 description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
630 return false;
631 }
632 }
633 }
634
635 return true;
636 }
Yafit Hadar5796d972015-10-15 13:16:11 +0300637
Ray Milkeydb358082015-01-13 16:34:38 -0800638 @Override
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800639 public boolean matchesSafely(JsonNode jsonCriterion,
640 Description description) {
Ray Milkey46670a82015-02-08 17:57:17 -0800641 this.description = description;
642 this.jsonCriterion = jsonCriterion;
Ray Milkeydb358082015-01-13 16:34:38 -0800643 final String type = criterion.type().name();
644 final String jsonType = jsonCriterion.get("type").asText();
645 if (!type.equals(jsonType)) {
646 description.appendText("type was " + type);
647 return false;
648 }
649
650 switch (criterion.type()) {
651
652 case IN_PORT:
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800653 case IN_PHY_PORT:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700654 return matchCriterion((PortCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800655
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800656 case METADATA:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700657 return matchCriterion((MetadataCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800658
Ray Milkeydb358082015-01-13 16:34:38 -0800659 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800660 case ETH_SRC:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700661 return matchCriterion((EthCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800662
663 case ETH_TYPE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700664 return matchCriterion((EthTypeCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800665
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800666 case VLAN_VID:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700667 return matchCriterion((VlanIdCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800668
669 case VLAN_PCP:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700670 return matchCriterion((VlanPcpCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800671
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800672 case IP_DSCP:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700673 return matchCriterion((IPDscpCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800674
675 case IP_ECN:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700676 return matchCriterion((IPEcnCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800677
Ray Milkeydb358082015-01-13 16:34:38 -0800678 case IP_PROTO:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700679 return matchCriterion((IPProtocolCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800680
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800681 case IPV4_SRC:
682 case IPV4_DST:
683 case IPV6_SRC:
684 case IPV6_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700685 return matchCriterion((IPCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800686
687 case TCP_SRC:
688 case TCP_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700689 return matchCriterion((TcpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800690
691 case UDP_SRC:
692 case UDP_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700693 return matchCriterion((UdpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800694
695 case SCTP_SRC:
696 case SCTP_DST:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700697 return matchCriterion((SctpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800698
699 case ICMPV4_TYPE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700700 return matchCriterion((IcmpTypeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800701
702 case ICMPV4_CODE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700703 return matchCriterion((IcmpCodeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800704
705 case IPV6_FLABEL:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700706 return matchCriterion((IPv6FlowLabelCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800707
708 case ICMPV6_TYPE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700709 return matchCriterion((Icmpv6TypeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800710
711 case ICMPV6_CODE:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700712 return matchCriterion((Icmpv6CodeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800713
714 case IPV6_ND_TARGET:
Ray Milkey46670a82015-02-08 17:57:17 -0800715 return matchCriterion(
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700716 (IPv6NDTargetAddressCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800717
718 case IPV6_ND_SLL:
719 case IPV6_ND_TLL:
Ray Milkey46670a82015-02-08 17:57:17 -0800720 return matchCriterion(
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700721 (IPv6NDLinkLayerAddressCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800722
723 case MPLS_LABEL:
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700724 return matchCriterion((MplsCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800725
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800726 case IPV6_EXTHDR:
Ray Milkey46670a82015-02-08 17:57:17 -0800727 return matchCriterion(
Sho SHIMIZUfbc80e52015-04-28 10:41:58 -0700728 (IPv6ExthdrFlagsCriterion) criterion);
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800729
Ray Milkeydb358082015-01-13 16:34:38 -0800730 case OCH_SIGID:
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700731 return matchCriterion((OchSignalCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800732
733 case OCH_SIGTYPE:
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700734 return matchCriterion((OchSignalTypeCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800735
Yafit Hadar5796d972015-10-15 13:16:11 +0300736 case ODU_SIGID:
737 return matchCriterion((OduSignalIdCriterion) criterion);
738
739 case ODU_SIGTYPE:
740 return matchCriterion((OduSignalTypeCriterion) criterion);
Frank Wang74ce2c12018-04-11 20:26:45 +0800741 case PROTOCOL_INDEPENDENT:
742 return matchCriterion((PiCriterion) criterion);
Yafit Hadar5796d972015-10-15 13:16:11 +0300743
Ray Milkeydb358082015-01-13 16:34:38 -0800744 default:
745 // Don't know how to format this type
746 description.appendText("unknown criterion type " +
747 criterion.type());
748 return false;
749 }
Ray Milkeydb358082015-01-13 16:34:38 -0800750 }
751
752 @Override
753 public void describeTo(Description description) {
754 description.appendText(criterion.toString());
755 }
Ray Milkeydb358082015-01-13 16:34:38 -0800756}