Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -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 | |
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.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(); |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 118 | description.appendText("Unmatching types "); |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 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 | /** |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 177 | * Matches the contents of a set queue instruction. |
| 178 | * |
| 179 | * @param instructionJson JSON instruction to match |
| 180 | * @param description Description object used for recording errors |
| 181 | * @return true if contents match, false otherwise |
| 182 | */ |
| 183 | private boolean matchSetQueueInstruction(JsonNode instructionJson, |
| 184 | Description description) { |
| 185 | final String jsonType = instructionJson.get("type").textValue(); |
| 186 | SetQueueInstruction instructionToMatch = (SetQueueInstruction) instruction; |
| 187 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 188 | description.appendText("type was " + jsonType); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | final long jsonQueueId = instructionJson.get("queueId").longValue(); |
| 193 | if (instructionToMatch.queueId() != jsonQueueId) { |
| 194 | description.appendText("queueId was " + jsonQueueId); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | if (instructionJson.get("port").isLong() || |
| 199 | instructionJson.get("port").isInt()) { |
| 200 | final long jsonPort = instructionJson.get("port").asLong(); |
| 201 | if (instructionToMatch.port().toLong() != (jsonPort)) { |
| 202 | description.appendText("port was " + jsonPort); |
| 203 | return false; |
| 204 | } |
| 205 | } else if (instructionJson.get("port").isTextual()) { |
| 206 | final String jsonPort = instructionJson.get("port").textValue(); |
| 207 | if (!instructionToMatch.port().toString().equals(jsonPort)) { |
| 208 | description.appendText("port was " + jsonPort); |
| 209 | return false; |
| 210 | } |
| 211 | } else { |
| 212 | final String jsonPort = instructionJson.get("port").toString(); |
| 213 | description.appendText("Unmatching types "); |
| 214 | description.appendText("instructionToMatch " + instructionToMatch.port().toString()); |
| 215 | description.appendText("jsonPort " + jsonPort); |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 222 | * Matches the contents of a mod OCh singal instruction. |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 223 | * |
| 224 | * @param instructionJson JSON instruction to match |
| 225 | * @param description Description object used for recording errors |
| 226 | * @return true if contents matches, false otherwise |
| 227 | */ |
| 228 | private boolean matchModOchSingalInstruction(JsonNode instructionJson, |
| 229 | Description description) { |
| 230 | ModOchSignalInstruction instructionToMatch = |
| 231 | (ModOchSignalInstruction) instruction; |
| 232 | |
| 233 | 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 | 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 | String jsonGridType = instructionJson.get("gridType").textValue(); |
| 246 | if (!instructionToMatch.lambda().gridType().name().equals(jsonGridType)) { |
| 247 | description.appendText("gridType was " + jsonGridType); |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | String jsonChannelSpacing = instructionJson.get("channelSpacing").textValue(); |
| 252 | if (!instructionToMatch.lambda().channelSpacing().name().equals(jsonChannelSpacing)) { |
| 253 | description.appendText("channelSpacing was " + jsonChannelSpacing); |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | int jsonSpacingMultiplier = instructionJson.get("spacingMultiplier").intValue(); |
| 258 | if (instructionToMatch.lambda().spacingMultiplier() != jsonSpacingMultiplier) { |
| 259 | description.appendText("spacingMultiplier was " + jsonSpacingMultiplier); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | int jsonSlotGranularity = instructionJson.get("slotGranularity").intValue(); |
| 264 | if (instructionToMatch.lambda().slotGranularity() != jsonSlotGranularity) { |
| 265 | description.appendText("slotGranularity was " + jsonSlotGranularity); |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 273 | * Matches the contents of a mod ODU singal Id instruction. |
| 274 | * |
| 275 | * @param instructionJson JSON instruction to match |
| 276 | * @param description Description object used for recording errors |
| 277 | * @return true if contents matches, false otherwise |
| 278 | */ |
| 279 | private boolean matchModOduSingalIdInstruction(JsonNode instructionJson, |
| 280 | Description description) { |
| 281 | ModOduSignalIdInstruction instructionToMatch = |
| 282 | (ModOduSignalIdInstruction) instruction; |
| 283 | String jsonSubType = instructionJson.get("subtype").textValue(); |
| 284 | if (!instructionToMatch.subtype().name().equals(jsonSubType)) { |
| 285 | description.appendText("subtype was " + jsonSubType); |
| 286 | return false; |
| 287 | } |
| 288 | String jsonType = instructionJson.get("type").textValue(); |
| 289 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 290 | description.appendText("type was " + jsonType); |
| 291 | return false; |
| 292 | } |
| 293 | final JsonNode jsonOduSignal = instructionJson.get("oduSignalId"); |
| 294 | int jsonTpn = jsonOduSignal.get("tributaryPortNumber").intValue(); |
| 295 | int jsonTsLen = jsonOduSignal.get("tributarySlotLength").intValue(); |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 296 | byte[] tributaryBitMap = HexString.fromHexString(jsonOduSignal.get("tributarySlotBitmap").asText()); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 297 | OduSignalId jsonOduSignalId = OduSignalId.oduSignalId(jsonTpn, jsonTsLen, tributaryBitMap); |
| 298 | if (!instructionToMatch.oduSignalId().equals(jsonOduSignalId)) { |
| 299 | description.appendText("oduSignalId was " + instructionToMatch); |
| 300 | return false; |
| 301 | } |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 307 | * Matches the contents of a mod Ethernet instruction. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 308 | * |
| 309 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 310 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 311 | * @return true if contents match, false otherwise |
| 312 | */ |
| 313 | private boolean matchModEtherInstruction(JsonNode instructionJson, |
| 314 | Description description) { |
| 315 | ModEtherInstruction instructionToMatch = |
| 316 | (ModEtherInstruction) instruction; |
| 317 | final String jsonSubtype = instructionJson.get("subtype").textValue(); |
| 318 | if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { |
| 319 | description.appendText("subtype was " + jsonSubtype); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | final String jsonType = instructionJson.get("type").textValue(); |
| 324 | if (!instructionToMatch.type().name().equals(jsonType)) { |
| 325 | description.appendText("type was " + jsonType); |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | final String jsonMac = instructionJson.get("mac").textValue(); |
| 330 | final String mac = instructionToMatch.mac().toString(); |
| 331 | if (!mac.equals(jsonMac)) { |
| 332 | description.appendText("mac was " + jsonMac); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Matches the contents of a mod vlan id instruction. |
| 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 matchModVlanIdInstruction(JsonNode instructionJson, |
| 347 | Description description) { |
| 348 | ModVlanIdInstruction instructionToMatch = |
| 349 | (ModVlanIdInstruction) 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 short jsonVlanId = instructionJson.get("vlanId").shortValue(); |
| 363 | final short vlanId = instructionToMatch.vlanId().toShort(); |
| 364 | if (jsonVlanId != vlanId) { |
| 365 | description.appendText("vlan id was " + jsonVlanId); |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Matches the contents of a mod vlan pcp 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 matchModVlanPcpInstruction(JsonNode instructionJson, |
| 380 | Description description) { |
| 381 | ModVlanPcpInstruction instructionToMatch = |
| 382 | (ModVlanPcpInstruction) 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 jsonVlanPcp = instructionJson.get("vlanPcp").shortValue(); |
| 396 | final short vlanId = instructionToMatch.vlanPcp(); |
| 397 | if (jsonVlanPcp != vlanId) { |
| 398 | description.appendText("vlan pcp was " + jsonVlanPcp); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Matches the contents of a mod ip 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 matchModIpInstruction(JsonNode instructionJson, |
| 413 | Description description) { |
| 414 | ModIPInstruction instructionToMatch = |
| 415 | (ModIPInstruction) 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 String jsonIp = instructionJson.get("ip").textValue(); |
| 429 | final String ip = instructionToMatch.ip().toString(); |
| 430 | if (!ip.equals(jsonIp)) { |
| 431 | description.appendText("ip was " + jsonIp); |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 439 | * Matches the contents of a mod IPv6 Flow Label instruction. |
| 440 | * |
| 441 | * @param instructionJson JSON instruction to match |
| 442 | * @param description Description object used for recording errors |
| 443 | * @return true if contents match, false otherwise |
| 444 | */ |
| 445 | private boolean matchModIPv6FlowLabelInstruction(JsonNode instructionJson, |
| 446 | Description description) { |
| 447 | ModIPv6FlowLabelInstruction instructionToMatch = |
| 448 | (ModIPv6FlowLabelInstruction) 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 int jsonFlowLabel = instructionJson.get("flowLabel").intValue(); |
| 462 | final int flowLabel = instructionToMatch.flowLabel(); |
| 463 | if (flowLabel != jsonFlowLabel) { |
| 464 | description.appendText("IPv6 flow label was " + jsonFlowLabel); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | /** |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 472 | * Matches the contents of a mod MPLS label instruction. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 473 | * |
| 474 | * @param instructionJson JSON instruction to match |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 475 | * @param description Description object used for recording errors |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 476 | * @return true if contents match, false otherwise |
| 477 | */ |
| 478 | private boolean matchModMplsLabelInstruction(JsonNode instructionJson, |
| 479 | Description description) { |
| 480 | ModMplsLabelInstruction instructionToMatch = |
| 481 | (ModMplsLabelInstruction) 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 jsonLabel = instructionJson.get("label").intValue(); |
Ray Milkey | 125572b | 2016-02-22 16:48:17 -0800 | [diff] [blame] | 495 | final int label = instructionToMatch.label().toInt(); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 496 | if (label != jsonLabel) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 497 | description.appendText("MPLS label was " + jsonLabel); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public boolean matchesSafely(JsonNode jsonInstruction, Description description) { |
| 506 | |
| 507 | // check type |
| 508 | final JsonNode jsonTypeNode = jsonInstruction.get("type"); |
| 509 | final String jsonType = jsonTypeNode.textValue(); |
| 510 | final String type = instruction.type().name(); |
| 511 | if (!jsonType.equals(type)) { |
| 512 | description.appendText("type was " + type); |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | if (instruction instanceof PushHeaderInstructions) { |
| 517 | return matchPushHeaderInstruction(jsonInstruction, description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 518 | } else if (instruction instanceof OutputInstruction) { |
| 519 | return matchOutputInstruction(jsonInstruction, description); |
Jian Li | ce8c560 | 2016-03-03 21:43:24 -0800 | [diff] [blame] | 520 | } else if (instruction instanceof GroupInstruction) { |
| 521 | return matchGroupInstruction(jsonInstruction, description); |
Jian Li | 47b2623 | 2016-03-07 09:59:59 -0800 | [diff] [blame] | 522 | } else if (instruction instanceof MeterInstruction) { |
| 523 | return matchMeterInstruction(jsonInstruction, description); |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 524 | } else if (instruction instanceof SetQueueInstruction) { |
| 525 | return matchSetQueueInstruction(jsonInstruction, description); |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 526 | } else if (instruction instanceof ModOchSignalInstruction) { |
| 527 | return matchModOchSingalInstruction(jsonInstruction, description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 528 | } else if (instruction instanceof ModEtherInstruction) { |
| 529 | return matchModEtherInstruction(jsonInstruction, description); |
| 530 | } else if (instruction instanceof ModVlanIdInstruction) { |
| 531 | return matchModVlanIdInstruction(jsonInstruction, description); |
| 532 | } else if (instruction instanceof ModVlanPcpInstruction) { |
| 533 | return matchModVlanPcpInstruction(jsonInstruction, description); |
| 534 | } else if (instruction instanceof ModIPInstruction) { |
| 535 | return matchModIpInstruction(jsonInstruction, description); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 536 | } else if (instruction instanceof ModIPv6FlowLabelInstruction) { |
Jian Li | 70dffe4 | 2016-03-08 22:23:02 -0800 | [diff] [blame] | 537 | return matchModIPv6FlowLabelInstruction(jsonInstruction, description); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 538 | } else if (instruction instanceof ModMplsLabelInstruction) { |
| 539 | return matchModMplsLabelInstruction(jsonInstruction, description); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 540 | } else if (instruction instanceof ModOduSignalIdInstruction) { |
| 541 | return matchModOduSingalIdInstruction(jsonInstruction, description); |
Brian O'Connor | b75a67a | 2015-10-07 14:34:06 -0700 | [diff] [blame] | 542 | } else if (instruction instanceof NoActionInstruction) { |
| 543 | return true; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public void describeTo(Description description) { |
| 551 | description.appendText(instruction.toString()); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Factory to allocate an instruction matcher. |
| 556 | * |
| 557 | * @param instruction instruction object we are looking for |
| 558 | * @return matcher |
| 559 | */ |
| 560 | public static InstructionJsonMatcher matchesInstruction(Instruction instruction) { |
| 561 | return new InstructionJsonMatcher(instruction); |
| 562 | } |
| 563 | } |