blob: ec761d448a01bdd0e7681e5b7ad3a98451bb7045 [file] [log] [blame]
Yi Tseng2a81c9d2016-09-14 10:14:24 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.intent.impl.compiler;
18
19import com.google.common.collect.ImmutableSet;
Luca Pretede10c782017-01-05 17:23:08 -080020import com.google.common.collect.Lists;
21import com.google.common.collect.Sets;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070022import org.hamcrest.Matchers;
Luca Pretede10c782017-01-05 17:23:08 -080023import org.junit.Rule;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070024import org.junit.Test;
Luca Pretede10c782017-01-05 17:23:08 -080025import org.junit.rules.ExpectedException;
Pier Ventre973bb032016-10-11 08:57:39 -070026import org.onlab.packet.IpPrefix;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070027import org.onlab.packet.VlanId;
Luca Pretede10c782017-01-05 17:23:08 -080028import org.onlab.util.Bandwidth;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070029import org.onosproject.TestApplicationId;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.net.ConnectPoint;
Luca Pretede10c782017-01-05 17:23:08 -080032import org.onosproject.net.DeviceId;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070033import org.onosproject.net.FilteredConnectPoint;
Luca Pretede10c782017-01-05 17:23:08 -080034import org.onosproject.net.PortNumber;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070035import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.intent.AbstractIntentTest;
Luca Pretede10c782017-01-05 17:23:08 -080039import org.onosproject.net.intent.Constraint;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070040import org.onosproject.net.intent.Intent;
Luca Pretede10c782017-01-05 17:23:08 -080041import org.onosproject.net.intent.IntentException;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070042import org.onosproject.net.intent.IntentTestsMocks;
Luca Pretede10c782017-01-05 17:23:08 -080043import org.onosproject.net.intent.Key;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070044import org.onosproject.net.intent.LinkCollectionIntent;
45import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Luca Pretede10c782017-01-05 17:23:08 -080046import org.onosproject.net.intent.constraint.BandwidthConstraint;
47import org.onosproject.net.intent.constraint.PartialFailureConstraint;
48import org.onosproject.net.resource.ContinuousResource;
49import org.onosproject.net.resource.MockResourceService;
50import org.onosproject.net.resource.ResourceAllocation;
51import org.onosproject.net.resource.ResourceService;
52import org.onosproject.net.resource.Resources;
53import org.onosproject.net.topology.PathService;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070054
Luca Pretede10c782017-01-05 17:23:08 -080055import java.util.Collections;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070056import java.util.List;
57import java.util.Set;
Luca Pretede10c782017-01-05 17:23:08 -080058
Yi Tseng2a81c9d2016-09-14 10:14:24 -070059import static org.hamcrest.CoreMatchers.instanceOf;
60import static org.hamcrest.CoreMatchers.notNullValue;
61import static org.hamcrest.MatcherAssert.assertThat;
62import static org.hamcrest.Matchers.hasSize;
63import static org.hamcrest.Matchers.is;
Luca Pretede10c782017-01-05 17:23:08 -080064import static org.junit.Assert.assertEquals;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070065import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
66
67/**
68 * Unit tests for SinglePointToMultiPointIntentCompiler.
69 */
70public class SinglePointToMultiPointIntentCompilerTest extends AbstractIntentTest {
71
72 private static final ApplicationId APPID = new TestApplicationId("foo");
Luca Pretede10c782017-01-05 17:23:08 -080073 private static final String S1 = "s1";
74 private static final String S2 = "s2";
75 private static final String S3 = "s3";
76 private static final String S4 = "s4";
77 private static final String S5 = "s5";
78 private static final String S6 = "s6";
79 private static final String S7 = "s7";
80 private static final String S8 = "s8";
81
82 private static final DeviceId DID_1 = DeviceId.deviceId("of:" + S1);
83 private static final DeviceId DID_2 = DeviceId.deviceId("of:" + S2);
84 private static final DeviceId DID_3 = DeviceId.deviceId("of:" + S3);
85 private static final DeviceId DID_4 = DeviceId.deviceId("of:" + S4);
86 private static final DeviceId DID_5 = DeviceId.deviceId("of:" + S5);
87 private static final DeviceId DID_8 = DeviceId.deviceId("of:" + S8);
88
89 private static final PortNumber PORT_1 = PortNumber.portNumber(1);
90 private static final PortNumber PORT_2 = PortNumber.portNumber(2);
91 private static final PortNumber PORT_3 = PortNumber.portNumber(3);
Yi Tseng2a81c9d2016-09-14 10:14:24 -070092
93 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
94 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
95
Yi Tseng2a81c9d2016-09-14 10:14:24 -070096 /**
97 * Creates a SinglePointToMultiPoint intent for an ingress point
98 * and a group of egress points.
99 *
Luca Pretede10c782017-01-05 17:23:08 -0800100 * @param ingress the filtered ingress point
101 * @param egress the set of filtered egress points
102 * @return a single point to multi point intent
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700103 */
Luca Pretede10c782017-01-05 17:23:08 -0800104 private SinglePointToMultiPointIntent makeIntent(FilteredConnectPoint ingress,
105 Set<FilteredConnectPoint> egress) {
106 return makeIntent(ingress, egress, selector);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700107 }
108
109 /**
Luca Pretede10c782017-01-05 17:23:08 -0800110 * Generates SinglePointToMultiPointIntent with filtered connection point
111 * and the specified selector.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700112 *
Luca Pretede10c782017-01-05 17:23:08 -0800113 * @param ingress the filtered ingress point
114 * @param egress the filtered egress point
115 * @param trafficSelector the intent traffic selector
116 * @return a single point to multi point intent
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700117 */
Luca Pretede10c782017-01-05 17:23:08 -0800118 private SinglePointToMultiPointIntent makeIntent(FilteredConnectPoint ingress,
119 Set<FilteredConnectPoint> egress,
120 TrafficSelector trafficSelector) {
121 return makeIntent(ingress, egress, trafficSelector, Lists.newArrayList());
122 }
123
124 /**
125 * Generates SinglePointToMultiPointIntent with filtered connection point
126 * and the given constraints set.
127 *
128 * @param ingress the filtered ingress point
129 * @param egress the filtered egress point
130 * @param constraints the list of intent constraints
131 * @return a single point to multi point intent
132 */
133 private SinglePointToMultiPointIntent makeIntent(FilteredConnectPoint ingress,
134 Set<FilteredConnectPoint> egress,
135 List<Constraint> constraints) {
136 return makeIntent(ingress, egress, selector, constraints);
137 }
138
139 /**
140 * Generates SinglePointToMultiPointIntent with filtered connection point,
141 * and the specified traffic selector and intent constraints.
142 *
143 * @param ingress the filtered ingress point
144 * @param egress the set of filtered egress points
145 * @param trafficSelector the intent traffic selector
146 * @param constraints the list of intent constraints
147 * @return a single point to multi point intent
148 */
149 private SinglePointToMultiPointIntent makeIntent(FilteredConnectPoint ingress,
150 Set<FilteredConnectPoint> egress,
151 TrafficSelector trafficSelector,
152 List<Constraint> constraints) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700153 return SinglePointToMultiPointIntent.builder()
154 .appId(APPID)
155 .treatment(treatment)
Pier Ventre973bb032016-10-11 08:57:39 -0700156 .selector(trafficSelector)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700157 .filteredIngressPoint(ingress)
158 .filteredEgressPoints(egress)
Luca Pretede10c782017-01-05 17:23:08 -0800159 .constraints(constraints)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700160 .build();
161 }
162
163 /**
164 * Creates a compiler for SinglePointToMultiPoint intents.
165 *
166 * @param hops hops to use while computing paths for this intent
Luca Pretede10c782017-01-05 17:23:08 -0800167 * @param pathService the path service
168 * @param resourceService the resource service
169 * @return a single point to multi point intent compiler
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700170 */
Luca Pretede10c782017-01-05 17:23:08 -0800171 private SinglePointToMultiPointIntentCompiler makeCompiler(String[] hops,
172 PathService pathService,
173 ResourceService resourceService) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700174 SinglePointToMultiPointIntentCompiler compiler =
175 new SinglePointToMultiPointIntentCompiler();
Luca Pretede10c782017-01-05 17:23:08 -0800176 compiler.deviceService = new IntentTestsMocks.MockDeviceService();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700177
Luca Pretede10c782017-01-05 17:23:08 -0800178 if (pathService == null) {
179 compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
180 } else {
181 compiler.pathService = pathService;
182 }
183
184 if (resourceService == null) {
185 compiler.resourceService = new MockResourceService();
186 } else {
187 compiler.resourceService = resourceService;
188 }
189
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700190 return compiler;
191 }
192
193 /**
Luca Pretede10c782017-01-05 17:23:08 -0800194 * Creates a compiler for SinglePointToMultiPoint intents.
195 *
196 * @param hops the hops to use while computing paths for this intent
197 * @return a single point to multi point intent compiler
198 */
199 private SinglePointToMultiPointIntentCompiler makeCompiler(String[] hops) {
200 return makeCompiler(hops, null, null);
201 }
202
203 /**
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700204 * Tests a single ingress point with 8 hops to its egress point.
205 */
206 @Test
207 public void testSingleLongPathCompilation() {
Luca Pretede10c782017-01-05 17:23:08 -0800208 FilteredConnectPoint ingress =
209 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
210 Set<FilteredConnectPoint> egress =
211 Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_2)));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700212
Luca Pretede10c782017-01-05 17:23:08 -0800213 SinglePointToMultiPointIntent intent =
214 makeIntent(ingress, egress);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700215
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700216 assertThat(intent, is(notNullValue()));
217
Luca Pretede10c782017-01-05 17:23:08 -0800218 String[] hops = {S2, S3, S4, S5, S6, S7};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700219 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
220 assertThat(compiler, is(notNullValue()));
221
222 List<Intent> result = compiler.compile(intent, null);
223 assertThat(result, is(Matchers.notNullValue()));
224 assertThat(result, hasSize(1));
225 Intent resultIntent = result.get(0);
226 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
227
228 if (resultIntent instanceof LinkCollectionIntent) {
229 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800230 assertThat(linkIntent.links(), hasSize(7));
231 assertThat(linkIntent.links(), linksHasPath(S1, S2));
232 assertThat(linkIntent.links(), linksHasPath(S2, S3));
233 assertThat(linkIntent.links(), linksHasPath(S3, S4));
234 assertThat(linkIntent.links(), linksHasPath(S4, S5));
235 assertThat(linkIntent.links(), linksHasPath(S5, S6));
236 assertThat(linkIntent.links(), linksHasPath(S6, S7));
237 assertThat(linkIntent.links(), linksHasPath(S7, S8));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700238 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700239 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700240 }
241
242 /**
243 * Tests a simple topology where two egress points share some path segments
244 * and some path segments are not shared.
245 */
246 @Test
Luca Pretede10c782017-01-05 17:23:08 -0800247 public void testTwoEgressCompilation() {
248 FilteredConnectPoint ingress =
249 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
250
251 FilteredConnectPoint egressOne =
252 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
253 FilteredConnectPoint egressTwo =
254 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_2));
255 Set<FilteredConnectPoint> egress = Sets.newHashSet(egressOne, egressTwo);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700256
257 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
Luca Pretede10c782017-01-05 17:23:08 -0800258
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700259 assertThat(intent, is(notNullValue()));
260
Luca Pretede10c782017-01-05 17:23:08 -0800261 final String[] hops = {S2, S3};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700262 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
263 assertThat(compiler, is(notNullValue()));
264
265 List<Intent> result = compiler.compile(intent, null);
266 assertThat(result, is(notNullValue()));
267 assertThat(result, hasSize(1));
268 Intent resultIntent = result.get(0);
269 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
270
271 if (resultIntent instanceof LinkCollectionIntent) {
272 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
273 assertThat(linkIntent.links(), hasSize(4));
Luca Pretede10c782017-01-05 17:23:08 -0800274 assertThat(linkIntent.links(), linksHasPath(S1, S2));
275 assertThat(linkIntent.links(), linksHasPath(S2, S3));
276 assertThat(linkIntent.links(), linksHasPath(S3, S4));
277 assertThat(linkIntent.links(), linksHasPath(S3, S5));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700278 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700279 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700280 }
281
282 /**
Luca Pretede10c782017-01-05 17:23:08 -0800283 * Tests multiple egress points that share a common path to the ingress
284 * point.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700285 */
286 @Test
Luca Pretede10c782017-01-05 17:23:08 -0800287 public void testMultiEgressCompilation() {
288 FilteredConnectPoint ingress =
289 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
290
291 FilteredConnectPoint egressOne =
292 new FilteredConnectPoint(new ConnectPoint(DID_3, PORT_2));
293 FilteredConnectPoint egressTwo =
294 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
295 FilteredConnectPoint egressThree =
296 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_2));
297 Set<FilteredConnectPoint> egress = Sets.newHashSet(egressOne,
298 egressTwo,
299 egressThree);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700300
301 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
Luca Pretede10c782017-01-05 17:23:08 -0800302
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700303 assertThat(intent, is(notNullValue()));
304
Luca Pretede10c782017-01-05 17:23:08 -0800305 final String[] hops = {S2};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700306 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
307 assertThat(compiler, is(notNullValue()));
308
309 List<Intent> result = compiler.compile(intent, null);
310 assertThat(result, is(notNullValue()));
311 assertThat(result, hasSize(1));
312 Intent resultIntent = result.get(0);
313 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
314
315 if (resultIntent instanceof LinkCollectionIntent) {
316 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800317 assertThat(linkIntent.links(), hasSize(4));
318 assertThat(linkIntent.links(), linksHasPath(S1, S2));
319 assertThat(linkIntent.links(), linksHasPath(S2, S3));
320 assertThat(linkIntent.links(), linksHasPath(S2, S4));
321 assertThat(linkIntent.links(), linksHasPath(S2, S5));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700322 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700323 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700324 }
325
326 /**
327 * Tests ingress and egress on the same device.
328 */
329 @Test
330 public void testSameDeviceCompilation() {
Luca Pretede10c782017-01-05 17:23:08 -0800331 FilteredConnectPoint ingress =
332 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
333
334 Set<FilteredConnectPoint> egress =
335 Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2)),
336 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_3)));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700337
338 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
339 assertThat(intent, is(notNullValue()));
340
341 final String[] hops = {};
342 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
343 assertThat(compiler, is(notNullValue()));
344
345 List<Intent> result = compiler.compile(intent, null);
346 assertThat(result, is(notNullValue()));
347 assertThat(result, hasSize(1));
348 Intent resultIntent = result.get(0);
349 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
350
351 if (resultIntent instanceof LinkCollectionIntent) {
352 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800353 assertThat(linkIntent.links(), hasSize(0));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700354 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700355 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700356 }
357
358 /**
Luca Pretede10c782017-01-05 17:23:08 -0800359 * Tests filtered ingress and egress connect points.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700360 */
361 @Test
362 public void testFilteredConnectPointIntent() {
363
Luca Pretede10c782017-01-05 17:23:08 -0800364 FilteredConnectPoint ingress =
365 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700366
367 Set<FilteredConnectPoint> egress = ImmutableSet.of(
Luca Pretede10c782017-01-05 17:23:08 -0800368 new FilteredConnectPoint(new ConnectPoint(DID_3, PORT_1),
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700369 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
Luca Pretede10c782017-01-05 17:23:08 -0800370 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_1),
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700371 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
372 );
373
Luca Pretede10c782017-01-05 17:23:08 -0800374 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress, selector);
375 String[] hops = {S2};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700376
377 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
378 assertThat(compiler, is(notNullValue()));
379
380 List<Intent> result = compiler.compile(intent, null);
381 assertThat(result, is(notNullValue()));
382 assertThat(result, hasSize(1));
383
384 Intent resultIntent = result.get(0);
385 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
386
387 if (resultIntent instanceof LinkCollectionIntent) {
388 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
389 assertThat(linkIntent.links(), hasSize(3));
390
Luca Pretede10c782017-01-05 17:23:08 -0800391 assertThat(linkIntent.links(), linksHasPath(S1, S2));
392 assertThat(linkIntent.links(), linksHasPath(S2, S3));
393 assertThat(linkIntent.links(), linksHasPath(S2, S4));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700394
395 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
396 assertThat("Link collection ingress points do not match base intent",
397 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
398
399 assertThat("Link collection egress points do not match base intent",
400 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
401 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700402 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700403
404 }
Pier Ventre973bb032016-10-11 08:57:39 -0700405
406 /**
Luca Pretede10c782017-01-05 17:23:08 -0800407 * Tests filtered ingress and egress points with an intent selector set.
Pier Ventre973bb032016-10-11 08:57:39 -0700408 */
409 @Test
410 public void testNonTrivialSelectorsIntent() {
411
Luca Pretede10c782017-01-05 17:23:08 -0800412 FilteredConnectPoint ingress =
413 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
Pier Ventre973bb032016-10-11 08:57:39 -0700414
415 Set<FilteredConnectPoint> egress = ImmutableSet.of(
Luca Pretede10c782017-01-05 17:23:08 -0800416 new FilteredConnectPoint(new ConnectPoint(DID_3, PORT_1),
Pier Ventre973bb032016-10-11 08:57:39 -0700417 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
Luca Pretede10c782017-01-05 17:23:08 -0800418 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_1),
Pier Ventre973bb032016-10-11 08:57:39 -0700419 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
420 );
421
422 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
423 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
424 .build();
425
Luca Pretede10c782017-01-05 17:23:08 -0800426 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress, ipPrefixSelector);
427 String[] hops = {S2};
Pier Ventre973bb032016-10-11 08:57:39 -0700428
429 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
430 assertThat(compiler, is(notNullValue()));
431
432 List<Intent> result = compiler.compile(intent, null);
433 assertThat(result, is(notNullValue()));
434 assertThat(result, hasSize(1));
435
436 Intent resultIntent = result.get(0);
437 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
438
439 if (resultIntent instanceof LinkCollectionIntent) {
440 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
441 assertThat(linkIntent.links(), hasSize(3));
442
Luca Pretede10c782017-01-05 17:23:08 -0800443 assertThat(linkIntent.links(), linksHasPath(S1, S2));
444 assertThat(linkIntent.links(), linksHasPath(S2, S3));
445 assertThat(linkIntent.links(), linksHasPath(S2, S4));
Pier Ventre973bb032016-10-11 08:57:39 -0700446
447 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
448 assertThat("Link collection ingress points do not match base intent",
449 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
450
451 assertThat("Link collection egress points do not match base intent",
452 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
453 assertThat(linkIntent.selector(), is(ipPrefixSelector));
454 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700455 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Luca Pretede10c782017-01-05 17:23:08 -0800456 }
Pier Ventre973bb032016-10-11 08:57:39 -0700457
Luca Pretede10c782017-01-05 17:23:08 -0800458 /**
459 * Tests if bandwidth resources get allocated correctly.
460 */
461 @Test
462 public void testBandwidthConstrainedIntentAllocation() {
463 final double bpsTotal = 1000.0;
464 final double bpsToReserve = 100.0;
465
466 ContinuousResource resourceSw1P1 =
467 Resources.continuous(DID_1, PORT_1, Bandwidth.class)
468 .resource(bpsToReserve);
469 ContinuousResource resourceSw1P2 =
470 Resources.continuous(DID_1, PORT_2, Bandwidth.class)
471 .resource(bpsToReserve);
472 ContinuousResource resourceSw2P1 =
473 Resources.continuous(DID_2, PORT_1, Bandwidth.class)
474 .resource(bpsToReserve);
475 ContinuousResource resourceSw2P2 =
476 Resources.continuous(DID_2, PORT_2, Bandwidth.class)
477 .resource(bpsToReserve);
478 ContinuousResource resourceSw3P1 =
479 Resources.continuous(DID_3, PORT_1, Bandwidth.class)
480 .resource(bpsToReserve);
481 ContinuousResource resourceSw3P2 =
482 Resources.continuous(DID_3, PORT_2, Bandwidth.class)
483 .resource(bpsToReserve);
484 ContinuousResource resourceSw3P3 =
485 Resources.continuous(DID_3, PORT_3, Bandwidth.class)
486 .resource(bpsToReserve);
487 ContinuousResource resourceSw4P1 =
488 Resources.continuous(DID_4, PORT_1, Bandwidth.class)
489 .resource(bpsToReserve);
490 ContinuousResource resourceSw4P2 =
491 Resources.continuous(DID_4, PORT_2, Bandwidth.class)
492 .resource(bpsToReserve);
493
494 String[] hops = {DID_3.toString()};
495
496 final ResourceService resourceService =
497 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
498 final List<Constraint> constraints =
499 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
500
501 FilteredConnectPoint ingress =
502 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_1));
503
504 Set<FilteredConnectPoint> egress = ImmutableSet.of(
505 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2)),
506 new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_2)));
507
508 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
509 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
510 .build();
511
512 SinglePointToMultiPointIntent intent =
513 makeIntent(ingress, egress, ipPrefixSelector, constraints);
514
515 SinglePointToMultiPointIntentCompiler compiler =
516 makeCompiler(null,
517 new IntentTestsMocks.FixedMP2MPMockPathService(hops),
518 resourceService);
519
520 compiler.compile(intent, null);
521
522 Key intentKey = intent.key();
523
524 ResourceAllocation rA1 = new ResourceAllocation(resourceSw1P1, intentKey);
525 ResourceAllocation rA2 = new ResourceAllocation(resourceSw1P2, intentKey);
526 ResourceAllocation rA3 = new ResourceAllocation(resourceSw2P1, intentKey);
527 ResourceAllocation rA4 = new ResourceAllocation(resourceSw2P2, intentKey);
528 ResourceAllocation rA5 = new ResourceAllocation(resourceSw3P1, intentKey);
529 ResourceAllocation rA6 = new ResourceAllocation(resourceSw3P2, intentKey);
530 ResourceAllocation rA7 = new ResourceAllocation(resourceSw3P3, intentKey);
531 ResourceAllocation rA8 = new ResourceAllocation(resourceSw4P1, intentKey);
532 ResourceAllocation rA9 = new ResourceAllocation(resourceSw4P2, intentKey);
533
534 Set<ResourceAllocation> expectedResourceAllocations =
535 ImmutableSet.of(rA1, rA2, rA3, rA4, rA5, rA6, rA7, rA8, rA9);
536
537 Set<ResourceAllocation> resourceAllocations =
538 ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
539
540 assertThat(resourceAllocations, hasSize(9));
541 assertEquals(expectedResourceAllocations, resourceAllocations);
542 }
543
544 /**
545 * Tests if all expected links are present when a partial failure
546 * constraint is used and one ingress is not present.
547 */
548 @Test
549 public void testPartialFailureConstraintSuccess() {
550 FilteredConnectPoint ingress =
551 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
552
553 Set<FilteredConnectPoint> egress = ImmutableSet.of(
554 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)),
555 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_2)));
556
557 final List<Constraint> constraints =
558 Collections.singletonList(new PartialFailureConstraint());
559
560 SinglePointToMultiPointIntent intent =
561 makeIntent(ingress, egress, constraints);
562
563 String[] hops = {S3};
564
565 SinglePointToMultiPointIntentCompiler compiler =
566 makeCompiler(null,
567 new IntentTestsMocks.FixedMP2MPMockPathService(hops),
568 null);
569 assertThat(compiler, is(notNullValue()));
570
571 List<Intent> result = compiler.compile(intent, null);
572 assertThat(result, is(notNullValue()));
573 assertThat(result, hasSize(1));
574
575 Intent resultIntent = result.get(0);
576 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
577
578 if (resultIntent instanceof LinkCollectionIntent) {
579 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
580 assertThat(linkIntent.links(), hasSize(2));
581 assertThat(linkIntent.links(), linksHasPath(S1, S3));
582 assertThat(linkIntent.links(), linksHasPath(S3, S4));
583 }
584 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
585 }
586
587 /**
588 * Exception expected to be raised when an intent does not find all paths
589 * and a partiale failure constraint is not specified.
590 */
591 @Rule
592 public ExpectedException intentException = ExpectedException.none();
593
594 /**
595 * Tests if compiling an intent without partial failure constraints set and
596 * with a missing egress connect point generates an exception and no other
597 * results.
598 */
599 @Test
600 public void testPartialFailureConstraintFailure() {
601 FilteredConnectPoint ingress =
602 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
603
604 Set<FilteredConnectPoint> egress = ImmutableSet.of(
605 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)),
606 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_2)));
607
608 SinglePointToMultiPointIntent intent =
609 makeIntent(ingress, egress);
610
611 String[] hops = {S3};
612
613 SinglePointToMultiPointIntentCompiler compiler =
614 makeCompiler(null,
615 new IntentTestsMocks.FixedMP2MPMockPathService(hops),
616 null);
617 assertThat(compiler, is(notNullValue()));
618
619 intentException.expect(IntentException.class);
620
621 List<Intent> result = compiler.compile(intent, null);
622 assertThat(result, null);
Pier Ventre973bb032016-10-11 08:57:39 -0700623 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700624}