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