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