blob: beb9545667eee45a2ec396c7ca0182067c338037 [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
18import org.hamcrest.Description;
19import org.hamcrest.TypeSafeDiagnosingMatcher;
20import org.onosproject.net.flow.criteria.Criteria;
21import org.onosproject.net.flow.criteria.Criterion;
22
23import com.fasterxml.jackson.databind.JsonNode;
24
25/**
26 * Hamcrest matcher for criterion objects.
27 */
Ray Milkey46670a82015-02-08 17:57:17 -080028public final class CriterionJsonMatcher extends
29 TypeSafeDiagnosingMatcher<JsonNode> {
Ray Milkeydb358082015-01-13 16:34:38 -080030
31 final Criterion criterion;
Ray Milkey46670a82015-02-08 17:57:17 -080032 Description description;
33 JsonNode jsonCriterion;
Ray Milkeydb358082015-01-13 16:34:38 -080034
Ray Milkey46670a82015-02-08 17:57:17 -080035 /**
36 * Constructs a matcher object.
37 *
38 * @param criterionValue criterion to match
39 */
Ray Milkeydb358082015-01-13 16:34:38 -080040 private CriterionJsonMatcher(Criterion criterionValue) {
41 criterion = criterionValue;
42 }
43
Ray Milkey46670a82015-02-08 17:57:17 -080044 /**
45 * Factory to allocate an criterion matcher.
46 *
47 * @param criterion criterion object we are looking for
48 * @return matcher
49 */
50 public static CriterionJsonMatcher matchesCriterion(Criterion criterion) {
51 return new CriterionJsonMatcher(criterion);
52 }
53
54 /**
55 * Matches a port criterion object.
56 *
57 * @param criterion criterion to match
58 * @return true if the JSON matches the criterion, false otherwise.
59 */
60 private boolean matchCriterion(Criteria.PortCriterion criterion) {
61 final long port = criterion.port().toLong();
62 final long jsonPort = jsonCriterion.get("port").asLong();
63 if (port != jsonPort) {
64 description.appendText("port was " + Long.toString(jsonPort));
65 return false;
66 }
67 return true;
68 }
69
70 /**
71 * Matches a metadata criterion object.
72 *
73 * @param criterion criterion to match
74 * @return true if the JSON matches the criterion, false otherwise.
75 */
76 private boolean matchCriterion(Criteria.MetadataCriterion criterion) {
77 final long metadata = criterion.metadata();
78 final long jsonMetadata = jsonCriterion.get("metadata").asLong();
79 if (metadata != jsonMetadata) {
80 description.appendText("metadata was "
81 + Long.toString(jsonMetadata));
82 return false;
83 }
84 return true;
85 }
86
87 /**
88 * Matches an eth criterion object.
89 *
90 * @param criterion criterion to match
91 * @return true if the JSON matches the criterion, false otherwise.
92 */
93 private boolean matchCriterion(Criteria.EthCriterion criterion) {
94 final String mac = criterion.mac().toString();
95 final String jsonMac = jsonCriterion.get("mac").textValue();
96 if (!mac.equals(jsonMac)) {
97 description.appendText("mac was " + jsonMac);
98 return false;
99 }
100 return true;
101 }
102
103 /**
104 * Matches an eth type criterion object.
105 *
106 * @param criterion criterion to match
107 * @return true if the JSON matches the criterion, false otherwise.
108 */
109 private boolean matchCriterion(Criteria.EthTypeCriterion criterion) {
110 final int ethType = criterion.ethType();
111 final int jsonEthType = jsonCriterion.get("ethType").intValue();
112 if (ethType != jsonEthType) {
113 description.appendText("ethType was "
114 + Integer.toString(jsonEthType));
115 return false;
116 }
117 return true;
118 }
119
120 /**
121 * Matches a VLAN ID criterion object.
122 *
123 * @param criterion criterion to match
124 * @return true if the JSON matches the criterion, false otherwise.
125 */
126 private boolean matchCriterion(Criteria.VlanIdCriterion criterion) {
127 final short vlanId = criterion.vlanId().toShort();
128 final short jsonVlanId = jsonCriterion.get("vlanId").shortValue();
129 if (vlanId != jsonVlanId) {
130 description.appendText("vlanId was " + Short.toString(jsonVlanId));
131 return false;
132 }
133 return true;
134 }
135
136 /**
137 * Matches a VLAN PCP criterion object.
138 *
139 * @param criterion criterion to match
140 * @return true if the JSON matches the criterion, false otherwise.
141 */
142 private boolean matchCriterion(Criteria.VlanPcpCriterion criterion) {
143 final byte priority = criterion.priority();
144 final byte jsonPriority =
145 (byte) jsonCriterion.get("priority").shortValue();
146 if (priority != jsonPriority) {
147 description.appendText("priority was " + Byte.toString(jsonPriority));
148 return false;
149 }
150 return true;
151 }
152
153 /**
154 * Matches an IP DSCP criterion object.
155 *
156 * @param criterion criterion to match
157 * @return true if the JSON matches the criterion, false otherwise.
158 */
159 private boolean matchCriterion(Criteria.IPDscpCriterion criterion) {
160 final byte ipDscp = criterion.ipDscp();
161 final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue();
162 if (ipDscp != jsonIpDscp) {
163 description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp));
164 return false;
165 }
166 return true;
167 }
168
169 /**
170 * Matches an IP ECN criterion object.
171 *
172 * @param criterion criterion to match
173 * @return true if the JSON matches the criterion, false otherwise.
174 */
175 private boolean matchCriterion(Criteria.IPEcnCriterion criterion) {
176 final byte ipEcn = criterion.ipEcn();
177 final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue();
178 if (ipEcn != jsonIpEcn) {
179 description.appendText("IP ECN was " + Byte.toString(jsonIpEcn));
180 return false;
181 }
182 return true;
183 }
184
185 /**
186 * Matches an IP protocol criterion object.
187 *
188 * @param criterion criterion to match
189 * @return true if the JSON matches the criterion, false otherwise.
190 */
191 private boolean matchCriterion(Criteria.IPProtocolCriterion criterion) {
192 final short protocol = criterion.protocol();
193 final short jsonProtocol = jsonCriterion.get("protocol").shortValue();
194 if (protocol != jsonProtocol) {
195 description.appendText("protocol was "
196 + Short.toString(jsonProtocol));
197 return false;
198 }
199 return true;
200 }
201
202 /**
203 * Matches an IP address criterion object.
204 *
205 * @param criterion criterion to match
206 * @return true if the JSON matches the criterion, false otherwise.
207 */
208 private boolean matchCriterion(Criteria.IPCriterion criterion) {
209 final String ip = criterion.ip().toString();
210 final String jsonIp = jsonCriterion.get("ip").textValue();
211 if (!ip.equals(jsonIp)) {
212 description.appendText("ip was " + jsonIp);
213 return false;
214 }
215 return true;
216 }
217
218 /**
219 * Matches a TCP port criterion object.
220 *
221 * @param criterion criterion to match
222 * @return true if the JSON matches the criterion, false otherwise.
223 */
224 private boolean matchCriterion(Criteria.TcpPortCriterion criterion) {
225 final int tcpPort = criterion.tcpPort();
226 final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue();
227 if (tcpPort != jsonTcpPort) {
228 description.appendText("tcp port was "
229 + Integer.toString(jsonTcpPort));
230 return false;
231 }
232 return true;
233 }
234
235 /**
236 * Matches a UDP port criterion object.
237 *
238 * @param criterion criterion to match
239 * @return true if the JSON matches the criterion, false otherwise.
240 */
241 private boolean matchCriterion(Criteria.UdpPortCriterion criterion) {
242 final int udpPort = criterion.udpPort();
243 final int jsonUdpPort = jsonCriterion.get("udpPort").intValue();
244 if (udpPort != jsonUdpPort) {
245 description.appendText("udp port was "
246 + Integer.toString(jsonUdpPort));
247 return false;
248 }
249 return true;
250 }
251
252 /**
253 * Matches an SCTP port criterion object.
254 *
255 * @param criterion criterion to match
256 * @return true if the JSON matches the criterion, false otherwise.
257 */
258 private boolean matchCriterion(Criteria.SctpPortCriterion criterion) {
259 final int sctpPort = criterion.sctpPort();
260 final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue();
261 if (sctpPort != jsonSctpPort) {
262 description.appendText("sctp port was "
263 + Integer.toString(jsonSctpPort));
264 return false;
265 }
266 return true;
267 }
268
269 /**
270 * Matches an ICMP type criterion object.
271 *
272 * @param criterion criterion to match
273 * @return true if the JSON matches the criterion, false otherwise.
274 */
275 private boolean matchCriterion(Criteria.IcmpTypeCriterion criterion) {
276 final short icmpType = criterion.icmpType();
277 final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue();
278 if (icmpType != jsonIcmpType) {
279 description.appendText("icmp type was "
280 + Short.toString(jsonIcmpType));
281 return false;
282 }
283 return true;
284 }
285
286 /**
287 * Matches an ICMP code criterion object.
288 *
289 * @param criterion criterion to match
290 * @return true if the JSON matches the criterion, false otherwise.
291 */
292 private boolean matchCriterion(Criteria.IcmpCodeCriterion criterion) {
293 final short icmpCode = criterion.icmpCode();
294 final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue();
295 if (icmpCode != jsonIcmpCode) {
296 description.appendText("icmp code was "
297 + Short.toString(jsonIcmpCode));
298 return false;
299 }
300 return true;
301 }
302
303 /**
304 * Matches an IPV6 flow label criterion object.
305 *
306 * @param criterion criterion to match
307 * @return true if the JSON matches the criterion, false otherwise.
308 */
309 private boolean matchCriterion(Criteria.IPv6FlowLabelCriterion criterion) {
310 final int flowLabel = criterion.flowLabel();
311 final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue();
312 if (flowLabel != jsonFlowLabel) {
313 description.appendText("IPv6 flow label was "
314 + Integer.toString(jsonFlowLabel));
315 return false;
316 }
317 return true;
318 }
319
320 /**
321 * Matches an ICMP V6 type criterion object.
322 *
323 * @param criterion criterion to match
324 * @return true if the JSON matches the criterion, false otherwise.
325 */
326 private boolean matchCriterion(Criteria.Icmpv6TypeCriterion criterion) {
327 final short icmpv6Type = criterion.icmpv6Type();
328 final short jsonIcmpv6Type =
329 jsonCriterion.get("icmpv6Type").shortValue();
330 if (icmpv6Type != jsonIcmpv6Type) {
331 description.appendText("icmpv6 type was "
332 + Short.toString(jsonIcmpv6Type));
333 return false;
334 }
335 return true;
336 }
337
338 /**
339 * Matches an IPV6 code criterion object.
340 *
341 * @param criterion criterion to match
342 * @return true if the JSON matches the criterion, false otherwise.
343 */
344 private boolean matchCriterion(Criteria.Icmpv6CodeCriterion criterion) {
345 final short icmpv6Code = criterion.icmpv6Code();
346 final short jsonIcmpv6Code =
347 jsonCriterion.get("icmpv6Code").shortValue();
348 if (icmpv6Code != jsonIcmpv6Code) {
349 description.appendText("icmpv6 code was "
350 + Short.toString(jsonIcmpv6Code));
351 return false;
352 }
353 return true;
354 }
355
356 /**
357 * Matches an IPV6 ND target criterion object.
358 *
359 * @param criterion criterion to match
360 * @return true if the JSON matches the criterion, false otherwise.
361 */
362 private boolean matchCriterion(Criteria.IPv6NDTargetAddressCriterion criterion) {
363 final String targetAddress =
364 criterion.targetAddress().toString();
365 final String jsonTargetAddress =
366 jsonCriterion.get("targetAddress").textValue();
367 if (!targetAddress.equals(jsonTargetAddress)) {
368 description.appendText("target address was " +
369 jsonTargetAddress);
370 return false;
371 }
372 return true;
373 }
374
375 /**
376 * Matches an IPV6 ND link layer criterion object.
377 *
378 * @param criterion criterion to match
379 * @return true if the JSON matches the criterion, false otherwise.
380 */
381 private boolean matchCriterion(Criteria.IPv6NDLinkLayerAddressCriterion criterion) {
382 final String llAddress =
383 criterion.mac().toString();
384 final String jsonLlAddress =
385 jsonCriterion.get("mac").textValue();
386 if (!llAddress.equals(jsonLlAddress)) {
387 description.appendText("mac was " + jsonLlAddress);
388 return false;
389 }
390 return true;
391 }
392
393 /**
394 * Matches an MPLS label criterion object.
395 *
396 * @param criterion criterion to match
397 * @return true if the JSON matches the criterion, false otherwise.
398 */
399 private boolean matchCriterion(Criteria.MplsCriterion criterion) {
Michele Santuari4b6019e2014-12-19 11:31:45 +0100400 final int label = criterion.label().toInt();
Ray Milkey46670a82015-02-08 17:57:17 -0800401 final int jsonLabel = jsonCriterion.get("label").intValue();
402 if (label != jsonLabel) {
403 description.appendText("label was " + Integer.toString(jsonLabel));
404 return false;
405 }
406 return true;
407 }
408
409 /**
410 * Matches an IPV6 exthdr criterion object.
411 *
412 * @param criterion criterion to match
413 * @return true if the JSON matches the criterion, false otherwise.
414 */
415 private boolean matchCriterion(Criteria.IPv6ExthdrFlagsCriterion criterion) {
416 final int exthdrFlags = criterion.exthdrFlags();
417 final int jsonExthdrFlags =
418 jsonCriterion.get("exthdrFlags").intValue();
419 if (exthdrFlags != jsonExthdrFlags) {
420 description.appendText("exthdrFlags was "
421 + Long.toHexString(jsonExthdrFlags));
422 return false;
423 }
424 return true;
425 }
426
427 /**
428 * Matches a lambda criterion object.
429 *
430 * @param criterion criterion to match
431 * @return true if the JSON matches the criterion, false otherwise.
432 */
433 private boolean matchCriterion(Criteria.LambdaCriterion criterion) {
434 final int lambda = criterion.lambda();
435 final int jsonLambda = jsonCriterion.get("lambda").intValue();
436 if (lambda != jsonLambda) {
437 description.appendText("lambda was " + Integer.toString(lambda));
438 return false;
439 }
440 return true;
441 }
442
443 /**
444 * Matches an optical signal type criterion object.
445 *
446 * @param criterion criterion to match
447 * @return true if the JSON matches the criterion, false otherwise.
448 */
449 private boolean matchCriterion(Criteria.OpticalSignalTypeCriterion criterion) {
450 final short signalType = criterion.signalType();
451 final short jsonSignalType = jsonCriterion.get("signalType").shortValue();
452 if (signalType != jsonSignalType) {
453 description.appendText("signal type was " + Short.toString(signalType));
454 return false;
455 }
456 return true;
457 }
458
Ray Milkeydb358082015-01-13 16:34:38 -0800459 @Override
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800460 public boolean matchesSafely(JsonNode jsonCriterion,
461 Description description) {
Ray Milkey46670a82015-02-08 17:57:17 -0800462 this.description = description;
463 this.jsonCriterion = jsonCriterion;
Ray Milkeydb358082015-01-13 16:34:38 -0800464 final String type = criterion.type().name();
465 final String jsonType = jsonCriterion.get("type").asText();
466 if (!type.equals(jsonType)) {
467 description.appendText("type was " + type);
468 return false;
469 }
470
471 switch (criterion.type()) {
472
473 case IN_PORT:
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800474 case IN_PHY_PORT:
Ray Milkey46670a82015-02-08 17:57:17 -0800475 return matchCriterion((Criteria.PortCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800476
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800477 case METADATA:
Ray Milkey46670a82015-02-08 17:57:17 -0800478 return matchCriterion((Criteria.MetadataCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800479
Ray Milkeydb358082015-01-13 16:34:38 -0800480 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800481 case ETH_SRC:
Ray Milkey46670a82015-02-08 17:57:17 -0800482 return matchCriterion((Criteria.EthCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800483
484 case ETH_TYPE:
Ray Milkey46670a82015-02-08 17:57:17 -0800485 return matchCriterion((Criteria.EthTypeCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800486
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800487 case VLAN_VID:
Ray Milkey46670a82015-02-08 17:57:17 -0800488 return matchCriterion((Criteria.VlanIdCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800489
490 case VLAN_PCP:
Ray Milkey46670a82015-02-08 17:57:17 -0800491 return matchCriterion((Criteria.VlanPcpCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800492
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800493 case IP_DSCP:
Ray Milkey46670a82015-02-08 17:57:17 -0800494 return matchCriterion((Criteria.IPDscpCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800495
496 case IP_ECN:
Ray Milkey46670a82015-02-08 17:57:17 -0800497 return matchCriterion((Criteria.IPEcnCriterion) criterion);
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800498
Ray Milkeydb358082015-01-13 16:34:38 -0800499 case IP_PROTO:
Ray Milkey46670a82015-02-08 17:57:17 -0800500 return matchCriterion((Criteria.IPProtocolCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800501
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800502 case IPV4_SRC:
503 case IPV4_DST:
504 case IPV6_SRC:
505 case IPV6_DST:
Ray Milkey46670a82015-02-08 17:57:17 -0800506 return matchCriterion((Criteria.IPCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800507
508 case TCP_SRC:
509 case TCP_DST:
Ray Milkey46670a82015-02-08 17:57:17 -0800510 return matchCriterion((Criteria.TcpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800511
512 case UDP_SRC:
513 case UDP_DST:
Ray Milkey46670a82015-02-08 17:57:17 -0800514 return matchCriterion((Criteria.UdpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800515
516 case SCTP_SRC:
517 case SCTP_DST:
Ray Milkey46670a82015-02-08 17:57:17 -0800518 return matchCriterion((Criteria.SctpPortCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800519
520 case ICMPV4_TYPE:
Ray Milkey46670a82015-02-08 17:57:17 -0800521 return matchCriterion((Criteria.IcmpTypeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800522
523 case ICMPV4_CODE:
Ray Milkey46670a82015-02-08 17:57:17 -0800524 return matchCriterion((Criteria.IcmpCodeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800525
526 case IPV6_FLABEL:
Ray Milkey46670a82015-02-08 17:57:17 -0800527 return matchCriterion((Criteria.IPv6FlowLabelCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800528
529 case ICMPV6_TYPE:
Ray Milkey46670a82015-02-08 17:57:17 -0800530 return matchCriterion((Criteria.Icmpv6TypeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800531
532 case ICMPV6_CODE:
Ray Milkey46670a82015-02-08 17:57:17 -0800533 return matchCriterion((Criteria.Icmpv6CodeCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800534
535 case IPV6_ND_TARGET:
Ray Milkey46670a82015-02-08 17:57:17 -0800536 return matchCriterion(
537 (Criteria.IPv6NDTargetAddressCriterion) criterion);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800538
539 case IPV6_ND_SLL:
540 case IPV6_ND_TLL:
Ray Milkey46670a82015-02-08 17:57:17 -0800541 return matchCriterion(
542 (Criteria.IPv6NDLinkLayerAddressCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800543
544 case MPLS_LABEL:
Ray Milkey46670a82015-02-08 17:57:17 -0800545 return matchCriterion((Criteria.MplsCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800546
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800547 case IPV6_EXTHDR:
Ray Milkey46670a82015-02-08 17:57:17 -0800548 return matchCriterion(
549 (Criteria.IPv6ExthdrFlagsCriterion) criterion);
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800550
Ray Milkeydb358082015-01-13 16:34:38 -0800551 case OCH_SIGID:
Ray Milkey46670a82015-02-08 17:57:17 -0800552 return matchCriterion((Criteria.LambdaCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800553
554 case OCH_SIGTYPE:
Ray Milkey46670a82015-02-08 17:57:17 -0800555 return matchCriterion(
556 (Criteria.OpticalSignalTypeCriterion) criterion);
Ray Milkeydb358082015-01-13 16:34:38 -0800557
558 default:
559 // Don't know how to format this type
560 description.appendText("unknown criterion type " +
561 criterion.type());
562 return false;
563 }
Ray Milkeydb358082015-01-13 16:34:38 -0800564 }
565
566 @Override
567 public void describeTo(Description description) {
568 description.appendText(criterion.toString());
569 }
Ray Milkeydb358082015-01-13 16:34:38 -0800570}