Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [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 | |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
| 20 | import java.util.SortedMap; |
| 21 | import java.util.TreeMap; |
| 22 | |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 25 | import org.onlab.packet.EthType; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.MplsLabel; |
| 31 | import org.onlab.packet.VlanId; |
| 32 | import org.onosproject.codec.JsonCodec; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.net.ChannelSpacing; |
| 35 | import org.onosproject.net.GridType; |
| 36 | import org.onosproject.net.Lambda; |
| 37 | import org.onosproject.net.OchSignal; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | import org.onosproject.net.flow.criteria.Criterion; |
| 41 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 42 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 43 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 44 | import org.onosproject.net.flow.criteria.IPDscpCriterion; |
| 45 | import org.onosproject.net.flow.criteria.IPEcnCriterion; |
| 46 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 47 | import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; |
| 48 | import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; |
| 49 | import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; |
| 50 | import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; |
| 51 | import org.onosproject.net.flow.criteria.IcmpCodeCriterion; |
| 52 | import org.onosproject.net.flow.criteria.IcmpTypeCriterion; |
| 53 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 54 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
| 55 | import org.onosproject.net.flow.criteria.IndexedLambdaCriterion; |
| 56 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 57 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 58 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 59 | import org.onosproject.net.flow.criteria.SctpPortCriterion; |
| 60 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 62 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 63 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 64 | import org.onosproject.net.flow.criteria.VlanPcpCriterion; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 65 | |
| 66 | import com.fasterxml.jackson.databind.JsonNode; |
| 67 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 68 | import org.onosproject.net.flow.instructions.Instruction; |
| 69 | import org.onosproject.net.flow.instructions.Instructions; |
| 70 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 71 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 72 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 73 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 74 | |
| 75 | import static org.easymock.EasyMock.createMock; |
| 76 | import static org.easymock.EasyMock.expect; |
| 77 | import static org.easymock.EasyMock.replay; |
| 78 | import static org.hamcrest.MatcherAssert.assertThat; |
| 79 | import static org.hamcrest.Matchers.instanceOf; |
| 80 | import static org.hamcrest.Matchers.is; |
| 81 | import static org.hamcrest.Matchers.notNullValue; |
| 82 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 83 | |
| 84 | /** |
| 85 | * Flow rule codec unit tests. |
| 86 | */ |
| 87 | public class FlowRuleCodecTest { |
| 88 | |
| 89 | MockCodecContext context; |
| 90 | JsonCodec<FlowRule> flowRuleCodec; |
| 91 | final CoreService mockCoreService = createMock(CoreService.class); |
| 92 | |
| 93 | /** |
| 94 | * Sets up for each test. Creates a context and fetches the flow rule |
| 95 | * codec. |
| 96 | */ |
| 97 | @Before |
| 98 | public void setUp() { |
| 99 | context = new MockCodecContext(); |
| 100 | flowRuleCodec = context.codec(FlowRule.class); |
| 101 | assertThat(flowRuleCodec, notNullValue()); |
| 102 | |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 103 | expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID)) |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 104 | .andReturn(APP_ID).anyTimes(); |
| 105 | replay(mockCoreService); |
| 106 | context.registerService(CoreService.class, mockCoreService); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Reads in a rule from the given resource and decodes it. |
| 111 | * |
| 112 | * @param resourceName resource to use to read the JSON for the rule |
| 113 | * @return decoded flow rule |
| 114 | * @throws IOException if processing the resource fails |
| 115 | */ |
| 116 | private FlowRule getRule(String resourceName) throws IOException { |
| 117 | InputStream jsonStream = FlowRuleCodecTest.class |
| 118 | .getResourceAsStream(resourceName); |
| 119 | JsonNode json = context.mapper().readTree(jsonStream); |
| 120 | assertThat(json, notNullValue()); |
| 121 | FlowRule rule = flowRuleCodec.decode((ObjectNode) json, context); |
| 122 | assertThat(rule, notNullValue()); |
| 123 | return rule; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Checks that the data shared by all the resources is correct for a |
| 128 | * given rule. |
| 129 | * |
| 130 | * @param rule rule to check |
| 131 | */ |
| 132 | private void checkCommonData(FlowRule rule) { |
| 133 | assertThat(rule.appId(), is(APP_ID.id())); |
| 134 | assertThat(rule.isPermanent(), is(false)); |
| 135 | assertThat(rule.timeout(), is(1)); |
| 136 | assertThat(rule.priority(), is(1)); |
| 137 | assertThat(rule.deviceId().toString(), is("of:0000000000000001")); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Checks that a simple rule decodes properly. |
| 142 | * |
| 143 | * @throws IOException if the resource cannot be processed |
| 144 | */ |
| 145 | @Test |
| 146 | public void codecSimpleFlowTest() throws IOException { |
| 147 | FlowRule rule = getRule("simple-flow.json"); |
| 148 | |
| 149 | checkCommonData(rule); |
| 150 | |
| 151 | assertThat(rule.selector().criteria().size(), is(1)); |
| 152 | Criterion criterion1 = rule.selector().criteria().iterator().next(); |
| 153 | assertThat(criterion1.type(), is(Criterion.Type.ETH_TYPE)); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 154 | assertThat(((EthTypeCriterion) criterion1).ethType(), is(new EthType(2054))); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 155 | |
| 156 | assertThat(rule.treatment().allInstructions().size(), is(1)); |
| 157 | Instruction instruction1 = rule.treatment().allInstructions().get(0); |
| 158 | assertThat(instruction1.type(), is(Instruction.Type.OUTPUT)); |
| 159 | assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.CONTROLLER)); |
| 160 | } |
| 161 | |
| 162 | SortedMap<String, Instruction> instructions = new TreeMap<>(); |
| 163 | |
| 164 | /** |
| 165 | * Looks up an instruction in the instruction map based on type and subtype. |
| 166 | * |
| 167 | * @param type type string |
| 168 | * @param subType subtype string |
| 169 | * @return instruction that matches |
| 170 | */ |
| 171 | private Instruction getInstruction(Instruction.Type type, String subType) { |
| 172 | Instruction instruction = instructions.get(type.name() + "/" + subType); |
| 173 | assertThat(instruction, notNullValue()); |
| 174 | assertThat(instruction.type(), is(type)); |
| 175 | return instruction; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Checks that a rule with one of each instruction type decodes properly. |
| 180 | * |
| 181 | * @throws IOException if the resource cannot be processed |
| 182 | */ |
| 183 | @Test |
| 184 | public void decodeInstructionsFlowTest() throws Exception { |
| 185 | FlowRule rule = getRule("instructions-flow.json"); |
| 186 | |
| 187 | checkCommonData(rule); |
| 188 | |
| 189 | rule.treatment().allInstructions() |
| 190 | .stream() |
| 191 | .forEach(instruction -> |
| 192 | { |
| 193 | String subType; |
| 194 | if (instruction.type() == Instruction.Type.L0MODIFICATION) { |
| 195 | subType = ((L0ModificationInstruction) instruction) |
| 196 | .subtype().name(); |
| 197 | } else if (instruction.type() == Instruction.Type.L2MODIFICATION) { |
| 198 | subType = ((L2ModificationInstruction) instruction) |
| 199 | .subtype().name(); |
| 200 | } else if (instruction.type() == Instruction.Type.L3MODIFICATION) { |
| 201 | subType = ((L3ModificationInstruction) instruction) |
| 202 | .subtype().name(); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 203 | } else if (instruction.type() == Instruction.Type.L4MODIFICATION) { |
| 204 | subType = ((L4ModificationInstruction) instruction) |
| 205 | .subtype().name(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 206 | } else { |
| 207 | subType = ""; |
| 208 | } |
| 209 | instructions.put( |
| 210 | instruction.type().name() + "/" + subType, instruction); |
| 211 | }); |
| 212 | |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 213 | assertThat(rule.treatment().allInstructions().size(), is(24)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 214 | |
| 215 | Instruction instruction; |
| 216 | |
| 217 | instruction = getInstruction(Instruction.Type.OUTPUT, ""); |
| 218 | assertThat(instruction.type(), is(Instruction.Type.OUTPUT)); |
| 219 | assertThat(((Instructions.OutputInstruction) instruction).port(), is(PortNumber.CONTROLLER)); |
| 220 | |
| 221 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 222 | L2ModificationInstruction.L2SubType.ETH_SRC.name()); |
| 223 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 224 | assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(), |
| 225 | is(MacAddress.valueOf("12:34:56:78:90:12"))); |
| 226 | |
| 227 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 228 | L2ModificationInstruction.L2SubType.ETH_DST.name()); |
| 229 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 230 | assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(), |
| 231 | is(MacAddress.valueOf("98:76:54:32:01:00"))); |
| 232 | |
| 233 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 234 | L2ModificationInstruction.L2SubType.VLAN_ID.name()); |
| 235 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 236 | assertThat(((L2ModificationInstruction.ModVlanIdInstruction) instruction).vlanId().toShort(), |
| 237 | is((short) 22)); |
| 238 | |
| 239 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 240 | L2ModificationInstruction.L2SubType.VLAN_PCP.name()); |
| 241 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 242 | assertThat(((L2ModificationInstruction.ModVlanPcpInstruction) instruction).vlanPcp(), |
| 243 | is((byte) 1)); |
| 244 | |
| 245 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 246 | L2ModificationInstruction.L2SubType.MPLS_LABEL.name()); |
| 247 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 248 | assertThat(((L2ModificationInstruction.ModMplsLabelInstruction) instruction) |
| 249 | .label().shortValue(), |
| 250 | is((short) 777)); |
| 251 | |
| 252 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 253 | L2ModificationInstruction.L2SubType.MPLS_PUSH.name()); |
| 254 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 255 | assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction) |
| 256 | .ethernetType().toShort(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 257 | is(Ethernet.MPLS_UNICAST)); |
| 258 | |
| 259 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 260 | L2ModificationInstruction.L2SubType.MPLS_POP.name()); |
| 261 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 262 | assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction) |
| 263 | .ethernetType().toShort(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 264 | is(Ethernet.MPLS_UNICAST)); |
| 265 | |
| 266 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 267 | L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name()); |
| 268 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 269 | assertThat(instruction, instanceOf(L2ModificationInstruction.ModMplsTtlInstruction.class)); |
| 270 | |
| 271 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 272 | L2ModificationInstruction.L2SubType.VLAN_POP.name()); |
| 273 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 274 | assertThat(instruction, instanceOf(L2ModificationInstruction.PopVlanInstruction.class)); |
| 275 | |
| 276 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 277 | L2ModificationInstruction.L2SubType.VLAN_PUSH.name()); |
| 278 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 279 | assertThat(instruction, instanceOf(L2ModificationInstruction.PushHeaderInstructions.class)); |
| 280 | |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 281 | instruction = getInstruction(Instruction.Type.L2MODIFICATION, |
| 282 | L2ModificationInstruction.L2SubType.TUNNEL_ID.name()); |
| 283 | assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION)); |
| 284 | assertThat(((L2ModificationInstruction.ModTunnelIdInstruction) instruction) |
| 285 | .tunnelId(), is(100L)); |
| 286 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 287 | instruction = getInstruction(Instruction.Type.L3MODIFICATION, |
| 288 | L3ModificationInstruction.L3SubType.IPV4_SRC.name()); |
| 289 | assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION)); |
| 290 | assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(), |
| 291 | is(IpAddress.valueOf("1.2.3.4"))); |
| 292 | |
| 293 | instruction = getInstruction(Instruction.Type.L3MODIFICATION, |
| 294 | L3ModificationInstruction.L3SubType.IPV4_DST.name()); |
| 295 | assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION)); |
| 296 | assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(), |
| 297 | is(IpAddress.valueOf("1.2.3.3"))); |
| 298 | |
| 299 | instruction = getInstruction(Instruction.Type.L3MODIFICATION, |
| 300 | L3ModificationInstruction.L3SubType.IPV6_SRC.name()); |
| 301 | assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION)); |
| 302 | assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(), |
| 303 | is(IpAddress.valueOf("1.2.3.2"))); |
| 304 | |
| 305 | instruction = getInstruction(Instruction.Type.L3MODIFICATION, |
| 306 | L3ModificationInstruction.L3SubType.IPV6_DST.name()); |
| 307 | assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION)); |
| 308 | assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(), |
| 309 | is(IpAddress.valueOf("1.2.3.1"))); |
| 310 | |
| 311 | instruction = getInstruction(Instruction.Type.L3MODIFICATION, |
| 312 | L3ModificationInstruction.L3SubType.IPV6_FLABEL.name()); |
| 313 | assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION)); |
| 314 | assertThat(((L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction) |
| 315 | .flowLabel(), |
| 316 | is(8)); |
| 317 | |
| 318 | instruction = getInstruction(Instruction.Type.L0MODIFICATION, |
| 319 | L0ModificationInstruction.L0SubType.LAMBDA.name()); |
| 320 | assertThat(instruction.type(), is(Instruction.Type.L0MODIFICATION)); |
| 321 | assertThat(((L0ModificationInstruction.ModLambdaInstruction) instruction) |
| 322 | .lambda(), |
| 323 | is((short) 7)); |
| 324 | |
| 325 | instruction = getInstruction(Instruction.Type.L0MODIFICATION, |
| 326 | L0ModificationInstruction.L0SubType.OCH.name()); |
| 327 | assertThat(instruction.type(), is(Instruction.Type.L0MODIFICATION)); |
| 328 | L0ModificationInstruction.ModOchSignalInstruction och = |
| 329 | (L0ModificationInstruction.ModOchSignalInstruction) instruction; |
| 330 | assertThat(och.lambda().spacingMultiplier(), is(4)); |
| 331 | assertThat(och.lambda().slotGranularity(), is(8)); |
| 332 | assertThat(och.lambda().gridType(), is(GridType.DWDM)); |
| 333 | assertThat(och.lambda().channelSpacing(), is(ChannelSpacing.CHL_100GHZ)); |
Hyunsun Moon | fab2950 | 2015-08-25 13:39:16 -0700 | [diff] [blame] | 334 | |
| 335 | instruction = getInstruction(Instruction.Type.L4MODIFICATION, |
| 336 | L4ModificationInstruction.L4SubType.TCP_DST.name()); |
| 337 | assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION)); |
| 338 | assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction) |
| 339 | .port().toInt(), is(40001)); |
| 340 | |
| 341 | instruction = getInstruction(Instruction.Type.L4MODIFICATION, |
| 342 | L4ModificationInstruction.L4SubType.TCP_SRC.name()); |
| 343 | assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION)); |
| 344 | assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction) |
| 345 | .port().toInt(), is(40002)); |
| 346 | |
| 347 | instruction = getInstruction(Instruction.Type.L4MODIFICATION, |
| 348 | L4ModificationInstruction.L4SubType.UDP_DST.name()); |
| 349 | assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION)); |
| 350 | assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction) |
| 351 | .port().toInt(), is(40003)); |
| 352 | |
| 353 | instruction = getInstruction(Instruction.Type.L4MODIFICATION, |
| 354 | L4ModificationInstruction.L4SubType.UDP_SRC.name()); |
| 355 | assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION)); |
| 356 | assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction) |
| 357 | .port().toInt(), is(40004)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | SortedMap<String, Criterion> criteria = new TreeMap<>(); |
| 361 | |
| 362 | /** |
| 363 | * Looks up a criterion in the instruction map based on type and subtype. |
| 364 | * |
| 365 | * @param type type string |
| 366 | * @return criterion that matches |
| 367 | */ |
| 368 | private Criterion getCriterion(Criterion.Type type) { |
| 369 | Criterion criterion = criteria.get(type.name()); |
| 370 | assertThat(criterion.type(), is(type)); |
| 371 | return criterion; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Checks that a rule with one of each kind of criterion decodes properly. |
| 376 | * |
| 377 | * @throws IOException if the resource cannot be processed |
| 378 | */ |
| 379 | @Test |
| 380 | public void codecCriteriaFlowTest() throws Exception { |
| 381 | FlowRule rule = getRule("criteria-flow.json"); |
| 382 | |
| 383 | checkCommonData(rule); |
| 384 | |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 385 | assertThat(rule.selector().criteria().size(), is(33)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 386 | |
| 387 | rule.selector().criteria() |
| 388 | .stream() |
| 389 | .forEach(criterion -> |
| 390 | criteria.put(criterion.type().name(), criterion)); |
| 391 | |
| 392 | Criterion criterion; |
| 393 | |
| 394 | criterion = getCriterion(Criterion.Type.ETH_TYPE); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 395 | assertThat(((EthTypeCriterion) criterion).ethType(), is(new EthType(2054))); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 396 | |
| 397 | criterion = getCriterion(Criterion.Type.ETH_DST); |
| 398 | assertThat(((EthCriterion) criterion).mac(), |
| 399 | is(MacAddress.valueOf("00:11:22:33:44:55"))); |
| 400 | |
| 401 | criterion = getCriterion(Criterion.Type.ETH_SRC); |
| 402 | assertThat(((EthCriterion) criterion).mac(), |
| 403 | is(MacAddress.valueOf("00:11:22:33:44:55"))); |
| 404 | |
| 405 | criterion = getCriterion(Criterion.Type.IN_PORT); |
| 406 | assertThat(((PortCriterion) criterion).port(), |
| 407 | is(PortNumber.portNumber(23))); |
| 408 | |
| 409 | criterion = getCriterion(Criterion.Type.IN_PHY_PORT); |
| 410 | assertThat(((PortCriterion) criterion).port(), |
| 411 | is(PortNumber.portNumber(44))); |
| 412 | |
| 413 | criterion = getCriterion(Criterion.Type.VLAN_VID); |
| 414 | assertThat(((VlanIdCriterion) criterion).vlanId(), |
| 415 | is(VlanId.vlanId((short) 777))); |
| 416 | |
| 417 | criterion = getCriterion(Criterion.Type.VLAN_PCP); |
| 418 | assertThat(((VlanPcpCriterion) criterion).priority(), |
| 419 | is(((byte) 3))); |
| 420 | |
| 421 | criterion = getCriterion(Criterion.Type.IP_DSCP); |
| 422 | assertThat(((IPDscpCriterion) criterion).ipDscp(), |
| 423 | is(((byte) 2))); |
| 424 | |
| 425 | criterion = getCriterion(Criterion.Type.IP_ECN); |
| 426 | assertThat(((IPEcnCriterion) criterion).ipEcn(), |
| 427 | is(((byte) 1))); |
| 428 | |
| 429 | criterion = getCriterion(Criterion.Type.IP_PROTO); |
| 430 | assertThat(((IPProtocolCriterion) criterion).protocol(), |
| 431 | is(((short) 4))); |
| 432 | |
| 433 | criterion = getCriterion(Criterion.Type.IPV4_SRC); |
| 434 | assertThat(((IPCriterion) criterion).ip(), |
| 435 | is((IpPrefix.valueOf("1.2.0.0/32")))); |
| 436 | |
| 437 | criterion = getCriterion(Criterion.Type.IPV4_DST); |
| 438 | assertThat(((IPCriterion) criterion).ip(), |
| 439 | is((IpPrefix.valueOf("2.2.0.0/32")))); |
| 440 | |
| 441 | criterion = getCriterion(Criterion.Type.IPV6_SRC); |
| 442 | assertThat(((IPCriterion) criterion).ip(), |
| 443 | is((IpPrefix.valueOf("3.2.0.0/32")))); |
| 444 | |
| 445 | criterion = getCriterion(Criterion.Type.IPV6_DST); |
| 446 | assertThat(((IPCriterion) criterion).ip(), |
| 447 | is((IpPrefix.valueOf("4.2.0.0/32")))); |
| 448 | |
| 449 | criterion = getCriterion(Criterion.Type.TCP_SRC); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 450 | assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 451 | is(80)); |
| 452 | |
| 453 | criterion = getCriterion(Criterion.Type.TCP_DST); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 454 | assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 455 | is(443)); |
| 456 | |
| 457 | criterion = getCriterion(Criterion.Type.UDP_SRC); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 458 | assertThat(((UdpPortCriterion) criterion).udpPort().toInt(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 459 | is(180)); |
| 460 | |
| 461 | criterion = getCriterion(Criterion.Type.UDP_DST); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 462 | assertThat(((UdpPortCriterion) criterion).udpPort().toInt(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 463 | is(1443)); |
| 464 | |
| 465 | criterion = getCriterion(Criterion.Type.SCTP_SRC); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 466 | assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 467 | is(280)); |
| 468 | |
| 469 | criterion = getCriterion(Criterion.Type.SCTP_DST); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 470 | assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(), |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 471 | is(2443)); |
| 472 | |
| 473 | criterion = getCriterion(Criterion.Type.ICMPV4_TYPE); |
| 474 | assertThat(((IcmpTypeCriterion) criterion).icmpType(), |
| 475 | is((short) 24)); |
| 476 | |
| 477 | criterion = getCriterion(Criterion.Type.ICMPV4_CODE); |
| 478 | assertThat(((IcmpCodeCriterion) criterion).icmpCode(), |
| 479 | is((short) 16)); |
| 480 | |
| 481 | criterion = getCriterion(Criterion.Type.ICMPV6_TYPE); |
| 482 | assertThat(((Icmpv6TypeCriterion) criterion).icmpv6Type(), |
| 483 | is((short) 14)); |
| 484 | |
| 485 | criterion = getCriterion(Criterion.Type.ICMPV6_CODE); |
| 486 | assertThat(((Icmpv6CodeCriterion) criterion).icmpv6Code(), |
| 487 | is((short) 6)); |
| 488 | |
| 489 | criterion = getCriterion(Criterion.Type.IPV6_FLABEL); |
| 490 | assertThat(((IPv6FlowLabelCriterion) criterion).flowLabel(), |
| 491 | is(8)); |
| 492 | |
| 493 | criterion = getCriterion(Criterion.Type.IPV6_ND_TARGET); |
| 494 | assertThat(((IPv6NDTargetAddressCriterion) criterion) |
| 495 | .targetAddress().toString(), |
| 496 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 497 | |
| 498 | criterion = getCriterion(Criterion.Type.IPV6_ND_SLL); |
| 499 | assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(), |
| 500 | is(MacAddress.valueOf("00:11:22:33:44:56"))); |
| 501 | |
| 502 | criterion = getCriterion(Criterion.Type.IPV6_ND_TLL); |
| 503 | assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(), |
| 504 | is(MacAddress.valueOf("00:11:22:33:44:57"))); |
| 505 | |
| 506 | criterion = getCriterion(Criterion.Type.MPLS_LABEL); |
| 507 | assertThat(((MplsCriterion) criterion).label(), |
| 508 | is(MplsLabel.mplsLabel(123))); |
| 509 | |
| 510 | criterion = getCriterion(Criterion.Type.IPV6_EXTHDR); |
| 511 | assertThat(((IPv6ExthdrFlagsCriterion) criterion).exthdrFlags(), |
| 512 | is(99)); |
| 513 | |
| 514 | criterion = getCriterion(Criterion.Type.OCH_SIGID); |
| 515 | assertThat(((IndexedLambdaCriterion) criterion).lambda(), |
| 516 | is(Lambda.indexedLambda(122))); |
Hyunsun Moon | 7080a0d | 2015-08-14 19:18:48 -0700 | [diff] [blame] | 517 | |
| 518 | criterion = getCriterion(Criterion.Type.TUNNEL_ID); |
| 519 | assertThat(((TunnelIdCriterion) criterion).tunnelId(), |
| 520 | is(100L)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Checks that a rule with a SigId criterion decodes properly. |
| 525 | * |
| 526 | * @throws IOException if the resource cannot be processed |
| 527 | */ |
| 528 | @Test |
| 529 | public void codecSigIdCriteriaFlowTest() throws Exception { |
| 530 | FlowRule rule = getRule("sigid-flow.json"); |
| 531 | |
| 532 | checkCommonData(rule); |
| 533 | |
| 534 | assertThat(rule.selector().criteria().size(), is(1)); |
| 535 | Criterion criterion = rule.selector().criteria().iterator().next(); |
| 536 | assertThat(criterion.type(), is(Criterion.Type.OCH_SIGID)); |
| 537 | Lambda lambda = ((OchSignalCriterion) criterion).lambda(); |
| 538 | assertThat(lambda, instanceOf(OchSignal.class)); |
| 539 | OchSignal ochSignal = (OchSignal) lambda; |
| 540 | assertThat(ochSignal.spacingMultiplier(), is(3)); |
| 541 | assertThat(ochSignal.slotGranularity(), is(4)); |
| 542 | assertThat(ochSignal.gridType(), is(GridType.CWDM)); |
| 543 | assertThat(ochSignal.channelSpacing(), is(ChannelSpacing.CHL_25GHZ)); |
| 544 | } |
| 545 | |
| 546 | } |