Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -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 | |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 19 | import org.hamcrest.Description; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 21 | import org.onlab.util.HexString; |
| 22 | import org.onosproject.net.OduSignalId; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.instructions.Instruction; |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame^] | 25 | import org.onosproject.net.flow.instructions.Instructions.MeterInstruction; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction; |
| 27 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 28 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
| 29 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction; |
| 30 | import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction; |
| 31 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 32 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
| 33 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 34 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
| 35 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions; |
| 36 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
| 37 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 38 | |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Hamcrest matcher for instructions. |
| 41 | */ |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 42 | public final class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 43 | |
| 44 | private final Instruction instruction; |
| 45 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 46 | private InstructionJsonMatcher(Instruction instructionValue) { |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 47 | instruction = instructionValue; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Matches the contents of a push header instruction. |
| 52 | * |
| 53 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 54 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 55 | * @return true if contents match, false otherwise |
| 56 | */ |
| 57 | private boolean matchPushHeaderInstruction(JsonNode instructionJson, |
| 58 | Description description) { |
| 59 | PushHeaderInstructions instructionToMatch = |
| 60 | (PushHeaderInstructions) instruction; |
| 61 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 62 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 63 | description.appendText("subtype was " + jsonSubtype); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | final String jsonType = instructionJson.get("type").textValue(); |
| 68 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 69 | description.appendText("type was " + jsonType); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | final JsonNode ethJson = instructionJson.get("ethernetType"); |
| 74 | if (ethJson == null) { |
| 75 | description.appendText("ethernetType was not null"); |
| 76 | return false; |
| 77 | } |
| 78 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 79 | if (instructionToMatch.ethernetType().toShort() != ethJson.asInt()) { |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 80 | description.appendText("ethernetType was " + ethJson); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 81 | return false; |
| 82 | } |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 83 | |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Matches the contents of an output instruction. |
| 89 | * |
| 90 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 91 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 92 | * @return true if contents match, false otherwise |
| 93 | */ |
| 94 | private boolean matchOutputInstruction(JsonNode instructionJson, |
| 95 | Description description) { |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 96 | final String jsonType = instructionJson.get("type").textValue(); |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 97 | OutputInstruction instructionToMatch = (OutputInstruction) instruction; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 98 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 99 | description.appendText("type was " + jsonType); |
| 100 | return false; |
| 101 | } |
| 102 | |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 103 | if (instructionJson.get("port").isLong() || |
| 104 | instructionJson.get("port").isInt()) { |
| 105 | final long jsonPort = instructionJson.get("port").asLong(); |
| 106 | if (instructionToMatch.port().toLong() != (jsonPort)) { |
| 107 | description.appendText("port was " + jsonPort); |
| 108 | return false; |
| 109 | } |
| 110 | } else if (instructionJson.get("port").isTextual()) { |
| 111 | final String jsonPort = instructionJson.get("port").textValue(); |
| 112 | if (!instructionToMatch.port().toString().equals(jsonPort)) { |
| 113 | description.appendText("port was " + jsonPort); |
| 114 | return false; |
| 115 | } |
| 116 | } else { |
| 117 | final String jsonPort = instructionJson.get("port").toString(); |
| 118 | description.appendText("Unmathcing types "); |
| 119 | description.appendText("instructionToMatch " + instructionToMatch.port().toString()); |
| 120 | description.appendText("jsonPort " + jsonPort); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /** |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 127 | * Matches the contents of a group instruction. |
| 128 | * |
| 129 | * @param instructionJson JSON instruction to match |
| 130 | * @param description Description object used for recording errors |
| 131 | * @return true if contents match, false otherwise |
| 132 | */ |
| 133 | private boolean matchGroupInstruction(JsonNode instructionJson, |
| 134 | Description description) { |
| 135 | final String jsonType = instructionJson.get("type").textValue(); |
| 136 | GroupInstruction instructionToMatch = (GroupInstruction) instruction; |
| 137 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 138 | description.appendText("type was " + jsonType); |
| 139 | return false; |
| 140 | } |
| 141 | |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame^] | 142 | final int jsonGroupId = instructionJson.get("groupId").intValue(); |
| 143 | if (instructionToMatch.groupId().id() != jsonGroupId) { |
| 144 | description.appendText("groupId was " + jsonGroupId); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Matches the contents of a meter instruction. |
| 153 | * |
| 154 | * @param instructionJson JSON instruction to match |
| 155 | * @param description Description object used for recording errors |
| 156 | * @return true if contents match, false otherwise |
| 157 | */ |
| 158 | private boolean matchMeterInstruction(JsonNode instructionJson, |
| 159 | Description description) { |
| 160 | final String jsonType = instructionJson.get("type").textValue(); |
| 161 | MeterInstruction instructionToMatch = (MeterInstruction) instruction; |
| 162 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 163 | description.appendText("type was " + jsonType); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | final long jsonMeterId = instructionJson.get("meterId").longValue(); |
| 168 | if (instructionToMatch.meterId().id() != jsonMeterId) { |
| 169 | description.appendText("meterId was " + jsonMeterId); |
| 170 | return false; |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | /** |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 177 | * Matches the contents of a mod lambda instruction. |
| 178 | * |
| 179 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 180 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 181 | * @return true if contents match, false otherwise |
| 182 | */ |
| 183 | private boolean matchModLambdaInstruction(JsonNode instructionJson, |
| 184 | Description description) { |
| 185 | ModLambdaInstruction instructionToMatch = |
| 186 | (ModLambdaInstruction) instruction; |
| 187 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 188 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 189 | description.appendText("subtype was " + jsonSubtype); |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | final String jsonType = instructionJson.get("type").textValue(); |
| 194 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 195 | description.appendText("type was " + jsonType); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | final long jsonLambda = instructionJson.get("lambda").shortValue(); |
| 200 | if (instructionToMatch.lambda() != jsonLambda) { |
| 201 | description.appendText("lambda was " + jsonLambda); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 209 | * Matches the contents of a mod OCh singal instruction. |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 210 | * |
| 211 | * @param instructionJson JSON instruction to match |
| 212 | * @param description Description object used for recording errors |
| 213 | * @return true if contents matches, false otherwise |
| 214 | */ |
| 215 | private boolean matchModOchSingalInstruction(JsonNode instructionJson, |
| 216 | Description description) { |
| 217 | ModOchSignalInstruction instructionToMatch = |
| 218 | (ModOchSignalInstruction) instruction; |
| 219 | |
| 220 | String jsonSubType = instructionJson.get("subtype").textValue(); |
| 221 | if (!instructionToMatch.subtype().name().equals(jsonSubType)) { |
| 222 | description.appendText("subtype was " + jsonSubType); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | String jsonType = instructionJson.get("type").textValue(); |
| 227 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 228 | description.appendText("type was " + jsonType); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | String jsonGridType = instructionJson.get("gridType").textValue(); |
| 233 | if (!instructionToMatch.lambda().gridType().name().equals(jsonGridType)) { |
| 234 | description.appendText("gridType was " + jsonGridType); |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | String jsonChannelSpacing = instructionJson.get("channelSpacing").textValue(); |
| 239 | if (!instructionToMatch.lambda().channelSpacing().name().equals(jsonChannelSpacing)) { |
| 240 | description.appendText("channelSpacing was " + jsonChannelSpacing); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | int jsonSpacingMultiplier = instructionJson.get("spacingMultiplier").intValue(); |
| 245 | if (instructionToMatch.lambda().spacingMultiplier() != jsonSpacingMultiplier) { |
| 246 | description.appendText("spacingMultiplier was " + jsonSpacingMultiplier); |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | int jsonSlotGranularity = instructionJson.get("slotGranularity").intValue(); |
| 251 | if (instructionToMatch.lambda().slotGranularity() != jsonSlotGranularity) { |
| 252 | description.appendText("slotGranularity was " + jsonSlotGranularity); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 260 | * Matches the contents of a mod ODU singal Id instruction. |
| 261 | * |
| 262 | * @param instructionJson JSON instruction to match |
| 263 | * @param description Description object used for recording errors |
| 264 | * @return true if contents matches, false otherwise |
| 265 | */ |
| 266 | private boolean matchModOduSingalIdInstruction(JsonNode instructionJson, |
| 267 | Description description) { |
| 268 | ModOduSignalIdInstruction instructionToMatch = |
| 269 | (ModOduSignalIdInstruction) instruction; |
| 270 | String jsonSubType = instructionJson.get("subtype").textValue(); |
| 271 | if (!instructionToMatch.subtype().name().equals(jsonSubType)) { |
| 272 | description.appendText("subtype was " + jsonSubType); |
| 273 | return false; |
| 274 | } |
| 275 | String jsonType = instructionJson.get("type").textValue(); |
| 276 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 277 | description.appendText("type was " + jsonType); |
| 278 | return false; |
| 279 | } |
| 280 | final JsonNode jsonOduSignal = instructionJson.get("oduSignalId"); |
| 281 | int jsonTpn = jsonOduSignal.get("tributaryPortNumber").intValue(); |
| 282 | int jsonTsLen = jsonOduSignal.get("tributarySlotLength").intValue(); |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 283 | byte[] tributaryBitMap = HexString.fromHexString(jsonOduSignal.get("tributarySlotBitmap").asText()); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 284 | OduSignalId jsonOduSignalId = OduSignalId.oduSignalId(jsonTpn, jsonTsLen, tributaryBitMap); |
| 285 | if (!instructionToMatch.oduSignalId().equals(jsonOduSignalId)) { |
| 286 | description.appendText("oduSignalId was " + instructionToMatch); |
| 287 | return false; |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 294 | * Matches the contents of a mod Ethernet instruction. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 295 | * |
| 296 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 297 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 298 | * @return true if contents match, false otherwise |
| 299 | */ |
| 300 | private boolean matchModEtherInstruction(JsonNode instructionJson, |
| 301 | Description description) { |
| 302 | ModEtherInstruction instructionToMatch = |
| 303 | (ModEtherInstruction) instruction; |
| 304 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 305 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 306 | description.appendText("subtype was " + jsonSubtype); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | final String jsonType = instructionJson.get("type").textValue(); |
| 311 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 312 | description.appendText("type was " + jsonType); |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | final String jsonMac = instructionJson.get("mac").textValue(); |
| 317 | final String mac = instructionToMatch.mac().toString(); |
| 318 | if (!mac.equals(jsonMac)) { |
| 319 | description.appendText("mac was " + jsonMac); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Matches the contents of a mod vlan id instruction. |
| 328 | * |
| 329 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 330 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 331 | * @return true if contents match, false otherwise |
| 332 | */ |
| 333 | private boolean matchModVlanIdInstruction(JsonNode instructionJson, |
| 334 | Description description) { |
| 335 | ModVlanIdInstruction instructionToMatch = |
| 336 | (ModVlanIdInstruction) instruction; |
| 337 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 338 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 339 | description.appendText("subtype was " + jsonSubtype); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | final String jsonType = instructionJson.get("type").textValue(); |
| 344 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 345 | description.appendText("type was " + jsonType); |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | final short jsonVlanId = instructionJson.get("vlanId").shortValue(); |
| 350 | final short vlanId = instructionToMatch.vlanId().toShort(); |
| 351 | if (jsonVlanId != vlanId) { |
| 352 | description.appendText("vlan id was " + jsonVlanId); |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Matches the contents of a mod vlan pcp instruction. |
| 361 | * |
| 362 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 363 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 364 | * @return true if contents match, false otherwise |
| 365 | */ |
| 366 | private boolean matchModVlanPcpInstruction(JsonNode instructionJson, |
| 367 | Description description) { |
| 368 | ModVlanPcpInstruction instructionToMatch = |
| 369 | (ModVlanPcpInstruction) instruction; |
| 370 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 371 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 372 | description.appendText("subtype was " + jsonSubtype); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | final String jsonType = instructionJson.get("type").textValue(); |
| 377 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 378 | description.appendText("type was " + jsonType); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | final short jsonVlanPcp = instructionJson.get("vlanPcp").shortValue(); |
| 383 | final short vlanId = instructionToMatch.vlanPcp(); |
| 384 | if (jsonVlanPcp != vlanId) { |
| 385 | description.appendText("vlan pcp was " + jsonVlanPcp); |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Matches the contents of a mod ip instruction. |
| 394 | * |
| 395 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 396 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 397 | * @return true if contents match, false otherwise |
| 398 | */ |
| 399 | private boolean matchModIpInstruction(JsonNode instructionJson, |
| 400 | Description description) { |
| 401 | ModIPInstruction instructionToMatch = |
| 402 | (ModIPInstruction) instruction; |
| 403 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 404 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 405 | description.appendText("subtype was " + jsonSubtype); |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | final String jsonType = instructionJson.get("type").textValue(); |
| 410 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 411 | description.appendText("type was " + jsonType); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | final String jsonIp = instructionJson.get("ip").textValue(); |
| 416 | final String ip = instructionToMatch.ip().toString(); |
| 417 | if (!ip.equals(jsonIp)) { |
| 418 | description.appendText("ip was " + jsonIp); |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 426 | * Matches the contents of a mod IPv6 Flow Label instruction. |
| 427 | * |
| 428 | * @param instructionJson JSON instruction to match |
| 429 | * @param description Description object used for recording errors |
| 430 | * @return true if contents match, false otherwise |
| 431 | */ |
| 432 | private boolean matchModIPv6FlowLabelInstruction(JsonNode instructionJson, |
| 433 | Description description) { |
| 434 | ModIPv6FlowLabelInstruction instructionToMatch = |
| 435 | (ModIPv6FlowLabelInstruction) instruction; |
| 436 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 437 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 438 | description.appendText("subtype was " + jsonSubtype); |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | final String jsonType = instructionJson.get("type").textValue(); |
| 443 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 444 | description.appendText("type was " + jsonType); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | final int jsonFlowLabel = instructionJson.get("flowLabel").intValue(); |
| 449 | final int flowLabel = instructionToMatch.flowLabel(); |
| 450 | if (flowLabel != jsonFlowLabel) { |
| 451 | description.appendText("IPv6 flow label was " + jsonFlowLabel); |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | /** |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 459 | * Matches the contents of a mod MPLS label instruction. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 460 | * |
| 461 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 462 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 463 | * @return true if contents match, false otherwise |
| 464 | */ |
| 465 | private boolean matchModMplsLabelInstruction(JsonNode instructionJson, |
| 466 | Description description) { |
| 467 | ModMplsLabelInstruction instructionToMatch = |
| 468 | (ModMplsLabelInstruction) instruction; |
| 469 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 470 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 471 | description.appendText("subtype was " + jsonSubtype); |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | final String jsonType = instructionJson.get("type").textValue(); |
| 476 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 477 | description.appendText("type was " + jsonType); |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | final int jsonLabel = instructionJson.get("label").intValue(); |
Ray Milkey | 125572b | 2016-02-22 16:48:17 -0800 | [diff] [blame] | 482 | final int label = instructionToMatch.label().toInt(); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 483 | if (label != jsonLabel) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 484 | description.appendText("MPLS label was " + jsonLabel); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public boolean matchesSafely(JsonNode jsonInstruction, Description description) { |
| 493 | |
| 494 | // check type |
| 495 | final JsonNode jsonTypeNode = jsonInstruction.get("type"); |
| 496 | final String jsonType = jsonTypeNode.textValue(); |
| 497 | final String type = instruction.type().name(); |
| 498 | if (!jsonType.equals(type)) { |
| 499 | description.appendText("type was " + type); |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | if (instruction instanceof PushHeaderInstructions) { |
| 504 | return matchPushHeaderInstruction(jsonInstruction, description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 505 | } else if (instruction instanceof OutputInstruction) { |
| 506 | return matchOutputInstruction(jsonInstruction, description); |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 507 | } else if (instruction instanceof GroupInstruction) { |
| 508 | return matchGroupInstruction(jsonInstruction, description); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame^] | 509 | } else if (instruction instanceof MeterInstruction) { |
| 510 | return matchMeterInstruction(jsonInstruction, description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 511 | } else if (instruction instanceof ModLambdaInstruction) { |
| 512 | return matchModLambdaInstruction(jsonInstruction, description); |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 513 | } else if (instruction instanceof ModOchSignalInstruction) { |
| 514 | return matchModOchSingalInstruction(jsonInstruction, description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 515 | } else if (instruction instanceof ModEtherInstruction) { |
| 516 | return matchModEtherInstruction(jsonInstruction, description); |
| 517 | } else if (instruction instanceof ModVlanIdInstruction) { |
| 518 | return matchModVlanIdInstruction(jsonInstruction, description); |
| 519 | } else if (instruction instanceof ModVlanPcpInstruction) { |
| 520 | return matchModVlanPcpInstruction(jsonInstruction, description); |
| 521 | } else if (instruction instanceof ModIPInstruction) { |
| 522 | return matchModIpInstruction(jsonInstruction, description); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 523 | } else if (instruction instanceof ModIPv6FlowLabelInstruction) { |
| 524 | return matchModIPv6FlowLabelInstruction(jsonInstruction, |
| 525 | description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 526 | } else if (instruction instanceof ModMplsLabelInstruction) { |
| 527 | return matchModMplsLabelInstruction(jsonInstruction, description); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 528 | } else if (instruction instanceof ModOduSignalIdInstruction) { |
| 529 | return matchModOduSingalIdInstruction(jsonInstruction, description); |
Brian O'Connor | b75a67a | 2015-10-07 14:34:06 -0700 | [diff] [blame] | 530 | } else if (instruction instanceof NoActionInstruction) { |
| 531 | return true; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void describeTo(Description description) { |
| 539 | description.appendText(instruction.toString()); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Factory to allocate an instruction matcher. |
| 544 | * |
| 545 | * @param instruction instruction object we are looking for |
| 546 | * @return matcher |
| 547 | */ |
| 548 | public static InstructionJsonMatcher matchesInstruction(Instruction instruction) { |
| 549 | return new InstructionJsonMatcher(instruction); |
| 550 | } |
| 551 | } |