blob: 4c45bb4bc6026e9c691e0072ff2b997f930d13dd [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.intent.impl.compiler;
17
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.After;
21import org.junit.Before;
22import org.junit.Test;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070023import org.onlab.packet.MacAddress;
Pier Ventre647138f2016-08-26 17:32:44 -070024import org.onlab.packet.VlanId;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080025import org.onosproject.cfg.ComponentConfigAdapter;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080026import org.onosproject.core.CoreService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DefaultLink;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070029import org.onosproject.net.FilteredConnectPoint;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080030import org.onosproject.net.Link;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070031import org.onosproject.net.PortNumber;
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;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044
45import java.util.Collection;
46import java.util.Collections;
47import java.util.List;
48import java.util.Set;
Pier Ventre647138f2016-08-26 17:32:44 -070049import java.util.stream.Collectors;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050
51import static org.easymock.EasyMock.createMock;
52import static org.easymock.EasyMock.expect;
53import static org.easymock.EasyMock.replay;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070054import static org.hamcrest.CoreMatchers.instanceOf;
55import static org.hamcrest.CoreMatchers.notNullValue;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080056import static org.hamcrest.MatcherAssert.assertThat;
57import static org.hamcrest.Matchers.hasSize;
58import static org.hamcrest.Matchers.is;
Pier Ventre766995d2016-10-05 22:15:56 -070059import static org.onlab.packet.EthType.EtherType.IPV4;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060import static org.onosproject.net.Link.Type.DIRECT;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070061import static org.onosproject.net.NetTestTools.*;
Pier Ventre766995d2016-10-05 22:15:56 -070062import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT;
63import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL;
64import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
65import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080066
Pier Ventre766995d2016-10-05 22:15:56 -070067public class LinkCollectionIntentCompilerTest extends AbstractLinkCollectionTest {
Yi Tseng2a81c9d2016-09-14 10:14:24 -070068
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080069 @Before
Brian O'Connor81134662015-06-25 17:23:33 -040070 public void setUp() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080071 sut = new LinkCollectionIntentCompiler();
72 coreService = createMock(CoreService.class);
73 expect(coreService.registerApplication("org.onosproject.net.intent"))
74 .andReturn(appId);
75 sut.coreService = coreService;
76
77 Intent.bindIdGenerator(idGenerator);
78
79 intent = LinkCollectionIntent.builder()
80 .appId(APP_ID)
81 .selector(selector)
82 .treatment(treatment)
83 .links(links)
84 .ingressPoints(ImmutableSet.of(d1p1))
85 .egressPoints(ImmutableSet.of(d3p1))
86 .build();
Pier Ventre647138f2016-08-26 17:32:44 -070087
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080088 intentExtensionService = createMock(IntentExtensionService.class);
89 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
90 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080091
92 registrator = new IntentConfigurableRegistrator();
93 registrator.extensionService = intentExtensionService;
94 registrator.cfgService = new ComponentConfigAdapter();
95 registrator.activate();
96
97 sut.registrator = registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080098
Pier Ventre81c47bf2016-11-04 07:26:22 -070099 LinkCollectionCompiler.optimize = false;
100 LinkCollectionCompiler.copyTtl = false;
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700101
Pier Ventre81c47bf2016-11-04 07:26:22 -0700102 replay(coreService, intentExtensionService);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700103
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 }
105
106 @After
107 public void tearDown() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700108
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800109 Intent.unbindIdGenerator(idGenerator);
110 }
111
112 @Test
113 public void testCompile() {
114 sut.activate();
115
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800116 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800117 assertThat(compiled, hasSize(1));
118
119 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
120 assertThat(rules, hasSize(links.size()));
121
122 // if not found, get() raises an exception
123 FlowRule rule1 = rules.stream()
Pier Ventre766995d2016-10-05 22:15:56 -0700124 .filter(rule -> rule.deviceId().equals(d1p10.deviceId()))
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800125 .findFirst()
126 .get();
127 assertThat(rule1.selector(), is(
128 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build()
129 ));
130 assertThat(rule1.treatment(), is(
131 DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build()
132 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400133 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800134
135 FlowRule rule2 = rules.stream()
136 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
137 .findFirst()
138 .get();
139 assertThat(rule2.selector(), is(
140 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build()
141 ));
142 assertThat(rule2.treatment(), is(
143 DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()
144 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400145 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800146
147 FlowRule rule3 = rules.stream()
148 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
149 .findFirst()
150 .get();
151 assertThat(rule3.selector(), is(
152 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build()
153 ));
154 assertThat(rule3.treatment(), is(
155 DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build()
156 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400157 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800158
159 sut.deactivate();
160 }
Pier Ventre647138f2016-08-26 17:32:44 -0700161
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700162 /**
163 * Single point to multi point case.
164 * -1 of1 2-1 of2 2--1 of3 2-
165 * 3
166 * `-1 of4 2-
167 */
168 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700169 public void testFilteredConnectPointForSp() {
Pier Ventre647138f2016-08-26 17:32:44 -0700170 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700171 Set<Link> testLinks = ImmutableSet.of(
172 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
173 DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
174 DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
175 );
Pier Ventre647138f2016-08-26 17:32:44 -0700176
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700177 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
178 .matchInPort(PortNumber.portNumber(1))
179 .build();
180
181 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
182 .setOutput(PortNumber.portNumber(2))
183 .build();
184
185 TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(vlan100Selector)
186 .matchInPort(PortNumber.portNumber(1))
187 .build();
188
189 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
190 .setOutput(PortNumber.portNumber(2))
191 .setOutput(PortNumber.portNumber(3))
192 .build();
193
194 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
195 .matchInPort(PortNumber.portNumber(1))
196 .build();
197
198 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
199 .setOutput(PortNumber.portNumber(2))
200 .build();
201
202 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
203 .matchInPort(PortNumber.portNumber(1))
204 .build();
205
206 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
207 .setVlanId(VlanId.vlanId("200"))
208 .setOutput(PortNumber.portNumber(2))
209 .build();
Pier Ventre647138f2016-08-26 17:32:44 -0700210
211
Pier Ventre647138f2016-08-26 17:32:44 -0700212
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700213 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
214 new FilteredConnectPoint(of1p1, vlan100Selector)
215 );
Pier Ventre647138f2016-08-26 17:32:44 -0700216
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700217 Set<FilteredConnectPoint> egress = ImmutableSet.of(
218 new FilteredConnectPoint(of3p2, vlan100Selector),
219 new FilteredConnectPoint(of4p2, vlan200Selector)
220 );
Pier Ventre647138f2016-08-26 17:32:44 -0700221
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700222 intent = LinkCollectionIntent.builder()
223 .appId(APP_ID)
224 .filteredIngressPoints(ingress)
225 .filteredEgressPoints(egress)
226 .treatment(treatment)
227 .applyTreatmentOnEgress(true)
228 .links(testLinks)
229 .build();
Pier Ventre647138f2016-08-26 17:32:44 -0700230
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700231 assertThat(sut, is(notNullValue()));
Pier Ventre647138f2016-08-26 17:32:44 -0700232
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700233 List<Intent> result = sut.compile(intent, Collections.emptyList());
234
235 assertThat(result, is(notNullValue()));
236 assertThat(result, hasSize(1));
237
238 Intent resultIntent = result.get(0);
239 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
240
241 if (resultIntent instanceof FlowRuleIntent) {
242 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
243
244 assertThat(frIntent.flowRules(), hasSize(4));
245
246 List<FlowRule> deviceFlowRules;
247 FlowRule flowRule;
248
249 // Of1
250 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
251 assertThat(deviceFlowRules, hasSize(1));
252 flowRule = deviceFlowRules.get(0);
253 assertThat(flowRule.selector(), is(expectOf1Selector));
254 assertThat(flowRule.treatment(), is(expectOf1Treatment));
255
256 // Of2
257 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
258 assertThat(deviceFlowRules, hasSize(1));
259 flowRule = deviceFlowRules.get(0);
260 assertThat(flowRule.selector(), is(expectOf2Selector));
261 assertThat(flowRule.treatment(), is(expectOf2Treatment));
262
263 // Of3
264 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
265 assertThat(deviceFlowRules, hasSize(1));
266 flowRule = deviceFlowRules.get(0);
267 assertThat(flowRule.selector(), is(expectOf3Selector));
268 assertThat(flowRule.treatment(), is(expectOf3Treatment));
269
270 // Of4
271 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
272 assertThat(deviceFlowRules, hasSize(1));
273 flowRule = deviceFlowRules.get(0);
274 assertThat(flowRule.selector(), is(expectOf4Selector));
275 assertThat(flowRule.treatment(), is(expectOf4Treatment));
276
277 }
278 sut.deactivate();
279 }
280
281 /**
282 * Multi point to single point intent with filtered connect point.
283 *
284 * -1 of1 2-1 of2 2-1 of4 2-
285 * 3
286 * -1 of3 2---/
287 */
288 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700289 public void testFilteredConnectPointForMp() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700290 sut.activate();
291 Set<Link> testlinks = ImmutableSet.of(
292 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
293 DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
294 DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
295 );
296
297 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
298 new FilteredConnectPoint(of1p1, vlan100Selector),
299 new FilteredConnectPoint(of3p1, vlan100Selector)
300 );
301
302 Set<FilteredConnectPoint> egress = ImmutableSet.of(
303 new FilteredConnectPoint(of4p2, vlan200Selector)
304 );
305
306 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
307 .matchInPort(PortNumber.portNumber(1))
308 .build();
309
310 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
311 .setVlanId(VlanId.vlanId("200"))
312 .setOutput(PortNumber.portNumber(2))
313 .build();
314
315 TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(vlan200Selector)
316 .matchInPort(PortNumber.portNumber(1))
317 .build();
318
319 TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(vlan200Selector)
320 .matchInPort(PortNumber.portNumber(3))
321 .build();
322
323 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
324 .setOutput(PortNumber.portNumber(2))
325 .build();
326
327 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
328 .matchInPort(PortNumber.portNumber(1))
329 .build();
330
331 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
332 .setVlanId(VlanId.vlanId("200"))
333 .setOutput(PortNumber.portNumber(2))
334 .build();
335
336 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
337 .matchInPort(PortNumber.portNumber(1))
338 .matchVlanId(VlanId.vlanId("200"))
339 .build();
340
341 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
342 .setOutput(PortNumber.portNumber(2))
343 .build();
344
345 intent = LinkCollectionIntent.builder()
346 .appId(APP_ID)
347 .filteredIngressPoints(ingress)
348 .filteredEgressPoints(egress)
349 .treatment(treatment)
350 .links(testlinks)
351 .build();
352
353 List<Intent> result = sut.compile(intent, Collections.emptyList());
354
355 assertThat(result, is(notNullValue()));
356 assertThat(result, hasSize(1));
357
358 Intent resultIntent = result.get(0);
359 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
360
361 if (resultIntent instanceof FlowRuleIntent) {
362 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
363 assertThat(frIntent.flowRules(), hasSize(5));
364
365 List<FlowRule> deviceFlowRules;
366 FlowRule flowRule;
367
368 // Of1
369 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
370 assertThat(deviceFlowRules, hasSize(1));
371 flowRule = deviceFlowRules.get(0);
372 assertThat(flowRule.selector(), is(expectOf1Selector));
373 assertThat(flowRule.treatment(), is(expectOf1Treatment));
374
375 // Of2 (has 2 flows)
376 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
377 assertThat(deviceFlowRules, hasSize(2));
378 flowRule = deviceFlowRules.get(0);
379 assertThat(flowRule.selector(), is(expectOf2Selector1));
380 assertThat(flowRule.treatment(), is(expectOf2Treatment));
381 flowRule = deviceFlowRules.get(1);
382 assertThat(flowRule.selector(), is(expectOf2Selector2));
383 assertThat(flowRule.treatment(), is(expectOf2Treatment));
384
385 // Of3
386 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
387 assertThat(deviceFlowRules, hasSize(1));
388 flowRule = deviceFlowRules.get(0);
389 assertThat(flowRule.selector(), is(expectOf3Selector));
390 assertThat(flowRule.treatment(), is(expectOf3Treatment));
391
392 // Of4
393 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
394 assertThat(deviceFlowRules, hasSize(1));
395 flowRule = deviceFlowRules.get(0);
396 assertThat(flowRule.selector(), is(expectOf4Selector));
397 assertThat(flowRule.treatment(), is(expectOf4Treatment));
398 }
Pier Ventre647138f2016-08-26 17:32:44 -0700399
400 sut.deactivate();
401 }
402
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700403 /**
404 * Single point to multi point without filtered connect point case.
405 * -1 of1 2-1 of2 2--1 of3 2-
406 * 3
407 * `-1 of4 2-
408 */
Pier Ventre27d42572016-08-29 17:37:08 -0700409 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700410 public void nonTrivialTranslationForSp() {
Pier Ventre27d42572016-08-29 17:37:08 -0700411 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700412 Set<Link> testLinks = ImmutableSet.of(
413 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
414 DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
415 DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
416 );
Pier Ventre27d42572016-08-29 17:37:08 -0700417
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700418 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
419 .matchInPort(PortNumber.portNumber(1))
420 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700421
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700422 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
423 .setOutput(PortNumber.portNumber(2))
424 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700425
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700426 TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
427 .matchInPort(PortNumber.portNumber(1))
428 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700429
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700430 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
431 .setOutput(PortNumber.portNumber(2))
432 .setOutput(PortNumber.portNumber(3))
433 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700434
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700435 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
436 .matchInPort(PortNumber.portNumber(1))
437 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700438
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700439 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
440 .setOutput(PortNumber.portNumber(2))
441 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700442
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700443 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
444 .matchInPort(PortNumber.portNumber(1))
445 .build();
446
447 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
448 .setOutput(PortNumber.portNumber(2))
449 .build();
450
451
452
453 Set<ConnectPoint> ingress = ImmutableSet.of(
454 of1p1
455 );
456
457 Set<ConnectPoint> egress = ImmutableSet.of(
458 of3p2,
459 of4p2
460 );
461
462 intent = LinkCollectionIntent.builder()
463 .appId(APP_ID)
464 .selector(ipPrefixSelector)
465 .treatment(ethDstTreatment)
466 .ingressPoints(ingress)
467 .egressPoints(egress)
468 .applyTreatmentOnEgress(true)
469 .links(testLinks)
470 .build();
471
472 assertThat(sut, is(notNullValue()));
473
474 List<Intent> result = sut.compile(intent, Collections.emptyList());
475
476 assertThat(result, is(notNullValue()));
477 assertThat(result, hasSize(1));
478
479 Intent resultIntent = result.get(0);
480 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
481
482 if (resultIntent instanceof FlowRuleIntent) {
483 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
484
485 assertThat(frIntent.flowRules(), hasSize(4));
486
487 List<FlowRule> deviceFlowRules;
488 FlowRule flowRule;
489
490 // Of1
491 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
492 assertThat(deviceFlowRules, hasSize(1));
493 flowRule = deviceFlowRules.get(0);
494 assertThat(flowRule.selector(), is(expectOf1Selector));
495 assertThat(flowRule.treatment(), is(expectOf1Treatment));
496
497 // Of2
498 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
499 assertThat(deviceFlowRules, hasSize(1));
500 flowRule = deviceFlowRules.get(0);
501 assertThat(flowRule.selector(), is(expectOf2Selector));
502 assertThat(flowRule.treatment(), is(expectOf2Treatment));
503
504 // Of3
505 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
506 assertThat(deviceFlowRules, hasSize(1));
507 flowRule = deviceFlowRules.get(0);
508 assertThat(flowRule.selector(), is(expectOf3Selector));
509 assertThat(flowRule.treatment(), is(expectOf3Treatment));
510
511 // Of4
512 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
513 assertThat(deviceFlowRules, hasSize(1));
514 flowRule = deviceFlowRules.get(0);
515 assertThat(flowRule.selector(), is(expectOf4Selector));
516 assertThat(flowRule.treatment(), is(expectOf4Treatment));
517
518 }
519 sut.deactivate();
Pier Ventre27d42572016-08-29 17:37:08 -0700520 }
521
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700522 /**
523 * Multi point to single point intent without filtered connect point.
524 *
525 * -1 of1 2-1 of2 2-1 of4 2-
526 * 3
527 * -1 of3 2---/
528 */
Pier Ventre27d42572016-08-29 17:37:08 -0700529 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700530 public void nonTrivialTranslationForMp() {
Pier Ventre27d42572016-08-29 17:37:08 -0700531 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700532 Set<Link> testlinks = ImmutableSet.of(
533 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
534 DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
535 DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
536 );
Pier Ventre27d42572016-08-29 17:37:08 -0700537
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700538 Set<ConnectPoint> ingress = ImmutableSet.of(
539 of1p1,
540 of3p1
541 );
Pier Ventre27d42572016-08-29 17:37:08 -0700542
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700543 Set<ConnectPoint> egress = ImmutableSet.of(
544 of4p2
545 );
Pier Ventre27d42572016-08-29 17:37:08 -0700546
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700547 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
548 .matchInPort(PortNumber.portNumber(1))
549 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700550
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700551 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
552 .setOutput(PortNumber.portNumber(2))
553 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700554
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700555 TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(ipPrefixSelector)
556 .matchInPort(PortNumber.portNumber(1))
557 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
558 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700559
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700560 TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(ipPrefixSelector)
561 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
562 .matchInPort(PortNumber.portNumber(3))
563 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700564
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700565 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
566 .setOutput(PortNumber.portNumber(2))
567 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700568
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700569 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
570 .matchInPort(PortNumber.portNumber(1))
571 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700572
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700573 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
574 .setOutput(PortNumber.portNumber(2))
575 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700576
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700577 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
578 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
579 .matchInPort(PortNumber.portNumber(1))
580 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700581
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700582 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
583 .setOutput(PortNumber.portNumber(2))
584 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700585
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700586 intent = LinkCollectionIntent.builder()
587 .appId(APP_ID)
588 .selector(ipPrefixSelector)
589 .ingressPoints(ingress)
590 .egressPoints(egress)
591 .treatment(ethDstTreatment)
592 .links(testlinks)
593 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700594
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700595 List<Intent> result = sut.compile(intent, Collections.emptyList());
Pier Ventre27d42572016-08-29 17:37:08 -0700596
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700597 assertThat(result, is(notNullValue()));
598 assertThat(result, hasSize(1));
599
600 Intent resultIntent = result.get(0);
601 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
602
603 if (resultIntent instanceof FlowRuleIntent) {
604 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
605 assertThat(frIntent.flowRules(), hasSize(5));
606
607 List<FlowRule> deviceFlowRules;
608 FlowRule flowRule;
609
610 // Of1
611 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
612 assertThat(deviceFlowRules, hasSize(1));
613 flowRule = deviceFlowRules.get(0);
614 assertThat(flowRule.selector(), is(expectOf1Selector));
615 assertThat(flowRule.treatment(), is(expectOf1Treatment));
616
617 // Of2 (has 2 flows)
618 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
619 assertThat(deviceFlowRules, hasSize(2));
620 flowRule = deviceFlowRules.get(0);
621 assertThat(flowRule.selector(), is(expectOf2Selector1));
622 assertThat(flowRule.treatment(), is(expectOf2Treatment));
623 flowRule = deviceFlowRules.get(1);
624 assertThat(flowRule.selector(), is(expectOf2Selector2));
625 assertThat(flowRule.treatment(), is(expectOf2Treatment));
626
627 // Of3
628 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
629 assertThat(deviceFlowRules, hasSize(1));
630 flowRule = deviceFlowRules.get(0);
631 assertThat(flowRule.selector(), is(expectOf3Selector));
632 assertThat(flowRule.treatment(), is(expectOf3Treatment));
633
634 // Of4
635 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
636 assertThat(deviceFlowRules, hasSize(1));
637 flowRule = deviceFlowRules.get(0);
638 assertThat(flowRule.selector(), is(expectOf4Selector));
639 assertThat(flowRule.treatment(), is(expectOf4Treatment));
640 }
641
642 sut.deactivate();
Pier Ventre27d42572016-08-29 17:37:08 -0700643 }
Pier Ventre766995d2016-10-05 22:15:56 -0700644
645 /**
646 * We test the proper compilation of mp2sp with
647 * trivial selector, trivial treatment and 1 hop.
648 */
649 @Test
650 public void singleHopTestForMp() {
651
652 intent = LinkCollectionIntent.builder()
653 .appId(APP_ID)
654 .selector(selector)
655 .treatment(treatment)
656 .links(ImmutableSet.of())
657 .filteredIngressPoints(ImmutableSet.of(
658 new FilteredConnectPoint(d1p10),
659 new FilteredConnectPoint(d1p11)
660 ))
661 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0)))
662 .build();
663
664 sut.activate();
665
666 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
667 assertThat(compiled, hasSize(1));
668
669 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
670 assertThat(rules, hasSize(2));
671
672 Collection<FlowRule> rulesS1 = rules.stream()
673 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
674 .collect(Collectors.toSet());
675 assertThat(rulesS1, hasSize(2));
676 FlowRule ruleS1 = rulesS1.stream()
677 .filter(rule -> {
678 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
679 return inPort.port().equals(d1p10.port());
680 })
681 .findFirst()
682 .get();
683 assertThat(ruleS1.selector(), Is.is(
684 DefaultTrafficSelector
685 .builder()
686 .matchInPort(d1p10.port())
687 .build()
688 ));
689 assertThat(ruleS1.treatment(), Is.is(
690 DefaultTrafficTreatment
691 .builder()
692 .setOutput(d1p0.port())
693 .build()
694 ));
695
696 ruleS1 = rulesS1.stream()
697 .filter(rule -> {
698 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
699 return inPort.port().equals(d1p11.port());
700 })
701 .findFirst()
702 .get();
703 assertThat(ruleS1.selector(), Is.is(
704 DefaultTrafficSelector
705 .builder()
706 .matchInPort(d1p11.port())
707 .build()
708 ));
709 assertThat(ruleS1.treatment(), Is.is(
710 DefaultTrafficTreatment
711 .builder()
712 .setOutput(d1p0.port())
713 .build()
714 ));
715
716 sut.deactivate();
717
718 }
719
720 /**
721 * We test the proper compilation of sp2mp with
722 * trivial selector, trivial treatment and 1 hop.
723 */
724 @Test
725 public void singleHopTestForSp() {
726
727 intent = LinkCollectionIntent.builder()
728 .appId(APP_ID)
729 .selector(selector)
730 .treatment(treatment)
731 .applyTreatmentOnEgress(true)
732 .links(ImmutableSet.of())
733 .filteredEgressPoints(ImmutableSet.of(
734 new FilteredConnectPoint(d1p10),
735 new FilteredConnectPoint(d1p11)
736 ))
737 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0)))
738 .build();
739
740 sut.activate();
741
742 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
743 assertThat(compiled, hasSize(1));
744
745 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
746 assertThat(rules, hasSize(1));
747
748 Collection<FlowRule> rulesS1 = rules.stream()
749 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
750 .collect(Collectors.toSet());
751 assertThat(rulesS1, hasSize(1));
752 FlowRule ruleS1 = rulesS1.stream()
753 .filter(rule -> {
754 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
755 return inPort.port().equals(d1p0.port());
756 })
757 .findFirst()
758 .get();
759 assertThat(ruleS1.selector(), Is.is(
760 DefaultTrafficSelector
761 .builder()
762 .matchInPort(d1p0.port())
763 .build()
764 ));
765 assertThat(ruleS1.treatment(), Is.is(
766 DefaultTrafficTreatment
767 .builder()
768 .setOutput(d1p10.port())
769 .setOutput(d1p11.port())
770 .build()
771 ));
772
773 sut.deactivate();
774
775 }
776
777 /**
778 * We test the proper compilation of mp2sp with
779 * trivial selector, trivial treatment, filtered
780 * points and 1 hop.
781 */
782 @Test
783 public void singleHopTestFilteredForMp() {
784
785 intent = LinkCollectionIntent.builder()
786 .appId(APP_ID)
787 .selector(selector)
788 .treatment(treatment)
789 .links(ImmutableSet.of())
790 .filteredIngressPoints(ImmutableSet.of(
791 new FilteredConnectPoint(d1p10, vlan100Selector),
792 new FilteredConnectPoint(d1p11, mpls69Selector)
793 ))
794 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
795 .build();
796
797 sut.activate();
798
799 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
800 assertThat(compiled, hasSize(1));
801
802 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
803 assertThat(rules, hasSize(2));
804
805 Collection<FlowRule> rulesS1 = rules.stream()
806 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
807 .collect(Collectors.toSet());
808 assertThat(rulesS1, hasSize(2));
809 FlowRule ruleS1 = rulesS1.stream()
810 .filter(rule -> {
811 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
812 return inPort.port().equals(d1p10.port());
813 })
814 .findFirst()
815 .get();
816 assertThat(ruleS1.selector(), Is.is(
817 DefaultTrafficSelector
818 .builder(vlan100Selector)
819 .matchInPort(d1p10.port())
820 .build()
821 ));
822 assertThat(ruleS1.treatment(), Is.is(
823 DefaultTrafficTreatment
824 .builder()
825 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
826 .setOutput(d1p0.port())
827 .build()
828 ));
829
830 ruleS1 = rulesS1.stream()
831 .filter(rule -> {
832 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
833 return inPort.port().equals(d1p11.port());
834 })
835 .findFirst()
836 .get();
837 assertThat(ruleS1.selector(), Is.is(
838 DefaultTrafficSelector
839 .builder(mpls69Selector)
840 .matchInPort(d1p11.port())
841 .build()
842 ));
843 assertThat(ruleS1.treatment(), Is.is(
844 DefaultTrafficTreatment
845 .builder()
846 .popMpls(IPV4.ethType())
847 .pushVlan()
848 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
849 .setOutput(d1p0.port())
850 .build()
851 ));
852
853 sut.deactivate();
854
855 }
856
857 /**
858 * We test the proper compilation of sp2mp with
859 * trivial selector, trivial treatment and 1 hop.
860 */
861 @Test
862 public void singleHopTestFilteredForSp() {
863
864 intent = LinkCollectionIntent.builder()
865 .appId(APP_ID)
866 .selector(selector)
867 .treatment(treatment)
868 .applyTreatmentOnEgress(true)
869 .links(ImmutableSet.of())
870 .filteredEgressPoints(ImmutableSet.of(
871 new FilteredConnectPoint(d1p10, vlan100Selector),
872 new FilteredConnectPoint(d1p11, mpls80Selector)
873 ))
874 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
875 .build();
876
877 sut.activate();
878
879 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
880 assertThat(compiled, hasSize(1));
881
882 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
883 assertThat(rules, hasSize(1));
884
885 Collection<FlowRule> rulesS1 = rules.stream()
886 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
887 .collect(Collectors.toSet());
888 assertThat(rulesS1, hasSize(1));
889 FlowRule ruleS1 = rulesS1.stream()
890 .filter(rule -> {
891 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
892 return inPort.port().equals(d1p0.port());
893 })
894 .findFirst()
895 .get();
896 assertThat(ruleS1.selector(), Is.is(
897 DefaultTrafficSelector
898 .builder(vlan200Selector)
899 .matchInPort(d1p0.port())
900 .build()
901 ));
902 assertThat(ruleS1.treatment(), Is.is(
903 DefaultTrafficTreatment
904 .builder()
905 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
906 .setOutput(d1p10.port())
907 .popVlan()
908 .pushMpls()
909 .setMpls(((MplsCriterion) mpls80Selector.getCriterion(MPLS_LABEL)).label())
910 .setOutput(d1p11.port())
911 .build()
912 ));
913
914 sut.deactivate();
915
916 }
917
918 /**
919 * We test the proper compilation of mp2sp with
920 * selector, treatment, filtered
921 * points and 1 hop.
922 */
923 @Test
924 public void singleHopNonTrivialForMp() {
925
926 intent = LinkCollectionIntent.builder()
927 .appId(APP_ID)
928 .selector(ipPrefixSelector)
929 .treatment(ethDstTreatment)
930 .links(ImmutableSet.of())
931 .filteredIngressPoints(ImmutableSet.of(
932 new FilteredConnectPoint(d1p10, vlan100Selector),
933 new FilteredConnectPoint(d1p11, mpls100Selector)
934 ))
935 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
936 .build();
937
938 sut.activate();
939
940 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
941 assertThat(compiled, hasSize(1));
942
943 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
944 assertThat(rules, hasSize(2));
945
946 Collection<FlowRule> rulesS1 = rules.stream()
947 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
948 .collect(Collectors.toSet());
949 assertThat(rulesS1, hasSize(2));
950 FlowRule ruleS1 = rulesS1.stream()
951 .filter(rule -> {
952 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
953 return inPort.port().equals(d1p10.port());
954 })
955 .findFirst()
956 .get();
957 assertThat(ruleS1.selector(), Is.is(
958 DefaultTrafficSelector
959 .builder(ipPrefixSelector)
960 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
961 .matchInPort(d1p10.port())
962 .build()
963 ));
964 assertThat(ruleS1.treatment(), Is.is(
965 DefaultTrafficTreatment
966 .builder()
967 .setEthDst(((ModEtherInstruction) ethDstTreatment
968 .allInstructions()
969 .stream()
970 .filter(instruction -> instruction instanceof ModEtherInstruction)
971 .findFirst().get()).mac())
972 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
973 .setOutput(d1p0.port())
974 .build()
975 ));
976
977 ruleS1 = rulesS1.stream()
978 .filter(rule -> {
979 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
980 return inPort.port().equals(d1p11.port());
981 })
982 .findFirst()
983 .get();
984 assertThat(ruleS1.selector(), Is.is(
985 DefaultTrafficSelector
986 .builder(ipPrefixSelector)
987 .matchMplsLabel(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
988 .matchInPort(d1p11.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 .popMpls(IPV4.ethType())
1000 .pushVlan()
1001 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1002 .setOutput(d1p0.port())
1003 .build()
1004 ));
1005
1006 sut.deactivate();
1007
1008 }
1009
1010 /**
1011 * We test the proper compilation of sp2mp with
1012 * selector, treatment and 1 hop.
1013 */
1014 @Test
1015 public void singleHopNonTrivialForSp() {
1016
1017 intent = LinkCollectionIntent.builder()
1018 .appId(APP_ID)
1019 .selector(ipPrefixSelector)
1020 .treatment(ethDstTreatment)
1021 .applyTreatmentOnEgress(true)
1022 .links(ImmutableSet.of())
1023 .filteredEgressPoints(ImmutableSet.of(
1024 new FilteredConnectPoint(d1p10, vlan100Selector),
1025 new FilteredConnectPoint(d1p11, mpls200Selector)
1026 ))
1027 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
1028 .build();
1029
1030 sut.activate();
1031
1032 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1033 assertThat(compiled, hasSize(1));
1034
1035 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1036 assertThat(rules, hasSize(1));
1037
1038 Collection<FlowRule> rulesS1 = rules.stream()
1039 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1040 .collect(Collectors.toSet());
1041 assertThat(rulesS1, hasSize(1));
1042 FlowRule ruleS1 = rulesS1.stream()
1043 .filter(rule -> {
1044 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1045 return inPort.port().equals(d1p0.port());
1046 })
1047 .findFirst()
1048 .get();
1049 assertThat(ruleS1.selector(), Is.is(
1050 DefaultTrafficSelector
1051 .builder(ipPrefixSelector)
1052 .matchVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1053 .matchInPort(d1p0.port())
1054 .build()
1055 ));
1056 assertThat(ruleS1.treatment(), Is.is(
1057 DefaultTrafficTreatment
1058 .builder()
1059 .setEthDst(((ModEtherInstruction) ethDstTreatment
1060 .allInstructions()
1061 .stream()
1062 .filter(instruction -> instruction instanceof ModEtherInstruction)
1063 .findFirst().get()).mac())
1064 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1065 .setOutput(d1p10.port())
1066 .setEthDst(((ModEtherInstruction) ethDstTreatment
1067 .allInstructions()
1068 .stream()
1069 .filter(instruction -> instruction instanceof ModEtherInstruction)
1070 .findFirst().get()).mac())
1071 .popVlan()
1072 .pushMpls()
1073 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1074 .setOutput(d1p11.port())
1075 .build()
1076 ));
1077
1078 sut.deactivate();
1079
1080 }
1081
Pier Ventreffe88d62016-10-13 14:34:40 -07001082 /**
1083 * We test the proper compilation of p2p with
1084 * trivial selector and trivial treatment.
1085 */
1086 @Test
1087 public void p2p() {
1088
1089 intent = LinkCollectionIntent.builder()
1090 .appId(APP_ID)
1091 .selector(selector)
1092 .treatment(treatment)
1093 .applyTreatmentOnEgress(true)
1094 .links(p2pLinks)
1095 .filteredIngressPoints(ImmutableSet.of(
1096 new FilteredConnectPoint(d1p10)
1097 ))
1098 .filteredEgressPoints(ImmutableSet.of(
1099 new FilteredConnectPoint(d3p0)
1100 ))
1101 .build();
1102
1103 sut.activate();
1104
1105 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1106 assertThat(compiled, hasSize(1));
1107
1108 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1109 assertThat(rules, hasSize(3));
1110
1111 Collection<FlowRule> rulesS1 = rules.stream()
1112 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1113 .collect(Collectors.toSet());
1114 assertThat(rulesS1, hasSize(1));
1115 FlowRule ruleS1 = rulesS1.iterator().next();
1116 assertThat(ruleS1.selector(), Is.is(
1117 DefaultTrafficSelector
1118 .builder()
1119 .matchInPort(d1p10.port())
1120 .build()
1121 ));
1122 assertThat(ruleS1.treatment(), Is.is(
1123 DefaultTrafficTreatment
1124 .builder()
1125 .setOutput(d1p0.port())
1126 .build()
1127 ));
1128
1129 Collection<FlowRule> rulesS2 = rules.stream()
1130 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1131 .collect(Collectors.toSet());
1132 assertThat(rulesS2, hasSize(1));
1133 FlowRule ruleS2 = rulesS2.iterator().next();
1134 assertThat(ruleS2.selector(), Is.is(
1135 DefaultTrafficSelector
1136 .builder()
1137 .matchInPort(d2p0.port())
1138 .build()
1139 ));
1140 assertThat(ruleS2.treatment(), Is.is(
1141 DefaultTrafficTreatment
1142 .builder()
1143 .setOutput(d2p1.port())
1144 .build()
1145 ));
1146
1147
1148 Collection<FlowRule> rulesS3 = rules.stream()
1149 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1150 .collect(Collectors.toSet());
1151 assertThat(rulesS3, hasSize(1));
1152 FlowRule ruleS3 = rulesS3.iterator().next();
1153 assertThat(ruleS3.selector(), Is.is(
1154 DefaultTrafficSelector
1155 .builder()
1156 .matchInPort(d3p1.port())
1157 .build()
1158 ));
1159 assertThat(ruleS3.treatment(), Is.is(
1160 DefaultTrafficTreatment
1161 .builder()
1162 .setOutput(d3p0.port())
1163 .build()
1164 ));
1165
1166 sut.deactivate();
1167
1168 }
1169
1170 /**
1171 * We test the proper compilation of p2p with
1172 * trivial selector, trivial treatment and filtered points.
1173 */
1174 @Test
1175 public void p2pFilteredPoint() {
1176
1177 intent = LinkCollectionIntent.builder()
1178 .appId(APP_ID)
1179 .selector(selector)
1180 .treatment(treatment)
1181 .applyTreatmentOnEgress(true)
1182 .links(p2pLinks)
1183 .filteredIngressPoints(ImmutableSet.of(
1184 new FilteredConnectPoint(d1p10, vlan100Selector)
1185 ))
1186 .filteredEgressPoints(ImmutableSet.of(
1187 new FilteredConnectPoint(d3p0, mpls200Selector)
1188 ))
1189 .build();
1190
1191 sut.activate();
1192
1193 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1194 assertThat(compiled, hasSize(1));
1195
1196 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1197 assertThat(rules, hasSize(3));
1198
1199 Collection<FlowRule> rulesS1 = rules.stream()
1200 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1201 .collect(Collectors.toSet());
1202 assertThat(rulesS1, hasSize(1));
1203 FlowRule ruleS1 = rulesS1.iterator().next();
1204 assertThat(ruleS1.selector(), Is.is(
1205 DefaultTrafficSelector
1206 .builder(vlan100Selector)
1207 .matchInPort(d1p10.port())
1208 .build()
1209 ));
1210 assertThat(ruleS1.treatment(), Is.is(
1211 DefaultTrafficTreatment
1212 .builder()
1213 .setOutput(d1p0.port())
1214 .build()
1215 ));
1216
1217 Collection<FlowRule> rulesS2 = rules.stream()
1218 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1219 .collect(Collectors.toSet());
1220 assertThat(rulesS2, hasSize(1));
1221 FlowRule ruleS2 = rulesS2.iterator().next();
1222 assertThat(ruleS2.selector(), Is.is(
1223 DefaultTrafficSelector
1224 .builder(vlan100Selector)
1225 .matchInPort(d2p0.port())
1226 .build()
1227 ));
1228 assertThat(ruleS2.treatment(), Is.is(
1229 DefaultTrafficTreatment
1230 .builder()
1231 .setOutput(d2p1.port())
1232 .build()
1233 ));
1234
1235
1236 Collection<FlowRule> rulesS3 = rules.stream()
1237 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1238 .collect(Collectors.toSet());
1239 assertThat(rulesS3, hasSize(1));
1240 FlowRule ruleS3 = rulesS3.iterator().next();
1241 assertThat(ruleS3.selector(), Is.is(
1242 DefaultTrafficSelector
1243 .builder(vlan100Selector)
1244 .matchInPort(d3p1.port())
1245 .build()
1246 ));
1247 assertThat(ruleS3.treatment(), Is.is(
1248 DefaultTrafficTreatment
1249 .builder()
1250 .popVlan()
1251 .pushMpls()
1252 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1253 .setOutput(d3p0.port())
1254 .build()
1255 ));
1256
1257 sut.deactivate();
1258
1259 }
1260
1261 /**
1262 * We test the proper compilation of p2p with
1263 * selector, treatment and filtered points.
1264 */
1265 @Test
1266 public void p2pNonTrivial() {
1267
1268 intent = LinkCollectionIntent.builder()
1269 .appId(APP_ID)
1270 .selector(ipPrefixSelector)
1271 .treatment(ethDstTreatment)
1272 .applyTreatmentOnEgress(true)
1273 .links(p2pLinks)
1274 .filteredIngressPoints(ImmutableSet.of(
1275 new FilteredConnectPoint(d1p10, vlan100Selector)
1276 ))
1277 .filteredEgressPoints(ImmutableSet.of(
1278 new FilteredConnectPoint(d3p0, mpls200Selector)
1279 ))
1280 .build();
1281
1282 sut.activate();
1283
1284 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1285 assertThat(compiled, hasSize(1));
1286
1287 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1288 assertThat(rules, hasSize(3));
1289
1290 Collection<FlowRule> rulesS1 = rules.stream()
1291 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1292 .collect(Collectors.toSet());
1293 assertThat(rulesS1, hasSize(1));
1294 FlowRule ruleS1 = rulesS1.iterator().next();
1295 assertThat(ruleS1.selector(), Is.is(
1296 DefaultTrafficSelector
1297 .builder(ipPrefixSelector)
1298 .matchInPort(d1p10.port())
1299 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1300 .build()
1301 ));
1302 assertThat(ruleS1.treatment(), Is.is(
1303 DefaultTrafficTreatment
1304 .builder()
1305 .setOutput(d1p0.port())
1306 .build()
1307 ));
1308
1309 Collection<FlowRule> rulesS2 = rules.stream()
1310 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1311 .collect(Collectors.toSet());
1312 assertThat(rulesS2, hasSize(1));
1313 FlowRule ruleS2 = rulesS2.iterator().next();
1314 assertThat(ruleS2.selector(), Is.is(
1315 DefaultTrafficSelector
1316 .builder(ipPrefixSelector)
1317 .matchInPort(d2p0.port())
1318 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1319 .build()
1320 ));
1321 assertThat(ruleS2.treatment(), Is.is(
1322 DefaultTrafficTreatment
1323 .builder()
1324 .setOutput(d2p1.port())
1325 .build()
1326 ));
1327
1328
1329 Collection<FlowRule> rulesS3 = rules.stream()
1330 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1331 .collect(Collectors.toSet());
1332 assertThat(rulesS3, hasSize(1));
1333 FlowRule ruleS3 = rulesS3.iterator().next();
1334 assertThat(ruleS3.selector(), Is.is(
1335 DefaultTrafficSelector
1336 .builder(ipPrefixSelector)
1337 .matchInPort(d3p1.port())
1338 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1339 .build()
1340 ));
1341 assertThat(ruleS3.treatment(), Is.is(
1342 DefaultTrafficTreatment
1343 .builder()
1344 .setEthDst(((ModEtherInstruction) ethDstTreatment
1345 .allInstructions()
1346 .stream()
1347 .filter(instruction -> instruction instanceof ModEtherInstruction)
1348 .findFirst().get()).mac())
1349 .popVlan()
1350 .pushMpls()
1351 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1352 .setOutput(d3p0.port())
1353 .build()
1354 ));
1355
1356 sut.deactivate();
1357
1358 }
1359
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001360}