blob: 11df9a132dd26706a153f924aa0af4f1dd9479d5 [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;
Jian Lie2a59f42016-05-18 15:15:53 -070042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
Ray Milkeyd43fe452015-05-29 09:35:12 -070044import org.onosproject.net.flow.FlowRule;
Jian Lie2a59f42016-05-18 15:15:53 -070045import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flow.criteria.Criteria;
Ray Milkeyd43fe452015-05-29 09:35:12 -070048import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.EthTypeCriterion;
51import org.onosproject.net.flow.criteria.IPCriterion;
52import org.onosproject.net.flow.criteria.IPDscpCriterion;
53import org.onosproject.net.flow.criteria.IPEcnCriterion;
54import org.onosproject.net.flow.criteria.IPProtocolCriterion;
55import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
56import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
57import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
58import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
59import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
60import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
61import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
62import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070063import org.onosproject.net.flow.criteria.MplsCriterion;
64import org.onosproject.net.flow.criteria.OchSignalCriterion;
Yafit Hadar5796d972015-10-15 13:16:11 +030065import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
66import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
67import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070068import org.onosproject.net.flow.criteria.PortCriterion;
69import org.onosproject.net.flow.criteria.SctpPortCriterion;
70import org.onosproject.net.flow.criteria.TcpPortCriterion;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -070071import org.onosproject.net.flow.criteria.TunnelIdCriterion;
Ray Milkeyd43fe452015-05-29 09:35:12 -070072import org.onosproject.net.flow.criteria.UdpPortCriterion;
73import org.onosproject.net.flow.criteria.VlanIdCriterion;
74import org.onosproject.net.flow.criteria.VlanPcpCriterion;
75import org.onosproject.net.flow.instructions.Instruction;
76import org.onosproject.net.flow.instructions.Instructions;
77import org.onosproject.net.flow.instructions.L0ModificationInstruction;
78import org.onosproject.net.flow.instructions.L2ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070080import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070081
Jian Li7c322f42016-03-04 11:00:59 -080082import java.io.IOException;
83import java.io.InputStream;
84import java.util.SortedMap;
85import java.util.TreeMap;
86
Jian Li2907ad22016-05-12 23:08:54 -070087import static org.easymock.EasyMock.anyShort;
Jian Li7c322f42016-03-04 11:00:59 -080088import static org.easymock.EasyMock.createMock;
89import static org.easymock.EasyMock.expect;
90import static org.easymock.EasyMock.replay;
91import static org.hamcrest.MatcherAssert.assertThat;
92import static org.hamcrest.Matchers.instanceOf;
93import static org.hamcrest.Matchers.is;
94import static org.hamcrest.Matchers.notNullValue;
95import static org.onosproject.net.NetTestTools.APP_ID;
Ray Milkeyd43fe452015-05-29 09:35:12 -070096
97/**
98 * Flow rule codec unit tests.
99 */
100public class FlowRuleCodecTest {
101
102 MockCodecContext context;
103 JsonCodec<FlowRule> flowRuleCodec;
104 final CoreService mockCoreService = createMock(CoreService.class);
105
106 /**
107 * Sets up for each test. Creates a context and fetches the flow rule
108 * codec.
109 */
110 @Before
111 public void setUp() {
112 context = new MockCodecContext();
113 flowRuleCodec = context.codec(FlowRule.class);
114 assertThat(flowRuleCodec, notNullValue());
115
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700116 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
Ray Milkeyd43fe452015-05-29 09:35:12 -0700117 .andReturn(APP_ID).anyTimes();
Jian Li2907ad22016-05-12 23:08:54 -0700118 expect(mockCoreService.getAppId(anyShort())).andReturn(APP_ID).anyTimes();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700119 replay(mockCoreService);
120 context.registerService(CoreService.class, mockCoreService);
121 }
122
123 /**
124 * Reads in a rule from the given resource and decodes it.
125 *
126 * @param resourceName resource to use to read the JSON for the rule
127 * @return decoded flow rule
128 * @throws IOException if processing the resource fails
129 */
130 private FlowRule getRule(String resourceName) throws IOException {
131 InputStream jsonStream = FlowRuleCodecTest.class
132 .getResourceAsStream(resourceName);
133 JsonNode json = context.mapper().readTree(jsonStream);
134 assertThat(json, notNullValue());
135 FlowRule rule = flowRuleCodec.decode((ObjectNode) json, context);
136 assertThat(rule, notNullValue());
137 return rule;
138 }
139
140 /**
141 * Checks that the data shared by all the resources is correct for a
142 * given rule.
143 *
144 * @param rule rule to check
145 */
146 private void checkCommonData(FlowRule rule) {
147 assertThat(rule.appId(), is(APP_ID.id()));
148 assertThat(rule.isPermanent(), is(false));
149 assertThat(rule.timeout(), is(1));
150 assertThat(rule.priority(), is(1));
Jian Li7c322f42016-03-04 11:00:59 -0800151 assertThat(rule.tableId(), is(1));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700152 assertThat(rule.deviceId().toString(), is("of:0000000000000001"));
153 }
154
155 /**
156 * Checks that a simple rule decodes properly.
157 *
158 * @throws IOException if the resource cannot be processed
159 */
160 @Test
161 public void codecSimpleFlowTest() throws IOException {
162 FlowRule rule = getRule("simple-flow.json");
163
164 checkCommonData(rule);
165
166 assertThat(rule.selector().criteria().size(), is(1));
167 Criterion criterion1 = rule.selector().criteria().iterator().next();
168 assertThat(criterion1.type(), is(Criterion.Type.ETH_TYPE));
alshabibcaf1ca22015-06-25 15:18:16 -0700169 assertThat(((EthTypeCriterion) criterion1).ethType(), is(new EthType(2054)));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700170
171 assertThat(rule.treatment().allInstructions().size(), is(1));
172 Instruction instruction1 = rule.treatment().allInstructions().get(0);
173 assertThat(instruction1.type(), is(Instruction.Type.OUTPUT));
174 assertThat(((Instructions.OutputInstruction) instruction1).port(), is(PortNumber.CONTROLLER));
175 }
176
177 SortedMap<String, Instruction> instructions = new TreeMap<>();
178
179 /**
Jian Li2907ad22016-05-12 23:08:54 -0700180 * Checks that a simple rule encodes properly.
181 */
182 @Test
183 public void testFlowRuleEncode() {
184
185 DeviceId deviceId = DeviceId.deviceId("of:000000000000000a");
Jian Lie2a59f42016-05-18 15:15:53 -0700186
187 Instruction output = Instructions.createOutput(PortNumber.portNumber(0));
188 Instruction modL2Src = Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
189 Instruction modL2Dst = Instructions.modL2Dst(MacAddress.valueOf("44:55:66:77:88:99"));
190 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
191 TrafficTreatment treatment = tBuilder
192 .add(output)
193 .add(modL2Src)
194 .add(modL2Dst)
195 .build();
196
197 Criterion inPort = Criteria.matchInPort(PortNumber.portNumber(0));
198 Criterion ethSrc = Criteria.matchEthSrc(MacAddress.valueOf("11:22:33:44:55:66"));
199 Criterion ethDst = Criteria.matchEthDst(MacAddress.valueOf("44:55:66:77:88:99"));
200 Criterion ethType = Criteria.matchEthType(Ethernet.TYPE_IPV4);
201
202 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
203 TrafficSelector selector = sBuilder
204 .add(inPort)
205 .add(ethSrc)
206 .add(ethDst)
207 .add(ethType)
208 .build();
209
Jian Li2907ad22016-05-12 23:08:54 -0700210 FlowRule permFlowRule = DefaultFlowRule.builder()
211 .withCookie(1)
212 .forTable(1)
213 .withPriority(1)
214 .makePermanent()
Jian Lie2a59f42016-05-18 15:15:53 -0700215 .withTreatment(treatment)
216 .withSelector(selector)
Jian Li2907ad22016-05-12 23:08:54 -0700217 .forDevice(deviceId).build();
218
219 FlowRule tempFlowRule = DefaultFlowRule.builder()
220 .withCookie(1)
221 .forTable(1)
222 .withPriority(1)
223 .makeTemporary(1000)
Jian Lie2a59f42016-05-18 15:15:53 -0700224 .withTreatment(treatment)
225 .withSelector(selector)
Jian Li2907ad22016-05-12 23:08:54 -0700226 .forDevice(deviceId).build();
227
228 ObjectNode permFlowRuleJson = flowRuleCodec.encode(permFlowRule, context);
229 ObjectNode tempFlowRuleJson = flowRuleCodec.encode(tempFlowRule, context);
230
231 assertThat(permFlowRuleJson, FlowRuleJsonMatcher.matchesFlowRule(permFlowRule));
232 assertThat(tempFlowRuleJson, FlowRuleJsonMatcher.matchesFlowRule(tempFlowRule));
233 }
234
235 private static final class FlowRuleJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
236
237 private final FlowRule flowRule;
238
239 private FlowRuleJsonMatcher(FlowRule flowRule) {
240 this.flowRule = flowRule;
241 }
242
243 @Override
244 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
245
246 // check id
247 long jsonId = jsonNode.get("id").asLong();
248 long id = flowRule.id().id();
249 if (jsonId != id) {
250 description.appendText("flow rule id was " + jsonId);
251 return false;
252 }
253
254 // TODO: need to check application ID
255
256 // check tableId
257 int jsonTableId = jsonNode.get("tableId").asInt();
258 int tableId = flowRule.tableId();
259 if (jsonTableId != tableId) {
260 description.appendText("table id was " + jsonId);
261 return false;
262 }
263
264 // check priority
265 int jsonPriority = jsonNode.get("priority").asInt();
266 int priority = flowRule.priority();
267 if (jsonPriority != priority) {
268 description.appendText("priority was " + jsonPriority);
269 return false;
270 }
271
272 // check timeout
273 int jsonTimeout = jsonNode.get("timeout").asInt();
274 int timeout = flowRule.timeout();
275 if (jsonTimeout != timeout) {
276 description.appendText("timeout was " + jsonTimeout);
277 return false;
278 }
279
280 // check isPermanent
281 boolean jsonIsPermanent = jsonNode.get("isPermanent").asBoolean();
282 boolean isPermanent = flowRule.isPermanent();
283 if (jsonIsPermanent != isPermanent) {
284 description.appendText("isPermanent was " + jsonIsPermanent);
285 return false;
286 }
287
288 // check deviceId
289 String jsonDeviceId = jsonNode.get("deviceId").asText();
290 String deviceId = flowRule.deviceId().toString();
291 if (!jsonDeviceId.equals(deviceId)) {
292 description.appendText("deviceId was " + jsonDeviceId);
293 return false;
294 }
295
Jian Lie2a59f42016-05-18 15:15:53 -0700296 // check traffic treatment
297 JsonNode jsonTreatment = jsonNode.get("treatment");
298 TrafficTreatmentCodecTest.TrafficTreatmentJsonMatcher treatmentMatcher =
299 TrafficTreatmentCodecTest.TrafficTreatmentJsonMatcher
300 .matchesTrafficTreatment(flowRule.treatment());
Jian Li2907ad22016-05-12 23:08:54 -0700301
Jian Lie2a59f42016-05-18 15:15:53 -0700302 if (!treatmentMatcher.matches(jsonTreatment)) {
303 description.appendText("treatment is not matched");
304 return false;
305 }
306
307 // check traffic selector
308 JsonNode jsonSelector = jsonNode.get("selector");
309 TrafficSelectorCodecTest.TrafficSelectorJsonMatcher selectorMatcher =
310 TrafficSelectorCodecTest.TrafficSelectorJsonMatcher
311 .matchesTrafficSelector(flowRule.selector());
312
313 if (!selectorMatcher.matches(jsonSelector)) {
314 description.appendText("selector is not matched");
315 return false;
316 }
Jian Li2907ad22016-05-12 23:08:54 -0700317
318 return true;
319 }
320
321 @Override
322 public void describeTo(Description description) {
323 description.appendText(flowRule.toString());
324 }
325
326 /**
327 * Factory to allocate a flow rule matcher.
328 *
329 * @param flowRule flow rule object we are looking for
330 * @return matcher
331 */
332 public static FlowRuleJsonMatcher matchesFlowRule(FlowRule flowRule) {
333 return new FlowRuleJsonMatcher(flowRule);
334 }
335 }
336
337 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700338 * Looks up an instruction in the instruction map based on type and subtype.
339 *
340 * @param type type string
341 * @param subType subtype string
342 * @return instruction that matches
343 */
344 private Instruction getInstruction(Instruction.Type type, String subType) {
345 Instruction instruction = instructions.get(type.name() + "/" + subType);
346 assertThat(instruction, notNullValue());
347 assertThat(instruction.type(), is(type));
348 return instruction;
349 }
350
351 /**
352 * Checks that a rule with one of each instruction type decodes properly.
353 *
354 * @throws IOException if the resource cannot be processed
355 */
356 @Test
357 public void decodeInstructionsFlowTest() throws Exception {
358 FlowRule rule = getRule("instructions-flow.json");
359
360 checkCommonData(rule);
361
362 rule.treatment().allInstructions()
Ray Milkeyd43fe452015-05-29 09:35:12 -0700363 .forEach(instruction ->
364 {
365 String subType;
366 if (instruction.type() == Instruction.Type.L0MODIFICATION) {
367 subType = ((L0ModificationInstruction) instruction)
368 .subtype().name();
369 } else if (instruction.type() == Instruction.Type.L2MODIFICATION) {
370 subType = ((L2ModificationInstruction) instruction)
371 .subtype().name();
372 } else if (instruction.type() == Instruction.Type.L3MODIFICATION) {
373 subType = ((L3ModificationInstruction) instruction)
374 .subtype().name();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700375 } else if (instruction.type() == Instruction.Type.L4MODIFICATION) {
376 subType = ((L4ModificationInstruction) instruction)
377 .subtype().name();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700378 } else {
379 subType = "";
380 }
381 instructions.put(
382 instruction.type().name() + "/" + subType, instruction);
383 });
384
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800385 assertThat(rule.treatment().allInstructions().size(), is(23));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700386
387 Instruction instruction;
388
389 instruction = getInstruction(Instruction.Type.OUTPUT, "");
390 assertThat(instruction.type(), is(Instruction.Type.OUTPUT));
391 assertThat(((Instructions.OutputInstruction) instruction).port(), is(PortNumber.CONTROLLER));
392
393 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
394 L2ModificationInstruction.L2SubType.ETH_SRC.name());
395 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
396 assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(),
397 is(MacAddress.valueOf("12:34:56:78:90:12")));
398
399 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
400 L2ModificationInstruction.L2SubType.ETH_DST.name());
401 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
402 assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(),
403 is(MacAddress.valueOf("98:76:54:32:01:00")));
404
405 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
406 L2ModificationInstruction.L2SubType.VLAN_ID.name());
407 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
408 assertThat(((L2ModificationInstruction.ModVlanIdInstruction) instruction).vlanId().toShort(),
409 is((short) 22));
410
411 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
412 L2ModificationInstruction.L2SubType.VLAN_PCP.name());
413 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
414 assertThat(((L2ModificationInstruction.ModVlanPcpInstruction) instruction).vlanPcp(),
415 is((byte) 1));
416
417 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
418 L2ModificationInstruction.L2SubType.MPLS_LABEL.name());
419 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
420 assertThat(((L2ModificationInstruction.ModMplsLabelInstruction) instruction)
Ray Milkey125572b2016-02-22 16:48:17 -0800421 .label().toInt(),
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700422 is(MplsLabel.MAX_MPLS));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700423
424 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
425 L2ModificationInstruction.L2SubType.MPLS_PUSH.name());
426 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700427 assertThat(((L2ModificationInstruction.ModMplsHeaderInstruction) instruction)
alshabib7b808c52015-06-26 14:22:24 -0700428 .ethernetType().toShort(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700429 is(Ethernet.MPLS_UNICAST));
430
431 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
432 L2ModificationInstruction.L2SubType.MPLS_POP.name());
433 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700434 assertThat(((L2ModificationInstruction.ModMplsHeaderInstruction) instruction)
alshabib7b808c52015-06-26 14:22:24 -0700435 .ethernetType().toShort(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700436 is(Ethernet.MPLS_UNICAST));
437
438 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
439 L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name());
440 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
441 assertThat(instruction, instanceOf(L2ModificationInstruction.ModMplsTtlInstruction.class));
442
443 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
444 L2ModificationInstruction.L2SubType.VLAN_POP.name());
445 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700446 assertThat(instruction, instanceOf(L2ModificationInstruction.ModVlanHeaderInstruction.class));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700447
448 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
449 L2ModificationInstruction.L2SubType.VLAN_PUSH.name());
450 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700451 assertThat(instruction, instanceOf(L2ModificationInstruction.ModVlanHeaderInstruction.class));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700452
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700453 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
454 L2ModificationInstruction.L2SubType.TUNNEL_ID.name());
455 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
456 assertThat(((L2ModificationInstruction.ModTunnelIdInstruction) instruction)
457 .tunnelId(), is(100L));
458
Ray Milkeyd43fe452015-05-29 09:35:12 -0700459 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
460 L3ModificationInstruction.L3SubType.IPV4_SRC.name());
461 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
462 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
463 is(IpAddress.valueOf("1.2.3.4")));
464
465 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
466 L3ModificationInstruction.L3SubType.IPV4_DST.name());
467 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
468 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
469 is(IpAddress.valueOf("1.2.3.3")));
470
471 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
472 L3ModificationInstruction.L3SubType.IPV6_SRC.name());
473 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
474 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
475 is(IpAddress.valueOf("1.2.3.2")));
476
477 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
478 L3ModificationInstruction.L3SubType.IPV6_DST.name());
479 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
480 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
481 is(IpAddress.valueOf("1.2.3.1")));
482
483 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
484 L3ModificationInstruction.L3SubType.IPV6_FLABEL.name());
485 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
486 assertThat(((L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction)
487 .flowLabel(),
488 is(8));
489
490 instruction = getInstruction(Instruction.Type.L0MODIFICATION,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700491 L0ModificationInstruction.L0SubType.OCH.name());
492 assertThat(instruction.type(), is(Instruction.Type.L0MODIFICATION));
493 L0ModificationInstruction.ModOchSignalInstruction och =
494 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
495 assertThat(och.lambda().spacingMultiplier(), is(4));
496 assertThat(och.lambda().slotGranularity(), is(8));
497 assertThat(och.lambda().gridType(), is(GridType.DWDM));
498 assertThat(och.lambda().channelSpacing(), is(ChannelSpacing.CHL_100GHZ));
Hyunsun Moonfab29502015-08-25 13:39:16 -0700499
500 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
501 L4ModificationInstruction.L4SubType.TCP_DST.name());
502 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
503 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
504 .port().toInt(), is(40001));
505
506 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
507 L4ModificationInstruction.L4SubType.TCP_SRC.name());
508 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
509 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
510 .port().toInt(), is(40002));
511
512 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
513 L4ModificationInstruction.L4SubType.UDP_DST.name());
514 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
515 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
516 .port().toInt(), is(40003));
517
518 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
519 L4ModificationInstruction.L4SubType.UDP_SRC.name());
520 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
521 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
522 .port().toInt(), is(40004));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700523 }
524
525 SortedMap<String, Criterion> criteria = new TreeMap<>();
526
527 /**
528 * Looks up a criterion in the instruction map based on type and subtype.
529 *
530 * @param type type string
531 * @return criterion that matches
532 */
533 private Criterion getCriterion(Criterion.Type type) {
534 Criterion criterion = criteria.get(type.name());
535 assertThat(criterion.type(), is(type));
536 return criterion;
537 }
538
539 /**
540 * Checks that a rule with one of each kind of criterion decodes properly.
541 *
542 * @throws IOException if the resource cannot be processed
543 */
544 @Test
545 public void codecCriteriaFlowTest() throws Exception {
546 FlowRule rule = getRule("criteria-flow.json");
547
548 checkCommonData(rule);
549
Sho SHIMIZU06810162016-02-24 12:52:32 -0800550 assertThat(rule.selector().criteria().size(), is(35));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700551
552 rule.selector().criteria()
Ray Milkeyd43fe452015-05-29 09:35:12 -0700553 .forEach(criterion ->
554 criteria.put(criterion.type().name(), criterion));
555
556 Criterion criterion;
557
558 criterion = getCriterion(Criterion.Type.ETH_TYPE);
alshabibcaf1ca22015-06-25 15:18:16 -0700559 assertThat(((EthTypeCriterion) criterion).ethType(), is(new EthType(2054)));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700560
561 criterion = getCriterion(Criterion.Type.ETH_DST);
562 assertThat(((EthCriterion) criterion).mac(),
563 is(MacAddress.valueOf("00:11:22:33:44:55")));
564
565 criterion = getCriterion(Criterion.Type.ETH_SRC);
566 assertThat(((EthCriterion) criterion).mac(),
567 is(MacAddress.valueOf("00:11:22:33:44:55")));
568
569 criterion = getCriterion(Criterion.Type.IN_PORT);
570 assertThat(((PortCriterion) criterion).port(),
571 is(PortNumber.portNumber(23)));
572
573 criterion = getCriterion(Criterion.Type.IN_PHY_PORT);
574 assertThat(((PortCriterion) criterion).port(),
575 is(PortNumber.portNumber(44)));
576
577 criterion = getCriterion(Criterion.Type.VLAN_VID);
578 assertThat(((VlanIdCriterion) criterion).vlanId(),
579 is(VlanId.vlanId((short) 777)));
580
581 criterion = getCriterion(Criterion.Type.VLAN_PCP);
582 assertThat(((VlanPcpCriterion) criterion).priority(),
583 is(((byte) 3)));
584
585 criterion = getCriterion(Criterion.Type.IP_DSCP);
586 assertThat(((IPDscpCriterion) criterion).ipDscp(),
587 is(((byte) 2)));
588
589 criterion = getCriterion(Criterion.Type.IP_ECN);
590 assertThat(((IPEcnCriterion) criterion).ipEcn(),
591 is(((byte) 1)));
592
593 criterion = getCriterion(Criterion.Type.IP_PROTO);
594 assertThat(((IPProtocolCriterion) criterion).protocol(),
595 is(((short) 4)));
596
597 criterion = getCriterion(Criterion.Type.IPV4_SRC);
598 assertThat(((IPCriterion) criterion).ip(),
599 is((IpPrefix.valueOf("1.2.0.0/32"))));
600
601 criterion = getCriterion(Criterion.Type.IPV4_DST);
602 assertThat(((IPCriterion) criterion).ip(),
603 is((IpPrefix.valueOf("2.2.0.0/32"))));
604
605 criterion = getCriterion(Criterion.Type.IPV6_SRC);
606 assertThat(((IPCriterion) criterion).ip(),
607 is((IpPrefix.valueOf("3.2.0.0/32"))));
608
609 criterion = getCriterion(Criterion.Type.IPV6_DST);
610 assertThat(((IPCriterion) criterion).ip(),
611 is((IpPrefix.valueOf("4.2.0.0/32"))));
612
613 criterion = getCriterion(Criterion.Type.TCP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700614 assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700615 is(80));
616
617 criterion = getCriterion(Criterion.Type.TCP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700618 assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700619 is(443));
620
621 criterion = getCriterion(Criterion.Type.UDP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700622 assertThat(((UdpPortCriterion) criterion).udpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700623 is(180));
624
625 criterion = getCriterion(Criterion.Type.UDP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700626 assertThat(((UdpPortCriterion) criterion).udpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700627 is(1443));
628
629 criterion = getCriterion(Criterion.Type.SCTP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700630 assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700631 is(280));
632
633 criterion = getCriterion(Criterion.Type.SCTP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700634 assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700635 is(2443));
636
637 criterion = getCriterion(Criterion.Type.ICMPV4_TYPE);
638 assertThat(((IcmpTypeCriterion) criterion).icmpType(),
639 is((short) 24));
640
641 criterion = getCriterion(Criterion.Type.ICMPV4_CODE);
642 assertThat(((IcmpCodeCriterion) criterion).icmpCode(),
643 is((short) 16));
644
645 criterion = getCriterion(Criterion.Type.ICMPV6_TYPE);
646 assertThat(((Icmpv6TypeCriterion) criterion).icmpv6Type(),
647 is((short) 14));
648
649 criterion = getCriterion(Criterion.Type.ICMPV6_CODE);
650 assertThat(((Icmpv6CodeCriterion) criterion).icmpv6Code(),
651 is((short) 6));
652
653 criterion = getCriterion(Criterion.Type.IPV6_FLABEL);
654 assertThat(((IPv6FlowLabelCriterion) criterion).flowLabel(),
655 is(8));
656
657 criterion = getCriterion(Criterion.Type.IPV6_ND_TARGET);
658 assertThat(((IPv6NDTargetAddressCriterion) criterion)
659 .targetAddress().toString(),
660 is("1111:2222:3333:4444:5555:6666:7777:8888"));
661
662 criterion = getCriterion(Criterion.Type.IPV6_ND_SLL);
663 assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(),
664 is(MacAddress.valueOf("00:11:22:33:44:56")));
665
666 criterion = getCriterion(Criterion.Type.IPV6_ND_TLL);
667 assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(),
668 is(MacAddress.valueOf("00:11:22:33:44:57")));
669
670 criterion = getCriterion(Criterion.Type.MPLS_LABEL);
671 assertThat(((MplsCriterion) criterion).label(),
672 is(MplsLabel.mplsLabel(123)));
673
674 criterion = getCriterion(Criterion.Type.IPV6_EXTHDR);
675 assertThat(((IPv6ExthdrFlagsCriterion) criterion).exthdrFlags(),
676 is(99));
677
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700678 criterion = getCriterion(Criterion.Type.TUNNEL_ID);
679 assertThat(((TunnelIdCriterion) criterion).tunnelId(),
680 is(100L));
Yafit Hadar5796d972015-10-15 13:16:11 +0300681
682 criterion = getCriterion(Criterion.Type.OCH_SIGTYPE);
683 assertThat(((OchSignalTypeCriterion) criterion).signalType(),
684 is(OchSignalType.FIXED_GRID));
685
686 criterion = getCriterion(Criterion.Type.ODU_SIGTYPE);
687 assertThat(((OduSignalTypeCriterion) criterion).signalType(),
688 is(OduSignalType.ODU4));
689
690
691 criterion = getCriterion(Criterion.Type.ODU_SIGID);
692 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributaryPortNumber(),
693 is(1));
694
695 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotLength(),
696 is(80));
697
698 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotBitmap(),
Jian Li68c4fc42016-01-11 16:07:03 -0800699 is(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700700 }
701
702 /**
703 * Checks that a rule with a SigId criterion decodes properly.
704 *
705 * @throws IOException if the resource cannot be processed
706 */
707 @Test
708 public void codecSigIdCriteriaFlowTest() throws Exception {
709 FlowRule rule = getRule("sigid-flow.json");
710
711 checkCommonData(rule);
712
713 assertThat(rule.selector().criteria().size(), is(1));
714 Criterion criterion = rule.selector().criteria().iterator().next();
715 assertThat(criterion.type(), is(Criterion.Type.OCH_SIGID));
716 Lambda lambda = ((OchSignalCriterion) criterion).lambda();
717 assertThat(lambda, instanceOf(OchSignal.class));
718 OchSignal ochSignal = (OchSignal) lambda;
719 assertThat(ochSignal.spacingMultiplier(), is(3));
720 assertThat(ochSignal.slotGranularity(), is(4));
721 assertThat(ochSignal.gridType(), is(GridType.CWDM));
722 assertThat(ochSignal.channelSpacing(), is(ChannelSpacing.CHL_25GHZ));
723 }
724
725}