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