blob: 4e5a602c3868b54cffa3dde9e66c80b135fb3ca3 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
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.net.intent.impl.compiler;
17
Michele Santuari69fc2ff2015-12-03 17:05:35 +010018import com.google.common.collect.ImmutableList;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Michele Santuari6096acd2016-02-09 17:00:37 +010022import org.onlab.packet.Ethernet;
23import org.onlab.packet.MplsLabel;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010024import org.onlab.packet.VlanId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080025import org.onosproject.TestApplicationId;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080026import org.onosproject.cfg.ComponentConfigAdapter;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.core.IdGenerator;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DefaultLink;
32import org.onosproject.net.DefaultPath;
Michele Santuari6096acd2016-02-09 17:00:37 +010033import org.onosproject.net.DeviceId;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010034import org.onosproject.net.EncapsulationType;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080035import org.onosproject.net.Link;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.FlowRule;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010041import org.onosproject.net.flow.instructions.Instructions;
42import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080043import org.onosproject.net.intent.FlowRuleIntent;
44import org.onosproject.net.intent.Intent;
45import org.onosproject.net.intent.IntentExtensionService;
46import org.onosproject.net.intent.MockIdGenerator;
47import org.onosproject.net.intent.PathIntent;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010048import org.onosproject.net.intent.constraint.EncapsulationConstraint;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049import org.onosproject.net.provider.ProviderId;
50
Michele Santuari69fc2ff2015-12-03 17:05:35 +010051import java.util.Arrays;
52import java.util.Collection;
53import java.util.Collections;
54import java.util.List;
55import java.util.Set;
56import java.util.stream.Collectors;
57
Michele Santuari6096acd2016-02-09 17:00:37 +010058import static org.easymock.EasyMock.createMock;
59import static org.easymock.EasyMock.expect;
60import static org.easymock.EasyMock.replay;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080061import static org.hamcrest.MatcherAssert.assertThat;
62import static org.hamcrest.Matchers.hasSize;
63import static org.hamcrest.Matchers.is;
Michele Santuari69fc2ff2015-12-03 17:05:35 +010064import static org.hamcrest.number.OrderingComparison.greaterThan;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080065import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
66import static org.onosproject.net.Link.Type.DIRECT;
Michele Santuari6096acd2016-02-09 17:00:37 +010067import static org.onosproject.net.NetTestTools.APP_ID;
68import static org.onosproject.net.NetTestTools.PID;
69import static org.onosproject.net.NetTestTools.connectPoint;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080070
71/**
72 * Unit tests for PathIntentCompiler.
73 */
74public class PathIntentCompilerTest {
75
76 private CoreService coreService;
77 private IntentExtensionService intentExtensionService;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080078 private IntentConfigurableRegistrator registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080079 private IdGenerator idGenerator = new MockIdGenerator();
80 private PathIntentCompiler sut;
81
82 private final TrafficSelector selector = DefaultTrafficSelector.builder().build();
83 private final TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Michele Santuari3ea53902016-02-15 11:21:56 +010084 private final VlanId ingressVlan = VlanId.vlanId(((short) 101));
85 private final TrafficSelector vlanSelector = DefaultTrafficSelector.builder()
86 .matchVlanId(ingressVlan).build();
87 private final VlanId egressVlan = VlanId.vlanId((short) 100);
88 private final TrafficTreatment vlanTreatment = DefaultTrafficTreatment.builder()
89 .setVlanId(egressVlan).build();
90
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080091 private final ApplicationId appId = new TestApplicationId("test");
92 private final ProviderId pid = new ProviderId("of", "test");
93 private final ConnectPoint d1p1 = connectPoint("s1", 0);
94 private final ConnectPoint d2p0 = connectPoint("s2", 0);
95 private final ConnectPoint d2p1 = connectPoint("s2", 1);
96 private final ConnectPoint d3p1 = connectPoint("s3", 1);
97 private final ConnectPoint d3p0 = connectPoint("s3", 10);
98 private final ConnectPoint d1p0 = connectPoint("s1", 10);
Brian O'Connor81134662015-06-25 17:23:33 -040099 private static final int PRIORITY = 555;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800100
101 private final List<Link> links = Arrays.asList(
102 createEdgeLink(d1p0, true),
Ray Milkey2693bda2016-01-22 16:08:14 -0800103 DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p0).type(DIRECT).build(),
104 DefaultLink.builder().providerId(PID).src(d2p1).dst(d3p1).type(DIRECT).build(),
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800105 createEdgeLink(d3p0, false)
106 );
107 private final int hops = links.size() - 1;
108 private PathIntent intent;
Michele Santuari6096acd2016-02-09 17:00:37 +0100109 private PathIntent constraintVlanIntent;
Michele Santuari3ea53902016-02-15 11:21:56 +0100110 private PathIntent constrainIngressEgressVlanIntent;
Michele Santuari6096acd2016-02-09 17:00:37 +0100111 private PathIntent constraintMplsIntent;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800112
113 /**
114 * Configures objects used in all the test cases.
115 */
116 @Before
117 public void setUp() {
118 sut = new PathIntentCompiler();
119 coreService = createMock(CoreService.class);
120 expect(coreService.registerApplication("org.onosproject.net.intent"))
121 .andReturn(appId);
122 sut.coreService = coreService;
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100123 sut.resourceService = new MockResourceService();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800124
125 Intent.bindIdGenerator(idGenerator);
126
127 intent = PathIntent.builder()
128 .appId(APP_ID)
129 .selector(selector)
130 .treatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400131 .priority(PRIORITY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800132 .path(new DefaultPath(pid, links, hops))
133 .build();
Michele Santuari6096acd2016-02-09 17:00:37 +0100134
Michele Santuari3ea53902016-02-15 11:21:56 +0100135 //Intent with VLAN encap without egress VLAN
Michele Santuari6096acd2016-02-09 17:00:37 +0100136 constraintVlanIntent = PathIntent.builder()
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100137 .appId(APP_ID)
138 .selector(selector)
139 .treatment(treatment)
140 .priority(PRIORITY)
141 .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN)))
142 .path(new DefaultPath(pid, links, hops))
143 .build();
Michele Santuari6096acd2016-02-09 17:00:37 +0100144
Michele Santuari3ea53902016-02-15 11:21:56 +0100145 //Intent with VLAN encap with ingress and egress VLAN
146 constrainIngressEgressVlanIntent = PathIntent.builder()
147 .appId(APP_ID)
148 .selector(vlanSelector)
149 .treatment(vlanTreatment)
150 .priority(PRIORITY)
151 .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.VLAN)))
152 .path(new DefaultPath(pid, links, hops))
153 .build();
Michele Santuari6096acd2016-02-09 17:00:37 +0100154
155 constraintMplsIntent = PathIntent.builder()
156 .appId(APP_ID)
157 .selector(selector)
158 .treatment(treatment)
159 .priority(PRIORITY)
160 .constraints(ImmutableList.of(new EncapsulationConstraint(EncapsulationType.MPLS)))
161 .path(new DefaultPath(pid, links, hops))
162 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800163 intentExtensionService = createMock(IntentExtensionService.class);
164 intentExtensionService.registerCompiler(PathIntent.class, sut);
165 intentExtensionService.unregisterCompiler(PathIntent.class);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800166
167 registrator = new IntentConfigurableRegistrator();
168 registrator.extensionService = intentExtensionService;
169 registrator.cfgService = new ComponentConfigAdapter();
170 registrator.activate();
171
172 sut.registrator = registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800173
174 replay(coreService, intentExtensionService);
175 }
176
177 /**
178 * Tears down objects used in all the test cases.
179 */
180 @After
181 public void tearDown() {
182 Intent.unbindIdGenerator(idGenerator);
183 }
184
185 /**
186 * Tests the compilation behavior of the path intent compiler.
187 */
188 @Test
189 public void testCompile() {
190 sut.activate();
191
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800192 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800193 assertThat(compiled, hasSize(1));
194
195 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
196
197 FlowRule rule1 = rules.stream()
198 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
199 .findFirst()
200 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100201 verifyIdAndPriority(rule1, d1p0.deviceId());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800202 assertThat(rule1.selector(),
203 is(DefaultTrafficSelector.builder(selector).matchInPort(d1p0.port()).build()));
204 assertThat(rule1.treatment(),
205 is(DefaultTrafficTreatment.builder().setOutput(d1p1.port()).build()));
Michele Santuari6096acd2016-02-09 17:00:37 +0100206
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800207
208 FlowRule rule2 = rules.stream()
209 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
210 .findFirst()
211 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100212 verifyIdAndPriority(rule2, d2p0.deviceId());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800213 assertThat(rule2.selector(),
214 is(DefaultTrafficSelector.builder(selector).matchInPort(d2p0.port()).build()));
215 assertThat(rule2.treatment(),
216 is(DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()));
217
218 FlowRule rule3 = rules.stream()
219 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
220 .findFirst()
221 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100222 verifyIdAndPriority(rule3, d3p1.deviceId());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800223 assertThat(rule3.selector(),
224 is(DefaultTrafficSelector.builder(selector).matchInPort(d3p1.port()).build()));
225 assertThat(rule3.treatment(),
226 is(DefaultTrafficTreatment.builder(treatment).setOutput(d3p0.port()).build()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800227
228 sut.deactivate();
229 }
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100230
231 /**
232 * Tests the compilation behavior of the path intent compiler in case of
Michele Santuari3ea53902016-02-15 11:21:56 +0100233 * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}.
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100234 */
235 @Test
Michele Santuari6096acd2016-02-09 17:00:37 +0100236 public void testVlanEncapCompile() {
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100237 sut.activate();
238
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800239 List<Intent> compiled = sut.compile(constraintVlanIntent, Collections.emptyList());
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100240 assertThat(compiled, hasSize(1));
241
242 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
243 assertThat(rules, hasSize(3));
244
245 FlowRule rule1 = rules.stream()
246 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
247 .findFirst()
248 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100249 verifyIdAndPriority(rule1, d1p0.deviceId());
250 assertThat(rule1.selector(), is(DefaultTrafficSelector.builder(selector)
251 .matchInPort(d1p0.port()).build()));
252 VlanId vlanToEncap = verifyVlanEncapTreatment(rule1.treatment(), d1p1, true, false);
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100253
254 FlowRule rule2 = rules.stream()
255 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
256 .findFirst()
257 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100258 verifyIdAndPriority(rule2, d2p0.deviceId());
259 verifyVlanEncapSelector(rule2.selector(), d2p0, vlanToEncap);
260 verifyVlanEncapTreatment(rule2.treatment(), d2p1, false, false);
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100261
262 FlowRule rule3 = rules.stream()
263 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
264 .findFirst()
265 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100266 verifyIdAndPriority(rule3, d3p1.deviceId());
267 verifyVlanEncapSelector(rule3.selector(), d3p1, vlanToEncap);
268 verifyVlanEncapTreatment(rule3.treatment(), d3p0, false, true);
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100269
270 sut.deactivate();
271 }
272
Michele Santuari3ea53902016-02-15 11:21:56 +0100273 /**
274 * Tests the compilation behavior of the path intent compiler in case of
275 * VLAN {@link EncapsulationType} encapsulation constraint {@link EncapsulationConstraint}.
276 * This test includes a selector to match a VLAN at the ingress and a treatment to set VLAN at the egress.
277 */
278 @Test
279 public void testEncapIngressEgressVlansCompile() {
280 sut.activate();
281
282 List<Intent> compiled = sut.compile(constrainIngressEgressVlanIntent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800283 Collections.emptyList());
Michele Santuari3ea53902016-02-15 11:21:56 +0100284 assertThat(compiled, hasSize(1));
285
286 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
287 assertThat(rules, hasSize(3));
288
289 FlowRule rule1 = rules.stream()
290 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
291 .findFirst()
292 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100293 verifyIdAndPriority(rule1, d1p0.deviceId());
294 verifyVlanEncapSelector(rule1.selector(), d1p0, ingressVlan);
295 VlanId vlanToEncap = verifyVlanEncapTreatment(rule1.treatment(), d1p1, true, false);
Michele Santuari3ea53902016-02-15 11:21:56 +0100296
297 FlowRule rule2 = rules.stream()
298 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
299 .findFirst()
300 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100301 verifyIdAndPriority(rule2, d2p0.deviceId());
302 verifyVlanEncapSelector(rule2.selector(), d2p0, vlanToEncap);
303 verifyVlanEncapTreatment(rule2.treatment(), d2p1, false, false);
Michele Santuari3ea53902016-02-15 11:21:56 +0100304
305 FlowRule rule3 = rules.stream()
306 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
307 .findFirst()
308 .get();
Michele Santuari6096acd2016-02-09 17:00:37 +0100309 verifyIdAndPriority(rule3, d3p1.deviceId());
310 verifyVlanEncapSelector(rule3.selector(), d3p1, vlanToEncap);
Michele Santuari3ea53902016-02-15 11:21:56 +0100311 Set<L2ModificationInstruction.ModVlanIdInstruction> vlanMod = rule3.treatment().allInstructions().stream()
312 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
313 .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x)
314 .collect(Collectors.toSet());
315 assertThat(rule3.treatment().allInstructions().stream()
316 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
317 .collect(Collectors.toSet()), hasSize(1));
318 assertThat(vlanMod.iterator().next().vlanId(), is(egressVlan));
319 assertThat(rule3.treatment().allInstructions().stream()
320 .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction)
321 .collect(Collectors.toSet()), hasSize(0));
322
323 sut.deactivate();
324 }
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100325
Michele Santuari6096acd2016-02-09 17:00:37 +0100326 private VlanId verifyVlanEncapTreatment(TrafficTreatment trafficTreatment,
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100327 ConnectPoint egress, boolean isIngress, boolean isEgress) {
328 Set<Instructions.OutputInstruction> ruleOutput = trafficTreatment.allInstructions().stream()
329 .filter(treat -> treat instanceof Instructions.OutputInstruction)
330 .map(treat -> (Instructions.OutputInstruction) treat)
331 .collect(Collectors.toSet());
332 assertThat(ruleOutput, hasSize(1));
333 assertThat((ruleOutput.iterator().next()).port(), is(egress.port()));
334 VlanId vlanToEncap = VlanId.NONE;
335 if (isIngress && !isEgress) {
336 Set<L2ModificationInstruction.ModVlanIdInstruction> vlanRules = trafficTreatment.allInstructions().stream()
337 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
338 .map(x -> (L2ModificationInstruction.ModVlanIdInstruction) x)
339 .collect(Collectors.toSet());
340 assertThat(vlanRules, hasSize(1));
341 L2ModificationInstruction.ModVlanIdInstruction vlanRule = vlanRules.iterator().next();
342 assertThat(vlanRule.vlanId().toShort(), greaterThan((short) 0));
343 vlanToEncap = vlanRule.vlanId();
344 } else if (!isIngress && !isEgress) {
345 assertThat(trafficTreatment.allInstructions().stream()
346 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
347 .collect(Collectors.toSet()), hasSize(0));
348 } else {
349 assertThat(trafficTreatment.allInstructions().stream()
350 .filter(treat -> treat instanceof L2ModificationInstruction.ModVlanIdInstruction)
351 .collect(Collectors.toSet()), hasSize(0));
352 assertThat(trafficTreatment.allInstructions().stream()
353 .filter(treat -> treat instanceof L2ModificationInstruction.PopVlanInstruction)
354 .collect(Collectors.toSet()), hasSize(1));
355
356 }
357
358 return vlanToEncap;
359
360 }
361
Michele Santuari6096acd2016-02-09 17:00:37 +0100362 private void verifyVlanEncapSelector(TrafficSelector trafficSelector, ConnectPoint ingress, VlanId vlanToMatch) {
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100363
Michele Santuari6096acd2016-02-09 17:00:37 +0100364 assertThat(trafficSelector, is(DefaultTrafficSelector.builder().matchInPort(ingress.port())
365 .matchVlanId(vlanToMatch).build()));
366 }
367
368 /**
369 * Tests the compilation behavior of the path intent compiler in case of
370 * encasulation costraint {@link EncapsulationConstraint}.
371 */
372 @Test
373 public void testMplsEncapCompile() {
374 sut.activate();
375
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800376 List<Intent> compiled = sut.compile(constraintMplsIntent, Collections.emptyList());
Michele Santuari6096acd2016-02-09 17:00:37 +0100377 assertThat(compiled, hasSize(1));
378
379 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
380 assertThat(rules, hasSize(3));
381
382 FlowRule rule1 = rules.stream()
383 .filter(x -> x.deviceId().equals(d1p0.deviceId()))
384 .findFirst()
385 .get();
386 verifyIdAndPriority(rule1, d1p0.deviceId());
387 assertThat(rule1.selector(), is(DefaultTrafficSelector.builder(selector)
388 .matchInPort(d1p0.port()).build()));
389 MplsLabel mplsLabelToEncap = verifyMplsEncapTreatment(rule1.treatment(), d1p1, true, false);
390
391 FlowRule rule2 = rules.stream()
392 .filter(x -> x.deviceId().equals(d2p0.deviceId()))
393 .findFirst()
394 .get();
395 verifyIdAndPriority(rule2, d2p0.deviceId());
396 verifyMplsEncapSelector(rule2.selector(), d2p0, mplsLabelToEncap);
397 verifyMplsEncapTreatment(rule2.treatment(), d2p1, false, false);
398
399 FlowRule rule3 = rules.stream()
400 .filter(x -> x.deviceId().equals(d3p0.deviceId()))
401 .findFirst()
402 .get();
403 verifyIdAndPriority(rule3, d3p1.deviceId());
404 verifyMplsEncapSelector(rule3.selector(), d3p1, mplsLabelToEncap);
405 verifyMplsEncapTreatment(rule3.treatment(), d3p0, false, true);
406
407 sut.deactivate();
408 }
409
410
411 private MplsLabel verifyMplsEncapTreatment(TrafficTreatment trafficTreatment,
412 ConnectPoint egress, boolean isIngress, boolean isEgress) {
413 Set<Instructions.OutputInstruction> ruleOutput = trafficTreatment.allInstructions().stream()
414 .filter(treat -> treat instanceof Instructions.OutputInstruction)
415 .map(treat -> (Instructions.OutputInstruction) treat)
416 .collect(Collectors.toSet());
417 assertThat(ruleOutput, hasSize(1));
418 assertThat((ruleOutput.iterator().next()).port(), is(egress.port()));
419 MplsLabel mplsToEncap = MplsLabel.mplsLabel(0);
420 if (isIngress && !isEgress) {
421 Set<L2ModificationInstruction.ModMplsLabelInstruction> mplsRules =
422 trafficTreatment.allInstructions().stream()
423 .filter(treat -> treat instanceof L2ModificationInstruction.ModMplsLabelInstruction)
424 .map(x -> (L2ModificationInstruction.ModMplsLabelInstruction) x)
425 .collect(Collectors.toSet());
426 assertThat(mplsRules, hasSize(1));
427 L2ModificationInstruction.ModMplsLabelInstruction mplsRule = mplsRules.iterator().next();
428 assertThat(mplsRule.mplsLabel().toInt(), greaterThan(0));
429 mplsToEncap = mplsRule.mplsLabel();
430 } else if (!isIngress && !isEgress) {
431 assertThat(trafficTreatment.allInstructions().stream()
432 .filter(treat -> treat instanceof L2ModificationInstruction.ModMplsLabelInstruction)
433 .collect(Collectors.toSet()), hasSize(0));
434 } else {
435 assertThat(trafficTreatment.allInstructions().stream()
436 .filter(treat -> treat instanceof L2ModificationInstruction.ModMplsLabelInstruction)
437 .collect(Collectors.toSet()), hasSize(0));
438 assertThat(trafficTreatment.allInstructions().stream()
439 .filter(treat -> treat instanceof L2ModificationInstruction.PushHeaderInstructions)
440 .collect(Collectors.toSet()), hasSize(1));
441
442 }
443
444 return mplsToEncap;
445
446 }
447
448 private void verifyMplsEncapSelector(TrafficSelector trafficSelector, ConnectPoint ingress, MplsLabel mplsLabel) {
449
450 assertThat(trafficSelector, is(DefaultTrafficSelector.builder()
451 .matchInPort(ingress.port()).matchEthType(Ethernet.MPLS_UNICAST)
452 .matchMplsLabel(mplsLabel).build()));
453 }
454
455 private void verifyIdAndPriority(FlowRule rule, DeviceId deviceId) {
456 assertThat(rule.deviceId(), is(deviceId));
457 assertThat(rule.priority(), is(PRIORITY));
Michele Santuari69fc2ff2015-12-03 17:05:35 +0100458 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800459}