blob: 6b507e11185362e95c212f089e9b569adf0b2b0e [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()
363 .stream()
364 .forEach(instruction ->
365 {
366 String subType;
367 if (instruction.type() == Instruction.Type.L0MODIFICATION) {
368 subType = ((L0ModificationInstruction) instruction)
369 .subtype().name();
370 } else if (instruction.type() == Instruction.Type.L2MODIFICATION) {
371 subType = ((L2ModificationInstruction) instruction)
372 .subtype().name();
373 } else if (instruction.type() == Instruction.Type.L3MODIFICATION) {
374 subType = ((L3ModificationInstruction) instruction)
375 .subtype().name();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700376 } else if (instruction.type() == Instruction.Type.L4MODIFICATION) {
377 subType = ((L4ModificationInstruction) instruction)
378 .subtype().name();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700379 } else {
380 subType = "";
381 }
382 instructions.put(
383 instruction.type().name() + "/" + subType, instruction);
384 });
385
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800386 assertThat(rule.treatment().allInstructions().size(), is(23));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700387
388 Instruction instruction;
389
390 instruction = getInstruction(Instruction.Type.OUTPUT, "");
391 assertThat(instruction.type(), is(Instruction.Type.OUTPUT));
392 assertThat(((Instructions.OutputInstruction) instruction).port(), is(PortNumber.CONTROLLER));
393
394 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
395 L2ModificationInstruction.L2SubType.ETH_SRC.name());
396 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
397 assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(),
398 is(MacAddress.valueOf("12:34:56:78:90:12")));
399
400 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
401 L2ModificationInstruction.L2SubType.ETH_DST.name());
402 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
403 assertThat(((L2ModificationInstruction.ModEtherInstruction) instruction).mac(),
404 is(MacAddress.valueOf("98:76:54:32:01:00")));
405
406 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
407 L2ModificationInstruction.L2SubType.VLAN_ID.name());
408 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
409 assertThat(((L2ModificationInstruction.ModVlanIdInstruction) instruction).vlanId().toShort(),
410 is((short) 22));
411
412 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
413 L2ModificationInstruction.L2SubType.VLAN_PCP.name());
414 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
415 assertThat(((L2ModificationInstruction.ModVlanPcpInstruction) instruction).vlanPcp(),
416 is((byte) 1));
417
418 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
419 L2ModificationInstruction.L2SubType.MPLS_LABEL.name());
420 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
421 assertThat(((L2ModificationInstruction.ModMplsLabelInstruction) instruction)
Ray Milkey125572b2016-02-22 16:48:17 -0800422 .label().toInt(),
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700423 is(MplsLabel.MAX_MPLS));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700424
425 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
426 L2ModificationInstruction.L2SubType.MPLS_PUSH.name());
427 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700428 assertThat(((L2ModificationInstruction.ModMplsHeaderInstruction) instruction)
alshabib7b808c52015-06-26 14:22:24 -0700429 .ethernetType().toShort(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700430 is(Ethernet.MPLS_UNICAST));
431
432 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
433 L2ModificationInstruction.L2SubType.MPLS_POP.name());
434 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700435 assertThat(((L2ModificationInstruction.ModMplsHeaderInstruction) instruction)
alshabib7b808c52015-06-26 14:22:24 -0700436 .ethernetType().toShort(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700437 is(Ethernet.MPLS_UNICAST));
438
439 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
440 L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name());
441 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
442 assertThat(instruction, instanceOf(L2ModificationInstruction.ModMplsTtlInstruction.class));
443
444 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
445 L2ModificationInstruction.L2SubType.VLAN_POP.name());
446 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700447 assertThat(instruction, instanceOf(L2ModificationInstruction.ModVlanHeaderInstruction.class));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700448
449 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
450 L2ModificationInstruction.L2SubType.VLAN_PUSH.name());
451 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
Jian Li11260a02016-05-19 13:07:22 -0700452 assertThat(instruction, instanceOf(L2ModificationInstruction.ModVlanHeaderInstruction.class));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700453
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700454 instruction = getInstruction(Instruction.Type.L2MODIFICATION,
455 L2ModificationInstruction.L2SubType.TUNNEL_ID.name());
456 assertThat(instruction.type(), is(Instruction.Type.L2MODIFICATION));
457 assertThat(((L2ModificationInstruction.ModTunnelIdInstruction) instruction)
458 .tunnelId(), is(100L));
459
Ray Milkeyd43fe452015-05-29 09:35:12 -0700460 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
461 L3ModificationInstruction.L3SubType.IPV4_SRC.name());
462 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
463 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
464 is(IpAddress.valueOf("1.2.3.4")));
465
466 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
467 L3ModificationInstruction.L3SubType.IPV4_DST.name());
468 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
469 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
470 is(IpAddress.valueOf("1.2.3.3")));
471
472 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
473 L3ModificationInstruction.L3SubType.IPV6_SRC.name());
474 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
475 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
476 is(IpAddress.valueOf("1.2.3.2")));
477
478 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
479 L3ModificationInstruction.L3SubType.IPV6_DST.name());
480 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
481 assertThat(((L3ModificationInstruction.ModIPInstruction) instruction).ip(),
482 is(IpAddress.valueOf("1.2.3.1")));
483
484 instruction = getInstruction(Instruction.Type.L3MODIFICATION,
485 L3ModificationInstruction.L3SubType.IPV6_FLABEL.name());
486 assertThat(instruction.type(), is(Instruction.Type.L3MODIFICATION));
487 assertThat(((L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction)
488 .flowLabel(),
489 is(8));
490
491 instruction = getInstruction(Instruction.Type.L0MODIFICATION,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700492 L0ModificationInstruction.L0SubType.OCH.name());
493 assertThat(instruction.type(), is(Instruction.Type.L0MODIFICATION));
494 L0ModificationInstruction.ModOchSignalInstruction och =
495 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
496 assertThat(och.lambda().spacingMultiplier(), is(4));
497 assertThat(och.lambda().slotGranularity(), is(8));
498 assertThat(och.lambda().gridType(), is(GridType.DWDM));
499 assertThat(och.lambda().channelSpacing(), is(ChannelSpacing.CHL_100GHZ));
Hyunsun Moonfab29502015-08-25 13:39:16 -0700500
501 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
502 L4ModificationInstruction.L4SubType.TCP_DST.name());
503 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
504 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
505 .port().toInt(), is(40001));
506
507 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
508 L4ModificationInstruction.L4SubType.TCP_SRC.name());
509 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
510 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
511 .port().toInt(), is(40002));
512
513 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
514 L4ModificationInstruction.L4SubType.UDP_DST.name());
515 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
516 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
517 .port().toInt(), is(40003));
518
519 instruction = getInstruction(Instruction.Type.L4MODIFICATION,
520 L4ModificationInstruction.L4SubType.UDP_SRC.name());
521 assertThat(instruction.type(), is(Instruction.Type.L4MODIFICATION));
522 assertThat(((L4ModificationInstruction.ModTransportPortInstruction) instruction)
523 .port().toInt(), is(40004));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700524 }
525
526 SortedMap<String, Criterion> criteria = new TreeMap<>();
527
528 /**
529 * Looks up a criterion in the instruction map based on type and subtype.
530 *
531 * @param type type string
532 * @return criterion that matches
533 */
534 private Criterion getCriterion(Criterion.Type type) {
535 Criterion criterion = criteria.get(type.name());
536 assertThat(criterion.type(), is(type));
537 return criterion;
538 }
539
540 /**
541 * Checks that a rule with one of each kind of criterion decodes properly.
542 *
543 * @throws IOException if the resource cannot be processed
544 */
545 @Test
546 public void codecCriteriaFlowTest() throws Exception {
547 FlowRule rule = getRule("criteria-flow.json");
548
549 checkCommonData(rule);
550
Sho SHIMIZU06810162016-02-24 12:52:32 -0800551 assertThat(rule.selector().criteria().size(), is(35));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700552
553 rule.selector().criteria()
554 .stream()
555 .forEach(criterion ->
556 criteria.put(criterion.type().name(), criterion));
557
558 Criterion criterion;
559
560 criterion = getCriterion(Criterion.Type.ETH_TYPE);
alshabibcaf1ca22015-06-25 15:18:16 -0700561 assertThat(((EthTypeCriterion) criterion).ethType(), is(new EthType(2054)));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700562
563 criterion = getCriterion(Criterion.Type.ETH_DST);
564 assertThat(((EthCriterion) criterion).mac(),
565 is(MacAddress.valueOf("00:11:22:33:44:55")));
566
567 criterion = getCriterion(Criterion.Type.ETH_SRC);
568 assertThat(((EthCriterion) criterion).mac(),
569 is(MacAddress.valueOf("00:11:22:33:44:55")));
570
571 criterion = getCriterion(Criterion.Type.IN_PORT);
572 assertThat(((PortCriterion) criterion).port(),
573 is(PortNumber.portNumber(23)));
574
575 criterion = getCriterion(Criterion.Type.IN_PHY_PORT);
576 assertThat(((PortCriterion) criterion).port(),
577 is(PortNumber.portNumber(44)));
578
579 criterion = getCriterion(Criterion.Type.VLAN_VID);
580 assertThat(((VlanIdCriterion) criterion).vlanId(),
581 is(VlanId.vlanId((short) 777)));
582
583 criterion = getCriterion(Criterion.Type.VLAN_PCP);
584 assertThat(((VlanPcpCriterion) criterion).priority(),
585 is(((byte) 3)));
586
587 criterion = getCriterion(Criterion.Type.IP_DSCP);
588 assertThat(((IPDscpCriterion) criterion).ipDscp(),
589 is(((byte) 2)));
590
591 criterion = getCriterion(Criterion.Type.IP_ECN);
592 assertThat(((IPEcnCriterion) criterion).ipEcn(),
593 is(((byte) 1)));
594
595 criterion = getCriterion(Criterion.Type.IP_PROTO);
596 assertThat(((IPProtocolCriterion) criterion).protocol(),
597 is(((short) 4)));
598
599 criterion = getCriterion(Criterion.Type.IPV4_SRC);
600 assertThat(((IPCriterion) criterion).ip(),
601 is((IpPrefix.valueOf("1.2.0.0/32"))));
602
603 criterion = getCriterion(Criterion.Type.IPV4_DST);
604 assertThat(((IPCriterion) criterion).ip(),
605 is((IpPrefix.valueOf("2.2.0.0/32"))));
606
607 criterion = getCriterion(Criterion.Type.IPV6_SRC);
608 assertThat(((IPCriterion) criterion).ip(),
609 is((IpPrefix.valueOf("3.2.0.0/32"))));
610
611 criterion = getCriterion(Criterion.Type.IPV6_DST);
612 assertThat(((IPCriterion) criterion).ip(),
613 is((IpPrefix.valueOf("4.2.0.0/32"))));
614
615 criterion = getCriterion(Criterion.Type.TCP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700616 assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700617 is(80));
618
619 criterion = getCriterion(Criterion.Type.TCP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700620 assertThat(((TcpPortCriterion) criterion).tcpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700621 is(443));
622
623 criterion = getCriterion(Criterion.Type.UDP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700624 assertThat(((UdpPortCriterion) criterion).udpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700625 is(180));
626
627 criterion = getCriterion(Criterion.Type.UDP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700628 assertThat(((UdpPortCriterion) criterion).udpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700629 is(1443));
630
631 criterion = getCriterion(Criterion.Type.SCTP_SRC);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700632 assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700633 is(280));
634
635 criterion = getCriterion(Criterion.Type.SCTP_DST);
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700636 assertThat(((SctpPortCriterion) criterion).sctpPort().toInt(),
Ray Milkeyd43fe452015-05-29 09:35:12 -0700637 is(2443));
638
639 criterion = getCriterion(Criterion.Type.ICMPV4_TYPE);
640 assertThat(((IcmpTypeCriterion) criterion).icmpType(),
641 is((short) 24));
642
643 criterion = getCriterion(Criterion.Type.ICMPV4_CODE);
644 assertThat(((IcmpCodeCriterion) criterion).icmpCode(),
645 is((short) 16));
646
647 criterion = getCriterion(Criterion.Type.ICMPV6_TYPE);
648 assertThat(((Icmpv6TypeCriterion) criterion).icmpv6Type(),
649 is((short) 14));
650
651 criterion = getCriterion(Criterion.Type.ICMPV6_CODE);
652 assertThat(((Icmpv6CodeCriterion) criterion).icmpv6Code(),
653 is((short) 6));
654
655 criterion = getCriterion(Criterion.Type.IPV6_FLABEL);
656 assertThat(((IPv6FlowLabelCriterion) criterion).flowLabel(),
657 is(8));
658
659 criterion = getCriterion(Criterion.Type.IPV6_ND_TARGET);
660 assertThat(((IPv6NDTargetAddressCriterion) criterion)
661 .targetAddress().toString(),
662 is("1111:2222:3333:4444:5555:6666:7777:8888"));
663
664 criterion = getCriterion(Criterion.Type.IPV6_ND_SLL);
665 assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(),
666 is(MacAddress.valueOf("00:11:22:33:44:56")));
667
668 criterion = getCriterion(Criterion.Type.IPV6_ND_TLL);
669 assertThat(((IPv6NDLinkLayerAddressCriterion) criterion).mac(),
670 is(MacAddress.valueOf("00:11:22:33:44:57")));
671
672 criterion = getCriterion(Criterion.Type.MPLS_LABEL);
673 assertThat(((MplsCriterion) criterion).label(),
674 is(MplsLabel.mplsLabel(123)));
675
676 criterion = getCriterion(Criterion.Type.IPV6_EXTHDR);
677 assertThat(((IPv6ExthdrFlagsCriterion) criterion).exthdrFlags(),
678 is(99));
679
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700680 criterion = getCriterion(Criterion.Type.TUNNEL_ID);
681 assertThat(((TunnelIdCriterion) criterion).tunnelId(),
682 is(100L));
Yafit Hadar5796d972015-10-15 13:16:11 +0300683
684 criterion = getCriterion(Criterion.Type.OCH_SIGTYPE);
685 assertThat(((OchSignalTypeCriterion) criterion).signalType(),
686 is(OchSignalType.FIXED_GRID));
687
688 criterion = getCriterion(Criterion.Type.ODU_SIGTYPE);
689 assertThat(((OduSignalTypeCriterion) criterion).signalType(),
690 is(OduSignalType.ODU4));
691
692
693 criterion = getCriterion(Criterion.Type.ODU_SIGID);
694 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributaryPortNumber(),
695 is(1));
696
697 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotLength(),
698 is(80));
699
700 assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotBitmap(),
Jian Li68c4fc42016-01-11 16:07:03 -0800701 is(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
Ray Milkeyd43fe452015-05-29 09:35:12 -0700702 }
703
704 /**
705 * Checks that a rule with a SigId criterion decodes properly.
706 *
707 * @throws IOException if the resource cannot be processed
708 */
709 @Test
710 public void codecSigIdCriteriaFlowTest() throws Exception {
711 FlowRule rule = getRule("sigid-flow.json");
712
713 checkCommonData(rule);
714
715 assertThat(rule.selector().criteria().size(), is(1));
716 Criterion criterion = rule.selector().criteria().iterator().next();
717 assertThat(criterion.type(), is(Criterion.Type.OCH_SIGID));
718 Lambda lambda = ((OchSignalCriterion) criterion).lambda();
719 assertThat(lambda, instanceOf(OchSignal.class));
720 OchSignal ochSignal = (OchSignal) lambda;
721 assertThat(ochSignal.spacingMultiplier(), is(3));
722 assertThat(ochSignal.slotGranularity(), is(4));
723 assertThat(ochSignal.gridType(), is(GridType.CWDM));
724 assertThat(ochSignal.channelSpacing(), is(ChannelSpacing.CHL_25GHZ));
725 }
726
727}