blob: 6c7d9c11d5cac0825fa02e9525a5b54ad4717c50 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Ray Milkeye6684082014-10-16 16:59:47 -070017
Yi Tseng2a81c9d2016-09-14 10:14:24 -070018import com.google.common.collect.ImmutableSet;
Luca Pretede10c782017-01-05 17:23:08 -080019import com.google.common.collect.Lists;
20import com.google.common.collect.Sets;
Ray Milkeye6684082014-10-16 16:59:47 -070021import org.hamcrest.Matchers;
Luca Pretede10c782017-01-05 17:23:08 -080022import org.junit.Rule;
Ray Milkeye6684082014-10-16 16:59:47 -070023import org.junit.Test;
Luca Pretede10c782017-01-05 17:23:08 -080024import org.junit.rules.ExpectedException;
Pier Ventre973bb032016-10-11 08:57:39 -070025import org.onlab.packet.IpPrefix;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070026import org.onlab.packet.VlanId;
Luca Pretede10c782017-01-05 17:23:08 -080027import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.TestApplicationId;
Ray Milkey6e0fb302015-04-16 14:44:12 -070029import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
Luca Pretede10c782017-01-05 17:23:08 -080031import org.onosproject.net.DeviceId;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070032import org.onosproject.net.FilteredConnectPoint;
Luca Pretede10c782017-01-05 17:23:08 -080033import org.onosproject.net.PortNumber;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070034import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.AbstractIntentTest;
Luca Pretede10c782017-01-05 17:23:08 -080038import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.Intent;
Luca Pretede10c782017-01-05 17:23:08 -080040import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.intent.IntentTestsMocks;
Luca Pretede10c782017-01-05 17:23:08 -080042import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.intent.LinkCollectionIntent;
44import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Luca Pretede10c782017-01-05 17:23:08 -080045import org.onosproject.net.intent.constraint.BandwidthConstraint;
46import org.onosproject.net.intent.constraint.PartialFailureConstraint;
47import org.onosproject.net.resource.ContinuousResource;
48import org.onosproject.net.resource.MockResourceService;
49import org.onosproject.net.resource.ResourceAllocation;
50import org.onosproject.net.resource.ResourceService;
51import org.onosproject.net.resource.Resources;
52import org.onosproject.net.topology.PathService;
Ray Milkeye6684082014-10-16 16:59:47 -070053
Luca Pretede10c782017-01-05 17:23:08 -080054import java.util.Collections;
Jonathan Hart066244c2015-06-23 09:46:19 -070055import java.util.List;
56import java.util.Set;
57
Ray Milkey6e0fb302015-04-16 14:44:12 -070058import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeye6684082014-10-16 16:59:47 -070059import static org.hamcrest.CoreMatchers.notNullValue;
60import static org.hamcrest.MatcherAssert.assertThat;
61import static org.hamcrest.Matchers.hasSize;
62import static org.hamcrest.Matchers.is;
Luca Pretede10c782017-01-05 17:23:08 -080063import static org.junit.Assert.assertEquals;
Brian O'Connorabafb502014-12-02 22:26:20 -080064import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070065
66/**
67 * Unit tests for the MultiPointToSinglePoint intent compiler.
68 */
Ray Milkey37f6a382014-11-25 14:54:42 -080069public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070070
Thomas Vachuskab97cf282014-10-20 23:31:12 -070071 private static final ApplicationId APPID = new TestApplicationId("foo");
72
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);
92
Ray Milkeye6684082014-10-16 16:59:47 -070093 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
94 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
95
96 /**
Ray Milkeye6684082014-10-16 16:59:47 -070097 * Creates a MultiPointToSinglePoint intent for a group of ingress points
98 * and an egress point.
99 *
Luca Pretede10c782017-01-05 17:23:08 -0800100 * @param ingress the filtered ingress points
101 * @param egress the filtered egress point
102 * @return a MultiPointToSinglePoint intent
Ray Milkeye6684082014-10-16 16:59:47 -0700103 */
Luca Pretede10c782017-01-05 17:23:08 -0800104 private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress,
105 FilteredConnectPoint egress) {
106 return makeIntent(ingress, egress, Lists.newArrayList());
Ray Milkeye6684082014-10-16 16:59:47 -0700107 }
108
109 /**
Luca Pretede10c782017-01-05 17:23:08 -0800110 * Generates a MultiPointToSinglePointIntent with filtered connection point.
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700111 *
112 * @param ingress filtered ingress points
113 * @param egress filtered egress point
114 * @return
115 */
Luca Pretede10c782017-01-05 17:23:08 -0800116 private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress,
117 FilteredConnectPoint egress,
118 TrafficSelector trafficSelector) {
119 return makeIntent(ingress,
120 egress,
121 trafficSelector,
122 Lists.newArrayList());
123 }
124
125 /**
126 * Creates a MultiPointToSinglePoint intent for a group of ingress points,
127 * egress points and a list of constraints.
128 *
129 * @param ingress the filtered ingress point
130 * @param egress the filtered egress point
131 * @param constraints the list of intent constraints
132 * @return a MultiPointToSinglePoint intent
133 */
134 private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress,
135 FilteredConnectPoint egress,
136 List<Constraint> constraints) {
137 return makeIntent(ingress, egress, selector, constraints);
138 }
139
140 /**
141 * Generates a MultiPointToSinglePointIntent with filtered connection point.
142 *
143 * @param ingress filtered ingress points
144 * @param egress filtered egress point
145 * @param constraints the list of intent constraints
146 * @return
147 */
148 private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress,
149 FilteredConnectPoint egress,
150 TrafficSelector trafficSelector,
151 List<Constraint> constraints) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700152 return MultiPointToSinglePointIntent.builder()
153 .appId(APPID)
154 .treatment(treatment)
Pier Ventre973bb032016-10-11 08:57:39 -0700155 .selector(trafficSelector)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700156 .filteredIngressPoints(ingress)
157 .filteredEgressPoint(egress)
Luca Pretede10c782017-01-05 17:23:08 -0800158 .constraints(constraints)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700159 .build();
160 }
161
162 /**
Ray Milkeye6684082014-10-16 16:59:47 -0700163 * Creates a compiler for MultiPointToSinglePoint intents.
164 *
165 * @param hops hops to use while computing paths for this intent
Luca Pretede10c782017-01-05 17:23:08 -0800166 * @param pathService the path service
167 * @param resourceService the resource service
168 * @return MultiPointToSinglePoint intent
169 */
170 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops,
171 PathService pathService,
172 ResourceService resourceService) {
173 MultiPointToSinglePointIntentCompiler compiler =
174 new MultiPointToSinglePointIntentCompiler();
175
176 compiler.deviceService = new IntentTestsMocks.MockDeviceService();
177
178 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
190 return compiler;
191 }
192
193 /**
194 * Creates a compiler for MultiPointToSinglePoint intents.
195 *
196 * @param hops hops to use while computing paths for this intent
Ray Milkeye6684082014-10-16 16:59:47 -0700197 * @return MultiPointToSinglePoint intent
198 */
199 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
Luca Pretede10c782017-01-05 17:23:08 -0800200 return makeCompiler(hops, null, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700201 }
202
203 /**
204 * 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 Set<FilteredConnectPoint> ingress =
209 Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)));
210 FilteredConnectPoint egress =
211 new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1));
Ray Milkeye6684082014-10-16 16:59:47 -0700212
213 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
214 assertThat(intent, is(notNullValue()));
215
Luca Pretede10c782017-01-05 17:23:08 -0800216 String[] hops = {S2, S3, S4, S5, S6, S7};
Ray Milkeye6684082014-10-16 16:59:47 -0700217 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
218 assertThat(compiler, is(notNullValue()));
219
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800220 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700221 assertThat(result, is(Matchers.notNullValue()));
222 assertThat(result, hasSize(1));
223 Intent resultIntent = result.get(0);
224 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
225
226 if (resultIntent instanceof LinkCollectionIntent) {
227 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800228 assertThat(linkIntent.links(), hasSize(7));
229 assertThat(linkIntent.links(), linksHasPath(S1, S2));
230 assertThat(linkIntent.links(), linksHasPath(S2, S3));
231 assertThat(linkIntent.links(), linksHasPath(S3, S4));
232 assertThat(linkIntent.links(), linksHasPath(S4, S5));
233 assertThat(linkIntent.links(), linksHasPath(S5, S6));
234 assertThat(linkIntent.links(), linksHasPath(S6, S7));
235 assertThat(linkIntent.links(), linksHasPath(S7, S8));
Ray Milkeye6684082014-10-16 16:59:47 -0700236 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700237 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkeye6684082014-10-16 16:59:47 -0700238 }
239
240 /**
241 * Tests a simple topology where two ingress points share some path segments
242 * and some path segments are not shared.
243 */
244 @Test
245 public void testTwoIngressCompilation() {
Luca Pretede10c782017-01-05 17:23:08 -0800246 Set<FilteredConnectPoint> ingress =
247 Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)),
248 new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1)));
249 FilteredConnectPoint egress =
250 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_1));
Ray Milkeye6684082014-10-16 16:59:47 -0700251
252 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
253 assertThat(intent, is(notNullValue()));
254
Luca Pretede10c782017-01-05 17:23:08 -0800255 final String[] hops = {S3};
256 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
257 assertThat(compiler, is(notNullValue()));
258
259 List<Intent> result = compiler.compile(intent, null);
260 assertThat(result, is(notNullValue()));
261 assertThat(result, hasSize(1));
262 Intent resultIntent = result.get(0);
263 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
264
265 if (resultIntent instanceof LinkCollectionIntent) {
266 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
267 assertThat(linkIntent.links(), hasSize(3));
268 assertThat(linkIntent.links(), linksHasPath(S1, S3));
269 assertThat(linkIntent.links(), linksHasPath(S2, S3));
270 assertThat(linkIntent.links(), linksHasPath(S3, S4));
271 }
272 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
273 }
274
275 /**
276 * Tests a large number of ingress points that share a common path to the
277 * egress point.
278 */
279 @Test
280 public void testMultiIngressCompilation() {
281 Set<FilteredConnectPoint> ingress =
282 Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)),
283 new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1)),
284 new FilteredConnectPoint(new ConnectPoint(DID_3, PORT_1)));
285 FilteredConnectPoint egress =
286 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_1));
287
288 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
289 assertThat(intent, is(notNullValue()));
290
291 final String[] hops = {S4};
Ray Milkeye6684082014-10-16 16:59:47 -0700292 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
293 assertThat(compiler, is(notNullValue()));
294
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800295 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700296 assertThat(result, is(notNullValue()));
297 assertThat(result, hasSize(1));
298 Intent resultIntent = result.get(0);
299 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
300
301 if (resultIntent instanceof LinkCollectionIntent) {
302 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
303 assertThat(linkIntent.links(), hasSize(4));
Luca Pretede10c782017-01-05 17:23:08 -0800304 assertThat(linkIntent.links(), linksHasPath(S1, S4));
305 assertThat(linkIntent.links(), linksHasPath(S2, S4));
306 assertThat(linkIntent.links(), linksHasPath(S3, S4));
307 assertThat(linkIntent.links(), linksHasPath(S4, S5));
Ray Milkeye6684082014-10-16 16:59:47 -0700308 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700309 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkeye6684082014-10-16 16:59:47 -0700310 }
Ray Milkey6e0fb302015-04-16 14:44:12 -0700311
312 /**
313 * Tests ingress and egress on the same device.
314 */
315 @Test
316 public void testSameDeviceCompilation() {
Luca Pretede10c782017-01-05 17:23:08 -0800317 Set<FilteredConnectPoint> ingress =
318 Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)),
319 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2)));
320 FilteredConnectPoint egress =
321 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_3));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700322
323 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
324 assertThat(intent, is(notNullValue()));
325
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700326 final String[] hops = {};
Ray Milkey6e0fb302015-04-16 14:44:12 -0700327 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
328 assertThat(compiler, is(notNullValue()));
329
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800330 List<Intent> result = compiler.compile(intent, null);
Ray Milkey6e0fb302015-04-16 14:44:12 -0700331 assertThat(result, is(notNullValue()));
332 assertThat(result, hasSize(1));
333 Intent resultIntent = result.get(0);
334 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
335
336 if (resultIntent instanceof LinkCollectionIntent) {
337 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800338 assertThat(linkIntent.links(), hasSize(0));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700339 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700340 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700341 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700342
343 /**
344 * Tests filtered ingress and egress.
345 */
346 @Test
347 public void testFilteredConnectPointIntent() {
348
349 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
Luca Pretede10c782017-01-05 17:23:08 -0800350 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1),
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700351 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
Luca Pretede10c782017-01-05 17:23:08 -0800352 new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1),
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700353 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
354 );
355
Luca Pretede10c782017-01-05 17:23:08 -0800356 FilteredConnectPoint egress = new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700357
Luca Pretede10c782017-01-05 17:23:08 -0800358 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress, selector);
359 String[] hops = {S3};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700360
361 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
362 assertThat(compiler, is(notNullValue()));
363
364 List<Intent> result = compiler.compile(intent, null);
365 assertThat(result, is(notNullValue()));
366 assertThat(result, hasSize(1));
367
368 Intent resultIntent = result.get(0);
369 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
370
371 if (resultIntent instanceof LinkCollectionIntent) {
372 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
373 assertThat(linkIntent.links(), hasSize(3));
Luca Pretede10c782017-01-05 17:23:08 -0800374 assertThat(linkIntent.links(), linksHasPath(S1, S3));
375 assertThat(linkIntent.links(), linksHasPath(S2, S3));
376 assertThat(linkIntent.links(), linksHasPath(S3, S4));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700377 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700378 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700379
380 }
381
Pier Ventre973bb032016-10-11 08:57:39 -0700382 /**
383 * Tests selector, filtered ingress and egress.
384 */
385 @Test
386 public void testNonTrivialSelectorsIntent() {
Pier Ventre973bb032016-10-11 08:57:39 -0700387 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
Luca Pretede10c782017-01-05 17:23:08 -0800388 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1),
Pier Ventre973bb032016-10-11 08:57:39 -0700389 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
Luca Pretede10c782017-01-05 17:23:08 -0800390 new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1),
Pier Ventre973bb032016-10-11 08:57:39 -0700391 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
392 );
393
Luca Pretede10c782017-01-05 17:23:08 -0800394 FilteredConnectPoint egress =
395 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
396
Pier Ventre973bb032016-10-11 08:57:39 -0700397 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
398 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
399 .build();
400
Pier Ventre973bb032016-10-11 08:57:39 -0700401
Luca Pretede10c782017-01-05 17:23:08 -0800402 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress, ipPrefixSelector);
403 String[] hops = {S3};
Pier Ventre973bb032016-10-11 08:57:39 -0700404
405 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
406 assertThat(compiler, is(notNullValue()));
407
408 List<Intent> result = compiler.compile(intent, null);
409 assertThat(result, is(notNullValue()));
410 assertThat(result, hasSize(1));
411
412 Intent resultIntent = result.get(0);
413 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
414
415 if (resultIntent instanceof LinkCollectionIntent) {
416 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
417 assertThat(linkIntent.links(), hasSize(3));
Luca Pretede10c782017-01-05 17:23:08 -0800418 assertThat(linkIntent.links(), linksHasPath(S1, S3));
419 assertThat(linkIntent.links(), linksHasPath(S2, S3));
420 assertThat(linkIntent.links(), linksHasPath(S3, S4));
Pier Ventre973bb032016-10-11 08:57:39 -0700421 assertThat(linkIntent.selector(), is(ipPrefixSelector));
422 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700423 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Pier Ventre973bb032016-10-11 08:57:39 -0700424 }
425
Luca Pretede10c782017-01-05 17:23:08 -0800426 /**
427 * Tests if bandwidth resources get allocated correctly.
428 */
429 @Test
430 public void testBandwidthConstrainedIntentAllocation() {
431 final double bpsTotal = 1000.0;
432 final double bpsToReserve = 100.0;
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700433
Luca Pretede10c782017-01-05 17:23:08 -0800434 ContinuousResource resourceSw1P1 =
435 Resources.continuous(DID_1, PORT_1, Bandwidth.class)
436 .resource(bpsToReserve);
437 ContinuousResource resourceSw1P2 =
438 Resources.continuous(DID_1, PORT_2, Bandwidth.class)
439 .resource(bpsToReserve);
440 ContinuousResource resourceSw2P1 =
441 Resources.continuous(DID_2, PORT_1, Bandwidth.class)
442 .resource(bpsToReserve);
443 ContinuousResource resourceSw2P2 =
444 Resources.continuous(DID_2, PORT_2, Bandwidth.class)
445 .resource(bpsToReserve);
446 ContinuousResource resourceSw3P1 =
447 Resources.continuous(DID_3, PORT_1, Bandwidth.class)
448 .resource(bpsToReserve);
449 ContinuousResource resourceSw3P2 =
450 Resources.continuous(DID_3, PORT_2, Bandwidth.class)
451 .resource(bpsToReserve);
452 ContinuousResource resourceSw3P3 =
453 Resources.continuous(DID_3, PORT_3, Bandwidth.class)
454 .resource(bpsToReserve);
455 ContinuousResource resourceSw4P1 =
456 Resources.continuous(DID_4, PORT_1, Bandwidth.class)
457 .resource(bpsToReserve);
458 ContinuousResource resourceSw4P2 =
459 Resources.continuous(DID_4, PORT_2, Bandwidth.class)
460 .resource(bpsToReserve);
461
462 String[] hops = {DID_3.toString()};
463
464 final ResourceService resourceService =
465 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
466 final List<Constraint> constraints =
467 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
468
469 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
470 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)),
471 new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1)));
472
473 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
474 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
475 .build();
476
477 FilteredConnectPoint egress = new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
478
479 MultiPointToSinglePointIntent intent =
480 makeIntent(ingress, egress, ipPrefixSelector, constraints);
481
482 MultiPointToSinglePointIntentCompiler compiler =
483 makeCompiler(null,
484 new IntentTestsMocks.FixedMP2MPMockPathService(hops),
485 resourceService);
486
487 compiler.compile(intent, null);
488
489 Key intentKey = intent.key();
490
491 ResourceAllocation rA1 = new ResourceAllocation(resourceSw1P1, intentKey);
492 ResourceAllocation rA2 = new ResourceAllocation(resourceSw1P2, intentKey);
493 ResourceAllocation rA3 = new ResourceAllocation(resourceSw2P1, intentKey);
494 ResourceAllocation rA4 = new ResourceAllocation(resourceSw2P2, intentKey);
495 ResourceAllocation rA5 = new ResourceAllocation(resourceSw3P1, intentKey);
496 ResourceAllocation rA6 = new ResourceAllocation(resourceSw3P2, intentKey);
497 ResourceAllocation rA7 = new ResourceAllocation(resourceSw3P3, intentKey);
498 ResourceAllocation rA8 = new ResourceAllocation(resourceSw4P1, intentKey);
499 ResourceAllocation rA9 = new ResourceAllocation(resourceSw4P2, intentKey);
500
501 Set<ResourceAllocation> expectedResourceAllocations =
502 ImmutableSet.of(rA1, rA2, rA3, rA4, rA5, rA6, rA7, rA8, rA9);
503
504 Set<ResourceAllocation> resourceAllocations =
505 ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
506
507 assertThat(resourceAllocations, hasSize(9));
508 assertEquals(expectedResourceAllocations, resourceAllocations);
509 }
510
511 /**
512 * Tests if all expected links are present when a partial failure
513 * constraint is used and one ingress is not present.
514 */
515 @Test
516 public void testPartialFailureConstraintSuccess() {
517 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
518 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)),
519 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_1)));
520
521 FilteredConnectPoint egress =
522 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
523
524 final List<Constraint> constraints =
525 Collections.singletonList(new PartialFailureConstraint());
526
527 MultiPointToSinglePointIntent intent =
528 makeIntent(ingress, egress, constraints);
529
530 String[] hops = {S3};
531
532 MultiPointToSinglePointIntentCompiler compiler =
533 makeCompiler(null,
534 new IntentTestsMocks.FixedMP2MPMockPathService(hops),
535 null);
536 assertThat(compiler, is(notNullValue()));
537
538 List<Intent> result = compiler.compile(intent, null);
539 assertThat(result, is(notNullValue()));
540 assertThat(result, hasSize(1));
541
542 Intent resultIntent = result.get(0);
543 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
544
545 if (resultIntent instanceof LinkCollectionIntent) {
546 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
547 assertThat(linkIntent.links(), hasSize(2));
548 assertThat(linkIntent.links(), linksHasPath(S1, S3));
549 assertThat(linkIntent.links(), linksHasPath(S3, S4));
550 }
551 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
552 }
553
554 /**
555 * Exception expected to be raised when an intent does not find all paths
556 * and a partiale failure constraint is not specified.
557 */
558 @Rule
559 public ExpectedException intentException = ExpectedException.none();
560
561 /**
562 * Tests if compiling an intent without partial failure constraints set and
563 * with a missing ingress connect point generates an exception and no other
564 * results.
565 */
566 @Test
567 public void testPartialFailureConstraintFailure() {
568 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
569 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)),
570 new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_1)));
571
572 FilteredConnectPoint egress =
573 new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2));
574
575 MultiPointToSinglePointIntent intent =
576 makeIntent(ingress, egress);
577
578 String[] hops = {S3};
579
580 MultiPointToSinglePointIntentCompiler compiler =
581 makeCompiler(null,
582 new IntentTestsMocks.FixedMP2MPMockPathService(hops),
583 null);
584 assertThat(compiler, is(notNullValue()));
585
586 intentException.expect(IntentException.class);
587
588 List<Intent> result = compiler.compile(intent, null);
589 assertThat(result, null);
590 }
Ray Milkeye6684082014-10-16 16:59:47 -0700591}