blob: 11b7eb66e2e207b7075f7298d0b75ca347c401a8 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeyd43fe452015-05-29 09:35:12 -07003 *
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 */
16package org.onosproject.codec.impl;
17
Jian Li7c322f42016-03-04 11:00:59 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li2907ad22016-05-12 23:08:54 -070020import org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
Ray Milkeyd43fe452015-05-29 09:35:12 -070022import org.junit.Before;
23import org.junit.Test;
alshabibcaf1ca22015-06-25 15:18:16 -070024import org.onlab.packet.EthType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070025import org.onlab.packet.Ethernet;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.IpPrefix;
28import org.onlab.packet.MacAddress;
29import org.onlab.packet.MplsLabel;
30import org.onlab.packet.VlanId;
31import org.onosproject.codec.JsonCodec;
32import org.onosproject.core.CoreService;
33import org.onosproject.net.ChannelSpacing;
Jian Li2907ad22016-05-12 23:08:54 -070034import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070035import org.onosproject.net.GridType;
36import org.onosproject.net.Lambda;
37import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030038import org.onosproject.net.OchSignalType;
39import org.onosproject.net.OduSignalType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070040import org.onosproject.net.PortNumber;
Jian Li2907ad22016-05-12 23:08:54 -070041import org.onosproject.net.flow.DefaultFlowRule;
Ray Milkeyd43fe452015-05-29 09:35:12 -070042import org.onosproject.net.flow.FlowRule;
43import org.onosproject.net.flow.criteria.Criterion;
44import org.onosproject.net.flow.criteria.EthCriterion;
45import org.onosproject.net.flow.criteria.EthTypeCriterion;
46import org.onosproject.net.flow.criteria.IPCriterion;
47import org.onosproject.net.flow.criteria.IPDscpCriterion;
48import org.onosproject.net.flow.criteria.IPEcnCriterion;
49import org.onosproject.net.flow.criteria.IPProtocolCriterion;
50import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
51import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
52import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
53import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
54import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
55import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
56import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
57import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070058import org.onosproject.net.flow.criteria.MplsCriterion;
59import org.onosproject.net.flow.criteria.OchSignalCriterion;
Yafit Hadar5796d972015-10-15 13:16:11 +030060import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
61import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
62import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070063import org.onosproject.net.flow.criteria.PortCriterion;
64import org.onosproject.net.flow.criteria.SctpPortCriterion;
65import org.onosproject.net.flow.criteria.TcpPortCriterion;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -070066import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070067import org.onosproject.net.flow.criteria.UdpPortCriterion;
68import org.onosproject.net.flow.criteria.VlanIdCriterion;
69import org.onosproject.net.flow.criteria.VlanPcpCriterion;
70import org.onosproject.net.flow.instructions.Instruction;
71import org.onosproject.net.flow.instructions.Instructions;
72import org.onosproject.net.flow.instructions.L0ModificationInstruction;
73import org.onosproject.net.flow.instructions.L2ModificationInstruction;
74import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070075import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070076
Jian Li7c322f42016-03-04 11:00:59 -080077import java.io.IOException;
78import java.io.InputStream;
79import java.util.SortedMap;
80import java.util.TreeMap;
81
Jian Li2907ad22016-05-12 23:08:54 -070082import static org.easymock.EasyMock.anyShort;
Jian Li7c322f42016-03-04 11:00:59 -080083import static org.easymock.EasyMock.createMock;
84import static org.easymock.EasyMock.expect;
85import static org.easymock.EasyMock.replay;
86import static org.hamcrest.MatcherAssert.assertThat;
87import static org.hamcrest.Matchers.instanceOf;
88import static org.hamcrest.Matchers.is;
89import static org.hamcrest.Matchers.notNullValue;
90import static org.onosproject.net.NetTestTools.APP_ID;
Ray Milkeyd43fe452015-05-29 09:35:12 -070091
92/**
93 * Flow rule codec unit tests.
94 */
95public class FlowRuleCodecTest {
96
97 MockCodecContext context;
98 JsonCodec<FlowRule> flowRuleCodec;
99 final CoreService mockCoreService = createMock(CoreService.class);
100
101 /**
102 * Sets up for each test. Creates a context and fetches the flow rule
103 * codec.
104 */
105 @Before
106 public void setUp() {
107 context = new MockCodecContext();
108 flowRuleCodec = context.codec(FlowRule.class);
109 assertThat(flowRuleCodec, notNullValue());
110
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700111 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
Ray Milkeyd43fe452015-05-29 09:35:12 -0700112 .andReturn(APP_ID).anyTimes();
Jian Li2907ad22016-05-12 23:08:54 -0700113 expect(mockCoreService.getAppId(anyShort())).andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700114 replay(mockCoreService);
115 context.registerService(CoreService.class, mockCoreService);
116 }
117
118 /**
119 * Reads in a rule from the given resource and decodes it.
120 *
121 * @param resourceName resource to use to read the JSON for the rule
122 * @return decoded flow rule
123 * @throws IOException if processing the resource fails
124 */
125 private FlowRule getRule(String resourceName) throws IOException {
126 InputStream jsonStream = FlowRuleCodecTest.class
127 .getResourceAsStream(resourceName);
128 JsonNode json = context.mapper().readTree(jsonStream);
129 assertThat(json, notNullValue());
130 FlowRule rule = flowRuleCodec.decode((ObjectNode) json, context);
131 assertThat(rule, notNullValue());
132 return rule;
133 }
134
135 /**
136 * Checks that the data shared by all the resources is correct for a
137 * given rule.
138 *
139 * @param rule rule to check
140 */
141 private void checkCommonData(FlowRule rule) {
142 assertThat(rule.appId(), is(APP_ID.id()));
143 assertThat(rule.isPermanent(), is(false));
144 assertThat(rule.timeout(), is(1));
145 assertThat(rule.priority(), is(1));
Jian Li7c322f42016-03-04 11:00:59 -0800146 assertThat(rule.tableId(), is(1));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700147 assertThat(rule.deviceId().toString(), is("of:0000000000000001"));
148 }
149
150 /**
151 * Checks that a simple rule decodes properly.
152 *
153 * @throws IOException if the resource cannot be processed
154 */
155 @Test
156 public void codecSimpleFlowTest() throws IOException {
157 FlowRule rule = getRule("simple-flow.json");
158
159 checkCommonData(rule);
160
161 assertThat(rule.selector().criteria().size(), is(1));
162 Criterion criterion1 = rule.selector().criteria().iterator().next();
163 assertThat(criterion1.type(), is(Criterion.Type.ETH_TYPE));
alshabibcaf1ca22015-06-25 15:18:16 -0700164 assertThat(((EthTypeCriterion) criterion1).ethType(), is(new EthType(2054)));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700165
166 assertThat(rule.treatment().allInstructions().size(), is(1));
167 Instruction instruction1 = rule.treatment().allInstructions().get(0);
168 assertThat(instruction1.type(), is(Instruction.Type.OUTPUT));
169 assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.CONTROLLER));
170 }
171
172 SortedMap<String, Instruction> instructions = new TreeMap<>();
173
174 /**
Jian Li2907ad22016-05-12 23:08:54 -0700175 * Checks that a simple rule encodes properly.
176 */
177 @Test
178 public void testFlowRuleEncode() {
179
180 DeviceId deviceId = DeviceId.deviceId("of:000000000000000a");
181 FlowRule permFlowRule = DefaultFlowRule.builder()
182 .withCookie(1)
183 .forTable(1)
184 .withPriority(1)
185 .makePermanent()
186 .forDevice(deviceId).build();
187
188 FlowRule tempFlowRule = DefaultFlowRule.builder()
189 .withCookie(1)
190 .forTable(1)
191 .withPriority(1)
192 .makeTemporary(1000)
193 .forDevice(deviceId).build();
194
195 ObjectNode permFlowRuleJson = flowRuleCodec.encode(permFlowRule, context);
196 ObjectNode tempFlowRuleJson = flowRuleCodec.encode(tempFlowRule, context);
197
198 assertThat(permFlowRuleJson, FlowRuleJsonMatcher.matchesFlowRule(permFlowRule));
199 assertThat(tempFlowRuleJson, FlowRuleJsonMatcher.matchesFlowRule(tempFlowRule));
200 }
201
202 private static final class FlowRuleJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
203
204 private final FlowRule flowRule;
205
206 private FlowRuleJsonMatcher(FlowRule flowRule) {
207 this.flowRule = flowRule;
208 }
209
210 @Override
211 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
212
213 // check id
214 long jsonId = jsonNode.get("id").asLong();
215 long id = flowRule.id().id();
216 if (jsonId != id) {
217 description.appendText("flow rule id was " + jsonId);
218 return false;
219 }
220
221 // TODO: need to check application ID
222
223 // check tableId
224 int jsonTableId = jsonNode.get("tableId").asInt();
225 int tableId = flowRule.tableId();
226 if (jsonTableId != tableId) {
227 description.appendText("table id was " + jsonId);
228 return false;
229 }
230
231 // check priority
232 int jsonPriority = jsonNode.get("priority").asInt();
233 int priority = flowRule.priority();
234 if (jsonPriority != priority) {
235 description.appendText("priority was " + jsonPriority);
236 return false;
237 }
238
239 // check timeout
240 int jsonTimeout = jsonNode.get("timeout").asInt();
241 int timeout = flowRule.timeout();
242 if (jsonTimeout != timeout) {
243 description.appendText("timeout was " + jsonTimeout);
244 return false;
245 }
246
247 // check isPermanent
248 boolean jsonIsPermanent = jsonNode.get("isPermanent").asBoolean();
249 boolean isPermanent = flowRule.isPermanent();
250 if (jsonIsPermanent != isPermanent) {
251 description.appendText("isPermanent was " + jsonIsPermanent);
252 return false;
253 }
254
255 // check deviceId
256 String jsonDeviceId = jsonNode.get("deviceId").asText();
257 String deviceId = flowRule.deviceId().toString();
258 if (!jsonDeviceId.equals(deviceId)) {
259 description.appendText("deviceId was " + jsonDeviceId);
260 return false;
261 }
262
263 // TODO: need to check traffic treatment
264
265 // TODO: need to check selector
266
267 return true;
268 }
269
270 @Override
271 public void describeTo(Description description) {
272 description.appendText(flowRule.toString());
273 }
274
275 /**
276 * Factory to allocate a flow rule matcher.
277 *
278 * @param flowRule flow rule object we are looking for
279 * @return matcher
280 */
281 public static FlowRuleJsonMatcher matchesFlowRule(FlowRule flowRule) {
282 return new FlowRuleJsonMatcher(flowRule);
283 }
284 }
285
286 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700287 * Looks up an instruction in the instruction map based on type and subtype.
288 *
289 * @param type type string
290 * @param subType subtype string
291 * @return instruction that matches
292 */
293 private Instruction getInstruction(Instruction.Type type, String subType) {
294 Instruction instruction = instructions.get(type.name() + "/" + subType);
295 assertThat(instruction, notNullValue());
296 assertThat(instruction.type(), is(type));
297 return instruction;
298 }
299
300 /**
301 * Checks that a rule with one of each instruction type decodes properly.
302 *
303 * @throws IOException if the resource cannot be processed
304 */
305 @Test
306 public void decodeInstructionsFlowTest() throws Exception {
307 FlowRule rule = getRule("instructions-flow.json");
308
309 checkCommonData(rule);
310
311 rule.treatment().allInstructions()
312 .stream()
313 .forEach(instruction ->
314 {
315 String subType;
316 if (instruction.type() == Instruction.Type.L0MODIFICATION) {
317 subType = ((L0ModificationInstruction) instruction)
318 .subtype().name();
319 } else if (instruction.type() == Instruction.Type.L2MODIFICATION) {
320 subType = ((L2ModificationInstruction) instruction)
321 .subtype().name();
322 } else if (instruction.type() == Instruction.Type.L3MODIFICATION) {
323 subType = ((L3ModificationInstruction) instruction)
324 .subtype().name();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700325 } else if (instruction.type() == Instruction.Type.L4MODIFICATION) {
326 subType = ((L4ModificationInstruction) instruction)
327 .subtype().name();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700328 } else {
329 subType = "";
330 }
331 instructions.put(
332 instruction.type().name() + "/" + subType, instruction);
333 });
334
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800335 assertThat(rule.treatment().allInstructions().size(), is(23));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700336
337 Instruction instruction;
338
339 instruction = getInstruction(Instruction.Type.OUTPUT, "");
340 assertThat(instruction.type(), is(Instruction.Type.OUTPUT));
341 assertThat(((Instructions.OutputInstruction) instruction).port(), is(PortNumber.CONTROLLER));
342
343 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
344 L2ModificationInstruction.L2SubType.ETH_SRC.name());
345 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
346 assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(),
347 is(MacAddress.valueOf("12:34:56:78:90:12")));
348
349 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
350 L2ModificationInstruction.L2SubType.ETH_DST.name());
351 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
352 assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(),
353 is(MacAddress.valueOf("98:76:54:32:01:00")));
354
355 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
356 L2ModificationInstruction.L2SubType.VLAN_ID.name());
357 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
358 assertThat(((L2ModificationInstruction.ModVlanIdInstruction) instruction).vlanId().toShort(),
359 is((short) 22));
360
361 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
362 L2ModificationInstruction.L2SubType.VLAN_PCP.name());
363 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
364 assertThat(((L2ModificationInstruction.ModVlanPcpInstruction) instruction).vlanPcp(),
365 is((byte) 1));
366
367 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
368 L2ModificationInstruction.L2SubType.MPLS_LABEL.name());
369 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
370 assertThat(((L2ModificationInstruction.ModMplsLabelInstruction) instruction)
Ray Milkey125572b2016-02-22 16:48:17 -0800371 .label().toInt(),
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700372 is(MplsLabel.MAX_MPLS));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700373
374 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
375 L2ModificationInstruction.L2SubType.MPLS_PUSH.name());
376 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
alshabib7b808c52015-06-26 14:22:24 -0700377 assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction)
378 .ethernetType().toShort(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700379 is(Ethernet.MPLS_UNICAST));
380
381 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
382 L2ModificationInstruction.L2SubType.MPLS_POP.name());
383 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
alshabib7b808c52015-06-26 14:22:24 -0700384 assertThat(((L2ModificationInstruction.PushHeaderInstructions) instruction)
385 .ethernetType().toShort(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700386 is(Ethernet.MPLS_UNICAST));
387
388 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
389 L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name());
390 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
391 assertThat(instruction, instanceOf(L2ModificationInstruction.ModMplsTtlInstruction.class));
392
393 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
394 L2ModificationInstruction.L2SubType.VLAN_POP.name());
395 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
396 assertThat(instruction, instanceOf(L2ModificationInstruction.PopVlanInstruction.class));
397
398 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
399 L2ModificationInstruction.L2SubType.VLAN_PUSH.name());
400 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
401 assertThat(instruction, instanceOf(L2ModificationInstruction.PushHeaderInstructions.class));
402
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700403 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
404 L2ModificationInstruction.L2SubType.TUNNEL_ID.name());
405 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
406 assertThat(((L2ModificationInstruction.ModTunnelIdInstruction) instruction)
407 .tunnelId(), is(100L));
408
Ray Milkeyd43fe452015-05-29 09:35:12 -0700409 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
410 L3ModificationInstruction.L3SubType.IPV4_SRC.name());
411 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
412 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
413 is(IpAddress.valueOf("1.2.3.4")));
414
415 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
416 L3ModificationInstruction.L3SubType.IPV4_DST.name());
417 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
418 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
419 is(IpAddress.valueOf("1.2.3.3")));
420
421 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
422 L3ModificationInstruction.L3SubType.IPV6_SRC.name());
423 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
424 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
425 is(IpAddress.valueOf("1.2.3.2")));
426
427 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
428 L3ModificationInstruction.L3SubType.IPV6_DST.name());
429 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
430 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
431 is(IpAddress.valueOf("1.2.3.1")));
432
433 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
434 L3ModificationInstruction.L3SubType.IPV6_FLABEL.name());
435 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
436 assertThat(((L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction)
437 .flowLabel(),
438 is(8));
439
440 instruction = getInstruction(Instruction.Type.L0MODIFICATION,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700441 L0ModificationInstruction.L0SubType.OCH.name());
442 assertThat(instruction.type(), is(Instruction.Type.L0MODIFICATION));
443 L0ModificationInstruction.ModOchSignalInstruction och =
444 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
445 assertThat(och.lambda().spacingMultiplier(), is(4));
446 assertThat(och.lambda().slotGranularity(), is(8));
447 assertThat(och.lambda().gridType(), is(GridType.DWDM));
448 assertThat(och.lambda().channelSpacing(), is(ChannelSpacing.CHL_100GHZ));
Hyunsun Moonfab29502015-08-25 13:39:16 -0700449
450 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
451 L4ModificationInstruction.L4SubType.TCP_DST.name());
452 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
453 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
454 .port().toInt(), is(40001));
455
456 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
457 L4ModificationInstruction.L4SubType.TCP_SRC.name());
458 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
459 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
460 .port().toInt(), is(40002));
461
462 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
463 L4ModificationInstruction.L4SubType.UDP_DST.name());
464 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
465 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
466 .port().toInt(), is(40003));
467
468 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
469 L4ModificationInstruction.L4SubType.UDP_SRC.name());
470 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
471 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
472 .port().toInt(), is(40004));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700473 }
474
475 SortedMap<String, Criterion> criteria = new TreeMap<>();
476
477 /**
478 * Looks up a criterion in the instruction map based on type and subtype.
479 *
480 * @param type type string
481 * @return criterion that matches
482 */
483 private Criterion getCriterion(Criterion.Type type) {
484 Criterion criterion = criteria.get(type.name());
485 assertThat(criterion.type(), is(type));
486 return criterion;
487 }
488
489 /**
490 * Checks that a rule with one of each kind of criterion decodes properly.
491 *
492 * @throws IOException if the resource cannot be processed
493 */
494 @Test
495 public void codecCriteriaFlowTest() throws Exception {
496 FlowRule rule = getRule("criteria-flow.json");
497
498 checkCommonData(rule);
499
Sho SHIMIZU06810162016-02-24 12:52:32 -0800500 assertThat(rule.selector().criteria().size(), is(35));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700501
502 rule.selector().criteria()
503 .stream()
504 .forEach(criterion ->
505 criteria.put(criterion.type().name(), criterion));
506
507 Criterion criterion;
508
509 criterion = getCriterion(Criterion.Type.ETH_TYPE);
alshabibcaf1ca22015-06-25 15:18:16 -0700510 assertThat(((EthTypeCriterion) criterion).ethType(), is(new EthType(2054)));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700511
512 criterion = getCriterion(Criterion.Type.ETH_DST);
513 assertThat(((EthCriterion) criterion).mac(),
514 is(MacAddress.valueOf("00:11:22:33:44:55")));
515
516 criterion = getCriterion(Criterion.Type.ETH_SRC);
517 assertThat(((EthCriterion) criterion).mac(),
518 is(MacAddress.valueOf("00:11:22:33:44:55")));
519
520 criterion = getCriterion(Criterion.Type.IN_PORT);
521 assertThat(((PortCriterion) criterion).port(),
522 is(PortNumber.portNumber(23)));
523
524 criterion = getCriterion(Criterion.Type.IN_PHY_PORT);
525 assertThat(((PortCriterion) criterion).port(),
526 is(PortNumber.portNumber(44)));
527
528 criterion = getCriterion(Criterion.Type.VLAN_VID);
529 assertThat(((VlanIdCriterion) criterion).vlanId(),
530 is(VlanId.vlanId((short) 777)));
531
532 criterion = getCriterion(Criterion.Type.VLAN_PCP);
533 assertThat(((VlanPcpCriterion) criterion).priority(),
534 is(((byte) 3)));
535
536 criterion = getCriterion(Criterion.Type.IP_DSCP);
537 assertThat(((IPDscpCriterion) criterion).ipDscp(),
538 is(((byte) 2)));
539
540 criterion = getCriterion(Criterion.Type.IP_ECN);
541 assertThat(((IPEcnCriterion) criterion).ipEcn(),
542 is(((byte) 1)));
543
544 criterion = getCriterion(Criterion.Type.IP_PROTO);
545 assertThat(((IPProtocolCriterion) criterion).protocol(),
546 is(((short) 4)));
547
548 criterion = getCriterion(Criterion.Type.IPV4_SRC);
549 assertThat(((IPCriterion) criterion).ip(),
550 is((IpPrefix.valueOf("1.2.0.0/32"))));
551
552 criterion = getCriterion(Criterion.Type.IPV4_DST);
553 assertThat(((IPCriterion) criterion).ip(),
554 is((IpPrefix.valueOf("2.2.0.0/32"))));
555
556 criterion = getCriterion(Criterion.Type.IPV6_SRC);
557 assertThat(((IPCriterion) criterion).ip(),
558 is((IpPrefix.valueOf("3.2.0.0/32"))));
559
560 criterion = getCriterion(Criterion.Type.IPV6_DST);
561 assertThat(((IPCriterion) criterion).ip(),
562 is((IpPrefix.valueOf("4.2.0.0/32"))));
563
564 criterion = getCriterion(Criterion.Type.TCP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700565 assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700566 is(80));
567
568 criterion = getCriterion(Criterion.Type.TCP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700569 assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700570 is(443));
571
572 criterion = getCriterion(Criterion.Type.UDP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700573 assertThat(((UdpPortCriterion) criterion).udpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700574 is(180));
575
576 criterion = getCriterion(Criterion.Type.UDP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700577 assertThat(((UdpPortCriterion) criterion).udpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700578 is(1443));
579
580 criterion = getCriterion(Criterion.Type.SCTP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700581 assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700582 is(280));
583
584 criterion = getCriterion(Criterion.Type.SCTP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700585 assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700586 is(2443));
587
588 criterion = getCriterion(Criterion.Type.ICMPV4_TYPE);
589 assertThat(((IcmpTypeCriterion) criterion).icmpType(),
590 is((short) 24));
591
592 criterion = getCriterion(Criterion.Type.ICMPV4_CODE);
593 assertThat(((IcmpCodeCriterion) criterion).icmpCode(),
594 is((short) 16));
595
596 criterion = getCriterion(Criterion.Type.ICMPV6_TYPE);
597 assertThat(((Icmpv6TypeCriterion) criterion).icmpv6Type(),
598 is((short) 14));
599
600 criterion = getCriterion(Criterion.Type.ICMPV6_CODE);
601 assertThat(((Icmpv6CodeCriterion) criterion).icmpv6Code(),
602 is((short) 6));
603
604 criterion = getCriterion(Criterion.Type.IPV6_FLABEL);
605 assertThat(((IPv6FlowLabelCriterion) criterion).flowLabel(),
606 is(8));
607
608 criterion = getCriterion(Criterion.Type.IPV6_ND_TARGET);
609 assertThat(((IPv6NDTargetAddressCriterion) criterion)
610 .targetAddress().toString(),
611 is("1111:2222:3333:4444:5555:6666:7777:8888"));
612
613 criterion = getCriterion(Criterion.Type.IPV6_ND_SLL);
614 assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(),
615 is(MacAddress.valueOf("00:11:22:33:44:56")));
616
617 criterion = getCriterion(Criterion.Type.IPV6_ND_TLL);
618 assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(),
619 is(MacAddress.valueOf("00:11:22:33:44:57")));
620
621 criterion = getCriterion(Criterion.Type.MPLS_LABEL);
622 assertThat(((MplsCriterion) criterion).label(),
623 is(MplsLabel.mplsLabel(123)));
624
625 criterion = getCriterion(Criterion.Type.IPV6_EXTHDR);
626 assertThat(((IPv6ExthdrFlagsCriterion) criterion).exthdrFlags(),
627 is(99));
628
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700629 criterion = getCriterion(Criterion.Type.TUNNEL_ID);
630 assertThat(((TunnelIdCriterion) criterion).tunnelId(),
631 is(100L));
Yafit Hadar5796d972015-10-15 13:16:11 +0300632
633 criterion = getCriterion(Criterion.Type.OCH_SIGTYPE);
634 assertThat(((OchSignalTypeCriterion) criterion).signalType(),
635 is(OchSignalType.FIXED_GRID));
636
637 criterion = getCriterion(Criterion.Type.ODU_SIGTYPE);
638 assertThat(((OduSignalTypeCriterion) criterion).signalType(),
639 is(OduSignalType.ODU4));
640
641
642 criterion = getCriterion(Criterion.Type.ODU_SIGID);
643 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributaryPortNumber(),
644 is(1));
645
646 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotLength(),
647 is(80));
648
649 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotBitmap(),
Jian Li68c4fc42016-01-11 16:07:03 -0800650 is(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700651 }
652
653 /**
654 * Checks that a rule with a SigId criterion decodes properly.
655 *
656 * @throws IOException if the resource cannot be processed
657 */
658 @Test
659 public void codecSigIdCriteriaFlowTest() throws Exception {
660 FlowRule rule = getRule("sigid-flow.json");
661
662 checkCommonData(rule);
663
664 assertThat(rule.selector().criteria().size(), is(1));
665 Criterion criterion = rule.selector().criteria().iterator().next();
666 assertThat(criterion.type(), is(Criterion.Type.OCH_SIGID));
667 Lambda lambda = ((OchSignalCriterion) criterion).lambda();
668 assertThat(lambda, instanceOf(OchSignal.class));
669 OchSignal ochSignal = (OchSignal) lambda;
670 assertThat(ochSignal.spacingMultiplier(), is(3));
671 assertThat(ochSignal.slotGranularity(), is(4));
672 assertThat(ochSignal.gridType(), is(GridType.CWDM));
673 assertThat(ochSignal.channelSpacing(), is(ChannelSpacing.CHL_25GHZ));
674 }
675
676}