blob: 3970f8d37d1b9059d8e42647452eb165a393015d [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;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070057import static org.hamcrest.Matchers.everyItem;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058import static org.hamcrest.Matchers.hasSize;
59import static org.hamcrest.Matchers.is;
Pier Ventre766995d2016-10-05 22:15:56 -070060import static org.onlab.packet.EthType.EtherType.IPV4;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080061import static org.onosproject.net.Link.Type.DIRECT;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070062import static org.onosproject.net.NetTestTools.*;
Pier Ventre766995d2016-10-05 22:15:56 -070063import static org.onosproject.net.flow.criteria.Criterion.Type.IN_PORT;
64import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL;
65import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
66import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080067
Pier Ventre766995d2016-10-05 22:15:56 -070068public class LinkCollectionIntentCompilerTest extends AbstractLinkCollectionTest {
Yi Tseng2a81c9d2016-09-14 10:14:24 -070069
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080070 @Before
Brian O'Connor81134662015-06-25 17:23:33 -040071 public void setUp() {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080072 sut = new LinkCollectionIntentCompiler();
73 coreService = createMock(CoreService.class);
74 expect(coreService.registerApplication("org.onosproject.net.intent"))
75 .andReturn(appId);
76 sut.coreService = coreService;
77
78 Intent.bindIdGenerator(idGenerator);
79
80 intent = LinkCollectionIntent.builder()
81 .appId(APP_ID)
82 .selector(selector)
83 .treatment(treatment)
84 .links(links)
85 .ingressPoints(ImmutableSet.of(d1p1))
86 .egressPoints(ImmutableSet.of(d3p1))
87 .build();
Pier Ventre647138f2016-08-26 17:32:44 -070088
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080089 intentExtensionService = createMock(IntentExtensionService.class);
90 intentExtensionService.registerCompiler(LinkCollectionIntent.class, sut);
91 intentExtensionService.unregisterCompiler(LinkCollectionIntent.class);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080092
93 registrator = new IntentConfigurableRegistrator();
94 registrator.extensionService = intentExtensionService;
95 registrator.cfgService = new ComponentConfigAdapter();
96 registrator.activate();
97
98 sut.registrator = registrator;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099
Pier Ventre81c47bf2016-11-04 07:26:22 -0700100 LinkCollectionCompiler.optimize = false;
101 LinkCollectionCompiler.copyTtl = false;
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700102
Pier Ventre81c47bf2016-11-04 07:26:22 -0700103 replay(coreService, intentExtensionService);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700104
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800105 }
106
107 @After
108 public void tearDown() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700109
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800110 Intent.unbindIdGenerator(idGenerator);
111 }
112
113 @Test
114 public void testCompile() {
115 sut.activate();
116
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800117 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 assertThat(compiled, hasSize(1));
119
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700120 assertThat("key is inherited",
121 compiled.stream().map(Intent::key).collect(Collectors.toList()),
122 everyItem(is(intent.key())));
123
124
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800125 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
126 assertThat(rules, hasSize(links.size()));
127
128 // if not found, get() raises an exception
129 FlowRule rule1 = rules.stream()
Pier Ventre766995d2016-10-05 22:15:56 -0700130 .filter(rule -> rule.deviceId().equals(d1p10.deviceId()))
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800131 .findFirst()
132 .get();
133 assertThat(rule1.selector(), is(
134 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d1p1.port()).build()
135 ));
136 assertThat(rule1.treatment(), is(
137 DefaultTrafficTreatment.builder(intent.treatment()).setOutput(d1p1.port()).build()
138 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400139 assertThat(rule1.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800140
141 FlowRule rule2 = rules.stream()
142 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
143 .findFirst()
144 .get();
145 assertThat(rule2.selector(), is(
146 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d2p0.port()).build()
147 ));
148 assertThat(rule2.treatment(), is(
149 DefaultTrafficTreatment.builder().setOutput(d2p1.port()).build()
150 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400151 assertThat(rule2.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800152
153 FlowRule rule3 = rules.stream()
154 .filter(rule -> rule.deviceId().equals(d3p0.deviceId()))
155 .findFirst()
156 .get();
157 assertThat(rule3.selector(), is(
158 DefaultTrafficSelector.builder(intent.selector()).matchInPort(d3p1.port()).build()
159 ));
160 assertThat(rule3.treatment(), is(
161 DefaultTrafficTreatment.builder().setOutput(d3p1.port()).build()
162 ));
Brian O'Connor81134662015-06-25 17:23:33 -0400163 assertThat(rule3.priority(), is(intent.priority()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800164
165 sut.deactivate();
166 }
Pier Ventre647138f2016-08-26 17:32:44 -0700167
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700168 /**
169 * Single point to multi point case.
170 * -1 of1 2-1 of2 2--1 of3 2-
171 * 3
172 * `-1 of4 2-
173 */
174 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700175 public void testFilteredConnectPointForSp() {
Pier Ventre647138f2016-08-26 17:32:44 -0700176 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700177 Set<Link> testLinks = ImmutableSet.of(
178 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
179 DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
180 DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
181 );
Pier Ventre647138f2016-08-26 17:32:44 -0700182
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700183 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
184 .matchInPort(PortNumber.portNumber(1))
185 .build();
186
187 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
188 .setOutput(PortNumber.portNumber(2))
189 .build();
190
191 TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(vlan100Selector)
192 .matchInPort(PortNumber.portNumber(1))
193 .build();
194
195 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
196 .setOutput(PortNumber.portNumber(2))
197 .setOutput(PortNumber.portNumber(3))
198 .build();
199
200 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
201 .matchInPort(PortNumber.portNumber(1))
202 .build();
203
204 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
205 .setOutput(PortNumber.portNumber(2))
206 .build();
207
208 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
209 .matchInPort(PortNumber.portNumber(1))
210 .build();
211
212 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
213 .setVlanId(VlanId.vlanId("200"))
214 .setOutput(PortNumber.portNumber(2))
215 .build();
Pier Ventre647138f2016-08-26 17:32:44 -0700216
217
Pier Ventre647138f2016-08-26 17:32:44 -0700218
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700219 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
220 new FilteredConnectPoint(of1p1, vlan100Selector)
221 );
Pier Ventre647138f2016-08-26 17:32:44 -0700222
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700223 Set<FilteredConnectPoint> egress = ImmutableSet.of(
224 new FilteredConnectPoint(of3p2, vlan100Selector),
225 new FilteredConnectPoint(of4p2, vlan200Selector)
226 );
Pier Ventre647138f2016-08-26 17:32:44 -0700227
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700228 intent = LinkCollectionIntent.builder()
229 .appId(APP_ID)
230 .filteredIngressPoints(ingress)
231 .filteredEgressPoints(egress)
232 .treatment(treatment)
233 .applyTreatmentOnEgress(true)
234 .links(testLinks)
235 .build();
Pier Ventre647138f2016-08-26 17:32:44 -0700236
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700237 assertThat(sut, is(notNullValue()));
Pier Ventre647138f2016-08-26 17:32:44 -0700238
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700239 List<Intent> result = sut.compile(intent, Collections.emptyList());
240
241 assertThat(result, is(notNullValue()));
242 assertThat(result, hasSize(1));
243
244 Intent resultIntent = result.get(0);
245 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
246
247 if (resultIntent instanceof FlowRuleIntent) {
248 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
249
250 assertThat(frIntent.flowRules(), hasSize(4));
251
252 List<FlowRule> deviceFlowRules;
253 FlowRule flowRule;
254
255 // Of1
256 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
257 assertThat(deviceFlowRules, hasSize(1));
258 flowRule = deviceFlowRules.get(0);
259 assertThat(flowRule.selector(), is(expectOf1Selector));
260 assertThat(flowRule.treatment(), is(expectOf1Treatment));
261
262 // Of2
263 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
264 assertThat(deviceFlowRules, hasSize(1));
265 flowRule = deviceFlowRules.get(0);
266 assertThat(flowRule.selector(), is(expectOf2Selector));
267 assertThat(flowRule.treatment(), is(expectOf2Treatment));
268
269 // Of3
270 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
271 assertThat(deviceFlowRules, hasSize(1));
272 flowRule = deviceFlowRules.get(0);
273 assertThat(flowRule.selector(), is(expectOf3Selector));
274 assertThat(flowRule.treatment(), is(expectOf3Treatment));
275
276 // Of4
277 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
278 assertThat(deviceFlowRules, hasSize(1));
279 flowRule = deviceFlowRules.get(0);
280 assertThat(flowRule.selector(), is(expectOf4Selector));
281 assertThat(flowRule.treatment(), is(expectOf4Treatment));
282
283 }
284 sut.deactivate();
285 }
286
287 /**
288 * Multi point to single point intent with filtered connect point.
289 *
290 * -1 of1 2-1 of2 2-1 of4 2-
291 * 3
292 * -1 of3 2---/
293 */
294 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700295 public void testFilteredConnectPointForMp() {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700296 sut.activate();
297 Set<Link> testlinks = ImmutableSet.of(
298 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
299 DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
300 DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
301 );
302
303 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
304 new FilteredConnectPoint(of1p1, vlan100Selector),
305 new FilteredConnectPoint(of3p1, vlan100Selector)
306 );
307
308 Set<FilteredConnectPoint> egress = ImmutableSet.of(
309 new FilteredConnectPoint(of4p2, vlan200Selector)
310 );
311
312 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(vlan100Selector)
313 .matchInPort(PortNumber.portNumber(1))
314 .build();
315
316 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
317 .setVlanId(VlanId.vlanId("200"))
318 .setOutput(PortNumber.portNumber(2))
319 .build();
320
321 TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(vlan200Selector)
322 .matchInPort(PortNumber.portNumber(1))
323 .build();
324
325 TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(vlan200Selector)
326 .matchInPort(PortNumber.portNumber(3))
327 .build();
328
329 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
330 .setOutput(PortNumber.portNumber(2))
331 .build();
332
333 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(vlan100Selector)
334 .matchInPort(PortNumber.portNumber(1))
335 .build();
336
337 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder()
338 .setVlanId(VlanId.vlanId("200"))
339 .setOutput(PortNumber.portNumber(2))
340 .build();
341
342 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(vlan100Selector)
343 .matchInPort(PortNumber.portNumber(1))
344 .matchVlanId(VlanId.vlanId("200"))
345 .build();
346
347 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
348 .setOutput(PortNumber.portNumber(2))
349 .build();
350
351 intent = LinkCollectionIntent.builder()
352 .appId(APP_ID)
353 .filteredIngressPoints(ingress)
354 .filteredEgressPoints(egress)
355 .treatment(treatment)
356 .links(testlinks)
357 .build();
358
359 List<Intent> result = sut.compile(intent, Collections.emptyList());
360
361 assertThat(result, is(notNullValue()));
362 assertThat(result, hasSize(1));
363
364 Intent resultIntent = result.get(0);
365 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
366
367 if (resultIntent instanceof FlowRuleIntent) {
368 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
369 assertThat(frIntent.flowRules(), hasSize(5));
370
371 List<FlowRule> deviceFlowRules;
372 FlowRule flowRule;
373
374 // Of1
375 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
376 assertThat(deviceFlowRules, hasSize(1));
377 flowRule = deviceFlowRules.get(0);
378 assertThat(flowRule.selector(), is(expectOf1Selector));
379 assertThat(flowRule.treatment(), is(expectOf1Treatment));
380
381 // Of2 (has 2 flows)
382 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
383 assertThat(deviceFlowRules, hasSize(2));
384 flowRule = deviceFlowRules.get(0);
385 assertThat(flowRule.selector(), is(expectOf2Selector1));
386 assertThat(flowRule.treatment(), is(expectOf2Treatment));
387 flowRule = deviceFlowRules.get(1);
388 assertThat(flowRule.selector(), is(expectOf2Selector2));
389 assertThat(flowRule.treatment(), is(expectOf2Treatment));
390
391 // Of3
392 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
393 assertThat(deviceFlowRules, hasSize(1));
394 flowRule = deviceFlowRules.get(0);
395 assertThat(flowRule.selector(), is(expectOf3Selector));
396 assertThat(flowRule.treatment(), is(expectOf3Treatment));
397
398 // Of4
399 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
400 assertThat(deviceFlowRules, hasSize(1));
401 flowRule = deviceFlowRules.get(0);
402 assertThat(flowRule.selector(), is(expectOf4Selector));
403 assertThat(flowRule.treatment(), is(expectOf4Treatment));
404 }
Pier Ventre647138f2016-08-26 17:32:44 -0700405
406 sut.deactivate();
407 }
408
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700409 /**
410 * Single point to multi point without filtered connect point case.
411 * -1 of1 2-1 of2 2--1 of3 2-
412 * 3
413 * `-1 of4 2-
414 */
Pier Ventre27d42572016-08-29 17:37:08 -0700415 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700416 public void nonTrivialTranslationForSp() {
Pier Ventre27d42572016-08-29 17:37:08 -0700417 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700418 Set<Link> testLinks = ImmutableSet.of(
419 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
420 DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(),
421 DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build()
422 );
Pier Ventre27d42572016-08-29 17:37:08 -0700423
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700424 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
425 .matchInPort(PortNumber.portNumber(1))
426 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700427
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700428 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder()
429 .setOutput(PortNumber.portNumber(2))
430 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700431
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700432 TrafficSelector expectOf2Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
433 .matchInPort(PortNumber.portNumber(1))
434 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700435
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700436 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
437 .setOutput(PortNumber.portNumber(2))
438 .setOutput(PortNumber.portNumber(3))
439 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700440
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700441 TrafficSelector expectOf3Selector = 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 expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
446 .setOutput(PortNumber.portNumber(2))
447 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700448
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700449 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
450 .matchInPort(PortNumber.portNumber(1))
451 .build();
452
453 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
454 .setOutput(PortNumber.portNumber(2))
455 .build();
456
457
458
459 Set<ConnectPoint> ingress = ImmutableSet.of(
460 of1p1
461 );
462
463 Set<ConnectPoint> egress = ImmutableSet.of(
464 of3p2,
465 of4p2
466 );
467
468 intent = LinkCollectionIntent.builder()
469 .appId(APP_ID)
470 .selector(ipPrefixSelector)
471 .treatment(ethDstTreatment)
472 .ingressPoints(ingress)
473 .egressPoints(egress)
474 .applyTreatmentOnEgress(true)
475 .links(testLinks)
476 .build();
477
478 assertThat(sut, is(notNullValue()));
479
480 List<Intent> result = sut.compile(intent, Collections.emptyList());
481
482 assertThat(result, is(notNullValue()));
483 assertThat(result, hasSize(1));
484
485 Intent resultIntent = result.get(0);
486 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
487
488 if (resultIntent instanceof FlowRuleIntent) {
489 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
490
491 assertThat(frIntent.flowRules(), hasSize(4));
492
493 List<FlowRule> deviceFlowRules;
494 FlowRule flowRule;
495
496 // Of1
497 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
498 assertThat(deviceFlowRules, hasSize(1));
499 flowRule = deviceFlowRules.get(0);
500 assertThat(flowRule.selector(), is(expectOf1Selector));
501 assertThat(flowRule.treatment(), is(expectOf1Treatment));
502
503 // Of2
504 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
505 assertThat(deviceFlowRules, hasSize(1));
506 flowRule = deviceFlowRules.get(0);
507 assertThat(flowRule.selector(), is(expectOf2Selector));
508 assertThat(flowRule.treatment(), is(expectOf2Treatment));
509
510 // Of3
511 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
512 assertThat(deviceFlowRules, hasSize(1));
513 flowRule = deviceFlowRules.get(0);
514 assertThat(flowRule.selector(), is(expectOf3Selector));
515 assertThat(flowRule.treatment(), is(expectOf3Treatment));
516
517 // Of4
518 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
519 assertThat(deviceFlowRules, hasSize(1));
520 flowRule = deviceFlowRules.get(0);
521 assertThat(flowRule.selector(), is(expectOf4Selector));
522 assertThat(flowRule.treatment(), is(expectOf4Treatment));
523
524 }
525 sut.deactivate();
Pier Ventre27d42572016-08-29 17:37:08 -0700526 }
527
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700528 /**
529 * Multi point to single point intent without filtered connect point.
530 *
531 * -1 of1 2-1 of2 2-1 of4 2-
532 * 3
533 * -1 of3 2---/
534 */
Pier Ventre27d42572016-08-29 17:37:08 -0700535 @Test
Pier Ventre766995d2016-10-05 22:15:56 -0700536 public void nonTrivialTranslationForMp() {
Pier Ventre27d42572016-08-29 17:37:08 -0700537 sut.activate();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700538 Set<Link> testlinks = ImmutableSet.of(
539 DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(),
540 DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(),
541 DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build()
542 );
Pier Ventre27d42572016-08-29 17:37:08 -0700543
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700544 Set<ConnectPoint> ingress = ImmutableSet.of(
545 of1p1,
546 of3p1
547 );
Pier Ventre27d42572016-08-29 17:37:08 -0700548
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700549 Set<ConnectPoint> egress = ImmutableSet.of(
550 of4p2
551 );
Pier Ventre27d42572016-08-29 17:37:08 -0700552
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700553 TrafficSelector expectOf1Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
554 .matchInPort(PortNumber.portNumber(1))
555 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700556
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700557 TrafficTreatment expectOf1Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
558 .setOutput(PortNumber.portNumber(2))
559 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700560
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700561 TrafficSelector expectOf2Selector1 = DefaultTrafficSelector.builder(ipPrefixSelector)
562 .matchInPort(PortNumber.portNumber(1))
563 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
564 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700565
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700566 TrafficSelector expectOf2Selector2 = DefaultTrafficSelector.builder(ipPrefixSelector)
567 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
568 .matchInPort(PortNumber.portNumber(3))
569 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700570
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700571 TrafficTreatment expectOf2Treatment = DefaultTrafficTreatment.builder()
572 .setOutput(PortNumber.portNumber(2))
573 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700574
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700575 TrafficSelector expectOf3Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
576 .matchInPort(PortNumber.portNumber(1))
577 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700578
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700579 TrafficTreatment expectOf3Treatment = DefaultTrafficTreatment.builder(ethDstTreatment)
580 .setOutput(PortNumber.portNumber(2))
581 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700582
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700583 TrafficSelector expectOf4Selector = DefaultTrafficSelector.builder(ipPrefixSelector)
584 .matchEthDst(MacAddress.valueOf("C0:FF:EE:C0:FF:EE"))
585 .matchInPort(PortNumber.portNumber(1))
586 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700587
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700588 TrafficTreatment expectOf4Treatment = DefaultTrafficTreatment.builder()
589 .setOutput(PortNumber.portNumber(2))
590 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700591
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700592 intent = LinkCollectionIntent.builder()
593 .appId(APP_ID)
594 .selector(ipPrefixSelector)
595 .ingressPoints(ingress)
596 .egressPoints(egress)
597 .treatment(ethDstTreatment)
598 .links(testlinks)
599 .build();
Pier Ventre27d42572016-08-29 17:37:08 -0700600
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700601 List<Intent> result = sut.compile(intent, Collections.emptyList());
Pier Ventre27d42572016-08-29 17:37:08 -0700602
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700603 assertThat(result, is(notNullValue()));
604 assertThat(result, hasSize(1));
605
606 Intent resultIntent = result.get(0);
607 assertThat(resultIntent, instanceOf(FlowRuleIntent.class));
608
609 if (resultIntent instanceof FlowRuleIntent) {
610 FlowRuleIntent frIntent = (FlowRuleIntent) resultIntent;
611 assertThat(frIntent.flowRules(), hasSize(5));
612
613 List<FlowRule> deviceFlowRules;
614 FlowRule flowRule;
615
616 // Of1
617 deviceFlowRules = getFlowRulesByDevice(of1Id, frIntent.flowRules());
618 assertThat(deviceFlowRules, hasSize(1));
619 flowRule = deviceFlowRules.get(0);
620 assertThat(flowRule.selector(), is(expectOf1Selector));
621 assertThat(flowRule.treatment(), is(expectOf1Treatment));
622
623 // Of2 (has 2 flows)
624 deviceFlowRules = getFlowRulesByDevice(of2Id, frIntent.flowRules());
625 assertThat(deviceFlowRules, hasSize(2));
626 flowRule = deviceFlowRules.get(0);
627 assertThat(flowRule.selector(), is(expectOf2Selector1));
628 assertThat(flowRule.treatment(), is(expectOf2Treatment));
629 flowRule = deviceFlowRules.get(1);
630 assertThat(flowRule.selector(), is(expectOf2Selector2));
631 assertThat(flowRule.treatment(), is(expectOf2Treatment));
632
633 // Of3
634 deviceFlowRules = getFlowRulesByDevice(of3Id, frIntent.flowRules());
635 assertThat(deviceFlowRules, hasSize(1));
636 flowRule = deviceFlowRules.get(0);
637 assertThat(flowRule.selector(), is(expectOf3Selector));
638 assertThat(flowRule.treatment(), is(expectOf3Treatment));
639
640 // Of4
641 deviceFlowRules = getFlowRulesByDevice(of4Id, frIntent.flowRules());
642 assertThat(deviceFlowRules, hasSize(1));
643 flowRule = deviceFlowRules.get(0);
644 assertThat(flowRule.selector(), is(expectOf4Selector));
645 assertThat(flowRule.treatment(), is(expectOf4Treatment));
646 }
647
648 sut.deactivate();
Pier Ventre27d42572016-08-29 17:37:08 -0700649 }
Pier Ventre766995d2016-10-05 22:15:56 -0700650
651 /**
652 * We test the proper compilation of mp2sp with
653 * trivial selector, trivial treatment and 1 hop.
654 */
655 @Test
656 public void singleHopTestForMp() {
657
658 intent = LinkCollectionIntent.builder()
659 .appId(APP_ID)
660 .selector(selector)
661 .treatment(treatment)
662 .links(ImmutableSet.of())
663 .filteredIngressPoints(ImmutableSet.of(
664 new FilteredConnectPoint(d1p10),
665 new FilteredConnectPoint(d1p11)
666 ))
667 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0)))
668 .build();
669
670 sut.activate();
671
672 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
673 assertThat(compiled, hasSize(1));
674
675 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
676 assertThat(rules, hasSize(2));
677
678 Collection<FlowRule> rulesS1 = rules.stream()
679 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
680 .collect(Collectors.toSet());
681 assertThat(rulesS1, hasSize(2));
682 FlowRule ruleS1 = rulesS1.stream()
683 .filter(rule -> {
684 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
685 return inPort.port().equals(d1p10.port());
686 })
687 .findFirst()
688 .get();
689 assertThat(ruleS1.selector(), Is.is(
690 DefaultTrafficSelector
691 .builder()
692 .matchInPort(d1p10.port())
693 .build()
694 ));
695 assertThat(ruleS1.treatment(), Is.is(
696 DefaultTrafficTreatment
697 .builder()
698 .setOutput(d1p0.port())
699 .build()
700 ));
701
702 ruleS1 = rulesS1.stream()
703 .filter(rule -> {
704 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
705 return inPort.port().equals(d1p11.port());
706 })
707 .findFirst()
708 .get();
709 assertThat(ruleS1.selector(), Is.is(
710 DefaultTrafficSelector
711 .builder()
712 .matchInPort(d1p11.port())
713 .build()
714 ));
715 assertThat(ruleS1.treatment(), Is.is(
716 DefaultTrafficTreatment
717 .builder()
718 .setOutput(d1p0.port())
719 .build()
720 ));
721
722 sut.deactivate();
723
724 }
725
726 /**
727 * We test the proper compilation of sp2mp with
728 * trivial selector, trivial treatment and 1 hop.
729 */
730 @Test
731 public void singleHopTestForSp() {
732
733 intent = LinkCollectionIntent.builder()
734 .appId(APP_ID)
735 .selector(selector)
736 .treatment(treatment)
737 .applyTreatmentOnEgress(true)
738 .links(ImmutableSet.of())
739 .filteredEgressPoints(ImmutableSet.of(
740 new FilteredConnectPoint(d1p10),
741 new FilteredConnectPoint(d1p11)
742 ))
743 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0)))
744 .build();
745
746 sut.activate();
747
748 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
749 assertThat(compiled, hasSize(1));
750
751 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
752 assertThat(rules, hasSize(1));
753
754 Collection<FlowRule> rulesS1 = rules.stream()
755 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
756 .collect(Collectors.toSet());
757 assertThat(rulesS1, hasSize(1));
758 FlowRule ruleS1 = rulesS1.stream()
759 .filter(rule -> {
760 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
761 return inPort.port().equals(d1p0.port());
762 })
763 .findFirst()
764 .get();
765 assertThat(ruleS1.selector(), Is.is(
766 DefaultTrafficSelector
767 .builder()
768 .matchInPort(d1p0.port())
769 .build()
770 ));
771 assertThat(ruleS1.treatment(), Is.is(
772 DefaultTrafficTreatment
773 .builder()
774 .setOutput(d1p10.port())
775 .setOutput(d1p11.port())
776 .build()
777 ));
778
779 sut.deactivate();
780
781 }
782
783 /**
784 * We test the proper compilation of mp2sp with
785 * trivial selector, trivial treatment, filtered
786 * points and 1 hop.
787 */
788 @Test
789 public void singleHopTestFilteredForMp() {
790
791 intent = LinkCollectionIntent.builder()
792 .appId(APP_ID)
793 .selector(selector)
794 .treatment(treatment)
795 .links(ImmutableSet.of())
796 .filteredIngressPoints(ImmutableSet.of(
797 new FilteredConnectPoint(d1p10, vlan100Selector),
798 new FilteredConnectPoint(d1p11, mpls69Selector)
799 ))
800 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
801 .build();
802
803 sut.activate();
804
805 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
806 assertThat(compiled, hasSize(1));
807
808 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
809 assertThat(rules, hasSize(2));
810
811 Collection<FlowRule> rulesS1 = rules.stream()
812 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
813 .collect(Collectors.toSet());
814 assertThat(rulesS1, hasSize(2));
815 FlowRule ruleS1 = rulesS1.stream()
816 .filter(rule -> {
817 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
818 return inPort.port().equals(d1p10.port());
819 })
820 .findFirst()
821 .get();
822 assertThat(ruleS1.selector(), Is.is(
823 DefaultTrafficSelector
824 .builder(vlan100Selector)
825 .matchInPort(d1p10.port())
826 .build()
827 ));
828 assertThat(ruleS1.treatment(), Is.is(
829 DefaultTrafficTreatment
830 .builder()
831 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
832 .setOutput(d1p0.port())
833 .build()
834 ));
835
836 ruleS1 = rulesS1.stream()
837 .filter(rule -> {
838 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
839 return inPort.port().equals(d1p11.port());
840 })
841 .findFirst()
842 .get();
843 assertThat(ruleS1.selector(), Is.is(
844 DefaultTrafficSelector
845 .builder(mpls69Selector)
846 .matchInPort(d1p11.port())
847 .build()
848 ));
849 assertThat(ruleS1.treatment(), Is.is(
850 DefaultTrafficTreatment
851 .builder()
852 .popMpls(IPV4.ethType())
853 .pushVlan()
854 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
855 .setOutput(d1p0.port())
856 .build()
857 ));
858
859 sut.deactivate();
860
861 }
862
863 /**
864 * We test the proper compilation of sp2mp with
865 * trivial selector, trivial treatment and 1 hop.
866 */
867 @Test
868 public void singleHopTestFilteredForSp() {
869
870 intent = LinkCollectionIntent.builder()
871 .appId(APP_ID)
872 .selector(selector)
873 .treatment(treatment)
874 .applyTreatmentOnEgress(true)
875 .links(ImmutableSet.of())
876 .filteredEgressPoints(ImmutableSet.of(
877 new FilteredConnectPoint(d1p10, vlan100Selector),
878 new FilteredConnectPoint(d1p11, mpls80Selector)
879 ))
880 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
881 .build();
882
883 sut.activate();
884
885 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
886 assertThat(compiled, hasSize(1));
887
888 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
889 assertThat(rules, hasSize(1));
890
891 Collection<FlowRule> rulesS1 = rules.stream()
892 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
893 .collect(Collectors.toSet());
894 assertThat(rulesS1, hasSize(1));
895 FlowRule ruleS1 = rulesS1.stream()
896 .filter(rule -> {
897 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
898 return inPort.port().equals(d1p0.port());
899 })
900 .findFirst()
901 .get();
902 assertThat(ruleS1.selector(), Is.is(
903 DefaultTrafficSelector
904 .builder(vlan200Selector)
905 .matchInPort(d1p0.port())
906 .build()
907 ));
908 assertThat(ruleS1.treatment(), Is.is(
909 DefaultTrafficTreatment
910 .builder()
911 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
912 .setOutput(d1p10.port())
913 .popVlan()
914 .pushMpls()
915 .setMpls(((MplsCriterion) mpls80Selector.getCriterion(MPLS_LABEL)).label())
916 .setOutput(d1p11.port())
917 .build()
918 ));
919
920 sut.deactivate();
921
922 }
923
924 /**
925 * We test the proper compilation of mp2sp with
926 * selector, treatment, filtered
927 * points and 1 hop.
928 */
929 @Test
930 public void singleHopNonTrivialForMp() {
931
932 intent = LinkCollectionIntent.builder()
933 .appId(APP_ID)
934 .selector(ipPrefixSelector)
935 .treatment(ethDstTreatment)
936 .links(ImmutableSet.of())
937 .filteredIngressPoints(ImmutableSet.of(
938 new FilteredConnectPoint(d1p10, vlan100Selector),
939 new FilteredConnectPoint(d1p11, mpls100Selector)
940 ))
941 .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
942 .build();
943
944 sut.activate();
945
946 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
947 assertThat(compiled, hasSize(1));
948
949 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
950 assertThat(rules, hasSize(2));
951
952 Collection<FlowRule> rulesS1 = rules.stream()
953 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
954 .collect(Collectors.toSet());
955 assertThat(rulesS1, hasSize(2));
956 FlowRule ruleS1 = rulesS1.stream()
957 .filter(rule -> {
958 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
959 return inPort.port().equals(d1p10.port());
960 })
961 .findFirst()
962 .get();
963 assertThat(ruleS1.selector(), Is.is(
964 DefaultTrafficSelector
965 .builder(ipPrefixSelector)
966 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
967 .matchInPort(d1p10.port())
968 .build()
969 ));
970 assertThat(ruleS1.treatment(), Is.is(
971 DefaultTrafficTreatment
972 .builder()
973 .setEthDst(((ModEtherInstruction) ethDstTreatment
974 .allInstructions()
975 .stream()
976 .filter(instruction -> instruction instanceof ModEtherInstruction)
977 .findFirst().get()).mac())
978 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
979 .setOutput(d1p0.port())
980 .build()
981 ));
982
983 ruleS1 = rulesS1.stream()
984 .filter(rule -> {
985 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
986 return inPort.port().equals(d1p11.port());
987 })
988 .findFirst()
989 .get();
990 assertThat(ruleS1.selector(), Is.is(
991 DefaultTrafficSelector
992 .builder(ipPrefixSelector)
993 .matchMplsLabel(((MplsCriterion) mpls100Selector.getCriterion(MPLS_LABEL)).label())
994 .matchInPort(d1p11.port())
995 .build()
996 ));
997 assertThat(ruleS1.treatment(), Is.is(
998 DefaultTrafficTreatment
999 .builder()
1000 .setEthDst(((ModEtherInstruction) ethDstTreatment
1001 .allInstructions()
1002 .stream()
1003 .filter(instruction -> instruction instanceof ModEtherInstruction)
1004 .findFirst().get()).mac())
1005 .popMpls(IPV4.ethType())
1006 .pushVlan()
1007 .setVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1008 .setOutput(d1p0.port())
1009 .build()
1010 ));
1011
1012 sut.deactivate();
1013
1014 }
1015
1016 /**
1017 * We test the proper compilation of sp2mp with
1018 * selector, treatment and 1 hop.
1019 */
1020 @Test
1021 public void singleHopNonTrivialForSp() {
1022
1023 intent = LinkCollectionIntent.builder()
1024 .appId(APP_ID)
1025 .selector(ipPrefixSelector)
1026 .treatment(ethDstTreatment)
1027 .applyTreatmentOnEgress(true)
1028 .links(ImmutableSet.of())
1029 .filteredEgressPoints(ImmutableSet.of(
1030 new FilteredConnectPoint(d1p10, vlan100Selector),
1031 new FilteredConnectPoint(d1p11, mpls200Selector)
1032 ))
1033 .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p0, vlan200Selector)))
1034 .build();
1035
1036 sut.activate();
1037
1038 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1039 assertThat(compiled, hasSize(1));
1040
1041 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1042 assertThat(rules, hasSize(1));
1043
1044 Collection<FlowRule> rulesS1 = rules.stream()
1045 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1046 .collect(Collectors.toSet());
1047 assertThat(rulesS1, hasSize(1));
1048 FlowRule ruleS1 = rulesS1.stream()
1049 .filter(rule -> {
1050 PortCriterion inPort = (PortCriterion) rule.selector().getCriterion(IN_PORT);
1051 return inPort.port().equals(d1p0.port());
1052 })
1053 .findFirst()
1054 .get();
1055 assertThat(ruleS1.selector(), Is.is(
1056 DefaultTrafficSelector
1057 .builder(ipPrefixSelector)
1058 .matchVlanId(((VlanIdCriterion) vlan200Selector.getCriterion(VLAN_VID)).vlanId())
1059 .matchInPort(d1p0.port())
1060 .build()
1061 ));
1062 assertThat(ruleS1.treatment(), Is.is(
1063 DefaultTrafficTreatment
1064 .builder()
1065 .setEthDst(((ModEtherInstruction) ethDstTreatment
1066 .allInstructions()
1067 .stream()
1068 .filter(instruction -> instruction instanceof ModEtherInstruction)
1069 .findFirst().get()).mac())
1070 .setVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1071 .setOutput(d1p10.port())
1072 .setEthDst(((ModEtherInstruction) ethDstTreatment
1073 .allInstructions()
1074 .stream()
1075 .filter(instruction -> instruction instanceof ModEtherInstruction)
1076 .findFirst().get()).mac())
1077 .popVlan()
1078 .pushMpls()
1079 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1080 .setOutput(d1p11.port())
1081 .build()
1082 ));
1083
1084 sut.deactivate();
1085
1086 }
1087
Pier Ventreffe88d62016-10-13 14:34:40 -07001088 /**
1089 * We test the proper compilation of p2p with
1090 * trivial selector and trivial treatment.
1091 */
1092 @Test
1093 public void p2p() {
1094
1095 intent = LinkCollectionIntent.builder()
1096 .appId(APP_ID)
1097 .selector(selector)
1098 .treatment(treatment)
1099 .applyTreatmentOnEgress(true)
1100 .links(p2pLinks)
1101 .filteredIngressPoints(ImmutableSet.of(
1102 new FilteredConnectPoint(d1p10)
1103 ))
1104 .filteredEgressPoints(ImmutableSet.of(
1105 new FilteredConnectPoint(d3p0)
1106 ))
1107 .build();
1108
1109 sut.activate();
1110
1111 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1112 assertThat(compiled, hasSize(1));
1113
1114 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1115 assertThat(rules, hasSize(3));
1116
1117 Collection<FlowRule> rulesS1 = rules.stream()
1118 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1119 .collect(Collectors.toSet());
1120 assertThat(rulesS1, hasSize(1));
1121 FlowRule ruleS1 = rulesS1.iterator().next();
1122 assertThat(ruleS1.selector(), Is.is(
1123 DefaultTrafficSelector
1124 .builder()
1125 .matchInPort(d1p10.port())
1126 .build()
1127 ));
1128 assertThat(ruleS1.treatment(), Is.is(
1129 DefaultTrafficTreatment
1130 .builder()
1131 .setOutput(d1p0.port())
1132 .build()
1133 ));
1134
1135 Collection<FlowRule> rulesS2 = rules.stream()
1136 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1137 .collect(Collectors.toSet());
1138 assertThat(rulesS2, hasSize(1));
1139 FlowRule ruleS2 = rulesS2.iterator().next();
1140 assertThat(ruleS2.selector(), Is.is(
1141 DefaultTrafficSelector
1142 .builder()
1143 .matchInPort(d2p0.port())
1144 .build()
1145 ));
1146 assertThat(ruleS2.treatment(), Is.is(
1147 DefaultTrafficTreatment
1148 .builder()
1149 .setOutput(d2p1.port())
1150 .build()
1151 ));
1152
1153
1154 Collection<FlowRule> rulesS3 = rules.stream()
1155 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1156 .collect(Collectors.toSet());
1157 assertThat(rulesS3, hasSize(1));
1158 FlowRule ruleS3 = rulesS3.iterator().next();
1159 assertThat(ruleS3.selector(), Is.is(
1160 DefaultTrafficSelector
1161 .builder()
1162 .matchInPort(d3p1.port())
1163 .build()
1164 ));
1165 assertThat(ruleS3.treatment(), Is.is(
1166 DefaultTrafficTreatment
1167 .builder()
1168 .setOutput(d3p0.port())
1169 .build()
1170 ));
1171
1172 sut.deactivate();
1173
1174 }
1175
1176 /**
1177 * We test the proper compilation of p2p with
1178 * trivial selector, trivial treatment and filtered points.
1179 */
1180 @Test
1181 public void p2pFilteredPoint() {
1182
1183 intent = LinkCollectionIntent.builder()
1184 .appId(APP_ID)
1185 .selector(selector)
1186 .treatment(treatment)
1187 .applyTreatmentOnEgress(true)
1188 .links(p2pLinks)
1189 .filteredIngressPoints(ImmutableSet.of(
1190 new FilteredConnectPoint(d1p10, vlan100Selector)
1191 ))
1192 .filteredEgressPoints(ImmutableSet.of(
1193 new FilteredConnectPoint(d3p0, mpls200Selector)
1194 ))
1195 .build();
1196
1197 sut.activate();
1198
1199 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1200 assertThat(compiled, hasSize(1));
1201
1202 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1203 assertThat(rules, hasSize(3));
1204
1205 Collection<FlowRule> rulesS1 = rules.stream()
1206 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1207 .collect(Collectors.toSet());
1208 assertThat(rulesS1, hasSize(1));
1209 FlowRule ruleS1 = rulesS1.iterator().next();
1210 assertThat(ruleS1.selector(), Is.is(
1211 DefaultTrafficSelector
1212 .builder(vlan100Selector)
1213 .matchInPort(d1p10.port())
1214 .build()
1215 ));
1216 assertThat(ruleS1.treatment(), Is.is(
1217 DefaultTrafficTreatment
1218 .builder()
1219 .setOutput(d1p0.port())
1220 .build()
1221 ));
1222
1223 Collection<FlowRule> rulesS2 = rules.stream()
1224 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1225 .collect(Collectors.toSet());
1226 assertThat(rulesS2, hasSize(1));
1227 FlowRule ruleS2 = rulesS2.iterator().next();
1228 assertThat(ruleS2.selector(), Is.is(
1229 DefaultTrafficSelector
1230 .builder(vlan100Selector)
1231 .matchInPort(d2p0.port())
1232 .build()
1233 ));
1234 assertThat(ruleS2.treatment(), Is.is(
1235 DefaultTrafficTreatment
1236 .builder()
1237 .setOutput(d2p1.port())
1238 .build()
1239 ));
1240
1241
1242 Collection<FlowRule> rulesS3 = rules.stream()
1243 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1244 .collect(Collectors.toSet());
1245 assertThat(rulesS3, hasSize(1));
1246 FlowRule ruleS3 = rulesS3.iterator().next();
1247 assertThat(ruleS3.selector(), Is.is(
1248 DefaultTrafficSelector
1249 .builder(vlan100Selector)
1250 .matchInPort(d3p1.port())
1251 .build()
1252 ));
1253 assertThat(ruleS3.treatment(), Is.is(
1254 DefaultTrafficTreatment
1255 .builder()
1256 .popVlan()
1257 .pushMpls()
1258 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1259 .setOutput(d3p0.port())
1260 .build()
1261 ));
1262
1263 sut.deactivate();
1264
1265 }
1266
1267 /**
1268 * We test the proper compilation of p2p with
1269 * selector, treatment and filtered points.
1270 */
1271 @Test
1272 public void p2pNonTrivial() {
1273
1274 intent = LinkCollectionIntent.builder()
1275 .appId(APP_ID)
1276 .selector(ipPrefixSelector)
1277 .treatment(ethDstTreatment)
1278 .applyTreatmentOnEgress(true)
1279 .links(p2pLinks)
1280 .filteredIngressPoints(ImmutableSet.of(
1281 new FilteredConnectPoint(d1p10, vlan100Selector)
1282 ))
1283 .filteredEgressPoints(ImmutableSet.of(
1284 new FilteredConnectPoint(d3p0, mpls200Selector)
1285 ))
1286 .build();
1287
1288 sut.activate();
1289
1290 List<Intent> compiled = sut.compile(intent, Collections.emptyList());
1291 assertThat(compiled, hasSize(1));
1292
1293 Collection<FlowRule> rules = ((FlowRuleIntent) compiled.get(0)).flowRules();
1294 assertThat(rules, hasSize(3));
1295
1296 Collection<FlowRule> rulesS1 = rules.stream()
1297 .filter(rule -> rule.deviceId().equals(d1p0.deviceId()))
1298 .collect(Collectors.toSet());
1299 assertThat(rulesS1, hasSize(1));
1300 FlowRule ruleS1 = rulesS1.iterator().next();
1301 assertThat(ruleS1.selector(), Is.is(
1302 DefaultTrafficSelector
1303 .builder(ipPrefixSelector)
1304 .matchInPort(d1p10.port())
1305 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1306 .build()
1307 ));
1308 assertThat(ruleS1.treatment(), Is.is(
1309 DefaultTrafficTreatment
1310 .builder()
1311 .setOutput(d1p0.port())
1312 .build()
1313 ));
1314
1315 Collection<FlowRule> rulesS2 = rules.stream()
1316 .filter(rule -> rule.deviceId().equals(d2p0.deviceId()))
1317 .collect(Collectors.toSet());
1318 assertThat(rulesS2, hasSize(1));
1319 FlowRule ruleS2 = rulesS2.iterator().next();
1320 assertThat(ruleS2.selector(), Is.is(
1321 DefaultTrafficSelector
1322 .builder(ipPrefixSelector)
1323 .matchInPort(d2p0.port())
1324 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1325 .build()
1326 ));
1327 assertThat(ruleS2.treatment(), Is.is(
1328 DefaultTrafficTreatment
1329 .builder()
1330 .setOutput(d2p1.port())
1331 .build()
1332 ));
1333
1334
1335 Collection<FlowRule> rulesS3 = rules.stream()
1336 .filter(rule -> rule.deviceId().equals(d3p1.deviceId()))
1337 .collect(Collectors.toSet());
1338 assertThat(rulesS3, hasSize(1));
1339 FlowRule ruleS3 = rulesS3.iterator().next();
1340 assertThat(ruleS3.selector(), Is.is(
1341 DefaultTrafficSelector
1342 .builder(ipPrefixSelector)
1343 .matchInPort(d3p1.port())
1344 .matchVlanId(((VlanIdCriterion) vlan100Selector.getCriterion(VLAN_VID)).vlanId())
1345 .build()
1346 ));
1347 assertThat(ruleS3.treatment(), Is.is(
1348 DefaultTrafficTreatment
1349 .builder()
1350 .setEthDst(((ModEtherInstruction) ethDstTreatment
1351 .allInstructions()
1352 .stream()
1353 .filter(instruction -> instruction instanceof ModEtherInstruction)
1354 .findFirst().get()).mac())
1355 .popVlan()
1356 .pushMpls()
1357 .setMpls(((MplsCriterion) mpls200Selector.getCriterion(MPLS_LABEL)).label())
1358 .setOutput(d3p0.port())
1359 .build()
1360 ));
1361
1362 sut.deactivate();
1363
1364 }
1365
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001366}