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