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