blob: 56b6cc2ce0abeb623e7eda1faa8609d5acbb72d7 [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 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;
Luca Pretede10c782017-01-05 17:23:08 -080028import org.onosproject.net.PortNumber;
29import org.onosproject.net.ResourceGroup;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080033import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentTestsMocks;
Luca Pretede10c782017-01-05 17:23:08 -080036import org.onosproject.net.intent.Key;
Pier Ventreffe88d62016-10-13 14:34:40 -070037import org.onosproject.net.intent.LinkCollectionIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080039import org.onosproject.net.intent.constraint.BandwidthConstraint;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080040import org.onosproject.net.intent.impl.PathNotFoundException;
Luca Pretede10c782017-01-05 17:23:08 -080041import org.onosproject.net.resource.ContinuousResource;
42import org.onosproject.net.resource.MockResourceService;
43import org.onosproject.net.resource.ResourceAllocation;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080044import org.onosproject.net.resource.ResourceService;
Luca Pretede10c782017-01-05 17:23:08 -080045import org.onosproject.net.resource.Resources;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070046
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070047import java.util.Collections;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070048import java.util.List;
Pier Ventreffe88d62016-10-13 14:34:40 -070049import java.util.Set;
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070050import java.util.stream.Collectors;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070051
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080052import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070053import static org.hamcrest.MatcherAssert.assertThat;
Luca Pretede10c782017-01-05 17:23:08 -080054import static org.hamcrest.Matchers.*;
55import static org.junit.Assert.assertEquals;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080056import static org.junit.Assert.fail;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070058
59/**
60 * Unit tests for the HostToHost intent compiler.
61 */
Ray Milkey37f6a382014-11-25 14:54:42 -080062public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070063
Thomas Vachuskab97cf282014-10-20 23:31:12 -070064 private static final ApplicationId APPID = new TestApplicationId("foo");
65
Luca Pretede10c782017-01-05 17:23:08 -080066 private static final String S1 = "s1";
67 private static final String S2 = "s2";
68 private static final String S3 = "s3";
69 private static final String S4 = "s4";
70 private static final String S5 = "s5";
71 private static final String S6 = "s6";
72 private static final String S7 = "s7";
73 private static final String S8 = "s8";
74
75 private static final DeviceId DID_1 = DeviceId.deviceId("of:" + S1);
76 private static final DeviceId DID_2 = DeviceId.deviceId("of:" + S2);
77 private static final DeviceId DID_3 = DeviceId.deviceId("of:" + S3);
78 private static final DeviceId DID_8 = DeviceId.deviceId("of:" + S8);
79
80 private static final PortNumber PORT_1 = PortNumber.portNumber(1);
81 private static final PortNumber PORT_2 = PortNumber.portNumber(2);
82
83 private static final double BPS_TO_RESERVE = 100.0;
84
85 private static final ContinuousResource RESOURCE_SW1_P1 =
86 Resources.continuous(DID_1, PORT_1, Bandwidth.class)
87 .resource(BPS_TO_RESERVE);
88 private static final ContinuousResource RESOURCE_SW1_P2 =
89 Resources.continuous(DID_1, PORT_2, Bandwidth.class)
90 .resource(BPS_TO_RESERVE);
91 private static final ContinuousResource RESOURCE_SW2_P1 =
92 Resources.continuous(DID_2, PORT_1, Bandwidth.class)
93 .resource(BPS_TO_RESERVE);
94 private static final ContinuousResource RESOURCE_SW2_P2 =
95 Resources.continuous(DID_2, PORT_2, Bandwidth.class)
96 .resource(BPS_TO_RESERVE);
97 private static final ContinuousResource RESOURCE_SW3_P1 =
98 Resources.continuous(DID_3, PORT_1, Bandwidth.class)
99 .resource(BPS_TO_RESERVE);
100 private static final ContinuousResource RESOURCE_SW3_P2 =
101 Resources.continuous(DID_3, PORT_2, Bandwidth.class)
102 .resource(BPS_TO_RESERVE);
103
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700104 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
105 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
106
107 /**
108 * Creates a PointToPoint intent based on ingress and egress device Ids.
109 *
Luca Pretede10c782017-01-05 17:23:08 -0800110 * @param ingress the ingress connect point
111 * @param egress the egress connect point
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700112 * @return PointToPointIntent for the two devices
113 */
Luca Pretede10c782017-01-05 17:23:08 -0800114 private PointToPointIntent makeIntent(ConnectPoint ingress, ConnectPoint egress) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700115 return PointToPointIntent.builder()
116 .appId(APPID)
117 .selector(selector)
118 .treatment(treatment)
Luca Pretede10c782017-01-05 17:23:08 -0800119 .filteredIngressPoint(new FilteredConnectPoint(ingress))
120 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700121 .build();
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700122 }
123
124 /**
Luca Pretede10c782017-01-05 17:23:08 -0800125 * Creates a PointToPoint intent based on ingress and egress deviceIds and
126 * constraints.
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800127 *
Luca Pretede10c782017-01-05 17:23:08 -0800128 * @param ingress the ingress connect point
129 * @param egress the egress connect point
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800130 * @param constraints constraints
Luca Pretede10c782017-01-05 17:23:08 -0800131 * @return the PointToPointIntent connecting the two connect points with
132 * constraints
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800133 */
Luca Pretede10c782017-01-05 17:23:08 -0800134 private PointToPointIntent makeIntent(ConnectPoint ingress,
135 ConnectPoint egress,
136 List<Constraint> constraints) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700137 return PointToPointIntent.builder()
138 .appId(APPID)
139 .selector(selector)
140 .treatment(treatment)
Luca Pretede10c782017-01-05 17:23:08 -0800141 .filteredIngressPoint(new FilteredConnectPoint(ingress))
142 .filteredEgressPoint(new FilteredConnectPoint(egress))
143 .constraints(constraints)
144 .build();
145 }
146
147 /**
148 * Creates a PointToPoint intent based on ingress and egress deviceIds,
149 * constraints and a resource group.
150 *
151 * @param ingress the ingress connect point
152 * @param egress the egress connect point
153 * @param constraints constraints
154 * @param resourceGroup the resource group
155 * @return the PointToPointIntent connecting the two connect points with
156 * constraints
157 */
158 private PointToPointIntent makeIntent(ConnectPoint ingress,
159 ConnectPoint egress,
160 List<Constraint> constraints,
161 ResourceGroup resourceGroup) {
162 return PointToPointIntent.builder()
163 .appId(APPID)
164 .resourceGroup(resourceGroup)
165 .selector(selector)
166 .treatment(treatment)
167 .filteredIngressPoint(new FilteredConnectPoint(ingress))
168 .filteredEgressPoint(new FilteredConnectPoint(egress))
169 .constraints(constraints)
170 .build();
171 }
172
173 /**
174 * Creates a PointToPoint intent based on an intent key, ingress and egress
175 * deviceIds, constraints and a resource group.
176 *
177 * @param key the intent key
178 * @param ingress the ingress connect point
179 * @param egress the egress connect point
180 * @param constraints constraints
181 * @param resourceGroup the resource group
182 * @return the PointToPointIntent connecting the two connect points with
183 * constraints
184 */
185 private PointToPointIntent makeIntent(Key key,
186 ConnectPoint ingress,
187 ConnectPoint egress,
188 List<Constraint> constraints,
189 ResourceGroup resourceGroup) {
190 return PointToPointIntent.builder()
191 .appId(APPID)
192 .key(key)
193 .resourceGroup(resourceGroup)
194 .selector(selector)
195 .treatment(treatment)
196 .filteredIngressPoint(new FilteredConnectPoint(ingress))
197 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700198 .constraints(constraints)
199 .build();
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800200 }
201
202 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700203 * Creates a compiler for HostToHost intents.
204 *
205 * @param hops string array describing the path hops to use when compiling
206 * @return HostToHost intent compiler
207 */
208 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Luca Pretede10c782017-01-05 17:23:08 -0800209 return makeCompiler(hops, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700210 }
211
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800212 /**
213 * Creates a point to point intent compiler for a three switch linear
214 * topology.
215 *
216 * @param resourceService service to use for resource allocation requests
217 * @return point to point compiler
218 */
Luca Pretede10c782017-01-05 17:23:08 -0800219 private PointToPointIntentCompiler makeCompiler(String[] hops,
220 ResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800221 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800222 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Luca Pretede10c782017-01-05 17:23:08 -0800223
224 if (resourceService == null) {
225 compiler.resourceService = new MockResourceService();
226 } else {
227 compiler.resourceService = resourceService;
228 }
229
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800230 return compiler;
231 }
232
233 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700234 * Tests a pair of devices in an 8 hop path, forward direction.
235 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800236 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700237 public void testForwardPathCompilation() {
238
Luca Pretede10c782017-01-05 17:23:08 -0800239 PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
240 new ConnectPoint(DID_8, PORT_1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700241
Luca Pretede10c782017-01-05 17:23:08 -0800242 String[] hops = {S1, S2, S3, S4, S5, S6, S7, S8};
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700243 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700244
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800245 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700246 assertThat(result, is(Matchers.notNullValue()));
247 assertThat(result, hasSize(1));
248 Intent forwardResultIntent = result.get(0);
Pier Ventreffe88d62016-10-13 14:34:40 -0700249 assertThat(forwardResultIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700250
Pier Ventreffe88d62016-10-13 14:34:40 -0700251 if (forwardResultIntent instanceof LinkCollectionIntent) {
252 LinkCollectionIntent forwardIntent = (LinkCollectionIntent) forwardResultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800253 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
254 FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700255 // 7 links for the hops, plus one default lnk on ingress and egress
Pier Ventreffe88d62016-10-13 14:34:40 -0700256 assertThat(forwardIntent.links(), hasSize(hops.length - 1));
Luca Pretede10c782017-01-05 17:23:08 -0800257 assertThat(forwardIntent.links(), linksHasPath(S1, S2));
258 assertThat(forwardIntent.links(), linksHasPath(S2, S3));
259 assertThat(forwardIntent.links(), linksHasPath(S3, S4));
260 assertThat(forwardIntent.links(), linksHasPath(S4, S5));
261 assertThat(forwardIntent.links(), linksHasPath(S5, S6));
262 assertThat(forwardIntent.links(), linksHasPath(S6, S7));
263 assertThat(forwardIntent.links(), linksHasPath(S7, S8));
Pier Ventreffe88d62016-10-13 14:34:40 -0700264 assertThat(forwardIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
265 assertThat(forwardIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700266 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700267 assertThat("key is inherited", forwardResultIntent.key(), is(intent.key()));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700268 }
269
270 /**
271 * Tests a pair of devices in an 8 hop path, forward direction.
272 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800273 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700274 public void testReversePathCompilation() {
275
Luca Pretede10c782017-01-05 17:23:08 -0800276 PointToPointIntent intent = makeIntent(new ConnectPoint(DID_8, PORT_1),
277 new ConnectPoint(DID_1, PORT_1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700278
Luca Pretede10c782017-01-05 17:23:08 -0800279 String[] hops = {S1, S2, S3, S4, S5, S6, S7, S8};
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700280 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700281
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800282 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700283 assertThat(result, is(Matchers.notNullValue()));
284 assertThat(result, hasSize(1));
285 Intent reverseResultIntent = result.get(0);
Pier Ventreffe88d62016-10-13 14:34:40 -0700286 assertThat(reverseResultIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700287
Pier Ventreffe88d62016-10-13 14:34:40 -0700288 if (reverseResultIntent instanceof LinkCollectionIntent) {
289 LinkCollectionIntent reverseLinkCollectionIntent = (LinkCollectionIntent) reverseResultIntent;
Luca Pretede10c782017-01-05 17:23:08 -0800290 FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
291 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1));
Pier Ventreffe88d62016-10-13 14:34:40 -0700292 assertThat(reverseLinkCollectionIntent.links(), hasSize(hops.length - 1));
Luca Pretede10c782017-01-05 17:23:08 -0800293 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S2, S1));
294 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S3, S2));
295 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S4, S3));
296 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S5, S4));
297 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S6, S5));
298 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S7, S6));
299 assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S8, S7));
Pier Ventreffe88d62016-10-13 14:34:40 -0700300 assertThat(reverseLinkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
301 assertThat(reverseLinkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700302 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700303 assertThat("key is inherited", reverseResultIntent.key(), is(intent.key()));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700304 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800305
306 /**
Luca Pretede10c782017-01-05 17:23:08 -0800307 * Tests the compilation of an intent which designates two different ports
308 * on the same switch.
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800309 */
310 @Test
311 public void testSameSwitchDifferentPortsIntentCompilation() {
Luca Pretede10c782017-01-05 17:23:08 -0800312 FilteredConnectPoint src =
313 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1));
314 FilteredConnectPoint dst =
315 new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800316
Luca Pretede10c782017-01-05 17:23:08 -0800317 PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
318 new ConnectPoint(DID_1, PORT_2));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800319
Luca Pretede10c782017-01-05 17:23:08 -0800320 String[] hops = {S1};
321 PointToPointIntentCompiler compiler = makeCompiler(hops);
322
323 List<Intent> compiled = compiler.compile(intent, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800324
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700325 assertThat("key is inherited",
326 compiled.stream().map(Intent::key).collect(Collectors.toList()),
327 everyItem(is(intent.key())));
328
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800329 assertThat(compiled, hasSize(1));
Pier Ventreffe88d62016-10-13 14:34:40 -0700330 assertThat(compiled.get(0), is(instanceOf(LinkCollectionIntent.class)));
331 LinkCollectionIntent linkCollectionIntent = (LinkCollectionIntent) compiled.get(0);
332 Set<Link> links = linkCollectionIntent.links();
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800333
Pier Ventreffe88d62016-10-13 14:34:40 -0700334 assertThat(links, hasSize(0));
335 assertThat(linkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(src)));
336 assertThat(linkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(dst)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800337 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800338
339 /**
340 * Tests that requests with sufficient available bandwidth succeed.
341 */
342 @Test
343 public void testBandwidthConstrainedIntentSuccess() {
Luca Pretede10c782017-01-05 17:23:08 -0800344 final double bpsTotal = 1000.0;
345 final double bpsToReserve = 100.0;
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800346
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800347 final ResourceService resourceService =
Luca Pretede10c782017-01-05 17:23:08 -0800348 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700349 final List<Constraint> constraints =
Luca Pretede10c782017-01-05 17:23:08 -0800350 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800351
Luca Pretede10c782017-01-05 17:23:08 -0800352 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
353 new ConnectPoint(DID_3, PORT_2),
354 constraints);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800355
Luca Pretede10c782017-01-05 17:23:08 -0800356 String[] hops = {S1, S2, S3};
357 final PointToPointIntentCompiler compiler = makeCompiler(hops,
358 resourceService);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800359
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800360 final List<Intent> compiledIntents = compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800361
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800362 assertThat(compiledIntents, Matchers.notNullValue());
363 assertThat(compiledIntents, hasSize(1));
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700364
365 assertThat("key is inherited",
366 compiledIntents.stream().map(Intent::key).collect(Collectors.toList()),
367 everyItem(is(intent.key())));
368
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800369 }
370
371 /**
372 * Tests that requests with insufficient available bandwidth fail.
373 */
374 @Test
375 public void testBandwidthConstrainedIntentFailure() {
Luca Pretede10c782017-01-05 17:23:08 -0800376 final double bpsTotal = 10.0;
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800377
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800378 final ResourceService resourceService =
Luca Pretede10c782017-01-05 17:23:08 -0800379 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700380 final List<Constraint> constraints =
Luca Pretede10c782017-01-05 17:23:08 -0800381 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800382
383 try {
Luca Pretede10c782017-01-05 17:23:08 -0800384 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
385 new ConnectPoint(DID_3, PORT_2),
386 constraints);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800387
Luca Pretede10c782017-01-05 17:23:08 -0800388 String[] hops = {S1, S2, S3};
389 final PointToPointIntentCompiler compiler = makeCompiler(hops,
390 resourceService);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800391
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800392 compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800393
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800394 fail("Point to Point compilation with insufficient bandwidth does "
395 + "not throw exception.");
396 } catch (PathNotFoundException noPath) {
397 assertThat(noPath.getMessage(), containsString("No path"));
398 }
399 }
Luca Pretede10c782017-01-05 17:23:08 -0800400
401 /**
402 * Tests if bandwidth resources get allocated correctly. An intent with a
403 * key only is submitted.
404 */
405 @Test
406 public void testBandwidthConstrainedIntentAllocation() {
407 final double bpsTotal = 1000.0;
408
409 String[] hops = {S1, S2, S3};
410
411 final ResourceService resourceService =
412 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
413 final List<Constraint> constraints =
414 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
415
416 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
417 new ConnectPoint(DID_3, PORT_2),
418 constraints);
419
420 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
421
422 compiler.compile(intent, null);
423
424 Key intentKey = intent.key();
425
426 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, intentKey);
427 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, intentKey);
428 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, intentKey);
429 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, intentKey);
430 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, intentKey);
431 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, intentKey);
432
433 Set<ResourceAllocation> expectedresourceAllocations =
434 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
435
436 Set<ResourceAllocation> resourceAllocations =
437 ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
438
439 assertThat(resourceAllocations, hasSize(6));
440 assertEquals(expectedresourceAllocations, resourceAllocations);
441 }
442
443 /**
444 * Tests if bandwidth resources get allocated correctly using the resource
445 * group. An intent with a resource group is submitted.
446 */
447 @Test
448 public void testRGBandwidthConstrainedIntentAllocation() {
449 final double bpsTotal = 1000.0;
450
451 ResourceGroup resourceGroup = ResourceGroup.of(100);
452
453 String[] hops = {S1, S2, S3};
454
455 final ResourceService resourceService =
456 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
457 final List<Constraint> constraints =
458 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
459
460 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
461 new ConnectPoint(DID_3, PORT_2),
462 constraints,
463 resourceGroup);
464
465 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
466
467 compiler.compile(intent, null);
468
469 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, resourceGroup);
470 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, resourceGroup);
471 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, resourceGroup);
472 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, resourceGroup);
473 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, resourceGroup);
474 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, resourceGroup);
475
476 Set<ResourceAllocation> expectedresourceAllocations =
477 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
478
479 Set<ResourceAllocation> resourceAllocations =
480 ImmutableSet.copyOf(resourceService.getResourceAllocations(resourceGroup));
481
482 assertThat(resourceAllocations, hasSize(6));
483 assertEquals(expectedresourceAllocations, resourceAllocations);
484 }
485
486 /**
487 * Tests that bandwidth resources don't get allocated twice if the intent
488 * is submitted twice.
489 */
490 @Test
491 public void testTwoBandwidthConstrainedIntentAllocation() {
492 final double bpsTotal = 1000.0;
493
494 String[] hops = {S1, S2, S3};
495
496 final ResourceService resourceService =
497 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
498 final List<Constraint> constraints =
499 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
500
501 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
502 new ConnectPoint(DID_3, PORT_2),
503 constraints);
504
505 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
506
507 compiler.compile(intent, null);
508
509 // Resubmit the same intent
510 compiler.compile(intent, null);
511
512 Key intentKey = intent.key();
513
514 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, intentKey);
515 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, intentKey);
516 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, intentKey);
517 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, intentKey);
518 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, intentKey);
519 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, intentKey);
520
521 Set<ResourceAllocation> expectedresourceAllocations =
522 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
523
524 Set<ResourceAllocation> resourceAllocations =
525 ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey));
526
527 assertThat(resourceAllocations, hasSize(6));
528 assertEquals(expectedresourceAllocations, resourceAllocations);
529 }
530
531 /**
532 * Tests if bandwidth resources get allocated correctly using groups.
533 * An intent asks to allocate bandwidth using the intent key as a reference.
534 * Then, the intent is submitted with the same key and a group set.
535 * Previous allocations should be released and new resources should be
536 * allocated using the group.
537 */
538 @Test
539 public void testKeyRGBandwidthConstrainedIntentAllocation() {
540 final double bpsTotal = 1000.0;
541
542 String[] hops = {S1, S2, S3};
543
544 final ResourceService resourceService =
545 MockResourceService.makeCustomBandwidthResourceService(bpsTotal);
546 final List<Constraint> constraints =
547 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE)));
548
549 final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1),
550 new ConnectPoint(DID_3, PORT_2),
551 constraints);
552
553 PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
554
555 compiler.compile(intent, null);
556
557 Key intentKey = intent.key();
558
559 ResourceGroup resourceGroup = ResourceGroup.of(100);
560
561 final PointToPointIntent newIntent = makeIntent(intentKey,
562 new ConnectPoint(DID_1, PORT_1),
563 new ConnectPoint(DID_3, PORT_2),
564 constraints,
565 resourceGroup);
566
567 compiler.compile(newIntent, null);
568
569 ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, resourceGroup);
570 ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, resourceGroup);
571 ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, resourceGroup);
572 ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, resourceGroup);
573 ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, resourceGroup);
574 ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, resourceGroup);
575
576 Set<ResourceAllocation> expectedresourceAllocations =
577 ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix);
578
579 Set<ResourceAllocation> resourceAllocations =
580 ImmutableSet.copyOf(resourceService.getResourceAllocations(resourceGroup));
581
582 assertThat(resourceAllocations, hasSize(6));
583 assertEquals(expectedresourceAllocations, resourceAllocations);
584 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700585}