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