blob: 1fae4b923ac4c6db61adeb6126f4700c3c15693b [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.intent.impl.compiler;
17
18import com.google.common.collect.ImmutableSet;
Pier Ventre766995d2016-10-05 22:15:56 -070019import org.hamcrest.core.Is;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080020import org.junit.Before;
21import org.junit.Test;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070022import org.onlab.packet.MacAddress;
Pier Ventre647138f2016-08-26 17:32:44 -070023import org.onlab.packet.VlanId;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080024import org.onosproject.cfg.ComponentConfigAdapter;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080025import org.onosproject.core.CoreService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080026import org.onosproject.net.DefaultLink;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020027import org.onosproject.net.DeviceId;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070028import org.onosproject.net.FilteredConnectPoint;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080029import org.onosproject.net.Link;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070030import org.onosproject.net.PortNumber;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020031import org.onosproject.net.domain.DomainService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080032import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRule;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
Pier Ventre766995d2016-10-05 22:15:56 -070037import org.onosproject.net.flow.criteria.MplsCriterion;
38import org.onosproject.net.flow.criteria.PortCriterion;
39import org.onosproject.net.flow.criteria.VlanIdCriterion;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080040import org.onosproject.net.intent.FlowRuleIntent;
41import org.onosproject.net.intent.Intent;
42import org.onosproject.net.intent.IntentExtensionService;
43import org.onosproject.net.intent.LinkCollectionIntent;
Yuta HIGUCHI1f722032016-12-03 13:56:50 -080044import org.onosproject.net.resource.MockResourceService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045
46import java.util.Collection;
47import java.util.Collections;
48import java.util.List;
49import java.util.Set;
Pier Ventre647138f2016-08-26 17:32:44 -070050import java.util.stream.Collectors;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080051
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070052import static org.easymock.EasyMock.*;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070053import static org.hamcrest.CoreMatchers.instanceOf;
54import static org.hamcrest.CoreMatchers.notNullValue;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055import static org.hamcrest.MatcherAssert.assertThat;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070056import static org.hamcrest.Matchers.*;
Pier Ventre766995d2016-10-05 22:15:56 -070057import static org.onlab.packet.EthType.EtherType.IPV4;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058import static org.onosproject.net.Link.Type.DIRECT;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020059import static org.onosproject.net.NetTestTools.APP_ID;
60import static org.onosproject.net.NetTestTools.PID;
61import static org.onosproject.net.domain.DomainId.LOCAL;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070062import static org.onosproject.net.flow.criteria.Criterion.Type.*;
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020063import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080064
Pier Ventre5c4a0762016-11-21 10:28:13 -080065/**
66 * This set of tests are meant to test the LinkCollectionIntent
67 * compiler.
68 */
Pier Ventre766995d2016-10-05 22:15:56 -070069public class LinkCollectionIntentCompilerTest extends AbstractLinkCollectionTest {
Yi Tseng2a81c9d2016-09-14 10:14:24 -070070
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080071 @Before
Brian O'Connor81134662015-06-25 17:23:33 -040072 public void setUp() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080073 sut = new LinkCollectionIntentCompiler();
74 coreService = createMock(CoreService.class);
75 expect(coreService.registerApplication("org.onosproject.net.intent"))
76 .andReturn(appId);
77 sut.coreService = coreService;
78
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +020079 domainService = createMock(DomainService.class);
80 expect(domainService.getDomain(anyObject(DeviceId.class))).andReturn(LOCAL).anyTimes();
81 sut.domainService = domainService;
82
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070083 super.setUp();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080084
85 intent = LinkCollectionIntent.builder()
86 .appId(APP_ID)
87 .selector(selector)
88 .treatment(treatment)
89 .links(links)
Ray Milkey6e7843d2018-09-18 15:50:41 -070090 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p1)))
91 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p1)))
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092 .build();
Pier Ventre647138f2016-08-26 17:32:44 -070093
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094 intentExtensionService = createMock(IntentExtensionService.class);
95 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
96 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080097
98 registrator = new IntentConfigurableRegistrator();
99 registrator.extensionService = intentExtensionService;
100 registrator.cfgService = new ComponentConfigAdapter();
101 registrator.activate();
102
103 sut.registrator = registrator;
Yuta HIGUCHI1f722032016-12-03 13:56:50 -0800104 sut.resourceService = new MockResourceService();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800105
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700106 LinkCollectionCompiler.optimizeInstructions = false;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700107 LinkCollectionCompiler.copyTtl = false;
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700108
Thomas Szyrkowiec770093f2017-05-05 13:06:53 +0200109 replay(coreService, domainService, intentExtensionService);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700110
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800111 }
112
Pier Ventre5c4a0762016-11-21 10:28:13 -0800113 /**
114 * We test the proper compilation of a simple link collection intent
115 * with connect points, trivial treatment and trivial selector.
116 */
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800117 @Test
118 public void testCompile() {
119 sut.activate();
120
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800121 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800122 assertThat(compiled, hasSize(1));
123
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700124 assertThat("key is inherited",
125 compiled.stream().map(Intent::key).collect(Collectors.toList()),
126 everyItem(is(intent.key())));
127
128
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800129 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
130 assertThat(rules, hasSize(links.size()));
131
132 // if not found, get() raises an exception
133 FlowRule rule1 = rules.stream()
Pier Ventre766995d2016-10-05 22:15:56 -0700134 .filter(rule -> rule.deviceId().equals(d1p10.deviceId()))
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800135 .findFirst()
136 .get();
137 assertThat(rule1.selector(), is(
138 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build()
139 ));
140 assertThat(rule1.treatment(), is(
141 DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build()
142 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400143 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800144
145 FlowRule rule2 = rules.stream()
146 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
147 .findFirst()
148 .get();
149 assertThat(rule2.selector(), is(
150 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build()
151 ));
152 assertThat(rule2.treatment(), is(
153 DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()
154 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400155 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800156
157 FlowRule rule3 = rules.stream()
158 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
159 .findFirst()
160 .get();
161 assertThat(rule3.selector(), is(
162 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build()
163 ));
164 assertThat(rule3.treatment(), is(
165 DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build()
166 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400167 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800168
169 sut.deactivate();
170 }
Pier Ventre647138f2016-08-26 17:32:44 -0700171
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700172 /**
Pier Ventre5c4a0762016-11-21 10:28:13 -0800173 * Single point to multi point case. Scenario is the follow:
174 *
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700175 * -1 of1 2-1 of2 2--1 of3 2-
176 * 3
177 * `-1 of4 2-
Pier Ventre5c4a0762016-11-21 10:28:13 -0800178 *
179 * We test the proper compilation of sp2mp with trivial selector,
180 * trivial treatment and different filtered points
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700181 */
182 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700183 public void testFilteredConnectPointForSp() {
Pier Ventre647138f2016-08-26 17:32:44 -0700184 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700185 Set<Link> testLinks = ImmutableSet.of(
186 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
187 DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
188 DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
189 );
Pier Ventre647138f2016-08-26 17:32:44 -0700190
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700191 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
192 .matchInPort(PortNumber.portNumber(1))
193 .build();
194
195 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
196 .setOutput(PortNumber.portNumber(2))
197 .build();
198
199 TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(vlan100Selector)
200 .matchInPort(PortNumber.portNumber(1))
201 .build();
202
203 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
204 .setOutput(PortNumber.portNumber(2))
205 .setOutput(PortNumber.portNumber(3))
206 .build();
207
208 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
209 .matchInPort(PortNumber.portNumber(1))
210 .build();
211
212 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
213 .setOutput(PortNumber.portNumber(2))
214 .build();
215
216 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
217 .matchInPort(PortNumber.portNumber(1))
218 .build();
219
220 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
221 .setVlanId(VlanId.vlanId("200"))
222 .setOutput(PortNumber.portNumber(2))
223 .build();
Pier Ventre647138f2016-08-26 17:32:44 -0700224
225
Pier Ventre647138f2016-08-26 17:32:44 -0700226
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700227 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
228 new FilteredConnectPoint(of1p1, vlan100Selector)
229 );
Pier Ventre647138f2016-08-26 17:32:44 -0700230
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700231 Set<FilteredConnectPoint> egress = ImmutableSet.of(
232 new FilteredConnectPoint(of3p2, vlan100Selector),
233 new FilteredConnectPoint(of4p2, vlan200Selector)
234 );
Pier Ventre647138f2016-08-26 17:32:44 -0700235
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700236 intent = LinkCollectionIntent.builder()
237 .appId(APP_ID)
238 .filteredIngressPoints(ingress)
239 .filteredEgressPoints(egress)
240 .treatment(treatment)
241 .applyTreatmentOnEgress(true)
242 .links(testLinks)
243 .build();
Pier Ventre647138f2016-08-26 17:32:44 -0700244
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700245 assertThat(sut, is(notNullValue()));
Pier Ventre647138f2016-08-26 17:32:44 -0700246
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700247 List<Intent> result = sut.compile(intent, Collections.emptyList());
248
249 assertThat(result, is(notNullValue()));
250 assertThat(result, hasSize(1));
251
252 Intent resultIntent = result.get(0);
253 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
254
255 if (resultIntent instanceof FlowRuleIntent) {
256 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
257
258 assertThat(frIntent.flowRules(), hasSize(4));
259
260 List<FlowRule> deviceFlowRules;
261 FlowRule flowRule;
262
263 // Of1
264 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
265 assertThat(deviceFlowRules, hasSize(1));
266 flowRule = deviceFlowRules.get(0);
267 assertThat(flowRule.selector(), is(expectOf1Selector));
268 assertThat(flowRule.treatment(), is(expectOf1Treatment));
269
270 // Of2
271 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
272 assertThat(deviceFlowRules, hasSize(1));
273 flowRule = deviceFlowRules.get(0);
274 assertThat(flowRule.selector(), is(expectOf2Selector));
275 assertThat(flowRule.treatment(), is(expectOf2Treatment));
276
277 // Of3
278 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
279 assertThat(deviceFlowRules, hasSize(1));
280 flowRule = deviceFlowRules.get(0);
281 assertThat(flowRule.selector(), is(expectOf3Selector));
282 assertThat(flowRule.treatment(), is(expectOf3Treatment));
283
284 // Of4
285 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
286 assertThat(deviceFlowRules, hasSize(1));
287 flowRule = deviceFlowRules.get(0);
288 assertThat(flowRule.selector(), is(expectOf4Selector));
289 assertThat(flowRule.treatment(), is(expectOf4Treatment));
290
291 }
292 sut.deactivate();
293 }
294
295 /**
296 * Multi point to single point intent with filtered connect point.
Pier Ventre5c4a0762016-11-21 10:28:13 -0800297 * Scenario is the follow:
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700298 *
299 * -1 of1 2-1 of2 2-1 of4 2-
300 * 3
301 * -1 of3 2---/
Pier Ventre5c4a0762016-11-21 10:28:13 -0800302 *
303 * We test the proper compilation of mp2sp intents with trivial selector,
304 * trivial treatment and different filtered point.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700305 */
306 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700307 public void testFilteredConnectPointForMp() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700308 sut.activate();
309 Set<Link> testlinks = ImmutableSet.of(
310 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
311 DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
312 DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
313 );
314
315 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
316 new FilteredConnectPoint(of1p1, vlan100Selector),
317 new FilteredConnectPoint(of3p1, vlan100Selector)
318 );
319
320 Set<FilteredConnectPoint> egress = ImmutableSet.of(
321 new FilteredConnectPoint(of4p2, vlan200Selector)
322 );
323
324 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
325 .matchInPort(PortNumber.portNumber(1))
326 .build();
327
328 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
329 .setVlanId(VlanId.vlanId("200"))
330 .setOutput(PortNumber.portNumber(2))
331 .build();
332
333 TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(vlan200Selector)
334 .matchInPort(PortNumber.portNumber(1))
335 .build();
336
337 TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(vlan200Selector)
338 .matchInPort(PortNumber.portNumber(3))
339 .build();
340
341 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
342 .setOutput(PortNumber.portNumber(2))
343 .build();
344
345 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
346 .matchInPort(PortNumber.portNumber(1))
347 .build();
348
349 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
350 .setVlanId(VlanId.vlanId("200"))
351 .setOutput(PortNumber.portNumber(2))
352 .build();
353
354 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
355 .matchInPort(PortNumber.portNumber(1))
356 .matchVlanId(VlanId.vlanId("200"))
357 .build();
358
359 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
360 .setOutput(PortNumber.portNumber(2))
361 .build();
362
363 intent = LinkCollectionIntent.builder()
364 .appId(APP_ID)
365 .filteredIngressPoints(ingress)
366 .filteredEgressPoints(egress)
367 .treatment(treatment)
368 .links(testlinks)
369 .build();
370
371 List<Intent> result = sut.compile(intent, Collections.emptyList());
372
373 assertThat(result, is(notNullValue()));
374 assertThat(result, hasSize(1));
375
376 Intent resultIntent = result.get(0);
377 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
378
379 if (resultIntent instanceof FlowRuleIntent) {
380 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
381 assertThat(frIntent.flowRules(), hasSize(5));
382
383 List<FlowRule> deviceFlowRules;
384 FlowRule flowRule;
385
386 // Of1
387 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
388 assertThat(deviceFlowRules, hasSize(1));
389 flowRule = deviceFlowRules.get(0);
390 assertThat(flowRule.selector(), is(expectOf1Selector));
391 assertThat(flowRule.treatment(), is(expectOf1Treatment));
392
393 // Of2 (has 2 flows)
394 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
395 assertThat(deviceFlowRules, hasSize(2));
396 flowRule = deviceFlowRules.get(0);
397 assertThat(flowRule.selector(), is(expectOf2Selector1));
398 assertThat(flowRule.treatment(), is(expectOf2Treatment));
399 flowRule = deviceFlowRules.get(1);
400 assertThat(flowRule.selector(), is(expectOf2Selector2));
401 assertThat(flowRule.treatment(), is(expectOf2Treatment));
402
403 // Of3
404 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
405 assertThat(deviceFlowRules, hasSize(1));
406 flowRule = deviceFlowRules.get(0);
407 assertThat(flowRule.selector(), is(expectOf3Selector));
408 assertThat(flowRule.treatment(), is(expectOf3Treatment));
409
410 // Of4
411 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
412 assertThat(deviceFlowRules, hasSize(1));
413 flowRule = deviceFlowRules.get(0);
414 assertThat(flowRule.selector(), is(expectOf4Selector));
415 assertThat(flowRule.treatment(), is(expectOf4Treatment));
416 }
Pier Ventre647138f2016-08-26 17:32:44 -0700417
418 sut.deactivate();
419 }
420
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700421 /**
422 * Single point to multi point without filtered connect point case.
Pier Ventre5c4a0762016-11-21 10:28:13 -0800423 * Scenario is the follow:
424 *
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700425 * -1 of1 2-1 of2 2--1 of3 2-
426 * 3
427 * `-1 of4 2-
Pier Ventre5c4a0762016-11-21 10:28:13 -0800428 *
429 * We test the proper compilation of sp2mp with non trivial selector,
430 * non trivial treatment and simple connect points.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700431 */
Pier Ventre27d42572016-08-29 17:37:08 -0700432 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700433 public void nonTrivialTranslationForSp() {
Pier Ventre27d42572016-08-29 17:37:08 -0700434 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700435 Set<Link> testLinks = ImmutableSet.of(
436 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
437 DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
438 DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
439 );
Pier Ventre27d42572016-08-29 17:37:08 -0700440
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700441 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
442 .matchInPort(PortNumber.portNumber(1))
443 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700444
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700445 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
446 .setOutput(PortNumber.portNumber(2))
447 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700448
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700449 TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
450 .matchInPort(PortNumber.portNumber(1))
451 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700452
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700453 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
454 .setOutput(PortNumber.portNumber(2))
455 .setOutput(PortNumber.portNumber(3))
456 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700457
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700458 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
459 .matchInPort(PortNumber.portNumber(1))
460 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700461
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700462 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
463 .setOutput(PortNumber.portNumber(2))
464 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700465
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700466 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
467 .matchInPort(PortNumber.portNumber(1))
468 .build();
469
470 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
471 .setOutput(PortNumber.portNumber(2))
472 .build();
473
474
475
Ray Milkey6e7843d2018-09-18 15:50:41 -0700476 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
477 new FilteredConnectPoint(of1p1)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700478 );
479
Ray Milkey6e7843d2018-09-18 15:50:41 -0700480 Set<FilteredConnectPoint> egress = ImmutableSet.of(
481 new FilteredConnectPoint(of3p2),
482 new FilteredConnectPoint(of4p2)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700483 );
484
485 intent = LinkCollectionIntent.builder()
486 .appId(APP_ID)
487 .selector(ipPrefixSelector)
488 .treatment(ethDstTreatment)
Ray Milkey6e7843d2018-09-18 15:50:41 -0700489 .filteredIngressPoints(ingress)
490 .filteredEgressPoints(egress)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700491 .applyTreatmentOnEgress(true)
492 .links(testLinks)
493 .build();
494
495 assertThat(sut, is(notNullValue()));
496
497 List<Intent> result = sut.compile(intent, Collections.emptyList());
498
499 assertThat(result, is(notNullValue()));
500 assertThat(result, hasSize(1));
501
502 Intent resultIntent = result.get(0);
503 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
504
505 if (resultIntent instanceof FlowRuleIntent) {
506 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
507
508 assertThat(frIntent.flowRules(), hasSize(4));
509
510 List<FlowRule> deviceFlowRules;
511 FlowRule flowRule;
512
513 // Of1
514 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
515 assertThat(deviceFlowRules, hasSize(1));
516 flowRule = deviceFlowRules.get(0);
517 assertThat(flowRule.selector(), is(expectOf1Selector));
518 assertThat(flowRule.treatment(), is(expectOf1Treatment));
519
520 // Of2
521 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
522 assertThat(deviceFlowRules, hasSize(1));
523 flowRule = deviceFlowRules.get(0);
524 assertThat(flowRule.selector(), is(expectOf2Selector));
525 assertThat(flowRule.treatment(), is(expectOf2Treatment));
526
527 // Of3
528 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
529 assertThat(deviceFlowRules, hasSize(1));
530 flowRule = deviceFlowRules.get(0);
531 assertThat(flowRule.selector(), is(expectOf3Selector));
532 assertThat(flowRule.treatment(), is(expectOf3Treatment));
533
534 // Of4
535 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
536 assertThat(deviceFlowRules, hasSize(1));
537 flowRule = deviceFlowRules.get(0);
538 assertThat(flowRule.selector(), is(expectOf4Selector));
539 assertThat(flowRule.treatment(), is(expectOf4Treatment));
540
541 }
542 sut.deactivate();
Pier Ventre27d42572016-08-29 17:37:08 -0700543 }
544
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700545 /**
546 * Multi point to single point intent without filtered connect point.
Pier Ventre5c4a0762016-11-21 10:28:13 -0800547 * Scenario is the follow:
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700548 *
549 * -1 of1 2-1 of2 2-1 of4 2-
550 * 3
551 * -1 of3 2---/
Pier Ventre5c4a0762016-11-21 10:28:13 -0800552 *
553 * We test the proper compilation of mp2sp intent with non trivial selector,
554 * non trivial treatment and simple connect points.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700555 */
Pier Ventre27d42572016-08-29 17:37:08 -0700556 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700557 public void nonTrivialTranslationForMp() {
Pier Ventre27d42572016-08-29 17:37:08 -0700558 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700559 Set<Link> testlinks = ImmutableSet.of(
560 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
561 DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
562 DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
563 );
Pier Ventre27d42572016-08-29 17:37:08 -0700564
Ray Milkey6e7843d2018-09-18 15:50:41 -0700565 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
566 new FilteredConnectPoint(of1p1),
567 new FilteredConnectPoint(of3p1)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700568 );
Pier Ventre27d42572016-08-29 17:37:08 -0700569
Ray Milkey6e7843d2018-09-18 15:50:41 -0700570 Set<FilteredConnectPoint> egress = ImmutableSet.of(
571 new FilteredConnectPoint(of4p2)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700572 );
Pier Ventre27d42572016-08-29 17:37:08 -0700573
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700574 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
575 .matchInPort(PortNumber.portNumber(1))
576 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700577
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700578 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
579 .setOutput(PortNumber.portNumber(2))
580 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700581
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700582 TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(ipPrefixSelector)
583 .matchInPort(PortNumber.portNumber(1))
584 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
585 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700586
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700587 TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(ipPrefixSelector)
588 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
589 .matchInPort(PortNumber.portNumber(3))
590 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700591
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700592 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
593 .setOutput(PortNumber.portNumber(2))
594 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700595
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700596 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
597 .matchInPort(PortNumber.portNumber(1))
598 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700599
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700600 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
601 .setOutput(PortNumber.portNumber(2))
602 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700603
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700604 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
605 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
606 .matchInPort(PortNumber.portNumber(1))
607 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700608
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700609 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
610 .setOutput(PortNumber.portNumber(2))
611 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700612
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700613 intent = LinkCollectionIntent.builder()
614 .appId(APP_ID)
615 .selector(ipPrefixSelector)
Ray Milkey6e7843d2018-09-18 15:50:41 -0700616 .filteredIngressPoints(ingress)
617 .filteredEgressPoints(egress)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700618 .treatment(ethDstTreatment)
619 .links(testlinks)
620 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700621
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700622 List<Intent> result = sut.compile(intent, Collections.emptyList());
Pier Ventre27d42572016-08-29 17:37:08 -0700623
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700624 assertThat(result, is(notNullValue()));
625 assertThat(result, hasSize(1));
626
627 Intent resultIntent = result.get(0);
628 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
629
630 if (resultIntent instanceof FlowRuleIntent) {
631 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
632 assertThat(frIntent.flowRules(), hasSize(5));
633
634 List<FlowRule> deviceFlowRules;
635 FlowRule flowRule;
636
637 // Of1
638 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
639 assertThat(deviceFlowRules, hasSize(1));
640 flowRule = deviceFlowRules.get(0);
641 assertThat(flowRule.selector(), is(expectOf1Selector));
642 assertThat(flowRule.treatment(), is(expectOf1Treatment));
643
644 // Of2 (has 2 flows)
645 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
646 assertThat(deviceFlowRules, hasSize(2));
647 flowRule = deviceFlowRules.get(0);
648 assertThat(flowRule.selector(), is(expectOf2Selector1));
649 assertThat(flowRule.treatment(), is(expectOf2Treatment));
650 flowRule = deviceFlowRules.get(1);
651 assertThat(flowRule.selector(), is(expectOf2Selector2));
652 assertThat(flowRule.treatment(), is(expectOf2Treatment));
653
654 // Of3
655 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
656 assertThat(deviceFlowRules, hasSize(1));
657 flowRule = deviceFlowRules.get(0);
658 assertThat(flowRule.selector(), is(expectOf3Selector));
659 assertThat(flowRule.treatment(), is(expectOf3Treatment));
660
661 // Of4
662 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
663 assertThat(deviceFlowRules, hasSize(1));
664 flowRule = deviceFlowRules.get(0);
665 assertThat(flowRule.selector(), is(expectOf4Selector));
666 assertThat(flowRule.treatment(), is(expectOf4Treatment));
667 }
668
669 sut.deactivate();
Pier Ventre27d42572016-08-29 17:37:08 -0700670 }
Pier Ventre766995d2016-10-05 22:15:56 -0700671
672 /**
673 * We test the proper compilation of mp2sp with
674 * trivial selector, trivial treatment and 1 hop.
675 */
676 @Test
677 public void singleHopTestForMp() {
678
679 intent = LinkCollectionIntent.builder()
680 .appId(APP_ID)
681 .selector(selector)
682 .treatment(treatment)
683 .links(ImmutableSet.of())
684 .filteredIngressPoints(ImmutableSet.of(
685 new FilteredConnectPoint(d1p10),
686 new FilteredConnectPoint(d1p11)
687 ))
688 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0)))
689 .build();
690
691 sut.activate();
692
693 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
694 assertThat(compiled, hasSize(1));
695
696 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
697 assertThat(rules, hasSize(2));
698
699 Collection<FlowRule> rulesS1 = rules.stream()
700 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
701 .collect(Collectors.toSet());
702 assertThat(rulesS1, hasSize(2));
703 FlowRule ruleS1 = rulesS1.stream()
704 .filter(rule -> {
705 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
706 return inPort.port().equals(d1p10.port());
707 })
708 .findFirst()
709 .get();
710 assertThat(ruleS1.selector(), Is.is(
711 DefaultTrafficSelector
712 .builder()
713 .matchInPort(d1p10.port())
714 .build()
715 ));
716 assertThat(ruleS1.treatment(), Is.is(
717 DefaultTrafficTreatment
718 .builder()
719 .setOutput(d1p0.port())
720 .build()
721 ));
722
723 ruleS1 = rulesS1.stream()
724 .filter(rule -> {
725 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
726 return inPort.port().equals(d1p11.port());
727 })
728 .findFirst()
729 .get();
730 assertThat(ruleS1.selector(), Is.is(
731 DefaultTrafficSelector
732 .builder()
733 .matchInPort(d1p11.port())
734 .build()
735 ));
736 assertThat(ruleS1.treatment(), Is.is(
737 DefaultTrafficTreatment
738 .builder()
739 .setOutput(d1p0.port())
740 .build()
741 ));
742
743 sut.deactivate();
744
745 }
746
747 /**
748 * We test the proper compilation of sp2mp with
749 * trivial selector, trivial treatment and 1 hop.
750 */
751 @Test
752 public void singleHopTestForSp() {
753
754 intent = LinkCollectionIntent.builder()
755 .appId(APP_ID)
756 .selector(selector)
757 .treatment(treatment)
758 .applyTreatmentOnEgress(true)
759 .links(ImmutableSet.of())
760 .filteredEgressPoints(ImmutableSet.of(
761 new FilteredConnectPoint(d1p10),
762 new FilteredConnectPoint(d1p11)
763 ))
764 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0)))
765 .build();
766
767 sut.activate();
768
769 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
770 assertThat(compiled, hasSize(1));
771
772 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
773 assertThat(rules, hasSize(1));
774
775 Collection<FlowRule> rulesS1 = rules.stream()
776 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
777 .collect(Collectors.toSet());
778 assertThat(rulesS1, hasSize(1));
779 FlowRule ruleS1 = rulesS1.stream()
780 .filter(rule -> {
781 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
782 return inPort.port().equals(d1p0.port());
783 })
784 .findFirst()
785 .get();
786 assertThat(ruleS1.selector(), Is.is(
787 DefaultTrafficSelector
788 .builder()
789 .matchInPort(d1p0.port())
790 .build()
791 ));
792 assertThat(ruleS1.treatment(), Is.is(
793 DefaultTrafficTreatment
794 .builder()
795 .setOutput(d1p10.port())
796 .setOutput(d1p11.port())
797 .build()
798 ));
799
800 sut.deactivate();
801
802 }
803
804 /**
805 * We test the proper compilation of mp2sp with
806 * trivial selector, trivial treatment, filtered
807 * points and 1 hop.
808 */
809 @Test
810 public void singleHopTestFilteredForMp() {
811
812 intent = LinkCollectionIntent.builder()
813 .appId(APP_ID)
814 .selector(selector)
815 .treatment(treatment)
816 .links(ImmutableSet.of())
817 .filteredIngressPoints(ImmutableSet.of(
818 new FilteredConnectPoint(d1p10, vlan100Selector),
819 new FilteredConnectPoint(d1p11, mpls69Selector)
820 ))
821 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
822 .build();
823
824 sut.activate();
825
826 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
827 assertThat(compiled, hasSize(1));
828
829 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
830 assertThat(rules, hasSize(2));
831
832 Collection<FlowRule> rulesS1 = rules.stream()
833 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
834 .collect(Collectors.toSet());
835 assertThat(rulesS1, hasSize(2));
836 FlowRule ruleS1 = rulesS1.stream()
837 .filter(rule -> {
838 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
839 return inPort.port().equals(d1p10.port());
840 })
841 .findFirst()
842 .get();
843 assertThat(ruleS1.selector(), Is.is(
844 DefaultTrafficSelector
845 .builder(vlan100Selector)
846 .matchInPort(d1p10.port())
847 .build()
848 ));
849 assertThat(ruleS1.treatment(), Is.is(
850 DefaultTrafficTreatment
851 .builder()
852 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
853 .setOutput(d1p0.port())
854 .build()
855 ));
856
857 ruleS1 = rulesS1.stream()
858 .filter(rule -> {
859 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
860 return inPort.port().equals(d1p11.port());
861 })
862 .findFirst()
863 .get();
864 assertThat(ruleS1.selector(), Is.is(
865 DefaultTrafficSelector
866 .builder(mpls69Selector)
867 .matchInPort(d1p11.port())
868 .build()
869 ));
870 assertThat(ruleS1.treatment(), Is.is(
871 DefaultTrafficTreatment
872 .builder()
873 .popMpls(IPV4.ethType())
874 .pushVlan()
875 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
876 .setOutput(d1p0.port())
877 .build()
878 ));
879
880 sut.deactivate();
881
882 }
883
884 /**
885 * We test the proper compilation of sp2mp with
886 * trivial selector, trivial treatment and 1 hop.
887 */
888 @Test
889 public void singleHopTestFilteredForSp() {
890
891 intent = LinkCollectionIntent.builder()
892 .appId(APP_ID)
893 .selector(selector)
894 .treatment(treatment)
895 .applyTreatmentOnEgress(true)
896 .links(ImmutableSet.of())
897 .filteredEgressPoints(ImmutableSet.of(
898 new FilteredConnectPoint(d1p10, vlan100Selector),
899 new FilteredConnectPoint(d1p11, mpls80Selector)
900 ))
901 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
902 .build();
903
904 sut.activate();
905
906 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
907 assertThat(compiled, hasSize(1));
908
909 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
910 assertThat(rules, hasSize(1));
911
912 Collection<FlowRule> rulesS1 = rules.stream()
913 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
914 .collect(Collectors.toSet());
915 assertThat(rulesS1, hasSize(1));
916 FlowRule ruleS1 = rulesS1.stream()
917 .filter(rule -> {
918 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
919 return inPort.port().equals(d1p0.port());
920 })
921 .findFirst()
922 .get();
923 assertThat(ruleS1.selector(), Is.is(
924 DefaultTrafficSelector
925 .builder(vlan200Selector)
926 .matchInPort(d1p0.port())
927 .build()
928 ));
929 assertThat(ruleS1.treatment(), Is.is(
930 DefaultTrafficTreatment
931 .builder()
932 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
933 .setOutput(d1p10.port())
934 .popVlan()
935 .pushMpls()
936 .setMpls(((MplsCriterion) mpls80Selector.getCriterion(MPLS_LABEL)).label())
937 .setOutput(d1p11.port())
938 .build()
939 ));
940
941 sut.deactivate();
942
943 }
944
945 /**
946 * We test the proper compilation of mp2sp with
947 * selector, treatment, filtered
948 * points and 1 hop.
949 */
950 @Test
951 public void singleHopNonTrivialForMp() {
952
953 intent = LinkCollectionIntent.builder()
954 .appId(APP_ID)
955 .selector(ipPrefixSelector)
956 .treatment(ethDstTreatment)
957 .links(ImmutableSet.of())
958 .filteredIngressPoints(ImmutableSet.of(
959 new FilteredConnectPoint(d1p10, vlan100Selector),
960 new FilteredConnectPoint(d1p11, mpls100Selector)
961 ))
962 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
963 .build();
964
965 sut.activate();
966
967 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
968 assertThat(compiled, hasSize(1));
969
970 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
971 assertThat(rules, hasSize(2));
972
973 Collection<FlowRule> rulesS1 = rules.stream()
974 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
975 .collect(Collectors.toSet());
976 assertThat(rulesS1, hasSize(2));
977 FlowRule ruleS1 = rulesS1.stream()
978 .filter(rule -> {
979 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
980 return inPort.port().equals(d1p10.port());
981 })
982 .findFirst()
983 .get();
984 assertThat(ruleS1.selector(), Is.is(
985 DefaultTrafficSelector
986 .builder(ipPrefixSelector)
987 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
988 .matchInPort(d1p10.port())
989 .build()
990 ));
991 assertThat(ruleS1.treatment(), Is.is(
992 DefaultTrafficTreatment
993 .builder()
994 .setEthDst(((ModEtherInstruction) ethDstTreatment
995 .allInstructions()
996 .stream()
997 .filter(instruction -> instruction instanceof ModEtherInstruction)
998 .findFirst().get()).mac())
999 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1000 .setOutput(d1p0.port())
1001 .build()
1002 ));
1003
1004 ruleS1 = rulesS1.stream()
1005 .filter(rule -> {
1006 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1007 return inPort.port().equals(d1p11.port());
1008 })
1009 .findFirst()
1010 .get();
1011 assertThat(ruleS1.selector(), Is.is(
1012 DefaultTrafficSelector
1013 .builder(ipPrefixSelector)
1014 .matchMplsLabel(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
1015 .matchInPort(d1p11.port())
1016 .build()
1017 ));
1018 assertThat(ruleS1.treatment(), Is.is(
1019 DefaultTrafficTreatment
1020 .builder()
1021 .setEthDst(((ModEtherInstruction) ethDstTreatment
1022 .allInstructions()
1023 .stream()
1024 .filter(instruction -> instruction instanceof ModEtherInstruction)
1025 .findFirst().get()).mac())
1026 .popMpls(IPV4.ethType())
1027 .pushVlan()
1028 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1029 .setOutput(d1p0.port())
1030 .build()
1031 ));
1032
1033 sut.deactivate();
1034
1035 }
1036
1037 /**
1038 * We test the proper compilation of sp2mp with
1039 * selector, treatment and 1 hop.
1040 */
1041 @Test
1042 public void singleHopNonTrivialForSp() {
1043
1044 intent = LinkCollectionIntent.builder()
1045 .appId(APP_ID)
1046 .selector(ipPrefixSelector)
1047 .treatment(ethDstTreatment)
1048 .applyTreatmentOnEgress(true)
1049 .links(ImmutableSet.of())
1050 .filteredEgressPoints(ImmutableSet.of(
1051 new FilteredConnectPoint(d1p10, vlan100Selector),
1052 new FilteredConnectPoint(d1p11, mpls200Selector)
1053 ))
1054 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
1055 .build();
1056
1057 sut.activate();
1058
1059 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1060 assertThat(compiled, hasSize(1));
1061
1062 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1063 assertThat(rules, hasSize(1));
1064
1065 Collection<FlowRule> rulesS1 = rules.stream()
1066 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1067 .collect(Collectors.toSet());
1068 assertThat(rulesS1, hasSize(1));
1069 FlowRule ruleS1 = rulesS1.stream()
1070 .filter(rule -> {
1071 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1072 return inPort.port().equals(d1p0.port());
1073 })
1074 .findFirst()
1075 .get();
1076 assertThat(ruleS1.selector(), Is.is(
1077 DefaultTrafficSelector
1078 .builder(ipPrefixSelector)
1079 .matchVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1080 .matchInPort(d1p0.port())
1081 .build()
1082 ));
1083 assertThat(ruleS1.treatment(), Is.is(
1084 DefaultTrafficTreatment
1085 .builder()
1086 .setEthDst(((ModEtherInstruction) ethDstTreatment
1087 .allInstructions()
1088 .stream()
1089 .filter(instruction -> instruction instanceof ModEtherInstruction)
1090 .findFirst().get()).mac())
1091 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1092 .setOutput(d1p10.port())
1093 .setEthDst(((ModEtherInstruction) ethDstTreatment
1094 .allInstructions()
1095 .stream()
1096 .filter(instruction -> instruction instanceof ModEtherInstruction)
1097 .findFirst().get()).mac())
1098 .popVlan()
1099 .pushMpls()
1100 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1101 .setOutput(d1p11.port())
1102 .build()
1103 ));
1104
1105 sut.deactivate();
1106
1107 }
1108
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001109}