Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import org.hamcrest.Description; |
| 19 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 20 | import org.onosproject.net.flow.criteria.Criterion; |
| 21 | |
| 22 | import com.fasterxml.jackson.databind.JsonNode; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 24 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 25 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 26 | import org.onosproject.net.flow.criteria.IPDscpCriterion; |
| 27 | import org.onosproject.net.flow.criteria.IPEcnCriterion; |
| 28 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 29 | import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; |
| 30 | import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; |
| 31 | import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; |
| 32 | import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; |
| 33 | import org.onosproject.net.flow.criteria.IcmpCodeCriterion; |
| 34 | import org.onosproject.net.flow.criteria.IcmpTypeCriterion; |
| 35 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 36 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
| 37 | import org.onosproject.net.flow.criteria.LambdaCriterion; |
| 38 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
| 39 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 42 | import org.onosproject.net.flow.criteria.SctpPortCriterion; |
| 43 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 44 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 45 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 46 | import org.onosproject.net.flow.criteria.VlanPcpCriterion; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Hamcrest matcher for criterion objects. |
| 50 | */ |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 51 | public final class CriterionJsonMatcher extends |
| 52 | TypeSafeDiagnosingMatcher<JsonNode> { |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 53 | |
| 54 | final Criterion criterion; |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 55 | Description description; |
| 56 | JsonNode jsonCriterion; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 57 | |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Constructs a matcher object. |
| 60 | * |
| 61 | * @param criterionValue criterion to match |
| 62 | */ |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 63 | private CriterionJsonMatcher(Criterion criterionValue) { |
| 64 | criterion = criterionValue; |
| 65 | } |
| 66 | |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Factory to allocate an criterion matcher. |
| 69 | * |
| 70 | * @param criterion criterion object we are looking for |
| 71 | * @return matcher |
| 72 | */ |
| 73 | public static CriterionJsonMatcher matchesCriterion(Criterion criterion) { |
| 74 | return new CriterionJsonMatcher(criterion); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Matches a port criterion object. |
| 79 | * |
| 80 | * @param criterion criterion to match |
| 81 | * @return true if the JSON matches the criterion, false otherwise. |
| 82 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 83 | private boolean matchCriterion(PortCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 84 | final long port = criterion.port().toLong(); |
| 85 | final long jsonPort = jsonCriterion.get("port").asLong(); |
| 86 | if (port != jsonPort) { |
| 87 | description.appendText("port was " + Long.toString(jsonPort)); |
| 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Matches a metadata criterion object. |
| 95 | * |
| 96 | * @param criterion criterion to match |
| 97 | * @return true if the JSON matches the criterion, false otherwise. |
| 98 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 99 | private boolean matchCriterion(MetadataCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 100 | final long metadata = criterion.metadata(); |
| 101 | final long jsonMetadata = jsonCriterion.get("metadata").asLong(); |
| 102 | if (metadata != jsonMetadata) { |
| 103 | description.appendText("metadata was " |
| 104 | + Long.toString(jsonMetadata)); |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Matches an eth criterion object. |
| 112 | * |
| 113 | * @param criterion criterion to match |
| 114 | * @return true if the JSON matches the criterion, false otherwise. |
| 115 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 116 | private boolean matchCriterion(EthCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 117 | final String mac = criterion.mac().toString(); |
| 118 | final String jsonMac = jsonCriterion.get("mac").textValue(); |
| 119 | if (!mac.equals(jsonMac)) { |
| 120 | description.appendText("mac was " + jsonMac); |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Matches an eth type criterion object. |
| 128 | * |
| 129 | * @param criterion criterion to match |
| 130 | * @return true if the JSON matches the criterion, false otherwise. |
| 131 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 132 | private boolean matchCriterion(EthTypeCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 133 | final int ethType = criterion.ethType(); |
| 134 | final int jsonEthType = jsonCriterion.get("ethType").intValue(); |
| 135 | if (ethType != jsonEthType) { |
| 136 | description.appendText("ethType was " |
| 137 | + Integer.toString(jsonEthType)); |
| 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Matches a VLAN ID criterion object. |
| 145 | * |
| 146 | * @param criterion criterion to match |
| 147 | * @return true if the JSON matches the criterion, false otherwise. |
| 148 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 149 | private boolean matchCriterion(VlanIdCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 150 | final short vlanId = criterion.vlanId().toShort(); |
| 151 | final short jsonVlanId = jsonCriterion.get("vlanId").shortValue(); |
| 152 | if (vlanId != jsonVlanId) { |
| 153 | description.appendText("vlanId was " + Short.toString(jsonVlanId)); |
| 154 | return false; |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Matches a VLAN PCP criterion object. |
| 161 | * |
| 162 | * @param criterion criterion to match |
| 163 | * @return true if the JSON matches the criterion, false otherwise. |
| 164 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 165 | private boolean matchCriterion(VlanPcpCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 166 | final byte priority = criterion.priority(); |
| 167 | final byte jsonPriority = |
| 168 | (byte) jsonCriterion.get("priority").shortValue(); |
| 169 | if (priority != jsonPriority) { |
| 170 | description.appendText("priority was " + Byte.toString(jsonPriority)); |
| 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Matches an IP DSCP criterion object. |
| 178 | * |
| 179 | * @param criterion criterion to match |
| 180 | * @return true if the JSON matches the criterion, false otherwise. |
| 181 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 182 | private boolean matchCriterion(IPDscpCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 183 | final byte ipDscp = criterion.ipDscp(); |
| 184 | final byte jsonIpDscp = (byte) jsonCriterion.get("ipDscp").shortValue(); |
| 185 | if (ipDscp != jsonIpDscp) { |
| 186 | description.appendText("IP DSCP was " + Byte.toString(jsonIpDscp)); |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Matches an IP ECN criterion object. |
| 194 | * |
| 195 | * @param criterion criterion to match |
| 196 | * @return true if the JSON matches the criterion, false otherwise. |
| 197 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 198 | private boolean matchCriterion(IPEcnCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 199 | final byte ipEcn = criterion.ipEcn(); |
| 200 | final byte jsonIpEcn = (byte) jsonCriterion.get("ipEcn").shortValue(); |
| 201 | if (ipEcn != jsonIpEcn) { |
| 202 | description.appendText("IP ECN was " + Byte.toString(jsonIpEcn)); |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Matches an IP protocol criterion object. |
| 210 | * |
| 211 | * @param criterion criterion to match |
| 212 | * @return true if the JSON matches the criterion, false otherwise. |
| 213 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 214 | private boolean matchCriterion(IPProtocolCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 215 | final short protocol = criterion.protocol(); |
| 216 | final short jsonProtocol = jsonCriterion.get("protocol").shortValue(); |
| 217 | if (protocol != jsonProtocol) { |
| 218 | description.appendText("protocol was " |
| 219 | + Short.toString(jsonProtocol)); |
| 220 | return false; |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Matches an IP address criterion object. |
| 227 | * |
| 228 | * @param criterion criterion to match |
| 229 | * @return true if the JSON matches the criterion, false otherwise. |
| 230 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 231 | private boolean matchCriterion(IPCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 232 | final String ip = criterion.ip().toString(); |
| 233 | final String jsonIp = jsonCriterion.get("ip").textValue(); |
| 234 | if (!ip.equals(jsonIp)) { |
| 235 | description.appendText("ip was " + jsonIp); |
| 236 | return false; |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Matches a TCP port criterion object. |
| 243 | * |
| 244 | * @param criterion criterion to match |
| 245 | * @return true if the JSON matches the criterion, false otherwise. |
| 246 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 247 | private boolean matchCriterion(TcpPortCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 248 | final int tcpPort = criterion.tcpPort(); |
| 249 | final int jsonTcpPort = jsonCriterion.get("tcpPort").intValue(); |
| 250 | if (tcpPort != jsonTcpPort) { |
| 251 | description.appendText("tcp port was " |
| 252 | + Integer.toString(jsonTcpPort)); |
| 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Matches a UDP port criterion object. |
| 260 | * |
| 261 | * @param criterion criterion to match |
| 262 | * @return true if the JSON matches the criterion, false otherwise. |
| 263 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 264 | private boolean matchCriterion(UdpPortCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 265 | final int udpPort = criterion.udpPort(); |
| 266 | final int jsonUdpPort = jsonCriterion.get("udpPort").intValue(); |
| 267 | if (udpPort != jsonUdpPort) { |
| 268 | description.appendText("udp port was " |
| 269 | + Integer.toString(jsonUdpPort)); |
| 270 | return false; |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Matches an SCTP port criterion object. |
| 277 | * |
| 278 | * @param criterion criterion to match |
| 279 | * @return true if the JSON matches the criterion, false otherwise. |
| 280 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 281 | private boolean matchCriterion(SctpPortCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 282 | final int sctpPort = criterion.sctpPort(); |
| 283 | final int jsonSctpPort = jsonCriterion.get("sctpPort").intValue(); |
| 284 | if (sctpPort != jsonSctpPort) { |
| 285 | description.appendText("sctp port was " |
| 286 | + Integer.toString(jsonSctpPort)); |
| 287 | return false; |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Matches an ICMP type criterion object. |
| 294 | * |
| 295 | * @param criterion criterion to match |
| 296 | * @return true if the JSON matches the criterion, false otherwise. |
| 297 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 298 | private boolean matchCriterion(IcmpTypeCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 299 | final short icmpType = criterion.icmpType(); |
| 300 | final short jsonIcmpType = jsonCriterion.get("icmpType").shortValue(); |
| 301 | if (icmpType != jsonIcmpType) { |
| 302 | description.appendText("icmp type was " |
| 303 | + Short.toString(jsonIcmpType)); |
| 304 | return false; |
| 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Matches an ICMP code criterion object. |
| 311 | * |
| 312 | * @param criterion criterion to match |
| 313 | * @return true if the JSON matches the criterion, false otherwise. |
| 314 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 315 | private boolean matchCriterion(IcmpCodeCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 316 | final short icmpCode = criterion.icmpCode(); |
| 317 | final short jsonIcmpCode = jsonCriterion.get("icmpCode").shortValue(); |
| 318 | if (icmpCode != jsonIcmpCode) { |
| 319 | description.appendText("icmp code was " |
| 320 | + Short.toString(jsonIcmpCode)); |
| 321 | return false; |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Matches an IPV6 flow label criterion object. |
| 328 | * |
| 329 | * @param criterion criterion to match |
| 330 | * @return true if the JSON matches the criterion, false otherwise. |
| 331 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 332 | private boolean matchCriterion(IPv6FlowLabelCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 333 | final int flowLabel = criterion.flowLabel(); |
| 334 | final int jsonFlowLabel = jsonCriterion.get("flowLabel").intValue(); |
| 335 | if (flowLabel != jsonFlowLabel) { |
| 336 | description.appendText("IPv6 flow label was " |
| 337 | + Integer.toString(jsonFlowLabel)); |
| 338 | return false; |
| 339 | } |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Matches an ICMP V6 type criterion object. |
| 345 | * |
| 346 | * @param criterion criterion to match |
| 347 | * @return true if the JSON matches the criterion, false otherwise. |
| 348 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 349 | private boolean matchCriterion(Icmpv6TypeCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 350 | final short icmpv6Type = criterion.icmpv6Type(); |
| 351 | final short jsonIcmpv6Type = |
| 352 | jsonCriterion.get("icmpv6Type").shortValue(); |
| 353 | if (icmpv6Type != jsonIcmpv6Type) { |
| 354 | description.appendText("icmpv6 type was " |
| 355 | + Short.toString(jsonIcmpv6Type)); |
| 356 | return false; |
| 357 | } |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Matches an IPV6 code criterion object. |
| 363 | * |
| 364 | * @param criterion criterion to match |
| 365 | * @return true if the JSON matches the criterion, false otherwise. |
| 366 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 367 | private boolean matchCriterion(Icmpv6CodeCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 368 | final short icmpv6Code = criterion.icmpv6Code(); |
| 369 | final short jsonIcmpv6Code = |
| 370 | jsonCriterion.get("icmpv6Code").shortValue(); |
| 371 | if (icmpv6Code != jsonIcmpv6Code) { |
| 372 | description.appendText("icmpv6 code was " |
| 373 | + Short.toString(jsonIcmpv6Code)); |
| 374 | return false; |
| 375 | } |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Matches an IPV6 ND target criterion object. |
| 381 | * |
| 382 | * @param criterion criterion to match |
| 383 | * @return true if the JSON matches the criterion, false otherwise. |
| 384 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 385 | private boolean matchCriterion(IPv6NDTargetAddressCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 386 | final String targetAddress = |
| 387 | criterion.targetAddress().toString(); |
| 388 | final String jsonTargetAddress = |
| 389 | jsonCriterion.get("targetAddress").textValue(); |
| 390 | if (!targetAddress.equals(jsonTargetAddress)) { |
| 391 | description.appendText("target address was " + |
| 392 | jsonTargetAddress); |
| 393 | return false; |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Matches an IPV6 ND link layer criterion object. |
| 400 | * |
| 401 | * @param criterion criterion to match |
| 402 | * @return true if the JSON matches the criterion, false otherwise. |
| 403 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 404 | private boolean matchCriterion(IPv6NDLinkLayerAddressCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 405 | final String llAddress = |
| 406 | criterion.mac().toString(); |
| 407 | final String jsonLlAddress = |
| 408 | jsonCriterion.get("mac").textValue(); |
| 409 | if (!llAddress.equals(jsonLlAddress)) { |
| 410 | description.appendText("mac was " + jsonLlAddress); |
| 411 | return false; |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Matches an MPLS label criterion object. |
| 418 | * |
| 419 | * @param criterion criterion to match |
| 420 | * @return true if the JSON matches the criterion, false otherwise. |
| 421 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 422 | private boolean matchCriterion(MplsCriterion criterion) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 423 | final int label = criterion.label().toInt(); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 424 | final int jsonLabel = jsonCriterion.get("label").intValue(); |
| 425 | if (label != jsonLabel) { |
| 426 | description.appendText("label was " + Integer.toString(jsonLabel)); |
| 427 | return false; |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Matches an IPV6 exthdr criterion object. |
| 434 | * |
| 435 | * @param criterion criterion to match |
| 436 | * @return true if the JSON matches the criterion, false otherwise. |
| 437 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 438 | private boolean matchCriterion(IPv6ExthdrFlagsCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 439 | final int exthdrFlags = criterion.exthdrFlags(); |
| 440 | final int jsonExthdrFlags = |
| 441 | jsonCriterion.get("exthdrFlags").intValue(); |
| 442 | if (exthdrFlags != jsonExthdrFlags) { |
| 443 | description.appendText("exthdrFlags was " |
| 444 | + Long.toHexString(jsonExthdrFlags)); |
| 445 | return false; |
| 446 | } |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Matches a lambda criterion object. |
| 452 | * |
| 453 | * @param criterion criterion to match |
| 454 | * @return true if the JSON matches the criterion, false otherwise. |
| 455 | */ |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 456 | private boolean matchCriterion(LambdaCriterion criterion) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 457 | final int lambda = criterion.lambda(); |
| 458 | final int jsonLambda = jsonCriterion.get("lambda").intValue(); |
| 459 | if (lambda != jsonLambda) { |
| 460 | description.appendText("lambda was " + Integer.toString(lambda)); |
| 461 | return false; |
| 462 | } |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Matches an optical signal type criterion object. |
| 468 | * |
| 469 | * @param criterion criterion to match |
| 470 | * @return true if the JSON matches the criterion, false otherwise. |
| 471 | */ |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 472 | private boolean matchCriterion(OchSignalTypeCriterion criterion) { |
| 473 | final String signalType = criterion.signalType().name(); |
| 474 | final String jsonSignalType = jsonCriterion.get("ochSignalType").textValue(); |
| 475 | if (!signalType.equals(jsonSignalType)) { |
| 476 | description.appendText("signal type was " + signalType); |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 477 | return false; |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 482 | @Override |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 483 | public boolean matchesSafely(JsonNode jsonCriterion, |
| 484 | Description description) { |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 485 | this.description = description; |
| 486 | this.jsonCriterion = jsonCriterion; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 487 | final String type = criterion.type().name(); |
| 488 | final String jsonType = jsonCriterion.get("type").asText(); |
| 489 | if (!type.equals(jsonType)) { |
| 490 | description.appendText("type was " + type); |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | switch (criterion.type()) { |
| 495 | |
| 496 | case IN_PORT: |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 497 | case IN_PHY_PORT: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 498 | return matchCriterion((PortCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 499 | |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 500 | case METADATA: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 501 | return matchCriterion((MetadataCriterion) criterion); |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 502 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 503 | case ETH_DST: |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 504 | case ETH_SRC: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 505 | return matchCriterion((EthCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 506 | |
| 507 | case ETH_TYPE: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 508 | return matchCriterion((EthTypeCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 509 | |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 510 | case VLAN_VID: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 511 | return matchCriterion((VlanIdCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 512 | |
| 513 | case VLAN_PCP: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 514 | return matchCriterion((VlanPcpCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 515 | |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 516 | case IP_DSCP: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 517 | return matchCriterion((IPDscpCriterion) criterion); |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 518 | |
| 519 | case IP_ECN: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 520 | return matchCriterion((IPEcnCriterion) criterion); |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 521 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 522 | case IP_PROTO: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 523 | return matchCriterion((IPProtocolCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 524 | |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 525 | case IPV4_SRC: |
| 526 | case IPV4_DST: |
| 527 | case IPV6_SRC: |
| 528 | case IPV6_DST: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 529 | return matchCriterion((IPCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 530 | |
| 531 | case TCP_SRC: |
| 532 | case TCP_DST: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 533 | return matchCriterion((TcpPortCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 534 | |
| 535 | case UDP_SRC: |
| 536 | case UDP_DST: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 537 | return matchCriterion((UdpPortCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 538 | |
| 539 | case SCTP_SRC: |
| 540 | case SCTP_DST: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 541 | return matchCriterion((SctpPortCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 542 | |
| 543 | case ICMPV4_TYPE: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 544 | return matchCriterion((IcmpTypeCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 545 | |
| 546 | case ICMPV4_CODE: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 547 | return matchCriterion((IcmpCodeCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 548 | |
| 549 | case IPV6_FLABEL: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 550 | return matchCriterion((IPv6FlowLabelCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 551 | |
| 552 | case ICMPV6_TYPE: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 553 | return matchCriterion((Icmpv6TypeCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 554 | |
| 555 | case ICMPV6_CODE: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 556 | return matchCriterion((Icmpv6CodeCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 557 | |
| 558 | case IPV6_ND_TARGET: |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 559 | return matchCriterion( |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 560 | (IPv6NDTargetAddressCriterion) criterion); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 561 | |
| 562 | case IPV6_ND_SLL: |
| 563 | case IPV6_ND_TLL: |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 564 | return matchCriterion( |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 565 | (IPv6NDLinkLayerAddressCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 566 | |
| 567 | case MPLS_LABEL: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 568 | return matchCriterion((MplsCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 569 | |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 570 | case IPV6_EXTHDR: |
Ray Milkey | 46670a8 | 2015-02-08 17:57:17 -0800 | [diff] [blame] | 571 | return matchCriterion( |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 572 | (IPv6ExthdrFlagsCriterion) criterion); |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 573 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 574 | case OCH_SIGID: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 575 | return matchCriterion((LambdaCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 576 | |
| 577 | case OCH_SIGTYPE: |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 578 | return matchCriterion((OchSignalTypeCriterion) criterion); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 579 | |
| 580 | default: |
| 581 | // Don't know how to format this type |
| 582 | description.appendText("unknown criterion type " + |
| 583 | criterion.type()); |
| 584 | return false; |
| 585 | } |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public void describeTo(Description description) { |
| 590 | description.appendText(criterion.toString()); |
| 591 | } |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 592 | } |