blob: 31c15eef14cc71344dffee59d99196862a9aa1db [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 Milkeyc0fa4db2014-10-17 08:49:54 -070017
Pier Ventreffe88d62016-10-13 14:34:40 -070018import com.google.common.collect.ImmutableSet;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070019import org.hamcrest.Matchers;
Ray Milkey40f50b92014-11-07 13:25:53 -080020import org.junit.Test;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070021import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.TestApplicationId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.ConnectPoint;
Luca Pretede10c782017-01-05 17:23:08 -080025import org.onosproject.net.DeviceId;
Pier Ventreffe88d62016-10-13 14:34:40 -070026import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Link;
Daniele Moro3a6e1512017-12-22 12:14:44 +010028import org.onosproject.net.NetTestTools;
Luca Pretede10c782017-01-05 17:23:08 -080029import org.onosproject.net.PortNumber;
30import org.onosproject.net.ResourceGroup;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080034import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentTestsMocks;
Luca Pretede10c782017-01-05 17:23:08 -080037import org.onosproject.net.intent.Key;
Pier Ventreffe88d62016-10-13 14:34:40 -070038import org.onosproject.net.intent.LinkCollectionIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080040import org.onosproject.net.intent.constraint.BandwidthConstraint;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080041import org.onosproject.net.intent.impl.PathNotFoundException;
Luca Pretede10c782017-01-05 17:23:08 -080042import org.onosproject.net.resource.ContinuousResource;
43import org.onosproject.net.resource.MockResourceService;
44import org.onosproject.net.resource.ResourceAllocation;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080045import org.onosproject.net.resource.ResourceService;
Luca Pretede10c782017-01-05 17:23:08 -080046import org.onosproject.net.resource.Resources;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070047
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070048import java.util.Collections;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070049import java.util.List;
Pier Ventreffe88d62016-10-13 14:34:40 -070050import java.util.Set;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070051import java.util.stream.Collectors;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080053import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070054import static org.hamcrest.MatcherAssert.assertThat;
Luca Pretede10c782017-01-05 17:23:08 -080055import static org.hamcrest.Matchers.*;
56import static org.junit.Assert.assertEquals;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080057import static org.junit.Assert.fail;
Brian O'Connorabafb502014-12-02 22:26:20 -080058import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070059
60/**
61 * Unit tests for the HostToHost intent compiler.
62 */
Ray Milkey37f6a382014-11-25 14:54:42 -080063public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070064
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 private static final ApplicationId APPID = new TestApplicationId("foo");
66
Luca Pretede10c782017-01-05 17:23:08 -080067 private static final String S1 = "s1";
68 private static final String S2 = "s2";
69 private static final String S3 = "s3";
70 private static final String S4 = "s4";
71 private static final String S5 = "s5";
72 private static final String S6 = "s6";
73 private static final String S7 = "s7";
74 private static final String S8 = "s8";
75
76 private static final DeviceId DID_1 = DeviceId.deviceId("of:" + S1);
77 private static final DeviceId DID_2 = DeviceId.deviceId("of:" + S2);
78 private static final DeviceId DID_3 = DeviceId.deviceId("of:" + S3);
79 private static final DeviceId DID_8 = DeviceId.deviceId("of:" + S8);
80
81 private static final PortNumber PORT_1 = PortNumber.portNumber(1);
82 private static final PortNumber PORT_2 = PortNumber.portNumber(2);
83
84 private static final double BPS_TO_RESERVE = 100.0;
85
86 private static final ContinuousResource RESOURCE_SW1_P1 =
87 Resources.continuous(DID_1, PORT_1, Bandwidth.class)
88 .resource(BPS_TO_RESERVE);
89 private static final ContinuousResource RESOURCE_SW1_P2 =
90 Resources.continuous(DID_1, PORT_2, Bandwidth.class)
91 .resource(BPS_TO_RESERVE);
92 private static final ContinuousResource RESOURCE_SW2_P1 =
93 Resources.continuous(DID_2, PORT_1, Bandwidth.class)
94 .resource(BPS_TO_RESERVE);
95 private static final ContinuousResource RESOURCE_SW2_P2 =
96 Resources.continuous(DID_2, PORT_2, Bandwidth.class)
97 .resource(BPS_TO_RESERVE);
98 private static final ContinuousResource RESOURCE_SW3_P1 =
99 Resources.continuous(DID_3, PORT_1, Bandwidth.class)
100 .resource(BPS_TO_RESERVE);
101 private static final ContinuousResource RESOURCE_SW3_P2 =
102 Resources.continuous(DID_3, PORT_2, Bandwidth.class)
103 .resource(BPS_TO_RESERVE);
104
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700105 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
106 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
107
108 /**
109 * Creates a PointToPoint intent based on ingress and egress device Ids.
110 *
Luca Pretede10c782017-01-05 17:23:08 -0800111 * @param ingress the ingress connect point
112 * @param egress the egress connect point
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700113 * @return PointToPointIntent for the two devices
114 */
Luca Pretede10c782017-01-05 17:23:08 -0800115 private PointToPointIntent makeIntent(ConnectPoint ingress, ConnectPoint egress) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700116 return PointToPointIntent.builder()
117 .appId(APPID)
118 .selector(selector)
119 .treatment(treatment)
Luca Pretede10c782017-01-05 17:23:08 -0800120 .filteredIngressPoint(new FilteredConnectPoint(ingress))
121 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700122 .build();
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700123 }
124
125 /**
Luca Pretede10c782017-01-05 17:23:08 -0800126 * Creates a PointToPoint intent based on ingress and egress deviceIds and
127 * constraints.
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800128 *
Luca Pretede10c782017-01-05 17:23:08 -0800129 * @param ingress the ingress connect point
130 * @param egress the egress connect point
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800131 * @param constraints constraints
Luca Pretede10c782017-01-05 17:23:08 -0800132 * @return the PointToPointIntent connecting the two connect points with
133 * constraints
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800134 */
Luca Pretede10c782017-01-05 17:23:08 -0800135 private PointToPointIntent makeIntent(ConnectPoint ingress,
136 ConnectPoint egress,
137 List<Constraint> constraints) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700138 return PointToPointIntent.builder()
139 .appId(APPID)
140 .selector(selector)
141 .treatment(treatment)
Luca Pretede10c782017-01-05 17:23:08 -0800142 .filteredIngressPoint(new FilteredConnectPoint(ingress))
143 .filteredEgressPoint(new FilteredConnectPoint(egress))
144 .constraints(constraints)
145 .build();
146 }
147
148 /**
149 * Creates a PointToPoint intent based on ingress and egress deviceIds,
150 * constraints and a resource group.
151 *
152 * @param ingress the ingress connect point
153 * @param egress the egress connect point
154 * @param constraints constraints
155 * @param resourceGroup the resource group
156 * @return the PointToPointIntent connecting the two connect points with
157 * constraints
158 */
159 private PointToPointIntent makeIntent(ConnectPoint ingress,
160 ConnectPoint egress,
161 List<Constraint> constraints,
162 ResourceGroup resourceGroup) {
163 return PointToPointIntent.builder()
164 .appId(APPID)
165 .resourceGroup(resourceGroup)
166 .selector(selector)
167 .treatment(treatment)
168 .filteredIngressPoint(new FilteredConnectPoint(ingress))
169 .filteredEgressPoint(new FilteredConnectPoint(egress))
170 .constraints(constraints)
171 .build();
172 }
173
174 /**
175 * Creates a PointToPoint intent based on an intent key, ingress and egress
176 * deviceIds, constraints and a resource group.
177 *
178 * @param key the intent key
179 * @param ingress the ingress connect point
180 * @param egress the egress connect point
181 * @param constraints constraints
182 * @param resourceGroup the resource group
183 * @return the PointToPointIntent connecting the two connect points with
184 * constraints
185 */
186 private PointToPointIntent makeIntent(Key key,
187 ConnectPoint ingress,
188 ConnectPoint egress,
189 List<Constraint> constraints,
190 ResourceGroup resourceGroup) {
191 return PointToPointIntent.builder()
192 .appId(APPID)
193 .key(key)
194 .resourceGroup(resourceGroup)
195 .selector(selector)
196 .treatment(treatment)
197 .filteredIngressPoint(new FilteredConnectPoint(ingress))
198 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700199 .constraints(constraints)
200 .build();
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800201 }
202
203 /**
Daniele Moro3a6e1512017-12-22 12:14:44 +0100204 * Creates a PointToPoint intent based on ingress and egress deviceIds
205 * and a suggested path.
206 * @param ingress the ingress connect point
207 * @param egress the egress connect point
208 * @param suggestedPath the suggested path
209 * @return the PointToPointIntent connecting the two connect points with
210 * the suggested path (if available)
211 */
212 private PointToPointIntent makeIntentSuggestedPath(ConnectPoint ingress,
213 ConnectPoint egress,
214 List<Link> suggestedPath) {
215 return PointToPointIntent.builder()
216 .appId(APPID)
217 .selector(selector)
218 .treatment(treatment)
219 .filteredIngressPoint(new FilteredConnectPoint(ingress))
220 .filteredEgressPoint(new FilteredConnectPoint(egress))
221 .suggestedPath(suggestedPath)
222 .build();
223 }
224
225 /**
226 * Creates a PointToPoint intent based on ingress and egress deviceIds and
227 * constraints with a suggested path.
228 *
229 * @param ingress the ingress connect point
230 * @param egress the egress connect point
231 * @param suggestedPath the suggested path
232 * @param constraints constraints
233 * @return the PointToPointIntent connecting the two connect points with
234 * constraints and a suggested path
235 */
236 private PointToPointIntent makeIntentSuggestedPath(ConnectPoint ingress,
237 ConnectPoint egress,
238 List<Link> suggestedPath,
239 List<Constraint> constraints) {
240 return PointToPointIntent.builder()
241 .appId(APPID)
242 .selector(selector)
243 .treatment(treatment)
244 .filteredIngressPoint(new FilteredConnectPoint(ingress))
245 .filteredEgressPoint(new FilteredConnectPoint(egress))
246 .constraints(constraints)
247 .suggestedPath(suggestedPath)
248 .build();
249 }
250
251 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700252 * Creates a compiler for HostToHost intents.
253 *
254 * @param hops string array describing the path hops to use when compiling
255 * @return HostToHost intent compiler
256 */
257 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Luca Pretede10c782017-01-05 17:23:08 -0800258 return makeCompiler(hops, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700259 }
260
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800261 /**
Daniele Moro3a6e1512017-12-22 12:14:44 +0100262 * Creates a compiler for PointToPoint intents with suggested paths.
263 *
264 * @param paths all the possible paths in the network
265 * @return PointToPoint intent compiler
266 */
267 private PointToPointIntentCompiler makeCompilerSuggestedPath(String[][] paths) {
268 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
269 compiler.pathService = new IntentTestsMocks.MockMultiplePathService(paths);
270 compiler.linkService = new IntentTestsMocks.MockLinkService(paths);
271 return compiler;
272 }
273
274 /**
275 * Creates a point to point intent compiler for suggested path case.
276 *
277 * @param paths all the possible paths in the network
278 * @param resourceService service to use for resource allocation requests
279 * @return point to point compiler
280 */
281 private PointToPointIntentCompiler makeCompilerSuggestedPath(String[][] paths,
282 ResourceService resourceService) {
283 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
284 compiler.pathService = new IntentTestsMocks.MockMultiplePathService(paths);
285 compiler.linkService = new IntentTestsMocks.MockLinkService(paths);
286
287 if (resourceService == null) {
288 compiler.resourceService = new MockResourceService();
289 } else {
290 compiler.resourceService = resourceService;
291 }
292
293 return compiler;
294 }
295
296 /**
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800297 * Creates a point to point intent compiler for a three switch linear
298 * topology.
299 *
300 * @param resourceService service to use for resource allocation requests
301 * @return point to point compiler
302 */
Luca Pretede10c782017-01-05 17:23:08 -0800303 private PointToPointIntentCompiler makeCompiler(String[] hops,
304 ResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800305 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800306 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Luca Pretede10c782017-01-05 17:23:08 -0800307
308 if (resourceService == null) {
309 compiler.resourceService = new MockResourceService();
310 } else {
311 compiler.resourceService = resourceService;
312 }
313
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800314 return compiler;
315 }
316
317 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700318 * Tests a pair of devices in an 8 hop path, forward direction.
319 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800320 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700321 public void testForwardPathCompilation() {
322
Luca Pretede10c782017-01-05 17:23:08 -0800323 PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
324 new ConnectPoint(DID_8, PORT_1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700325
Luca Pretede10c782017-01-05 17:23:08 -0800326 String[] hops = {S1, S2, S3, S4, S5, S6, S7, S8};
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700327 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700328
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800329 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700330 assertThat(result, is(Matchers.notNullValue()));
331 assertThat(result, hasSize(1));
332 Intent forwardResultIntent = result.get(0);
Pier Ventreffe88d62016-10-13 14:34:40 -0700333 assertThat(forwardResultIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700334
Pier Ventreffe88d62016-10-13 14:34:40 -0700335 if (forwardResultIntent instanceof LinkCollectionIntent) {
336 LinkCollectionIntent forwardIntent = (LinkCollectionIntent) forwardResultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800337 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
338 FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700339 // 7 links for the hops, plus one default lnk on ingress and egress
Pier Ventreffe88d62016-10-13 14:34:40 -0700340 assertThat(forwardIntent.links(), hasSize(hops.length - 1));
Luca Pretede10c782017-01-05 17:23:08 -0800341 assertThat(forwardIntent.links(), linksHasPath(S1, S2));
342 assertThat(forwardIntent.links(), linksHasPath(S2, S3));
343 assertThat(forwardIntent.links(), linksHasPath(S3, S4));
344 assertThat(forwardIntent.links(), linksHasPath(S4, S5));
345 assertThat(forwardIntent.links(), linksHasPath(S5, S6));
346 assertThat(forwardIntent.links(), linksHasPath(S6, S7));
347 assertThat(forwardIntent.links(), linksHasPath(S7, S8));
Pier Ventreffe88d62016-10-13 14:34:40 -0700348 assertThat(forwardIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
349 assertThat(forwardIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700350 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700351 assertThat("key is inherited", forwardResultIntent.key(), is(intent.key()));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700352 }
353
354 /**
355 * Tests a pair of devices in an 8 hop path, forward direction.
356 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800357 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700358 public void testReversePathCompilation() {
359
Luca Pretede10c782017-01-05 17:23:08 -0800360 PointToPointIntent intent = makeIntent(new ConnectPoint(DID_8, PORT_1),
361 new ConnectPoint(DID_1, PORT_1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700362
Luca Pretede10c782017-01-05 17:23:08 -0800363 String[] hops = {S1, S2, S3, S4, S5, S6, S7, S8};
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700364 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700365
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800366 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700367 assertThat(result, is(Matchers.notNullValue()));
368 assertThat(result, hasSize(1));
369 Intent reverseResultIntent = result.get(0);
Pier Ventreffe88d62016-10-13 14:34:40 -0700370 assertThat(reverseResultIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700371
Pier Ventreffe88d62016-10-13 14:34:40 -0700372 if (reverseResultIntent instanceof LinkCollectionIntent) {
373 LinkCollectionIntent reverseLinkCollectionIntent = (LinkCollectionIntent) reverseResultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800374 FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
375 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1));
Pier Ventreffe88d62016-10-13 14:34:40 -0700376 assertThat(reverseLinkCollectionIntent.links(), hasSize(hops.length - 1));
Luca Pretede10c782017-01-05 17:23:08 -0800377 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S2, S1));
378 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S3, S2));
379 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S4, S3));
380 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S5, S4));
381 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S6, S5));
382 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S7, S6));
383 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S8, S7));
Pier Ventreffe88d62016-10-13 14:34:40 -0700384 assertThat(reverseLinkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
385 assertThat(reverseLinkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700386 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700387 assertThat("key is inherited", reverseResultIntent.key(), is(intent.key()));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700388 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800389
390 /**
Luca Pretede10c782017-01-05 17:23:08 -0800391 * Tests the compilation of an intent which designates two different ports
392 * on the same switch.
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800393 */
394 @Test
395 public void testSameSwitchDifferentPortsIntentCompilation() {
Luca Pretede10c782017-01-05 17:23:08 -0800396 FilteredConnectPoint src =
397 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
398 FilteredConnectPoint dst =
399 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800400
Luca Pretede10c782017-01-05 17:23:08 -0800401 PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
402 new ConnectPoint(DID_1, PORT_2));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800403
Luca Pretede10c782017-01-05 17:23:08 -0800404 String[] hops = {S1};
405 PointToPointIntentCompiler compiler = makeCompiler(hops);
406
407 List<Intent> compiled = compiler.compile(intent, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800408
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700409 assertThat("key is inherited",
410 compiled.stream().map(Intent::key).collect(Collectors.toList()),
411 everyItem(is(intent.key())));
412
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800413 assertThat(compiled, hasSize(1));
Pier Ventreffe88d62016-10-13 14:34:40 -0700414 assertThat(compiled.get(0), is(instanceOf(LinkCollectionIntent.class)));
415 LinkCollectionIntent linkCollectionIntent = (LinkCollectionIntent) compiled.get(0);
416 Set<Link> links = linkCollectionIntent.links();
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800417
Pier Ventreffe88d62016-10-13 14:34:40 -0700418 assertThat(links, hasSize(0));
419 assertThat(linkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(src)));
420 assertThat(linkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(dst)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800421 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800422
423 /**
424 * Tests that requests with sufficient available bandwidth succeed.
425 */
426 @Test
427 public void testBandwidthConstrainedIntentSuccess() {
Luca Pretede10c782017-01-05 17:23:08 -0800428 final double bpsTotal = 1000.0;
429 final double bpsToReserve = 100.0;
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800430
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800431 final ResourceService resourceService =
Luca Pretede10c782017-01-05 17:23:08 -0800432 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700433 final List<Constraint> constraints =
Luca Pretede10c782017-01-05 17:23:08 -0800434 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800435
Luca Pretede10c782017-01-05 17:23:08 -0800436 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
437 new ConnectPoint(DID_3, PORT_2),
438 constraints);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800439
Luca Pretede10c782017-01-05 17:23:08 -0800440 String[] hops = {S1, S2, S3};
441 final PointToPointIntentCompiler compiler = makeCompiler(hops,
442 resourceService);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800443
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800444 final List<Intent> compiledIntents = compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800445
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800446 assertThat(compiledIntents, Matchers.notNullValue());
447 assertThat(compiledIntents, hasSize(1));
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700448
449 assertThat("key is inherited",
450 compiledIntents.stream().map(Intent::key).collect(Collectors.toList()),
451 everyItem(is(intent.key())));
452
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800453 }
454
455 /**
456 * Tests that requests with insufficient available bandwidth fail.
457 */
458 @Test
459 public void testBandwidthConstrainedIntentFailure() {
Luca Pretede10c782017-01-05 17:23:08 -0800460 final double bpsTotal = 10.0;
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800461
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800462 final ResourceService resourceService =
Luca Pretede10c782017-01-05 17:23:08 -0800463 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700464 final List<Constraint> constraints =
Luca Pretede10c782017-01-05 17:23:08 -0800465 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800466
467 try {
Luca Pretede10c782017-01-05 17:23:08 -0800468 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
469 new ConnectPoint(DID_3, PORT_2),
470 constraints);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800471
Luca Pretede10c782017-01-05 17:23:08 -0800472 String[] hops = {S1, S2, S3};
473 final PointToPointIntentCompiler compiler = makeCompiler(hops,
474 resourceService);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800475
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800476 compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800477
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800478 fail("Point to Point compilation with insufficient bandwidth does "
479 + "not throw exception.");
480 } catch (PathNotFoundException noPath) {
481 assertThat(noPath.getMessage(), containsString("No path"));
482 }
483 }
Luca Pretede10c782017-01-05 17:23:08 -0800484
485 /**
486 * Tests if bandwidth resources get allocated correctly. An intent with a
487 * key only is submitted.
488 */
489 @Test
490 public void testBandwidthConstrainedIntentAllocation() {
491 final double bpsTotal = 1000.0;
492
493 String[] hops = {S1, S2, S3};
494
495 final ResourceService resourceService =
496 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
497 final List<Constraint> constraints =
498 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
499
500 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
501 new ConnectPoint(DID_3, PORT_2),
502 constraints);
503
504 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
505
506 compiler.compile(intent, null);
507
508 Key intentKey = intent.key();
509
510 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, intentKey);
511 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, intentKey);
512 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, intentKey);
513 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, intentKey);
514 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, intentKey);
515 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, intentKey);
516
517 Set<ResourceAllocation> expectedresourceAllocations =
518 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
519
520 Set<ResourceAllocation> resourceAllocations =
521 ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
522
523 assertThat(resourceAllocations, hasSize(6));
524 assertEquals(expectedresourceAllocations, resourceAllocations);
525 }
526
527 /**
528 * Tests if bandwidth resources get allocated correctly using the resource
529 * group. An intent with a resource group is submitted.
530 */
531 @Test
532 public void testRGBandwidthConstrainedIntentAllocation() {
533 final double bpsTotal = 1000.0;
534
535 ResourceGroup resourceGroup = ResourceGroup.of(100);
536
537 String[] hops = {S1, S2, S3};
538
539 final ResourceService resourceService =
540 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
541 final List<Constraint> constraints =
542 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
543
544 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
545 new ConnectPoint(DID_3, PORT_2),
546 constraints,
547 resourceGroup);
548
549 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
550
551 compiler.compile(intent, null);
552
553 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, resourceGroup);
554 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, resourceGroup);
555 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, resourceGroup);
556 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, resourceGroup);
557 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, resourceGroup);
558 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, resourceGroup);
559
560 Set<ResourceAllocation> expectedresourceAllocations =
561 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
562
563 Set<ResourceAllocation> resourceAllocations =
564 ImmutableSet.copyOf(resourceService.getResourceAllocations(resourceGroup));
565
566 assertThat(resourceAllocations, hasSize(6));
567 assertEquals(expectedresourceAllocations, resourceAllocations);
568 }
569
570 /**
571 * Tests that bandwidth resources don't get allocated twice if the intent
572 * is submitted twice.
573 */
574 @Test
575 public void testTwoBandwidthConstrainedIntentAllocation() {
576 final double bpsTotal = 1000.0;
577
578 String[] hops = {S1, S2, S3};
579
580 final ResourceService resourceService =
581 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
582 final List<Constraint> constraints =
583 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
584
585 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
586 new ConnectPoint(DID_3, PORT_2),
587 constraints);
588
589 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
590
591 compiler.compile(intent, null);
592
593 // Resubmit the same intent
594 compiler.compile(intent, null);
595
596 Key intentKey = intent.key();
597
598 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, intentKey);
599 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, intentKey);
600 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, intentKey);
601 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, intentKey);
602 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, intentKey);
603 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, intentKey);
604
605 Set<ResourceAllocation> expectedresourceAllocations =
606 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
607
608 Set<ResourceAllocation> resourceAllocations =
609 ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
610
611 assertThat(resourceAllocations, hasSize(6));
612 assertEquals(expectedresourceAllocations, resourceAllocations);
613 }
614
615 /**
616 * Tests if bandwidth resources get allocated correctly using groups.
617 * An intent asks to allocate bandwidth using the intent key as a reference.
618 * Then, the intent is submitted with the same key and a group set.
619 * Previous allocations should be released and new resources should be
620 * allocated using the group.
621 */
622 @Test
623 public void testKeyRGBandwidthConstrainedIntentAllocation() {
624 final double bpsTotal = 1000.0;
625
626 String[] hops = {S1, S2, S3};
627
628 final ResourceService resourceService =
629 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
630 final List<Constraint> constraints =
631 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
632
633 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
634 new ConnectPoint(DID_3, PORT_2),
635 constraints);
636
637 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
638
639 compiler.compile(intent, null);
640
641 Key intentKey = intent.key();
642
643 ResourceGroup resourceGroup = ResourceGroup.of(100);
644
645 final PointToPointIntent newIntent = makeIntent(intentKey,
646 new ConnectPoint(DID_1, PORT_1),
647 new ConnectPoint(DID_3, PORT_2),
648 constraints,
649 resourceGroup);
650
651 compiler.compile(newIntent, null);
652
653 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, resourceGroup);
654 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, resourceGroup);
655 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, resourceGroup);
656 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, resourceGroup);
657 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, resourceGroup);
658 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, resourceGroup);
659
660 Set<ResourceAllocation> expectedresourceAllocations =
661 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
662
663 Set<ResourceAllocation> resourceAllocations =
664 ImmutableSet.copyOf(resourceService.getResourceAllocations(resourceGroup));
665
666 assertThat(resourceAllocations, hasSize(6));
667 assertEquals(expectedresourceAllocations, resourceAllocations);
668 }
Daniele Moro3a6e1512017-12-22 12:14:44 +0100669
670 /**
671 * Test if a suggested path is correctly applied.
672 */
673 @Test
674 public void testSuggestedPath() {
675 String[] suggestedPathHops = {S1, S3, S4, S5, S6, S8};
676 List<Link> suggestedPath = NetTestTools.createPath(suggestedPathHops).links();
677
678 PointToPointIntent intent = makeIntentSuggestedPath(new ConnectPoint(DID_1, PORT_1),
679 new ConnectPoint(DID_8, PORT_2),
680 suggestedPath);
681
682 String[][] paths = {{S1, S2, S8}, suggestedPathHops};
683 PointToPointIntentCompiler compiler = makeCompilerSuggestedPath(paths);
684
685 List<Intent> result = compiler.compile(intent, null);
686 assertThat(result, is(Matchers.notNullValue()));
687 assertThat(result, hasSize(1));
688 Intent resultIntent = result.get(0);
689 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
690
691 if (resultIntent instanceof LinkCollectionIntent) {
692 LinkCollectionIntent resultLinkIntent = (LinkCollectionIntent) resultIntent;
693 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
694 FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_2));
695 // 5 links for the hops, plus one default link on ingress and egress
696 assertThat(resultLinkIntent.links(), hasSize(suggestedPathHops.length - 1));
697 assertThat(resultLinkIntent.links(), linksHasPath(S1, S3));
698 assertThat(resultLinkIntent.links(), linksHasPath(S3, S4));
699 assertThat(resultLinkIntent.links(), linksHasPath(S4, S5));
700 assertThat(resultLinkIntent.links(), linksHasPath(S5, S6));
701 assertThat(resultLinkIntent.links(), linksHasPath(S6, S8));
702 assertThat(resultLinkIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
703 assertThat(resultLinkIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
704 }
705 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
706 }
707
708 /**
709 * Test that if a suggested path isn't available it applies another available path.
710 */
711 @Test
712 public void testSuggestedPathNotAvailable() {
713 String[] suggestedPathHops = {S1, S3, S8};
714 String[] shortestPath = {S1, S2, S8};
715 List<Link> suggestedPath = NetTestTools.createPath(suggestedPathHops).links();
716
717 PointToPointIntent intent = makeIntentSuggestedPath(new ConnectPoint(DID_1, PORT_1),
718 new ConnectPoint(DID_8, PORT_2),
719 suggestedPath);
720
721 String[][] path = {shortestPath};
722 PointToPointIntentCompiler compiler = makeCompilerSuggestedPath(path);
723
724 List<Intent> result = compiler.compile(intent, null);
725 assertThat(result, is(Matchers.notNullValue()));
726 assertThat(result, hasSize(1));
727 Intent resultIntent = result.get(0);
728 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
729
730 if (resultIntent instanceof LinkCollectionIntent) {
731 LinkCollectionIntent resultLinkIntent = (LinkCollectionIntent) resultIntent;
732 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
733 FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_2));
734 // 5 links for the hops, plus one default link on ingress and egress
735 assertThat(resultLinkIntent.links(), hasSize(shortestPath.length - 1));
736 assertThat(resultLinkIntent.links(), linksHasPath(S1, S2));
737 assertThat(resultLinkIntent.links(), linksHasPath(S2, S8));
738 assertThat(resultLinkIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
739 assertThat(resultLinkIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
740 }
741 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
742 }
743
744 /**
745 * Tests that requests with suggested path
746 * and with sufficient available bandwidth succeed.
747 */
748 @Test
749 public void testSuggestedPathBandwidthConstrainedIntentSuccess() {
750 final double bpsTotal = 1000.0;
751 final double bpsToReserve = 100.0;
752
753 final ResourceService resourceService =
754 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
755 final List<Constraint> constraints =
756 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
757
758 String[] suggestedPathHops = {S1, S4, S5, S3};
759 List<Link> suggestedPath = NetTestTools.createPath(suggestedPathHops).links();
760
761 final PointToPointIntent intent = makeIntentSuggestedPath(
762 new ConnectPoint(DID_1, PORT_1),
763 new ConnectPoint(DID_3, PORT_2),
764 suggestedPath,
765 constraints);
766
767 String[][] hops = {{S1, S2, S3}, suggestedPathHops};
768 final PointToPointIntentCompiler compiler = makeCompilerSuggestedPath(hops,
769 resourceService);
770
771 final List<Intent> compiledIntents = compiler.compile(intent, null);
772
773 assertThat(compiledIntents, Matchers.notNullValue());
774 assertThat(compiledIntents, hasSize(1));
775
776 assertThat("key is inherited",
777 compiledIntents.stream().map(Intent::key).collect(Collectors.toList()),
778 everyItem(is(intent.key())));
779
780 }
781
782 /**
783 * Tests that requests with insufficient available bandwidth fail.
784 */
785 @Test
786 public void testSuggestedPathBandwidthConstrainedIntentFailure() {
787 final double bpsTotal = 10.0;
788
789 final ResourceService resourceService =
790 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
791 final List<Constraint> constraints =
792 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
793
794 String[] suggestedPathHops = {S1, S4, S5, S3};
795 List<Link> suggestedPath = NetTestTools.createPath(suggestedPathHops).links();
796
797 try {
798 final PointToPointIntent intent = makeIntentSuggestedPath(
799 new ConnectPoint(DID_1, PORT_1),
800 new ConnectPoint(DID_3, PORT_2),
801 suggestedPath,
802 constraints);
803
804 String[][] paths = {{S1, S2, S3}, suggestedPathHops};
805 final PointToPointIntentCompiler compiler = makeCompilerSuggestedPath(paths,
806 resourceService);
807
808 compiler.compile(intent, null);
809
810 fail("Point to Point compilation with insufficient bandwidth does "
811 + "not throw exception.");
812 } catch (PathNotFoundException noPath) {
813 assertThat(noPath.getMessage(), containsString("No path"));
814 }
815 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700816}