blob: 55c1d9b5c2d4ba20b22965c5ccb64dfc8131f699 [file] [log] [blame]
Pier Ventre766995d2016-10-05 22:15:56 -07001/*
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.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ethernet;
24import org.onlab.packet.MplsLabel;
25import org.onlab.packet.VlanId;
26import org.onosproject.cfg.ComponentConfigAdapter;
27import org.onosproject.core.CoreService;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020028import org.onosproject.net.DeviceId;
Pier Ventre766995d2016-10-05 22:15:56 -070029import org.onosproject.net.FilteredConnectPoint;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020030import org.onosproject.net.domain.DomainService;
Pier Ventre766995d2016-10-05 22:15:56 -070031import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.criteria.MplsCriterion;
35import org.onosproject.net.flow.criteria.PortCriterion;
36import org.onosproject.net.flow.criteria.VlanIdCriterion;
37import org.onosproject.net.intent.FlowRuleIntent;
38import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentExtensionService;
40import org.onosproject.net.intent.LinkCollectionIntent;
41import org.onosproject.net.resource.MockResourceService;
42
43import java.util.Collection;
44import java.util.Collections;
45import java.util.List;
46import java.util.stream.Collectors;
47
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020048import static org.easymock.EasyMock.anyObject;
49import static org.easymock.EasyMock.createMock;
50import static org.easymock.EasyMock.expect;
51import static org.easymock.EasyMock.replay;
Pier Ventre766995d2016-10-05 22:15:56 -070052import static org.hamcrest.MatcherAssert.assertThat;
53import static org.hamcrest.Matchers.hasSize;
54import static org.hamcrest.core.Is.is;
55import static org.onlab.packet.EthType.EtherType.IPV4;
Pier Ventre5c4a0762016-11-21 10:28:13 -080056import static org.onosproject.net.NetTestTools.APP_ID;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020057import static org.onosproject.net.domain.DomainId.LOCAL;
58import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT;
59import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL;
60import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Pier Ventre766995d2016-10-05 22:15:56 -070061import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
62
63/**
64 * This set of tests are meant to test the encapsulation
65 * in the LinkCollectionIntent.
66 */
67public class LinkCollectionEncapIntentCompilerTest extends AbstractLinkCollectionTest {
68
69 @Before
70 public void setUp() {
71 sut = new LinkCollectionIntentCompiler();
72 coreService = createMock(CoreService.class);
Pier Ventre5c4a0762016-11-21 10:28:13 -080073 expect(coreService.registerApplication("org.onosproject.net.intent")).andReturn(appId);
Pier Ventre766995d2016-10-05 22:15:56 -070074 sut.coreService = coreService;
75
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020076 domainService = createMock(DomainService.class);
77 expect(domainService.getDomain(anyObject(DeviceId.class))).andReturn(LOCAL).anyTimes();
78 sut.domainService = domainService;
79
Thomas Vachuska23235962017-02-03 11:44:15 -080080 Intent.unbindIdGenerator(idGenerator);
Pier Ventre766995d2016-10-05 22:15:56 -070081 Intent.bindIdGenerator(idGenerator);
82
83 intentExtensionService = createMock(IntentExtensionService.class);
84 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
85 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
86
87 registrator = new IntentConfigurableRegistrator();
88 registrator.extensionService = intentExtensionService;
89 registrator.cfgService = new ComponentConfigAdapter();
90 registrator.activate();
91
92 sut.registrator = registrator;
93 sut.resourceService = new MockResourceService();
94
Yi Tseng84c5a3d2017-04-14 16:42:59 -070095 LinkCollectionCompiler.optimizeInstructions = false;
Pier Ventre81c47bf2016-11-04 07:26:22 -070096 LinkCollectionCompiler.copyTtl = false;
97
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020098 replay(coreService, domainService, intentExtensionService);
Pier Ventre766995d2016-10-05 22:15:56 -070099 }
100
101 @After
102 public void tearDown() {
103 Intent.unbindIdGenerator(idGenerator);
104 }
105
106 /**
107 * We test the proper compilation of mp2Sp1 with the VLAN
108 * encapsulation, trivial selector.
109 */
110 @Test
111 public void testVlanEncapsulationForMp() {
112
113 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800114 .appId(APP_ID).selector(selector).treatment(treatment)
115 .constraints(constraintsForVlan).links(linksForMp2Sp)
Pier Ventre766995d2016-10-05 22:15:56 -0700116 .filteredIngressPoints(ImmutableSet.of(
117 new FilteredConnectPoint(d1p10),
118 new FilteredConnectPoint(d1p11),
119 new FilteredConnectPoint(d2p10)
120 ))
121 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10)))
122 .build();
123
124 sut.activate();
125 /*
126 * We use the FIRST_FIT to simplify tests.
127 */
128 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
129
130 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
131 assertThat(compiled, hasSize(1));
132
133 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
134 assertThat(rules, hasSize(5));
135
136 Collection<FlowRule> rulesS1 = rules.stream()
137 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
138 .collect(Collectors.toSet());
139 assertThat(rulesS1, hasSize(2));
140 FlowRule ruleS1 = rulesS1.stream()
141 .filter(rule -> {
142 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
143 return inPort.port().equals(d1p10.port());
Pier Ventre5c4a0762016-11-21 10:28:13 -0800144 }).findFirst().get();
Pier Ventre766995d2016-10-05 22:15:56 -0700145 assertThat(ruleS1.selector(), is(
146 DefaultTrafficSelector
147 .builder(intent.selector())
148 .matchInPort(d1p10.port())
149 .build()
150 ));
151 assertThat(ruleS1.treatment(), is(
152 DefaultTrafficTreatment
153 .builder()
154 .pushVlan()
155 .setVlanId(VlanId.vlanId(LABEL))
156 .setOutput(d1p0.port())
157 .build()
158 ));
159
160 ruleS1 = rulesS1.stream()
161 .filter(rule -> {
162 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
163 return inPort.port().equals(d1p11.port());
164 })
165 .findFirst()
166 .get();
167 assertThat(ruleS1.selector(), is(
168 DefaultTrafficSelector
169 .builder(intent.selector())
170 .matchInPort(d1p11.port())
171 .build()
172 ));
173 assertThat(ruleS1.treatment(), is(
174 DefaultTrafficTreatment
175 .builder()
176 .pushVlan()
177 .setVlanId(VlanId.vlanId(LABEL))
178 .setOutput(d1p0.port())
179 .build()
180 ));
181
182 Collection<FlowRule> rulesS2 = rules.stream()
183 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
184 .collect(Collectors.toSet());
185 assertThat(rulesS2, hasSize(2));
186 FlowRule ruleS2 = rulesS2.stream()
187 .filter(rule -> {
188 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
189 return inPort.port().equals(d2p10.port());
190 })
191 .findFirst()
192 .get();
193 assertThat(ruleS2.selector(), is(
194 DefaultTrafficSelector
195 .builder(intent.selector())
196 .matchInPort(d2p10.port())
197 .build()
198 ));
199 assertThat(ruleS2.treatment(), is(
200 DefaultTrafficTreatment
201 .builder()
202 .pushVlan()
203 .setVlanId(VlanId.vlanId(LABEL))
204 .setOutput(d2p1.port())
205 .build()
206 ));
207
208 ruleS2 = rulesS2.stream()
209 .filter(rule -> {
210 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
211 return inPort.port().equals(d2p0.port());
212 })
213 .findFirst()
214 .get();
215 assertThat(ruleS2.selector(), is(
216 DefaultTrafficSelector
217 .builder()
218 .matchInPort(d2p0.port())
219 .matchVlanId(VlanId.vlanId(LABEL))
220 .build()
221 ));
222 assertThat(ruleS2.treatment(), is(
223 DefaultTrafficTreatment
224 .builder()
225 .setVlanId(VlanId.vlanId(LABEL))
226 .setOutput(d2p1.port())
227 .build()
228 ));
229
230 Collection<FlowRule> rulesS3 = rules.stream()
231 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
232 .collect(Collectors.toSet());
233 assertThat(rulesS3, hasSize(1));
234 FlowRule ruleS3 = rulesS3.iterator().next();
235 assertThat(ruleS3.selector(), is(
236 DefaultTrafficSelector
237 .builder()
238 .matchInPort(d3p0.port())
239 .matchVlanId(VlanId.vlanId(LABEL))
240 .build()
241 ));
242 assertThat(ruleS3.treatment(), is(
243 DefaultTrafficTreatment
244 .builder()
245 .popVlan()
246 .setOutput(d3p10.port())
247 .build()
248 ));
249
250 sut.deactivate();
251 }
252
253 /**
254 * We test the proper compilation of sp2mp with the MPLS
255 * encapsulation and trivial selector.
256 */
257 @Test
258 public void testMplsEncapsulationForSp() {
259
260 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800261 .appId(APP_ID).selector(selector).treatment(treatment)
262 .constraints(constraintsForMPLS).links(linksForSp2Mp)
Pier Ventre766995d2016-10-05 22:15:56 -0700263 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10)))
264 .filteredEgressPoints(ImmutableSet.of(
265 new FilteredConnectPoint(d1p10),
266 new FilteredConnectPoint(d1p11),
267 new FilteredConnectPoint(d2p10)
Pier Ventre5c4a0762016-11-21 10:28:13 -0800268 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -0700269
270 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -0800271
Pier Ventre766995d2016-10-05 22:15:56 -0700272 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
273
274 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
275 assertThat(compiled, hasSize(1));
276
277 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
278 assertThat(rules, hasSize(3));
279
280 Collection<FlowRule> rulesS3 = rules.stream()
281 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
282 .collect(Collectors.toSet());
283 assertThat(rulesS3, hasSize(1));
284 FlowRule ruleS3 = rulesS3.iterator().next();
285 assertThat(ruleS3.selector(), is(
286 DefaultTrafficSelector
287 .builder()
288 .matchInPort(d3p10.port())
289 .build()
290 ));
291 assertThat(ruleS3.treatment(), is(
292 DefaultTrafficTreatment
293 .builder()
294 .pushMpls()
Pier Ventre81c47bf2016-11-04 07:26:22 -0700295 .setMpls(MplsLabel.mplsLabel(LABEL))
Pier Ventre766995d2016-10-05 22:15:56 -0700296 .setOutput(d3p0.port())
297 .build()
298 ));
299
300 Collection<FlowRule> rulesS2 = rules.stream()
301 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
302 .collect(Collectors.toSet());
303 assertThat(rulesS2, hasSize(1));
304 FlowRule ruleS2 = rulesS2.iterator().next();
305 assertThat(ruleS2.selector(), is(
306 DefaultTrafficSelector
307 .builder()
308 .matchInPort(d2p1.port())
309 .matchMplsLabel(MplsLabel.mplsLabel((LABEL)))
310 .matchEthType(Ethernet.MPLS_UNICAST)
311 .build()
312 ));
313 assertThat(ruleS2.treatment(), is(
314 DefaultTrafficTreatment
315 .builder()
316 .setMpls(MplsLabel.mplsLabel(LABEL))
317 .setOutput(d2p0.port())
318 .popMpls(IPV4.ethType())
319 .setOutput(d2p10.port())
320 .build()
321 ));
322
323 Collection<FlowRule> rulesS1 = rules.stream()
324 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
325 .collect(Collectors.toSet());
326 assertThat(rulesS1, hasSize(1));
327 FlowRule ruleS1 = rulesS1.iterator().next();
328 assertThat(ruleS1.selector(), is(
329 DefaultTrafficSelector
330 .builder()
331 .matchInPort(d1p0.port())
332 .matchMplsLabel(MplsLabel.mplsLabel((LABEL)))
333 .matchEthType(Ethernet.MPLS_UNICAST)
334 .build()
335 ));
336 assertThat(ruleS1.treatment(), is(
337 DefaultTrafficTreatment
338 .builder()
339 .popMpls(IPV4.ethType())
340 .setOutput(d1p10.port())
Pier Ventre766995d2016-10-05 22:15:56 -0700341 .setOutput(d1p11.port())
342 .build()
343 ));
344
345 sut.deactivate();
346
347 }
348
349 /**
350 * We test the proper compilation of mp2sp with the MPLS
351 * encapsulation and filtered selector.
352 */
353 @Test
354 public void testMplsEncapsulationFilteredForMp() {
355
356 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800357 .appId(APP_ID).selector(selector).treatment(treatment)
358 .constraints(constraintsForMPLS).links(linksForMp2Sp)
Pier Ventre766995d2016-10-05 22:15:56 -0700359 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, vlan69Selector)))
360 .filteredIngressPoints(ImmutableSet.of(
361 new FilteredConnectPoint(d1p10, vlan100Selector),
362 new FilteredConnectPoint(d1p11, vlan200Selector),
363 new FilteredConnectPoint(d2p10, vlan300Selector)
Pier Ventre5c4a0762016-11-21 10:28:13 -0800364 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -0700365
366 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -0800367
Pier Ventre766995d2016-10-05 22:15:56 -0700368 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
369
370 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
371 assertThat(compiled, hasSize(1));
372
373 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
374 assertThat(rules, hasSize(5));
375
376 Collection<FlowRule> rulesS1 = rules.stream()
377 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
378 .collect(Collectors.toSet());
379 assertThat(rulesS1, hasSize(2));
380 FlowRule ruleS1 = rulesS1.stream()
381 .filter(rule -> {
382 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
383 return inPort.port().equals(d1p10.port());
384 })
385 .findFirst()
386 .get();
387 assertThat(ruleS1.selector(), is(
388 DefaultTrafficSelector
389 .builder(vlan100Selector)
390 .matchInPort(d1p10.port())
391 .build()
392 ));
393 assertThat(ruleS1.treatment(), is(
394 DefaultTrafficTreatment
395 .builder()
396 .popVlan()
397 .pushMpls()
398 .setMpls(MplsLabel.mplsLabel(LABEL))
399 .setOutput(d1p0.port())
400 .build()
401 ));
402
403 ruleS1 = rulesS1.stream()
404 .filter(rule -> {
405 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
406 return inPort.port().equals(d1p11.port());
407 })
408 .findFirst()
409 .get();
410 assertThat(ruleS1.selector(), is(
411 DefaultTrafficSelector
412 .builder(vlan200Selector)
413 .matchInPort(d1p11.port())
414 .build()
415 ));
416 assertThat(ruleS1.treatment(), is(
417 DefaultTrafficTreatment
418 .builder()
419 .popVlan()
420 .pushMpls()
421 .setMpls(MplsLabel.mplsLabel(LABEL))
422 .setOutput(d1p0.port())
423 .build()
424 ));
425
426 Collection<FlowRule> rulesS2 = rules.stream()
427 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
428 .collect(Collectors.toSet());
429 assertThat(rulesS2, hasSize(2));
430 FlowRule ruleS2 = rulesS2.stream()
431 .filter(rule -> {
432 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
433 return inPort.port().equals(d2p10.port());
434 })
435 .findFirst()
436 .get();
437 assertThat(ruleS2.selector(), is(
438 DefaultTrafficSelector
439 .builder(vlan300Selector)
440 .matchInPort(d2p10.port())
441 .build()
442 ));
443 assertThat(ruleS2.treatment(), is(
444 DefaultTrafficTreatment
445 .builder()
446 .popVlan()
447 .pushMpls()
448 .setMpls(MplsLabel.mplsLabel(LABEL))
449 .setOutput(d2p1.port())
450 .build()
451 ));
452
453 ruleS2 = rulesS2.stream()
454 .filter(rule -> {
455 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
456 return inPort.port().equals(d2p0.port());
457 })
458 .findFirst()
459 .get();
460 assertThat(ruleS2.selector(), is(
461 DefaultTrafficSelector
462 .builder()
463 .matchInPort(d2p0.port())
464 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
465 .matchEthType(Ethernet.MPLS_UNICAST)
466 .build()
467 ));
468 assertThat(ruleS2.treatment(), is(
469 DefaultTrafficTreatment
470 .builder()
471 .setMpls(MplsLabel.mplsLabel(LABEL))
472 .setOutput(d2p1.port())
473 .build()
474 ));
475
476 Collection<FlowRule> rulesS3 = rules.stream()
477 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
478 .collect(Collectors.toSet());
479 assertThat(rulesS3, hasSize(1));
480 FlowRule ruleS3 = rulesS3.iterator().next();
481 assertThat(ruleS3.selector(), is(
482 DefaultTrafficSelector
483 .builder()
484 .matchInPort(d3p0.port())
485 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
486 .matchEthType(Ethernet.MPLS_UNICAST)
487 .build()
488 ));
489 assertThat(ruleS3.treatment(), is(
490 DefaultTrafficTreatment
491 .builder()
492 .popMpls(IPV4.ethType())
493 .pushVlan()
494 .setVlanId(((VlanIdCriterion) vlan69Selector.getCriterion(VLAN_VID)).vlanId())
495 .setOutput(d3p10.port())
496 .build()
497 ));
498
499 sut.deactivate();
500
501 }
502
503 /**
504 * We test the proper compilation of sp2mp with the VLAN
505 * encapsulation and filtered selector.
506 */
507 @Test
508 public void testVlanEncapsulationFilteredForSp() {
509
510 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800511 .appId(APP_ID).selector(selector).treatment(treatment)
512 .constraints(constraintsForVlan).links(linksForSp2Mp)
Pier Ventre766995d2016-10-05 22:15:56 -0700513 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls69Selector)))
514 .filteredEgressPoints(ImmutableSet.of(
515 new FilteredConnectPoint(d1p10, mpls100Selector),
516 new FilteredConnectPoint(d1p11, mpls200Selector),
517 new FilteredConnectPoint(d2p10, mpls80Selector)
Pier Ventre5c4a0762016-11-21 10:28:13 -0800518 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -0700519
520 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -0800521
Pier Ventre766995d2016-10-05 22:15:56 -0700522 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
523
524 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
525 assertThat(compiled, hasSize(1));
526
527 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
528 assertThat(rules, hasSize(3));
529
530 Collection<FlowRule> rulesS3 = rules.stream()
531 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
532 .collect(Collectors.toSet());
533 assertThat(rulesS3, hasSize(1));
534 FlowRule ruleS3 = rulesS3.iterator().next();
535 assertThat(ruleS3.selector(), is(
536 DefaultTrafficSelector
537 .builder(mpls69Selector)
538 .matchInPort(d3p10.port())
539 .build()
540 ));
541 assertThat(ruleS3.treatment(), is(
542 DefaultTrafficTreatment
543 .builder()
544 .popMpls(IPV4.ethType())
545 .pushVlan()
546 .setVlanId(VlanId.vlanId(LABEL))
547 .setOutput(d3p0.port())
548 .build()
549 ));
550
551 Collection<FlowRule> rulesS2 = rules.stream()
552 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
553 .collect(Collectors.toSet());
554 assertThat(rulesS2, hasSize(1));
555 FlowRule ruleS2 = rulesS2.iterator().next();
556 assertThat(ruleS2.selector(), is(
557 DefaultTrafficSelector
558 .builder()
559 .matchInPort(d2p1.port())
560 .matchVlanId(VlanId.vlanId(LABEL))
561 .build()
562 ));
563 assertThat(ruleS2.treatment(), is(
564 DefaultTrafficTreatment
565 .builder()
566 .setVlanId(VlanId.vlanId(LABEL))
567 .setOutput(d2p0.port())
568 .popVlan()
569 .pushMpls()
570 .setMpls(((MplsCriterion) mpls80Selector.getCriterion(MPLS_LABEL)).label())
571 .setOutput(d2p10.port())
572 .build()
573 ));
574
575 Collection<FlowRule> rulesS1 = rules.stream()
576 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
577 .collect(Collectors.toSet());
578 assertThat(rulesS1, hasSize(1));
579 FlowRule ruleS1 = rulesS1.iterator().next();
580 assertThat(ruleS1.selector(), is(
581 DefaultTrafficSelector
582 .builder()
583 .matchInPort(d1p0.port())
584 .matchVlanId(VlanId.vlanId(LABEL))
585 .build()
586 ));
587 assertThat(ruleS1.treatment(), is(
588 DefaultTrafficTreatment
589 .builder()
590 .popVlan()
591 .pushMpls()
592 .setMpls(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
593 .setOutput(d1p10.port())
Pier Ventre766995d2016-10-05 22:15:56 -0700594 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
595 .setOutput(d1p11.port())
596 .build()
597 ));
598
599 sut.deactivate();
600
601 }
602
603 /**
604 * We test the proper compilation of mp2sp with the VLAN
605 * encapsulation, filtered selectors, intent selector and intent treatment.
606 */
607 @Test
608 public void testVlanEncapsulationNonTrivialForMp() {
609
610 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800611 .appId(APP_ID).selector(ipPrefixSelector).treatment(ethDstTreatment)
612 .constraints(constraintsForVlan).links(linksForMp2Sp)
Pier Ventre766995d2016-10-05 22:15:56 -0700613 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls69Selector)))
614 .filteredIngressPoints(ImmutableSet.of(
615 new FilteredConnectPoint(d1p10, mpls80Selector),
616 new FilteredConnectPoint(d1p11, mpls100Selector),
617 new FilteredConnectPoint(d2p10, mpls200Selector)
Pier Ventre5c4a0762016-11-21 10:28:13 -0800618 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -0700619
620 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -0800621
Pier Ventre766995d2016-10-05 22:15:56 -0700622 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
623
624 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
625 assertThat(compiled, hasSize(1));
626
627 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
628 assertThat(rules, hasSize(5));
629
630 Collection<FlowRule> rulesS1 = rules.stream()
631 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
632 .collect(Collectors.toSet());
633 assertThat(rulesS1, hasSize(2));
634 FlowRule ruleS1 = rulesS1.stream()
635 .filter(rule -> {
636 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
637 return inPort.port().equals(d1p10.port());
638 })
639 .findFirst()
640 .get();
641 assertThat(ruleS1.selector(), is(
642 DefaultTrafficSelector
643 .builder(ipPrefixSelector)
644 .matchInPort(d1p10.port())
645 .matchMplsLabel(((MplsCriterion) mpls80Selector.getCriterion(MPLS_LABEL)).label())
646 .build()
647 ));
648 assertThat(ruleS1.treatment(), is(
649 DefaultTrafficTreatment
650 .builder()
651 .popMpls(IPV4.ethType())
652 .pushVlan()
653 .setVlanId(VlanId.vlanId(LABEL))
654 .setOutput(d1p0.port())
655 .build()
656 ));
657
658 ruleS1 = rulesS1.stream()
659 .filter(rule -> {
660 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
661 return inPort.port().equals(d1p11.port());
662 })
663 .findFirst()
664 .get();
665 assertThat(ruleS1.selector(), is(
666 DefaultTrafficSelector
667 .builder(ipPrefixSelector)
668 .matchInPort(d1p11.port())
669 .matchMplsLabel(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
670 .build()
671 ));
672 assertThat(ruleS1.treatment(), is(
673 DefaultTrafficTreatment
674 .builder()
675 .popMpls(IPV4.ethType())
676 .pushVlan()
677 .setVlanId(VlanId.vlanId(LABEL))
678 .setOutput(d1p0.port())
679 .build()
680 ));
681
682 Collection<FlowRule> rulesS2 = rules.stream()
683 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
684 .collect(Collectors.toSet());
685 assertThat(rulesS2, hasSize(2));
686 FlowRule ruleS2 = rulesS2.stream()
687 .filter(rule -> {
688 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
689 return inPort.port().equals(d2p10.port());
690 })
691 .findFirst()
692 .get();
693 assertThat(ruleS2.selector(), is(
694 DefaultTrafficSelector
695 .builder(ipPrefixSelector)
696 .matchInPort(d2p10.port())
697 .matchMplsLabel(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
698 .build()
699 ));
700 assertThat(ruleS2.treatment(), is(
701 DefaultTrafficTreatment
702 .builder()
703 .popMpls(IPV4.ethType())
704 .pushVlan()
705 .setVlanId(VlanId.vlanId(LABEL))
706 .setOutput(d2p1.port())
707 .build()
708 ));
709
710 ruleS2 = rulesS2.stream()
711 .filter(rule -> {
712 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
713 return inPort.port().equals(d2p0.port());
714 })
715 .findFirst()
716 .get();
717 assertThat(ruleS2.selector(), is(
718 DefaultTrafficSelector
719 .builder()
720 .matchInPort(d2p0.port())
721 .matchVlanId(VlanId.vlanId(LABEL))
722 .build()
723 ));
724 assertThat(ruleS2.treatment(), is(
725 DefaultTrafficTreatment
726 .builder()
727 .setVlanId(VlanId.vlanId(LABEL))
728 .setOutput(d2p1.port())
729 .build()
730 ));
731
732 Collection<FlowRule> rulesS3 = rules.stream()
733 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
734 .collect(Collectors.toSet());
735 assertThat(rulesS3, hasSize(1));
736 FlowRule ruleS3 = rulesS3.iterator().next();
737 assertThat(ruleS3.selector(), is(
738 DefaultTrafficSelector
739 .builder()
740 .matchInPort(d3p0.port())
741 .matchVlanId(VlanId.vlanId(LABEL))
742 .build()
743 ));
744 assertThat(ruleS3.treatment(), is(
745 DefaultTrafficTreatment
746 .builder(ethDstTreatment)
747 .popVlan()
748 .pushMpls()
749 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
750 .setOutput(d3p10.port())
751 .build()
752 ));
753
754 sut.deactivate();
755
756 }
757
758 /**
759 * We test the proper compilation of sp2mp with the MPLS
760 * encapsulation, filtered selector, intent selector, and
761 * intent treatment.
762 */
763 @Test
764 public void testMplsEncapsulationNonTrivialForSp() {
765
766 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800767 .appId(APP_ID).selector(ipPrefixSelector).treatment(ethDstTreatment)
768 .constraints(constraintsForMPLS).links(linksForSp2Mp)
Pier Ventre766995d2016-10-05 22:15:56 -0700769 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, vlan69Selector)))
770 .filteredEgressPoints(ImmutableSet.of(
771 new FilteredConnectPoint(d1p10, vlan100Selector),
772 new FilteredConnectPoint(d1p11, vlan200Selector),
773 new FilteredConnectPoint(d2p10, vlan300Selector)
Pier Ventre5c4a0762016-11-21 10:28:13 -0800774 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -0700775
776 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -0800777
Pier Ventre766995d2016-10-05 22:15:56 -0700778 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
779
780 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
781 assertThat(compiled, hasSize(1));
782
783 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
784 assertThat(rules, hasSize(3));
785
786 Collection<FlowRule> rulesS3 = rules.stream()
787 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
788 .collect(Collectors.toSet());
789 assertThat(rulesS3, hasSize(1));
790 FlowRule ruleS3 = rulesS3.iterator().next();
791 assertThat(ruleS3.selector(), is(
792 DefaultTrafficSelector
793 .builder(ipPrefixSelector)
794 .matchInPort(d3p10.port())
795 .matchVlanId(((VlanIdCriterion) vlan69Selector.getCriterion(VLAN_VID)).vlanId())
796 .build()
797 ));
798 assertThat(ruleS3.treatment(), is(
799 DefaultTrafficTreatment
800 .builder()
801 .popVlan()
802 .pushMpls()
803 .setMpls(MplsLabel.mplsLabel(LABEL))
804 .setOutput(d3p0.port())
805 .build()
806 ));
807
808 Collection<FlowRule> rulesS2 = rules.stream()
809 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
810 .collect(Collectors.toSet());
811 assertThat(rulesS2, hasSize(1));
812 FlowRule ruleS2 = rulesS2.iterator().next();
813 assertThat(ruleS2.selector(), is(
814 DefaultTrafficSelector
815 .builder()
816 .matchInPort(d2p1.port())
817 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
818 .matchEthType(Ethernet.MPLS_UNICAST)
819 .build()
820 ));
821 assertThat(ruleS2.treatment(), is(
822 DefaultTrafficTreatment
823 .builder()
824 .setMpls(MplsLabel.mplsLabel(LABEL))
825 .setOutput(d2p0.port())
826 .setEthDst(((ModEtherInstruction) ethDstTreatment
827 .allInstructions()
828 .stream()
829 .filter(instruction -> instruction instanceof ModEtherInstruction)
830 .findFirst().get()).mac())
831 .popMpls(IPV4.ethType())
832 .pushVlan()
833 .setVlanId(((VlanIdCriterion) vlan300Selector.getCriterion(VLAN_VID)).vlanId())
834 .setOutput(d2p10.port())
835 .build()
836 ));
837
838 Collection<FlowRule> rulesS1 = rules.stream()
839 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
840 .collect(Collectors.toSet());
841 assertThat(rulesS1, hasSize(1));
842 FlowRule ruleS1 = rulesS1.iterator().next();
843 assertThat(ruleS1.selector(), is(
844 DefaultTrafficSelector
845 .builder()
846 .matchInPort(d1p0.port())
847 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
848 .matchEthType(Ethernet.MPLS_UNICAST)
849 .build()
850 ));
851 assertThat(ruleS1.treatment(), is(
852 DefaultTrafficTreatment
853 .builder()
854 .setEthDst(((ModEtherInstruction) ethDstTreatment
855 .allInstructions()
856 .stream()
857 .filter(instruction -> instruction instanceof ModEtherInstruction)
858 .findFirst().get()).mac())
859 .popMpls(IPV4.ethType())
860 .pushVlan()
861 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
862 .setOutput(d1p10.port())
863 .setEthDst(((ModEtherInstruction) ethDstTreatment
864 .allInstructions()
865 .stream()
866 .filter(instruction -> instruction instanceof ModEtherInstruction)
867 .findFirst().get()).mac())
Pier Ventre766995d2016-10-05 22:15:56 -0700868 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
869 .setOutput(d1p11.port())
870 .build()
871 ));
872
873 sut.deactivate();
874
875 }
876
877 /**
878 * We test the proper compilation of mp2sp with the MPLS
879 * encapsulation and filtered selectors of different type.
880 */
881 @Test
882 public void testMplsEncapsulationDifferentFilterForMp() {
883
884 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -0800885 .appId(APP_ID).selector(selector).treatment(treatment)
886 .constraints(constraintsForMPLS).links(linksForMp2Sp)
Pier Ventre766995d2016-10-05 22:15:56 -0700887 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, mpls100Selector)))
888 .filteredIngressPoints(ImmutableSet.of(
889 new FilteredConnectPoint(d1p10, vlan100Selector),
890 new FilteredConnectPoint(d1p11, mpls200Selector),
891 new FilteredConnectPoint(d2p10, vlan200Selector)
Pier Ventre5c4a0762016-11-21 10:28:13 -0800892 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -0700893
894 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -0800895
Pier Ventre766995d2016-10-05 22:15:56 -0700896 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
897
898 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
899 assertThat(compiled, hasSize(1));
900
901 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
902 assertThat(rules, hasSize(5));
903
904 Collection<FlowRule> rulesS1 = rules.stream()
905 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
906 .collect(Collectors.toSet());
907 assertThat(rulesS1, hasSize(2));
908 FlowRule ruleS1 = rulesS1.stream()
909 .filter(rule -> {
910 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
911 return inPort.port().equals(d1p10.port());
912 })
913 .findFirst()
914 .get();
915 assertThat(ruleS1.selector(), is(
916 DefaultTrafficSelector
917 .builder(vlan100Selector)
918 .matchInPort(d1p10.port())
919 .build()
920 ));
921 assertThat(ruleS1.treatment(), is(
922 DefaultTrafficTreatment
923 .builder()
924 .popVlan()
925 .pushMpls()
926 .setMpls(MplsLabel.mplsLabel(LABEL))
927 .setOutput(d1p0.port())
928 .build()
929 ));
930
931 ruleS1 = rulesS1.stream()
932 .filter(rule -> {
933 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
934 return inPort.port().equals(d1p11.port());
935 })
936 .findFirst()
937 .get();
938 assertThat(ruleS1.selector(), is(
939 DefaultTrafficSelector
940 .builder(mpls200Selector)
941 .matchInPort(d1p11.port())
942 .build()
943 ));
944 assertThat(ruleS1.treatment(), is(
945 DefaultTrafficTreatment
946 .builder()
947 .setMpls(MplsLabel.mplsLabel(LABEL))
948 .setOutput(d1p0.port())
949 .build()
950 ));
951
952 Collection<FlowRule> rulesS2 = rules.stream()
953 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
954 .collect(Collectors.toSet());
955 assertThat(rulesS2, hasSize(2));
956 FlowRule ruleS2 = rulesS2.stream()
957 .filter(rule -> {
958 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
959 return inPort.port().equals(d2p10.port());
960 })
961 .findFirst()
962 .get();
963 assertThat(ruleS2.selector(), is(
964 DefaultTrafficSelector
965 .builder(vlan200Selector)
966 .matchInPort(d2p10.port())
967 .build()
968 ));
969 assertThat(ruleS2.treatment(), is(
970 DefaultTrafficTreatment
971 .builder()
972 .popVlan()
973 .pushMpls()
974 .setMpls(MplsLabel.mplsLabel(LABEL))
975 .setOutput(d2p1.port())
976 .build()
977 ));
978
979 ruleS2 = rulesS2.stream()
980 .filter(rule -> {
981 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
982 return inPort.port().equals(d2p0.port());
983 })
984 .findFirst()
985 .get();
986 assertThat(ruleS2.selector(), is(
987 DefaultTrafficSelector
988 .builder()
989 .matchInPort(d2p0.port())
990 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
991 .matchEthType(Ethernet.MPLS_UNICAST)
992 .build()
993 ));
994 assertThat(ruleS2.treatment(), is(
995 DefaultTrafficTreatment
996 .builder()
997 .setMpls(MplsLabel.mplsLabel(LABEL))
998 .setOutput(d2p1.port())
999 .build()
1000 ));
1001
1002 Collection<FlowRule> rulesS3 = rules.stream()
1003 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
1004 .collect(Collectors.toSet());
1005 assertThat(rulesS3, hasSize(1));
1006 FlowRule ruleS3 = rulesS3.iterator().next();
1007 assertThat(ruleS3.selector(), is(
1008 DefaultTrafficSelector
1009 .builder()
1010 .matchInPort(d3p0.port())
1011 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
1012 .matchEthType(Ethernet.MPLS_UNICAST)
1013 .build()
1014 ));
1015 assertThat(ruleS3.treatment(), is(
1016 DefaultTrafficTreatment
1017 .builder()
1018 .setMpls(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
1019 .setOutput(d3p10.port())
1020 .build()
1021 ));
1022
1023 sut.deactivate();
1024
1025 }
1026
1027 /**
1028 * We test the proper compilation of sp2mp with the VLAN
1029 * encapsulation and filtered selectors of different type.
1030 */
1031 @Test
Pier Ventre5c4a0762016-11-21 10:28:13 -08001032 public void testVlanEncapsulationDifferentFilterForSp() {
Pier Ventre766995d2016-10-05 22:15:56 -07001033
1034 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001035 .appId(APP_ID).selector(selector).treatment(treatment)
1036 .constraints(constraintsForVlan).links(linksForSp2Mp)
Pier Ventre766995d2016-10-05 22:15:56 -07001037 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p10, vlan200Selector)))
1038 .filteredEgressPoints(ImmutableSet.of(
1039 new FilteredConnectPoint(d1p10, mpls100Selector),
1040 new FilteredConnectPoint(d1p11, vlan100Selector),
1041 new FilteredConnectPoint(d2p10, mpls200Selector)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001042 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -07001043
1044 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -08001045
Pier Ventre766995d2016-10-05 22:15:56 -07001046 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1047
1048 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1049 assertThat(compiled, hasSize(1));
1050
1051 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1052 assertThat(rules, hasSize(3));
1053
1054 Collection<FlowRule> rulesS3 = rules.stream()
1055 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
1056 .collect(Collectors.toSet());
1057 assertThat(rulesS3, hasSize(1));
1058 FlowRule ruleS3 = rulesS3.iterator().next();
1059 assertThat(ruleS3.selector(), is(
1060 DefaultTrafficSelector
1061 .builder(vlan200Selector)
1062 .matchInPort(d3p10.port())
1063 .build()
1064 ));
1065 assertThat(ruleS3.treatment(), is(
1066 DefaultTrafficTreatment
1067 .builder()
1068 .setVlanId(VlanId.vlanId(LABEL))
1069 .setOutput(d3p0.port())
1070 .build()
1071 ));
1072
1073 Collection<FlowRule> rulesS2 = rules.stream()
1074 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1075 .collect(Collectors.toSet());
1076 assertThat(rulesS2, hasSize(1));
1077 FlowRule ruleS2 = rulesS2.iterator().next();
1078 assertThat(ruleS2.selector(), is(
1079 DefaultTrafficSelector
1080 .builder()
1081 .matchInPort(d2p1.port())
1082 .matchVlanId(VlanId.vlanId(LABEL))
1083 .build()
1084 ));
1085 assertThat(ruleS2.treatment(), is(
1086 DefaultTrafficTreatment
1087 .builder()
1088 .setVlanId(VlanId.vlanId(LABEL))
1089 .setOutput(d2p0.port())
1090 .popVlan()
1091 .pushMpls()
1092 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1093 .setOutput(d2p10.port())
1094 .build()
1095 ));
1096
1097 Collection<FlowRule> rulesS1 = rules.stream()
1098 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1099 .collect(Collectors.toSet());
1100 assertThat(rulesS1, hasSize(1));
1101 FlowRule ruleS1 = rulesS1.iterator().next();
1102 assertThat(ruleS1.selector(), is(
1103 DefaultTrafficSelector
1104 .builder()
1105 .matchInPort(d1p0.port())
1106 .matchVlanId(VlanId.vlanId(LABEL))
1107 .build()
1108 ));
1109 assertThat(ruleS1.treatment(), is(
1110 DefaultTrafficTreatment
1111 .builder()
Yi Tseng84c5a3d2017-04-14 16:42:59 -07001112 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1113 .setOutput(d1p11.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001114 .popVlan()
1115 .pushMpls()
1116 .setMpls(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
1117 .setOutput(d1p10.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001118 .build()
1119 ));
1120
1121 sut.deactivate();
1122
1123 }
1124
1125 /**
Pier Ventre5c4a0762016-11-21 10:28:13 -08001126 * We test the proper compilation of sp2mp with trivial selector,
1127 * trivial treatment, vlan encapsulation and co-located
1128 * ingress/egress points.
Pier Ventre766995d2016-10-05 22:15:56 -07001129 */
1130 @Test
Pier Ventre5c4a0762016-11-21 10:28:13 -08001131 public void testCoLocatedPointsTrivialForSp() {
Pier Ventre766995d2016-10-05 22:15:56 -07001132
1133 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001134 .appId(APP_ID).selector(selector).treatment(treatment)
1135 .applyTreatmentOnEgress(true).links(linksForSp2MpCoLoc)
Pier Ventre766995d2016-10-05 22:15:56 -07001136 .constraints(constraintsForVlan)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001137 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10)))
1138 .filteredEgressPoints(ImmutableSet.of(
1139 new FilteredConnectPoint(d1p11),
1140 new FilteredConnectPoint(d2p10),
1141 new FilteredConnectPoint(d3p10)
1142 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -07001143
1144 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -08001145
Pier Ventre766995d2016-10-05 22:15:56 -07001146 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1147
1148 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1149 assertThat(compiled, hasSize(1));
1150
1151 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1152 assertThat(rules, hasSize(3));
1153
1154 Collection<FlowRule> rulesS1 = rules.stream()
1155 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1156 .collect(Collectors.toSet());
1157 assertThat(rulesS1, hasSize(1));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001158
Pier Ventre766995d2016-10-05 22:15:56 -07001159 FlowRule ruleS1 = rulesS1.iterator().next();
1160 assertThat(ruleS1.selector(), is(
1161 DefaultTrafficSelector
Pier Ventre5c4a0762016-11-21 10:28:13 -08001162 .builder(selector)
Pier Ventre766995d2016-10-05 22:15:56 -07001163 .matchInPort(d1p10.port())
1164 .build()
1165 ));
1166 assertThat(ruleS1.treatment(), is(
1167 DefaultTrafficTreatment
1168 .builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001169 .pushVlan()
Pier Ventre766995d2016-10-05 22:15:56 -07001170 .setVlanId(VlanId.vlanId(LABEL))
1171 .setOutput(d1p0.port())
Yi Tseng84c5a3d2017-04-14 16:42:59 -07001172 .popVlan()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001173 .setOutput(d1p11.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001174 .build()
1175 ));
1176
1177 Collection<FlowRule> rulesS2 = rules.stream()
1178 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1179 .collect(Collectors.toSet());
1180 assertThat(rulesS2, hasSize(1));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001181
Pier Ventre766995d2016-10-05 22:15:56 -07001182 FlowRule ruleS2 = rulesS2.iterator().next();
1183 assertThat(ruleS2.selector(), is(
1184 DefaultTrafficSelector
1185 .builder()
1186 .matchInPort(d2p0.port())
1187 .matchVlanId(VlanId.vlanId(LABEL))
1188 .build()
1189 ));
1190 assertThat(ruleS2.treatment(), is(
1191 DefaultTrafficTreatment
1192 .builder()
1193 .setVlanId(VlanId.vlanId(LABEL))
1194 .setOutput(d2p1.port())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001195 .popVlan()
1196 .setOutput(d2p10.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001197 .build()
1198 ));
1199
1200 Collection<FlowRule> rulesS3 = rules.stream()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001201 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
Pier Ventre766995d2016-10-05 22:15:56 -07001202 .collect(Collectors.toSet());
1203 assertThat(rulesS3, hasSize(1));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001204
Pier Ventre766995d2016-10-05 22:15:56 -07001205 FlowRule ruleS3 = rulesS3.iterator().next();
1206 assertThat(ruleS3.selector(), is(
1207 DefaultTrafficSelector
1208 .builder()
1209 .matchInPort(d3p0.port())
1210 .matchVlanId(VlanId.vlanId(LABEL))
1211 .build()
1212 ));
1213 assertThat(ruleS3.treatment(), is(
1214 DefaultTrafficTreatment
1215 .builder()
1216 .popVlan()
Pier Ventre766995d2016-10-05 22:15:56 -07001217 .setOutput(d3p10.port())
1218 .build()
1219 ));
1220
1221 sut.deactivate();
1222
1223 }
1224
1225 /**
Pier Ventre5c4a0762016-11-21 10:28:13 -08001226 * We test the proper compilation of mp2sp with trivial selector,
1227 * trivial treatment, mpls encapsulation and co-located
1228 * ingress/egress points.
Pier Ventre766995d2016-10-05 22:15:56 -07001229 */
1230 @Test
Pier Ventre5c4a0762016-11-21 10:28:13 -08001231 public void testCoLocatedPointsTrivialForMp() {
Pier Ventre766995d2016-10-05 22:15:56 -07001232
1233 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001234 .appId(APP_ID).selector(selector).treatment(treatment)
1235 .links(linksForMp2SpCoLoc).constraints(constraintsForMPLS)
1236 .filteredIngressPoints(ImmutableSet.of(
1237 new FilteredConnectPoint(d1p10),
1238 new FilteredConnectPoint(d2p10)
1239 ))
1240 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p11)))
1241 .build();
1242
1243 sut.activate();
1244
1245 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1246
1247 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1248 assertThat(compiled, hasSize(1));
1249
1250 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1251 assertThat(rules, hasSize(3));
1252
1253 Collection<FlowRule> rulesS1 = rules.stream()
1254 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1255 .collect(Collectors.toSet());
1256 assertThat(rulesS1, hasSize(2));
1257
1258 FlowRule ruleS1 = rulesS1.stream()
1259 .filter(rule -> {
1260 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1261 return inPort.port().equals(d1p10.port());
1262 })
1263 .findFirst()
1264 .get();
1265 assertThat(ruleS1.selector(), is(
1266 DefaultTrafficSelector
1267 .builder()
1268 .matchInPort(d1p10.port())
1269 .build()
1270 ));
1271 assertThat(ruleS1.treatment(), is(
1272 DefaultTrafficTreatment
1273 .builder()
1274 .setOutput(d1p11.port())
1275 .build()
1276 ));
1277
1278 ruleS1 = rulesS1.stream()
1279 .filter(rule -> {
1280 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1281 return inPort.port().equals(d1p0.port());
1282 })
1283 .findFirst()
1284 .get();
1285 assertThat(ruleS1.selector(), is(
1286 DefaultTrafficSelector
1287 .builder()
1288 .matchEthType(Ethernet.MPLS_UNICAST)
1289 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
1290 .matchInPort(d1p0.port())
1291 .build()
1292 ));
1293 assertThat(ruleS1.treatment(), is(
1294 DefaultTrafficTreatment
1295 .builder()
1296 .popMpls(IPV4.ethType())
1297 .setOutput(d1p11.port())
1298 .build()
1299 ));
1300
1301 Collection<FlowRule> rulesS2 = rules.stream()
1302 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1303 .collect(Collectors.toSet());
1304 assertThat(rulesS2, hasSize(1));
1305
1306 FlowRule ruleS2 = rulesS2.iterator().next();
1307 assertThat(ruleS2.selector(), is(
1308 DefaultTrafficSelector
1309 .builder()
1310 .matchInPort(d2p10.port())
1311 .build()
1312 ));
1313 assertThat(ruleS2.treatment(), is(
1314 DefaultTrafficTreatment
1315 .builder()
1316 .pushMpls()
1317 .setMpls(MplsLabel.mplsLabel(LABEL))
1318 .setOutput(d2p0.port())
1319 .build()
1320 ));
1321
1322 sut.deactivate();
1323
1324 }
1325
1326 /**
1327 * We test the proper compilation of sp2mp with trivial selector,
1328 * trivial treatment, mpls encapsulation and co-located
1329 * filtered ingress/egress points.
1330 */
1331 @Test
1332 public void testCoLocatedFilteredPointsTrivialForSp() {
1333
1334 intent = LinkCollectionIntent.builder()
1335 .appId(APP_ID).selector(selector).treatment(treatment)
1336 .applyTreatmentOnEgress(true).links(linksForSp2MpCoLoc)
Pier Ventre766995d2016-10-05 22:15:56 -07001337 .constraints(constraintsForMPLS)
Pier Ventre766995d2016-10-05 22:15:56 -07001338 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
Pier Ventre5c4a0762016-11-21 10:28:13 -08001339 .filteredEgressPoints(ImmutableSet.of(
1340 new FilteredConnectPoint(d1p11, vlan200Selector),
1341 new FilteredConnectPoint(d2p10, vlan300Selector),
1342 new FilteredConnectPoint(d3p10, vlan69Selector)
1343 )).build();
Pier Ventre766995d2016-10-05 22:15:56 -07001344
1345 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -08001346
Pier Ventre766995d2016-10-05 22:15:56 -07001347 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1348
1349 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1350 assertThat(compiled, hasSize(1));
1351
1352 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1353 assertThat(rules, hasSize(3));
1354
1355 Collection<FlowRule> rulesS1 = rules.stream()
1356 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1357 .collect(Collectors.toSet());
1358 assertThat(rulesS1, hasSize(1));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001359
Pier Ventre766995d2016-10-05 22:15:56 -07001360 FlowRule ruleS1 = rulesS1.iterator().next();
1361 assertThat(ruleS1.selector(), is(
1362 DefaultTrafficSelector
1363 .builder(vlan100Selector)
1364 .matchInPort(d1p10.port())
1365 .build()
1366 ));
1367 assertThat(ruleS1.treatment(), is(
1368 DefaultTrafficTreatment
1369 .builder()
1370 .popVlan()
1371 .pushMpls()
1372 .setMpls(MplsLabel.mplsLabel(LABEL))
1373 .setOutput(d1p0.port())
Yi Tseng84c5a3d2017-04-14 16:42:59 -07001374 .popMpls(IPV4.ethType())
1375 .pushVlan()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001376 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1377 .setOutput(d1p11.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001378 .build()
1379 ));
1380
Pier Ventre5c4a0762016-11-21 10:28:13 -08001381
Pier Ventre766995d2016-10-05 22:15:56 -07001382 Collection<FlowRule> rulesS2 = rules.stream()
1383 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1384 .collect(Collectors.toSet());
1385 assertThat(rulesS2, hasSize(1));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001386
Pier Ventre766995d2016-10-05 22:15:56 -07001387 FlowRule ruleS2 = rulesS2.iterator().next();
1388 assertThat(ruleS2.selector(), is(
1389 DefaultTrafficSelector
1390 .builder()
1391 .matchInPort(d2p0.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001392 .matchEthType(Ethernet.MPLS_UNICAST)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001393 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
Pier Ventre766995d2016-10-05 22:15:56 -07001394 .build()
1395 ));
1396 assertThat(ruleS2.treatment(), is(
1397 DefaultTrafficTreatment
1398 .builder()
1399 .setMpls(MplsLabel.mplsLabel(LABEL))
1400 .setOutput(d2p1.port())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001401 .popMpls(IPV4.ethType())
1402 .pushVlan()
1403 .setVlanId(((VlanIdCriterion) vlan300Selector.getCriterion(VLAN_VID)).vlanId())
1404 .setOutput(d2p10.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001405 .build()
1406 ));
1407
Pier Ventre5c4a0762016-11-21 10:28:13 -08001408
Pier Ventre766995d2016-10-05 22:15:56 -07001409 Collection<FlowRule> rulesS3 = rules.stream()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001410 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
Pier Ventre766995d2016-10-05 22:15:56 -07001411 .collect(Collectors.toSet());
1412 assertThat(rulesS3, hasSize(1));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001413
Pier Ventre766995d2016-10-05 22:15:56 -07001414 FlowRule ruleS3 = rulesS3.iterator().next();
1415 assertThat(ruleS3.selector(), is(
1416 DefaultTrafficSelector
1417 .builder()
1418 .matchInPort(d3p0.port())
Pier Ventre766995d2016-10-05 22:15:56 -07001419 .matchEthType(Ethernet.MPLS_UNICAST)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001420 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
Pier Ventre766995d2016-10-05 22:15:56 -07001421 .build()
1422 ));
1423 assertThat(ruleS3.treatment(), is(
1424 DefaultTrafficTreatment
1425 .builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001426 .popMpls(IPV4.ethType())
1427 .pushVlan()
1428 .setVlanId(((VlanIdCriterion) vlan69Selector.getCriterion(VLAN_VID)).vlanId())
Pier Ventre766995d2016-10-05 22:15:56 -07001429 .setOutput(d3p10.port())
1430 .build()
1431 ));
1432
1433 sut.deactivate();
1434
1435 }
1436
Pier Ventreffe88d62016-10-13 14:34:40 -07001437 /**
Pier Ventre5c4a0762016-11-21 10:28:13 -08001438 * We test the proper compilation of mp2sp with trivial selector,
1439 * trivial treatment, vlan encapsulation and co-located
1440 * filtered ingress/egress points.
Pier Ventreffe88d62016-10-13 14:34:40 -07001441 */
1442 @Test
Pier Ventre5c4a0762016-11-21 10:28:13 -08001443 public void testCoLocatedFilteredPointsTrivialForMp() {
Pier Ventreffe88d62016-10-13 14:34:40 -07001444
1445 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001446 .appId(APP_ID).selector(selector).treatment(treatment).links(linksForMp2SpCoLoc)
Pier Ventreffe88d62016-10-13 14:34:40 -07001447 .constraints(constraintsForVlan)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001448 .filteredIngressPoints(ImmutableSet.of(
1449 new FilteredConnectPoint(d1p10, mpls100Selector),
1450 new FilteredConnectPoint(d2p10, mpls200Selector)
1451 ))
1452 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p11, mpls69Selector)))
Pier Ventreffe88d62016-10-13 14:34:40 -07001453 .build();
1454
1455 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -08001456
Pier Ventreffe88d62016-10-13 14:34:40 -07001457 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1458
1459 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1460 assertThat(compiled, hasSize(1));
1461
1462 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1463 assertThat(rules, hasSize(3));
1464
1465 Collection<FlowRule> rulesS1 = rules.stream()
1466 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1467 .collect(Collectors.toSet());
Pier Ventre5c4a0762016-11-21 10:28:13 -08001468 assertThat(rulesS1, hasSize(2));
1469
1470 FlowRule ruleS1 = rulesS1.stream()
1471 .filter(rule -> {
1472 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1473 return inPort.port().equals(d1p10.port());
1474 })
1475 .findFirst()
1476 .get();
Pier Ventreffe88d62016-10-13 14:34:40 -07001477 assertThat(ruleS1.selector(), is(
1478 DefaultTrafficSelector
Pier Ventre5c4a0762016-11-21 10:28:13 -08001479 .builder(mpls100Selector)
Pier Ventreffe88d62016-10-13 14:34:40 -07001480 .matchInPort(d1p10.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001481 .build()
1482 ));
1483 assertThat(ruleS1.treatment(), is(
1484 DefaultTrafficTreatment
1485 .builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001486 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
1487 .setOutput(d1p11.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001488 .build()
1489 ));
1490
Pier Ventre5c4a0762016-11-21 10:28:13 -08001491 ruleS1 = rulesS1.stream()
1492 .filter(rule -> {
1493 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1494 return inPort.port().equals(d1p0.port());
1495 })
1496 .findFirst()
1497 .get();
1498 assertThat(ruleS1.selector(), is(
Pier Ventreffe88d62016-10-13 14:34:40 -07001499 DefaultTrafficSelector
1500 .builder()
Pier Ventreffe88d62016-10-13 14:34:40 -07001501 .matchVlanId(VlanId.vlanId(LABEL))
Pier Ventre5c4a0762016-11-21 10:28:13 -08001502 .matchInPort(d1p0.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001503 .build()
1504 ));
Pier Ventre5c4a0762016-11-21 10:28:13 -08001505 assertThat(ruleS1.treatment(), is(
Pier Ventreffe88d62016-10-13 14:34:40 -07001506 DefaultTrafficTreatment
1507 .builder()
Pier Ventreffe88d62016-10-13 14:34:40 -07001508 .popVlan()
1509 .pushMpls()
1510 .setMpls(((MplsCriterion) mpls69Selector.getCriterion(MPLS_LABEL)).label())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001511 .setOutput(d1p11.port())
1512 .build()
1513 ));
1514
1515 Collection<FlowRule> rulesS2 = rules.stream()
1516 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1517 .collect(Collectors.toSet());
1518 assertThat(rulesS2, hasSize(1));
1519 FlowRule ruleS2 = rulesS2.iterator().next();
1520 assertThat(ruleS2.selector(), is(
1521 DefaultTrafficSelector
1522 .builder(mpls200Selector)
1523 .matchInPort(d2p10.port())
1524 .build()
1525 ));
1526 assertThat(ruleS2.treatment(), is(
1527 DefaultTrafficTreatment
1528 .builder()
1529 .popMpls(IPV4.ethType())
1530 .pushVlan()
1531 .setVlanId(VlanId.vlanId(LABEL))
1532 .setOutput(d2p0.port())
1533 .build()
1534 ));
1535
1536 sut.deactivate();
1537
1538 }
1539
1540 /**
1541 * We test the proper compilation of sp2mp with trivial selector,
1542 * trivial treatment, vlan encapsulation and co-located
1543 * different filtered ingress/egress points.
1544 */
1545 @Test
1546 public void testCoLocatedDifferentFilteredPointsTrivialForSp() {
1547
1548 intent = LinkCollectionIntent.builder()
1549 .appId(APP_ID).selector(selector).treatment(treatment)
1550 .applyTreatmentOnEgress(true).links(linksForSp2MpCoLoc)
1551 .constraints(constraintsForVlan)
1552 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
1553 .filteredEgressPoints(ImmutableSet.of(
1554 new FilteredConnectPoint(d1p11, mpls100Selector),
1555 new FilteredConnectPoint(d2p10, vlan200Selector),
1556 new FilteredConnectPoint(d3p10, mpls200Selector)
1557 )).build();
1558
1559 sut.activate();
1560
1561 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1562
1563 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1564 assertThat(compiled, hasSize(1));
1565
1566 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1567 assertThat(rules, hasSize(3));
1568
1569 Collection<FlowRule> rulesS1 = rules.stream()
1570 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1571 .collect(Collectors.toSet());
1572 assertThat(rulesS1, hasSize(1));
1573 FlowRule ruleS1 = rulesS1.iterator().next();
1574 assertThat(ruleS1.selector(), is(
1575 DefaultTrafficSelector
1576 .builder(vlan100Selector)
1577 .matchInPort(d1p10.port())
1578 .build()
1579 ));
1580 assertThat(ruleS1.treatment(), is(
1581 DefaultTrafficTreatment
1582 .builder()
1583 .setVlanId(VlanId.vlanId(LABEL))
1584 .setOutput(d1p0.port())
1585 .popVlan()
1586 .pushMpls()
1587 .setMpls(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
1588 .setOutput(d1p11.port())
1589 .build()
1590 ));
1591
1592 Collection<FlowRule> rulesS2 = rules.stream()
1593 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1594 .collect(Collectors.toSet());
1595 assertThat(rulesS2, hasSize(1));
1596 FlowRule ruleS2 = rulesS2.iterator().next();
1597 assertThat(ruleS2.selector(), is(
1598 DefaultTrafficSelector
1599 .builder()
1600 .matchInPort(d2p0.port())
1601 .matchVlanId(VlanId.vlanId(LABEL))
1602 .build()
1603 ));
1604 assertThat(ruleS2.treatment(), is(
1605 DefaultTrafficTreatment
1606 .builder()
1607 .setVlanId(VlanId.vlanId(LABEL))
1608 .setOutput(d2p1.port())
1609 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1610 .setOutput(d2p10.port())
1611 .build()
1612 ));
1613
1614 Collection<FlowRule> rulesS3 = rules.stream()
1615 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1616 .collect(Collectors.toSet());
1617 assertThat(rulesS3, hasSize(1));
1618 FlowRule ruleS3 = rulesS3.iterator().next();
1619 assertThat(ruleS3.selector(), is(
1620 DefaultTrafficSelector
1621 .builder()
1622 .matchVlanId(VlanId.vlanId(LABEL))
1623 .matchInPort(d3p0.port())
1624 .build()
1625 ));
1626 assertThat(ruleS3.treatment(), is(
1627 DefaultTrafficTreatment
1628 .builder()
1629 .popVlan()
1630 .pushMpls()
1631 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
Pier Ventreffe88d62016-10-13 14:34:40 -07001632 .setOutput(d3p10.port())
1633 .build()
1634 ));
1635
1636 sut.deactivate();
1637
1638 }
1639
1640 /**
Pier Ventre5c4a0762016-11-21 10:28:13 -08001641 * We test the proper compilation of mp2sp with trivial selector,
1642 * trivial treatment, mpls encapsulation and co-located
1643 * filtered ingress/egress points.
Pier Ventreffe88d62016-10-13 14:34:40 -07001644 */
1645 @Test
Pier Ventre5c4a0762016-11-21 10:28:13 -08001646 public void testCoLocatedDifferentFilteredPointsTrivialForMp() {
Pier Ventreffe88d62016-10-13 14:34:40 -07001647
1648 intent = LinkCollectionIntent.builder()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001649 .appId(APP_ID).selector(selector).treatment(treatment)
1650 .links(linksForMp2SpCoLoc).constraints(constraintsForMPLS)
1651 .filteredIngressPoints(ImmutableSet.of(
1652 new FilteredConnectPoint(d1p10, mpls100Selector),
1653 new FilteredConnectPoint(d2p10, vlan100Selector)
1654 ))
1655 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p11, mpls200Selector)))
Pier Ventreffe88d62016-10-13 14:34:40 -07001656 .build();
1657
1658 sut.activate();
Pier Ventre5c4a0762016-11-21 10:28:13 -08001659
1660 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1661
1662 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1663 assertThat(compiled, hasSize(1));
1664
1665 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1666 assertThat(rules, hasSize(3));
1667
1668 Collection<FlowRule> rulesS1 = rules.stream()
1669 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1670 .collect(Collectors.toSet());
1671 assertThat(rulesS1, hasSize(2));
1672 FlowRule ruleS1 = rulesS1.stream()
1673 .filter(rule -> {
1674 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1675 return inPort.port().equals(d1p10.port());
1676 })
1677 .findFirst()
1678 .get();
1679 assertThat(ruleS1.selector(), is(
1680 DefaultTrafficSelector
1681 .builder(mpls100Selector)
1682 .matchInPort(d1p10.port())
1683 .build()
1684 ));
1685 assertThat(ruleS1.treatment(), is(
1686 DefaultTrafficTreatment
1687 .builder()
1688 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1689 .setOutput(d1p11.port())
1690 .build()
1691 ));
1692 ruleS1 = rulesS1.stream()
1693 .filter(rule -> {
1694 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1695 return inPort.port().equals(d1p0.port());
1696 })
1697 .findFirst()
1698 .get();
1699 assertThat(ruleS1.selector(), is(
1700 DefaultTrafficSelector
1701 .builder()
1702 .matchEthType(Ethernet.MPLS_UNICAST)
1703 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
1704 .matchInPort(d1p0.port())
1705 .build()
1706 ));
1707 assertThat(ruleS1.treatment(), is(
1708 DefaultTrafficTreatment
1709 .builder()
1710 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1711 .setOutput(d1p11.port())
1712 .build()
1713 ));
1714
1715 Collection<FlowRule> rulesS2 = rules.stream()
1716 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1717 .collect(Collectors.toSet());
1718 assertThat(rulesS2, hasSize(1));
1719 FlowRule ruleS2 = rulesS2.iterator().next();
1720 assertThat(ruleS2.selector(), is(
1721 DefaultTrafficSelector
1722 .builder(vlan100Selector)
1723 .matchInPort(d2p10.port())
1724 .build()
1725 ));
1726 assertThat(ruleS2.treatment(), is(
1727 DefaultTrafficTreatment
1728 .builder()
1729 .popVlan()
1730 .pushMpls()
1731 .setMpls(MplsLabel.mplsLabel(LABEL))
1732 .setOutput(d2p0.port())
1733 .build()
1734 ));
1735
1736 sut.deactivate();
1737
1738 }
1739
1740 /**
1741 * We test the proper compilation of sp2mp with selector,
1742 * treatment, mpls encapsulation and co-located
1743 * different filtered ingress/egress points.
1744 */
1745 @Test
1746 public void testCoLocatedDifferentFilteredPointsNonTrivialForSp() {
1747
1748 intent = LinkCollectionIntent.builder()
1749 .appId(APP_ID).selector(ipPrefixSelector).treatment(ethDstTreatment)
1750 .applyTreatmentOnEgress(true).links(linksForSp2MpCoLoc)
1751 .constraints(constraintsForMPLS)
1752 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p10, vlan100Selector)))
1753 .filteredEgressPoints(ImmutableSet.of(
1754 new FilteredConnectPoint(d1p11, mpls100Selector),
1755 new FilteredConnectPoint(d2p10, vlan200Selector),
1756 new FilteredConnectPoint(d3p10, mpls200Selector)
1757 )).build();
1758
1759 sut.activate();
1760
Pier Ventreffe88d62016-10-13 14:34:40 -07001761 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1762
1763 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1764 assertThat(compiled, hasSize(1));
1765
1766 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1767 assertThat(rules, hasSize(3));
1768
1769 Collection<FlowRule> rulesS1 = rules.stream()
1770 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1771 .collect(Collectors.toSet());
1772 assertThat(rulesS1, hasSize(1));
1773 FlowRule ruleS1 = rulesS1.iterator().next();
1774 assertThat(ruleS1.selector(), is(
1775 DefaultTrafficSelector
1776 .builder(ipPrefixSelector)
1777 .matchInPort(d1p10.port())
1778 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1779 .build()
1780 ));
1781 assertThat(ruleS1.treatment(), is(
1782 DefaultTrafficTreatment
1783 .builder()
1784 .popVlan()
1785 .pushMpls()
1786 .setMpls(MplsLabel.mplsLabel(LABEL))
1787 .setOutput(d1p0.port())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001788 .setEthDst(((ModEtherInstruction) ethDstTreatment
1789 .allInstructions()
1790 .stream()
1791 .filter(instruction -> instruction instanceof ModEtherInstruction)
1792 .findFirst().get()).mac())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001793 .setMpls(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
1794 .setOutput(d1p11.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001795 .build()
1796 ));
1797
1798 Collection<FlowRule> rulesS2 = rules.stream()
1799 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1800 .collect(Collectors.toSet());
1801 assertThat(rulesS2, hasSize(1));
1802 FlowRule ruleS2 = rulesS2.iterator().next();
1803 assertThat(ruleS2.selector(), is(
1804 DefaultTrafficSelector
1805 .builder()
1806 .matchInPort(d2p0.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001807 .matchEthType(Ethernet.MPLS_UNICAST)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001808 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
Pier Ventreffe88d62016-10-13 14:34:40 -07001809 .build()
1810 ));
1811 assertThat(ruleS2.treatment(), is(
1812 DefaultTrafficTreatment
1813 .builder()
1814 .setMpls(MplsLabel.mplsLabel(LABEL))
1815 .setOutput(d2p1.port())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001816 .setEthDst(((ModEtherInstruction) ethDstTreatment
1817 .allInstructions()
1818 .stream()
1819 .filter(instruction -> instruction instanceof ModEtherInstruction)
1820 .findFirst().get()).mac())
1821 .popMpls(IPV4.ethType())
1822 .pushVlan()
1823 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1824 .setOutput(d2p10.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001825 .build()
1826 ));
1827
1828 Collection<FlowRule> rulesS3 = rules.stream()
Pier Ventre5c4a0762016-11-21 10:28:13 -08001829 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
Pier Ventreffe88d62016-10-13 14:34:40 -07001830 .collect(Collectors.toSet());
1831 assertThat(rulesS3, hasSize(1));
1832 FlowRule ruleS3 = rulesS3.iterator().next();
1833 assertThat(ruleS3.selector(), is(
1834 DefaultTrafficSelector
1835 .builder()
Pier Ventreffe88d62016-10-13 14:34:40 -07001836 .matchEthType(Ethernet.MPLS_UNICAST)
Pier Ventre5c4a0762016-11-21 10:28:13 -08001837 .matchMplsLabel(MplsLabel.mplsLabel(LABEL))
1838 .matchInPort(d3p0.port())
Pier Ventreffe88d62016-10-13 14:34:40 -07001839 .build()
1840 ));
1841 assertThat(ruleS3.treatment(), is(
1842 DefaultTrafficTreatment
1843 .builder()
1844 .setEthDst(((ModEtherInstruction) ethDstTreatment
1845 .allInstructions()
1846 .stream()
1847 .filter(instruction -> instruction instanceof ModEtherInstruction)
1848 .findFirst().get()).mac())
Pier Ventre5c4a0762016-11-21 10:28:13 -08001849 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
Pier Ventreffe88d62016-10-13 14:34:40 -07001850 .setOutput(d3p10.port())
1851 .build()
1852 ));
1853
1854 sut.deactivate();
1855
1856 }
1857
Pier Ventre5c4a0762016-11-21 10:28:13 -08001858 /**
1859 * We test the proper compilation of mp2sp with selector,
1860 * treatment, vlan encapsulation and co-located
1861 * filtered ingress/egress points.
1862 */
1863 @Test
1864 public void testCoLocatedDifferentFilteredPointsNonTrivialForMp() {
1865
1866 intent = LinkCollectionIntent.builder()
1867 .appId(APP_ID).selector(ipPrefixSelector).treatment(ethDstTreatment)
1868 .links(linksForMp2SpCoLoc).constraints(constraintsForVlan)
1869 .filteredIngressPoints(ImmutableSet.of(
1870 new FilteredConnectPoint(d1p10, mpls100Selector),
1871 new FilteredConnectPoint(d2p10, vlan100Selector)
1872 ))
1873 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p11, mpls200Selector)))
1874 .build();
1875
1876 sut.activate();
1877
1878 LinkCollectionCompiler.labelAllocator.setLabelSelection(LABEL_SELECTION);
1879
1880 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1881 assertThat(compiled, hasSize(1));
1882
1883 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1884 assertThat(rules, hasSize(3));
1885
1886 Collection<FlowRule> rulesS1 = rules.stream()
1887 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1888 .collect(Collectors.toSet());
1889 assertThat(rulesS1, hasSize(2));
1890 FlowRule ruleS1 = rulesS1.stream()
1891 .filter(rule -> {
1892 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1893 return inPort.port().equals(d1p10.port());
1894 })
1895 .findFirst()
1896 .get();
1897 assertThat(ruleS1.selector(), is(
1898 DefaultTrafficSelector
1899 .builder(ipPrefixSelector)
1900 .matchInPort(d1p10.port())
1901 .matchMplsLabel(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
1902 .build()
1903 ));
1904 assertThat(ruleS1.treatment(), is(
1905 DefaultTrafficTreatment
1906 .builder()
1907 .setEthDst(((ModEtherInstruction) ethDstTreatment
1908 .allInstructions()
1909 .stream()
1910 .filter(instruction -> instruction instanceof ModEtherInstruction)
1911 .findFirst().get()).mac())
1912 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1913 .setOutput(d1p11.port())
1914 .build()
1915 ));
1916
1917 ruleS1 = rulesS1.stream()
1918 .filter(rule -> {
1919 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1920 return inPort.port().equals(d1p0.port());
1921 })
1922 .findFirst()
1923 .get();
1924 assertThat(ruleS1.selector(), is(
1925 DefaultTrafficSelector
1926 .builder()
1927 .matchVlanId(VlanId.vlanId(LABEL))
1928 .matchInPort(d1p0.port())
1929 .build()
1930 ));
1931 assertThat(ruleS1.treatment(), is(
1932 DefaultTrafficTreatment
1933 .builder()
1934 .setEthDst(((ModEtherInstruction) ethDstTreatment
1935 .allInstructions()
1936 .stream()
1937 .filter(instruction -> instruction instanceof ModEtherInstruction)
1938 .findFirst().get()).mac())
1939 .popVlan()
1940 .pushMpls()
1941 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1942 .setOutput(d1p11.port())
1943 .build()
1944 ));
1945
1946 Collection<FlowRule> rulesS2 = rules.stream()
1947 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1948 .collect(Collectors.toSet());
1949 assertThat(rulesS2, hasSize(1));
1950 FlowRule ruleS2 = rulesS2.iterator().next();
1951 assertThat(ruleS2.selector(), is(
1952 DefaultTrafficSelector
1953 .builder(ipPrefixSelector)
1954 .matchInPort(d2p10.port())
1955 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1956 .build()
1957 ));
1958 assertThat(ruleS2.treatment(), is(
1959 DefaultTrafficTreatment
1960 .builder()
1961 .setVlanId(VlanId.vlanId(LABEL))
1962 .setOutput(d2p0.port())
1963 .build()
1964 ));
1965
1966 sut.deactivate();
1967
1968 }
1969
Pier Ventre766995d2016-10-05 22:15:56 -07001970}