blob: 7df4af3e6dd84485b573db2d00ba43fb5386cb16 [file] [log] [blame]
Pier Ventre5c4a0762016-11-21 10:28:13 -08001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
16
17package org.onosproject.net.intent.impl.compiler;
18
19import com.google.common.collect.ImmutableSet;
20import org.hamcrest.core.Is;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.Ethernet;
25import org.onlab.packet.MplsLabel;
26import org.onlab.packet.VlanId;
27import org.onosproject.cfg.ComponentConfigAdapter;
28import org.onosproject.core.CoreService;
29import org.onosproject.net.FilteredConnectPoint;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.FlowRule;
33import org.onosproject.net.flow.criteria.MplsCriterion;
34import org.onosproject.net.flow.criteria.VlanIdCriterion;
35import org.onosproject.net.intent.FlowRuleIntent;
36import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentExtensionService;
38import org.onosproject.net.intent.LinkCollectionIntent;
39import org.onosproject.net.resource.MockResourceService;
40
41import java.util.Collection;
42import java.util.Collections;
43import java.util.List;
44import java.util.stream.Collectors;
45
46import static org.easymock.EasyMock.createMock;
47import static org.easymock.EasyMock.expect;
48import static org.easymock.EasyMock.replay;
49import static org.hamcrest.MatcherAssert.assertThat;
50import static org.hamcrest.Matchers.hasSize;
51import static org.hamcrest.core.Is.is;
52import static org.onosproject.net.NetTestTools.APP_ID;
53import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL;
54import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
55import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*;
56
57/**
58 * This set of tests are meant to test the proper compilation
59 * of p2p intents.
60 */
61public class LinkCollectionIntentCompilerP2PTest extends AbstractLinkCollectionTest {
62
63 @Before
64 public void setUp() {
65 sut = new LinkCollectionIntentCompiler();
66 coreService = createMock(CoreService.class);
67 expect(coreService.registerApplication("org.onosproject.net.intent"))
68 .andReturn(appId);
69 sut.coreService = coreService;
70
Thomas Vachuska23235962017-02-03 11:44:15 -080071 Intent.unbindIdGenerator(idGenerator);
Pier Ventre5c4a0762016-11-21 10:28:13 -080072 Intent.bindIdGenerator(idGenerator);
73
74 intentExtensionService = createMock(IntentExtensionService.class);
75 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
76 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
77
78 registrator = new IntentConfigurableRegistrator();
79 registrator.extensionService = intentExtensionService;
80 registrator.cfgService = new ComponentConfigAdapter();
81 registrator.activate();
82
83 sut.registrator = registrator;
84 sut.resourceService = new MockResourceService();
85
86 LinkCollectionCompiler.optimize = false;
87 LinkCollectionCompiler.copyTtl = false;
88
89 replay(coreService, intentExtensionService);
90 }
91
92 @After
93 public void tearDown() {
94 Intent.unbindIdGenerator(idGenerator);
95 }
96
97 /**
98 * We test the proper compilation of p2p with
99 * trivial selector and trivial treatment.
100 */
101 @Test
102 public void testCompilationTrivialForP2P() {
103
104 intent = LinkCollectionIntent.builder()
105 .appId(APP_ID)
106 .selector(selector)
107 .treatment(treatment)
108 .applyTreatmentOnEgress(true)
109 .links(p2pLinks)
110 .filteredIngressPoints(ImmutableSet.of(
111 new FilteredConnectPoint(d1p10)
112 ))
113 .filteredEgressPoints(ImmutableSet.of(
114 new FilteredConnectPoint(d3p0)
115 ))
116 .build();
117
118 sut.activate();
119
120 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
121 assertThat(compiled, hasSize(1));
122
123 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
124 assertThat(rules, hasSize(3));
125
126 Collection<FlowRule> rulesS1 = rules.stream()
127 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
128 .collect(Collectors.toSet());
129 assertThat(rulesS1, hasSize(1));
130 FlowRule ruleS1 = rulesS1.iterator().next();
131 assertThat(ruleS1.selector(), Is.is(
132 DefaultTrafficSelector
133 .builder()
134 .matchInPort(d1p10.port())
135 .build()
136 ));
137 assertThat(ruleS1.treatment(), Is.is(
138 DefaultTrafficTreatment
139 .builder()
140 .setOutput(d1p0.port())
141 .build()
142 ));
143
144 Collection<FlowRule> rulesS2 = rules.stream()
145 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
146 .collect(Collectors.toSet());
147 assertThat(rulesS2, hasSize(1));
148 FlowRule ruleS2 = rulesS2.iterator().next();
149 assertThat(ruleS2.selector(), Is.is(
150 DefaultTrafficSelector
151 .builder()
152 .matchInPort(d2p0.port())
153 .build()
154 ));
155 assertThat(ruleS2.treatment(), Is.is(
156 DefaultTrafficTreatment
157 .builder()
158 .setOutput(d2p1.port())
159 .build()
160 ));
161
162
163 Collection<FlowRule> rulesS3 = rules.stream()
164 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
165 .collect(Collectors.toSet());
166 assertThat(rulesS3, hasSize(1));
167 FlowRule ruleS3 = rulesS3.iterator().next();
168 assertThat(ruleS3.selector(), Is.is(
169 DefaultTrafficSelector
170 .builder()
171 .matchInPort(d3p1.port())
172 .build()
173 ));
174 assertThat(ruleS3.treatment(), Is.is(
175 DefaultTrafficTreatment
176 .builder()
177 .setOutput(d3p0.port())
178 .build()
179 ));
180
181 sut.deactivate();
182
183 }
184
185 /**
186 * We test the proper compilation of p2p with
187 * trivial selector, trivial treatment and
188 * filtered points.
189 */
190 @Test
191 public void testCompilationFilteredPointForP2P() {
192
193 intent = LinkCollectionIntent.builder()
194 .appId(APP_ID)
195 .selector(selector)
196 .treatment(treatment)
197 .applyTreatmentOnEgress(true)
198 .links(p2pLinks)
199 .filteredIngressPoints(ImmutableSet.of(
200 new FilteredConnectPoint(d1p10, vlan100Selector)
201 ))
202 .filteredEgressPoints(ImmutableSet.of(
203 new FilteredConnectPoint(d3p0, mpls200Selector)
204 ))
205 .build();
206
207 sut.activate();
208
209 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
210 assertThat(compiled, hasSize(1));
211
212 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
213 assertThat(rules, hasSize(3));
214
215 Collection<FlowRule> rulesS1 = rules.stream()
216 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
217 .collect(Collectors.toSet());
218 assertThat(rulesS1, hasSize(1));
219 FlowRule ruleS1 = rulesS1.iterator().next();
220 assertThat(ruleS1.selector(), Is.is(
221 DefaultTrafficSelector
222 .builder(vlan100Selector)
223 .matchInPort(d1p10.port())
224 .build()
225 ));
226 assertThat(ruleS1.treatment(), Is.is(
227 DefaultTrafficTreatment
228 .builder()
229 .setOutput(d1p0.port())
230 .build()
231 ));
232
233 Collection<FlowRule> rulesS2 = rules.stream()
234 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
235 .collect(Collectors.toSet());
236 assertThat(rulesS2, hasSize(1));
237 FlowRule ruleS2 = rulesS2.iterator().next();
238 assertThat(ruleS2.selector(), Is.is(
239 DefaultTrafficSelector
240 .builder(vlan100Selector)
241 .matchInPort(d2p0.port())
242 .build()
243 ));
244 assertThat(ruleS2.treatment(), Is.is(
245 DefaultTrafficTreatment
246 .builder()
247 .setOutput(d2p1.port())
248 .build()
249 ));
250
251
252 Collection<FlowRule> rulesS3 = rules.stream()
253 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
254 .collect(Collectors.toSet());
255 assertThat(rulesS3, hasSize(1));
256 FlowRule ruleS3 = rulesS3.iterator().next();
257 assertThat(ruleS3.selector(), Is.is(
258 DefaultTrafficSelector
259 .builder(vlan100Selector)
260 .matchInPort(d3p1.port())
261 .build()
262 ));
263 assertThat(ruleS3.treatment(), Is.is(
264 DefaultTrafficTreatment
265 .builder()
266 .popVlan()
267 .pushMpls()
268 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
269 .setOutput(d3p0.port())
270 .build()
271 ));
272
273 sut.deactivate();
274
275 }
276
277 /**
278 * We test the proper compilation of p2p with
279 * selector, treatment and filtered points.
280 */
281 @Test
282 public void testCompilationNonTrivialForP2P() {
283
284 intent = LinkCollectionIntent.builder()
285 .appId(APP_ID)
286 .selector(ipPrefixSelector)
287 .treatment(ethDstTreatment)
288 .applyTreatmentOnEgress(true)
289 .links(p2pLinks)
290 .filteredIngressPoints(ImmutableSet.of(
291 new FilteredConnectPoint(d1p10, vlan100Selector)
292 ))
293 .filteredEgressPoints(ImmutableSet.of(
294 new FilteredConnectPoint(d3p0, mpls200Selector)
295 ))
296 .build();
297
298 sut.activate();
299
300 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
301 assertThat(compiled, hasSize(1));
302
303 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
304 assertThat(rules, hasSize(3));
305
306 Collection<FlowRule> rulesS1 = rules.stream()
307 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
308 .collect(Collectors.toSet());
309 assertThat(rulesS1, hasSize(1));
310 FlowRule ruleS1 = rulesS1.iterator().next();
311 assertThat(ruleS1.selector(), Is.is(
312 DefaultTrafficSelector
313 .builder(ipPrefixSelector)
314 .matchInPort(d1p10.port())
315 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
316 .build()
317 ));
318 assertThat(ruleS1.treatment(), Is.is(
319 DefaultTrafficTreatment
320 .builder()
321 .setOutput(d1p0.port())
322 .build()
323 ));
324
325 Collection<FlowRule> rulesS2 = rules.stream()
326 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
327 .collect(Collectors.toSet());
328 assertThat(rulesS2, hasSize(1));
329 FlowRule ruleS2 = rulesS2.iterator().next();
330 assertThat(ruleS2.selector(), Is.is(
331 DefaultTrafficSelector
332 .builder(ipPrefixSelector)
333 .matchInPort(d2p0.port())
334 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
335 .build()
336 ));
337 assertThat(ruleS2.treatment(), Is.is(
338 DefaultTrafficTreatment
339 .builder()
340 .setOutput(d2p1.port())
341 .build()
342 ));
343
344
345 Collection<FlowRule> rulesS3 = rules.stream()
346 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
347 .collect(Collectors.toSet());
348 assertThat(rulesS3, hasSize(1));
349 FlowRule ruleS3 = rulesS3.iterator().next();
350 assertThat(ruleS3.selector(), Is.is(
351 DefaultTrafficSelector
352 .builder(ipPrefixSelector)
353 .matchInPort(d3p1.port())
354 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
355 .build()
356 ));
357 assertThat(ruleS3.treatment(), Is.is(
358 DefaultTrafficTreatment
359 .builder()
360 .setEthDst(((ModEtherInstruction) ethDstTreatment
361 .allInstructions()
362 .stream()
363 .filter(instruction -> instruction instanceof ModEtherInstruction)
364 .findFirst().get()).mac())
365 .popVlan()
366 .pushMpls()
367 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
368 .setOutput(d3p0.port())
369 .build()
370 ));
371
372 sut.deactivate();
373
374 }
375
376 /**
377 * We test the proper compilation of p2p with the VLAN
378 * encapsulation and trivial filtered points.
379 */
380 @Test
381 public void testVlanEncapsulationForP2P() {
382
383 intent = LinkCollectionIntent.builder()
384 .appId(APP_ID)
385 .selector(selector)
386 .treatment(treatment)
387 .constraints(constraintsForVlan)
388 .links(linksForMp2Sp)
389 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
390 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls200Selector)))
391 .build();
392
393 sut.activate();
394 /*
395 * We use the FIRST_FIT to simplify tests.
396 */
397 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
398
399 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
400 assertThat(compiled, hasSize(1));
401
402 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
403 assertThat(rules, hasSize(3));
404
405 Collection<FlowRule> rulesS1 = rules.stream()
406 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
407 .collect(Collectors.toSet());
408 assertThat(rulesS1, hasSize(1));
409 FlowRule ruleS1 = rulesS1.iterator().next();
410 assertThat(ruleS1.selector(), is(
411 DefaultTrafficSelector
412 .builder(vlan100Selector)
413 .matchInPort(d1p10.port())
414 .build()
415 ));
416 assertThat(ruleS1.treatment(), is(
417 DefaultTrafficTreatment
418 .builder()
419 .setVlanId(VlanId.vlanId(LABEL))
420 .setOutput(d1p0.port())
421 .build()
422 ));
423
424 Collection<FlowRule> rulesS2 = rules.stream()
425 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
426 .collect(Collectors.toSet());
427 assertThat(rulesS2, hasSize(1));
428 FlowRule ruleS2 = rulesS2.iterator().next();
429 assertThat(ruleS2.selector(), is(
430 DefaultTrafficSelector
431 .builder()
432 .matchInPort(d2p0.port())
433 .matchVlanId(VlanId.vlanId(LABEL))
434 .build()
435 ));
436 assertThat(ruleS2.treatment(), is(
437 DefaultTrafficTreatment
438 .builder()
439 .setVlanId(VlanId.vlanId(LABEL))
440 .setOutput(d2p1.port())
441 .build()
442 ));
443
444 Collection<FlowRule> rulesS3 = rules.stream()
445 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
446 .collect(Collectors.toSet());
447 assertThat(rulesS3, hasSize(1));
448 FlowRule ruleS3 = rulesS3.iterator().next();
449 assertThat(ruleS3.selector(), is(
450 DefaultTrafficSelector
451 .builder()
452 .matchInPort(d3p0.port())
453 .matchVlanId(VlanId.vlanId(LABEL))
454 .build()
455 ));
456 assertThat(ruleS3.treatment(), is(
457 DefaultTrafficTreatment
458 .builder()
459 .popVlan()
460 .pushMpls()
461 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
462 .setOutput(d3p10.port())
463 .build()
464 ));
465
466 sut.deactivate();
467
468 }
469
470
471 /**
472 * We test the proper compilation of p2p with the MPLS
473 * encapsulation and trivial filtered points.
474 */
475 @Test
476 public void testMplsEncapsulationForP2P() {
477
478 intent = LinkCollectionIntent.builder()
479 .appId(APP_ID)
480 .selector(selector)
481 .treatment(treatment)
482 .constraints(constraintsForMPLS)
483 .links(linksForMp2Sp)
484 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
485 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls200Selector)))
486 .build();
487
488 sut.activate();
489 /*
490 * We use the FIRST_FIT to simplify tests.
491 */
492 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
493
494 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
495 assertThat(compiled, hasSize(1));
496
497 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
498 assertThat(rules, hasSize(3));
499
500 Collection<FlowRule> rulesS1 = rules.stream()
501 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
502 .collect(Collectors.toSet());
503 assertThat(rulesS1, hasSize(1));
504 FlowRule ruleS1 = rulesS1.iterator().next();
505 assertThat(ruleS1.selector(), is(
506 DefaultTrafficSelector
507 .builder(vlan100Selector)
508 .matchInPort(d1p10.port())
509 .build()
510 ));
511 assertThat(ruleS1.treatment(), is(
512 DefaultTrafficTreatment
513 .builder()
514 .popVlan()
515 .pushMpls()
516 .setMpls(MplsLabel.mplsLabel(LABEL))
517 .setOutput(d1p0.port())
518 .build()
519 ));
520
521 Collection<FlowRule> rulesS2 = rules.stream()
522 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
523 .collect(Collectors.toSet());
524 assertThat(rulesS2, hasSize(1));
525 FlowRule ruleS2 = rulesS2.iterator().next();
526 assertThat(ruleS2.selector(), is(
527 DefaultTrafficSelector
528 .builder()
529 .matchInPort(d2p0.port())
530 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
531 .matchEthType(Ethernet.MPLS_UNICAST)
532 .build()
533 ));
534 assertThat(ruleS2.treatment(), is(
535 DefaultTrafficTreatment
536 .builder()
537 .setMpls(MplsLabel.mplsLabel(LABEL))
538 .setOutput(d2p1.port())
539 .build()
540 ));
541
542 Collection<FlowRule> rulesS3 = rules.stream()
543 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
544 .collect(Collectors.toSet());
545 assertThat(rulesS3, hasSize(1));
546 FlowRule ruleS3 = rulesS3.iterator().next();
547 assertThat(ruleS3.selector(), is(
548 DefaultTrafficSelector
549 .builder()
550 .matchInPort(d3p0.port())
551 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
552 .matchEthType(Ethernet.MPLS_UNICAST)
553 .build()
554 ));
555 assertThat(ruleS3.treatment(), is(
556 DefaultTrafficTreatment
557 .builder()
558 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
559 .setOutput(d3p10.port())
560 .build()
561 ));
562
563 sut.deactivate();
564
565 }
566
567 /**
568 * We test the proper compilation of p2p with the VLAN
569 * encapsulation, filtered points, selector and treatment.
570 */
571 @Test
572 public void testVlanEncapsulationNonTrivialForP2P() {
573
574 intent = LinkCollectionIntent.builder()
575 .appId(APP_ID)
576 .selector(ipPrefixSelector)
577 .treatment(ethDstTreatment)
578 .constraints(constraintsForVlan)
579 .links(linksForMp2Sp)
580 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
581 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls69Selector)))
582 .build();
583
584 sut.activate();
585 /*
586 * We use the FIRST_FIT to simplify tests.
587 */
588 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
589
590 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
591 assertThat(compiled, hasSize(1));
592
593 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
594 assertThat(rules, hasSize(3));
595
596 Collection<FlowRule> rulesS1 = rules.stream()
597 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
598 .collect(Collectors.toSet());
599 assertThat(rulesS1, hasSize(1));
600 FlowRule ruleS1 = rulesS1.iterator().next();
601 assertThat(ruleS1.selector(), is(
602 DefaultTrafficSelector
603 .builder(ipPrefixSelector)
604 .matchInPort(d1p10.port())
605 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
606 .build()
607 ));
608 assertThat(ruleS1.treatment(), is(
609 DefaultTrafficTreatment
610 .builder()
611 .setVlanId(VlanId.vlanId(LABEL))
612 .setOutput(d1p0.port())
613 .build()
614 ));
615
616 Collection<FlowRule> rulesS2 = rules.stream()
617 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
618 .collect(Collectors.toSet());
619 assertThat(rulesS2, hasSize(1));
620 FlowRule ruleS2 = rulesS2.iterator().next();
621 assertThat(ruleS2.selector(), is(
622 DefaultTrafficSelector
623 .builder()
624 .matchInPort(d2p0.port())
625 .matchVlanId(VlanId.vlanId(LABEL))
626 .build()
627 ));
628 assertThat(ruleS2.treatment(), is(
629 DefaultTrafficTreatment
630 .builder()
631 .setVlanId(VlanId.vlanId(LABEL))
632 .setOutput(d2p1.port())
633 .build()
634 ));
635
636 Collection<FlowRule> rulesS3 = rules.stream()
637 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
638 .collect(Collectors.toSet());
639 assertThat(rulesS3, hasSize(1));
640 FlowRule ruleS3 = rulesS3.iterator().next();
641 assertThat(ruleS3.selector(), is(
642 DefaultTrafficSelector
643 .builder()
644 .matchInPort(d3p0.port())
645 .matchVlanId(VlanId.vlanId(LABEL))
646 .build()
647 ));
648 assertThat(ruleS3.treatment(), is(
649 DefaultTrafficTreatment
650 .builder()
651 .setEthDst(((ModEtherInstruction) ethDstTreatment
652 .allInstructions()
653 .stream()
654 .filter(instruction -> instruction instanceof ModEtherInstruction)
655 .findFirst().get()).mac())
656 .popVlan()
657 .pushMpls()
658 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
659 .setOutput(d3p10.port())
660 .build()
661 ));
662
663 sut.deactivate();
664
665 }
666
667 /**
668 * We test the proper compilation of p2p with the MPLS
669 * encapsulation, filtered points, selector and treatment.
670 */
671 @Test
672 public void testMplsEncapsulationNonTrivialForP2P() {
673
674 intent = LinkCollectionIntent.builder()
675 .appId(APP_ID)
676 .selector(ipPrefixSelector)
677 .treatment(ethDstTreatment)
678 .constraints(constraintsForMPLS)
679 .links(linksForMp2Sp)
680 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
681 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls69Selector)))
682 .build();
683
684 sut.activate();
685 /*
686 * We use the FIRST_FIT to simplify tests.
687 */
688 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
689
690 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
691 assertThat(compiled, hasSize(1));
692
693 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
694 assertThat(rules, hasSize(3));
695
696 Collection<FlowRule> rulesS1 = rules.stream()
697 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
698 .collect(Collectors.toSet());
699 assertThat(rulesS1, hasSize(1));
700 FlowRule ruleS1 = rulesS1.iterator().next();
701 assertThat(ruleS1.selector(), is(
702 DefaultTrafficSelector
703 .builder(ipPrefixSelector)
704 .matchInPort(d1p10.port())
705 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
706 .build()
707 ));
708 assertThat(ruleS1.treatment(), is(
709 DefaultTrafficTreatment
710 .builder()
711 .popVlan()
712 .pushMpls()
713 .setMpls(MplsLabel.mplsLabel(LABEL))
714 .setOutput(d1p0.port())
715 .build()
716 ));
717
718 Collection<FlowRule> rulesS2 = rules.stream()
719 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
720 .collect(Collectors.toSet());
721 assertThat(rulesS2, hasSize(1));
722 FlowRule ruleS2 = rulesS2.iterator().next();
723 assertThat(ruleS2.selector(), is(
724 DefaultTrafficSelector
725 .builder()
726 .matchInPort(d2p0.port())
727 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
728 .matchEthType(Ethernet.MPLS_UNICAST)
729 .build()
730 ));
731 assertThat(ruleS2.treatment(), is(
732 DefaultTrafficTreatment
733 .builder()
734 .setMpls(MplsLabel.mplsLabel(LABEL))
735 .setOutput(d2p1.port())
736 .build()
737 ));
738
739 Collection<FlowRule> rulesS3 = rules.stream()
740 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
741 .collect(Collectors.toSet());
742 assertThat(rulesS3, hasSize(1));
743 FlowRule ruleS3 = rulesS3.iterator().next();
744 assertThat(ruleS3.selector(), is(
745 DefaultTrafficSelector
746 .builder()
747 .matchInPort(d3p0.port())
748 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
749 .matchEthType(Ethernet.MPLS_UNICAST)
750 .build()
751 ));
752 assertThat(ruleS3.treatment(), is(
753 DefaultTrafficTreatment
754 .builder()
755 .setEthDst(((ModEtherInstruction) ethDstTreatment
756 .allInstructions()
757 .stream()
758 .filter(instruction -> instruction instanceof ModEtherInstruction)
759 .findFirst().get()).mac())
760 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
761 .setOutput(d3p10.port())
762 .build()
763 ));
764
765 sut.deactivate();
766
767 }
768
769}