Ray Milkey

ONOS-423 - Throw a specific exception when resources are exhausted

Create an exception to throw when no resources are available
Simple, Hazelcast and Distributed link resource stores throw ResourceAllocationException
Unit tests for successful and unsuccessful bandwidth and lambda allocations

Change-Id: If062d10d2233935dd59efabfa5f37a446e275a5b
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.resource;
17 +
18 +/**
19 + * Exception thrown for resource allocation errors.
20 + */
21 +public class ResourceAllocationException extends ResourceException {
22 + public ResourceAllocationException() {
23 + super();
24 + }
25 +
26 + public ResourceAllocationException(String message) {
27 + super(message);
28 + }
29 +
30 + public ResourceAllocationException(String message, Throwable cause) {
31 + super(message, cause);
32 + }
33 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.resource;
17 +
18 +/**
19 + * Represents a resource related error.
20 + */
21 +public class ResourceException extends RuntimeException {
22 +
23 + /**
24 + * Constructs an exception with no message and no underlying cause.
25 + */
26 + public ResourceException() {
27 + }
28 +
29 + /**
30 + * Constructs an exception with the specified message.
31 + *
32 + * @param message the message describing the specific nature of the error
33 + */
34 + public ResourceException(String message) {
35 + super(message);
36 + }
37 +
38 + /**
39 + * Constructs an exception with the specified message and the underlying cause.
40 + *
41 + * @param message the message describing the specific nature of the error
42 + * @param cause the underlying cause of this error
43 + */
44 + public ResourceException(String message, Throwable cause) {
45 + super(message, cause);
46 + }
47 +
48 +}
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -29,6 +29,7 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -29,6 +29,7 @@ import org.apache.felix.scr.annotations.Deactivate;
29 import org.apache.felix.scr.annotations.Reference; 29 import org.apache.felix.scr.annotations.Reference;
30 import org.apache.felix.scr.annotations.ReferenceCardinality; 30 import org.apache.felix.scr.annotations.ReferenceCardinality;
31 import org.apache.felix.scr.annotations.Service; 31 import org.apache.felix.scr.annotations.Service;
32 +import org.onlab.util.PositionalParameterStringFormatter;
32 import org.onosproject.net.AnnotationKeys; 33 import org.onosproject.net.AnnotationKeys;
33 import org.onosproject.net.Link; 34 import org.onosproject.net.Link;
34 import org.onosproject.net.LinkKey; 35 import org.onosproject.net.LinkKey;
...@@ -42,6 +43,7 @@ import org.onosproject.net.resource.LinkResourceAllocations; ...@@ -42,6 +43,7 @@ import org.onosproject.net.resource.LinkResourceAllocations;
42 import org.onosproject.net.resource.LinkResourceEvent; 43 import org.onosproject.net.resource.LinkResourceEvent;
43 import org.onosproject.net.resource.LinkResourceStore; 44 import org.onosproject.net.resource.LinkResourceStore;
44 import org.onosproject.net.resource.ResourceAllocation; 45 import org.onosproject.net.resource.ResourceAllocation;
46 +import org.onosproject.net.resource.ResourceAllocationException;
45 import org.onosproject.net.resource.ResourceType; 47 import org.onosproject.net.resource.ResourceType;
46 import org.onosproject.store.serializers.KryoSerializer; 48 import org.onosproject.store.serializers.KryoSerializer;
47 import org.onosproject.store.serializers.StoreSerializer; 49 import org.onosproject.store.serializers.StoreSerializer;
...@@ -384,19 +386,26 @@ public class DistributedLinkResourceStore implements LinkResourceStore { ...@@ -384,19 +386,26 @@ public class DistributedLinkResourceStore implements LinkResourceStore {
384 BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next(); 386 BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next();
385 double bwLeft = bw.bandwidth().toDouble(); 387 double bwLeft = bw.bandwidth().toDouble();
386 bwLeft -= ((BandwidthResourceAllocation) req).bandwidth().toDouble(); 388 bwLeft -= ((BandwidthResourceAllocation) req).bandwidth().toDouble();
389 + BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req);
387 if (bwLeft < 0) { 390 if (bwLeft < 0) {
388 - checkState(bwLeft >= 0, 391 + throw new ResourceAllocationException(
389 - "There's no Bandwidth left on %s. %s", 392 + PositionalParameterStringFormatter.format(
390 - link, bwLeft); 393 + "Unable to allocate bandwidth for link {} "
394 + + " requested amount is {} current allocation is {}",
395 + link,
396 + bwReq.bandwidth().toDouble(),
397 + bw));
391 } 398 }
392 } else if (req instanceof LambdaResourceAllocation) { 399 } else if (req instanceof LambdaResourceAllocation) {
393 - 400 + final LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req;
394 // check if allocation should be accepted 401 // check if allocation should be accepted
395 if (!avail.contains(req)) { 402 if (!avail.contains(req)) {
396 // requested lambda was not available 403 // requested lambda was not available
397 - checkState(avail.contains(req), 404 + throw new ResourceAllocationException(
398 - "Allocating %s on %s failed", 405 + PositionalParameterStringFormatter.format(
399 - req, link); 406 + "Unable to allocate lambda for link {} lamdba is {}",
407 + link,
408 + lambdaAllocation.lambda().toInt()));
400 } 409 }
401 } 410 }
402 } 411 }
......
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -30,6 +30,7 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -30,6 +30,7 @@ import org.apache.felix.scr.annotations.Deactivate;
30 import org.apache.felix.scr.annotations.Reference; 30 import org.apache.felix.scr.annotations.Reference;
31 import org.apache.felix.scr.annotations.ReferenceCardinality; 31 import org.apache.felix.scr.annotations.ReferenceCardinality;
32 import org.apache.felix.scr.annotations.Service; 32 import org.apache.felix.scr.annotations.Service;
33 +import org.onlab.util.PositionalParameterStringFormatter;
33 import org.onosproject.net.AnnotationKeys; 34 import org.onosproject.net.AnnotationKeys;
34 import org.onosproject.net.Link; 35 import org.onosproject.net.Link;
35 import org.onosproject.net.LinkKey; 36 import org.onosproject.net.LinkKey;
...@@ -43,6 +44,7 @@ import org.onosproject.net.resource.LinkResourceAllocations; ...@@ -43,6 +44,7 @@ import org.onosproject.net.resource.LinkResourceAllocations;
43 import org.onosproject.net.resource.LinkResourceEvent; 44 import org.onosproject.net.resource.LinkResourceEvent;
44 import org.onosproject.net.resource.LinkResourceStore; 45 import org.onosproject.net.resource.LinkResourceStore;
45 import org.onosproject.net.resource.ResourceAllocation; 46 import org.onosproject.net.resource.ResourceAllocation;
47 +import org.onosproject.net.resource.ResourceAllocationException;
46 import org.onosproject.net.resource.ResourceType; 48 import org.onosproject.net.resource.ResourceType;
47 import org.onosproject.store.StoreDelegate; 49 import org.onosproject.store.StoreDelegate;
48 import org.onosproject.store.hz.AbstractHazelcastStore; 50 import org.onosproject.store.hz.AbstractHazelcastStore;
...@@ -330,20 +332,27 @@ public class HazelcastLinkResourceStore ...@@ -330,20 +332,27 @@ public class HazelcastLinkResourceStore
330 } 332 }
331 BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next(); 333 BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next();
332 double bwLeft = bw.bandwidth().toDouble(); 334 double bwLeft = bw.bandwidth().toDouble();
333 - bwLeft -= ((BandwidthResourceAllocation) req).bandwidth().toDouble(); 335 + BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req);
336 + bwLeft -= bwReq.bandwidth().toDouble();
334 if (bwLeft < 0) { 337 if (bwLeft < 0) {
335 - checkState(bwLeft >= 0, 338 + throw new ResourceAllocationException(
336 - "There's no Bandwidth left on %s. %s", 339 + PositionalParameterStringFormatter.format(
337 - link, bwLeft); 340 + "Unable to allocate bandwidth for link {} "
341 + + " requested amount is {} current allocation is {}",
342 + link,
343 + bwReq.bandwidth().toDouble(),
344 + bw));
338 } 345 }
339 } else if (req instanceof LambdaResourceAllocation) { 346 } else if (req instanceof LambdaResourceAllocation) {
340 - 347 + LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req;
341 // check if allocation should be accepted 348 // check if allocation should be accepted
342 if (!avail.contains(req)) { 349 if (!avail.contains(req)) {
343 // requested lambda was not available 350 // requested lambda was not available
344 - checkState(avail.contains(req), 351 + throw new ResourceAllocationException(
345 - "Allocating %s on %s failed", 352 + PositionalParameterStringFormatter.format(
346 - req, link); 353 + "Unable to allocate lambda for link {} lamdba is {}",
354 + link,
355 + lambdaAllocation.lambda().toInt()));
347 } 356 }
348 } 357 }
349 } 358 }
......
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -27,17 +27,25 @@ import org.onosproject.net.ConnectPoint; ...@@ -27,17 +27,25 @@ import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.DefaultAnnotations; 27 import org.onosproject.net.DefaultAnnotations;
28 import org.onosproject.net.DefaultLink; 28 import org.onosproject.net.DefaultLink;
29 import org.onosproject.net.Link; 29 import org.onosproject.net.Link;
30 +import org.onosproject.net.intent.IntentId;
30 import org.onosproject.net.provider.ProviderId; 31 import org.onosproject.net.provider.ProviderId;
31 import org.onosproject.net.resource.Bandwidth; 32 import org.onosproject.net.resource.Bandwidth;
32 import org.onosproject.net.resource.BandwidthResourceAllocation; 33 import org.onosproject.net.resource.BandwidthResourceAllocation;
34 +import org.onosproject.net.resource.DefaultLinkResourceAllocations;
35 +import org.onosproject.net.resource.DefaultLinkResourceRequest;
36 +import org.onosproject.net.resource.Lambda;
33 import org.onosproject.net.resource.LambdaResourceAllocation; 37 import org.onosproject.net.resource.LambdaResourceAllocation;
34 import org.onosproject.net.resource.LinkResourceAllocations; 38 import org.onosproject.net.resource.LinkResourceAllocations;
39 +import org.onosproject.net.resource.LinkResourceRequest;
35 import org.onosproject.net.resource.LinkResourceStore; 40 import org.onosproject.net.resource.LinkResourceStore;
36 import org.onosproject.net.resource.ResourceAllocation; 41 import org.onosproject.net.resource.ResourceAllocation;
42 +import org.onosproject.net.resource.ResourceAllocationException;
37 import org.onosproject.net.resource.ResourceType; 43 import org.onosproject.net.resource.ResourceType;
38 import org.onosproject.store.hz.StoreService; 44 import org.onosproject.store.hz.StoreService;
39 import org.onosproject.store.hz.TestStoreManager; 45 import org.onosproject.store.hz.TestStoreManager;
40 46
47 +import com.google.common.collect.ImmutableMap;
48 +import com.google.common.collect.ImmutableSet;
41 import com.hazelcast.config.Config; 49 import com.hazelcast.config.Config;
42 import com.hazelcast.core.Hazelcast; 50 import com.hazelcast.core.Hazelcast;
43 51
...@@ -191,4 +199,103 @@ public class HazelcastLinkResourceStoreTest { ...@@ -191,4 +199,103 @@ public class HazelcastLinkResourceStoreTest {
191 } 199 }
192 200
193 } 201 }
202 +
203 + /**
204 + * Tests a successful bandwidth allocation.
205 + */
206 + @Test
207 + public void testSuccessfulBandwidthAllocation() {
208 + final Link link = newLink("of:1", 1, "of:2", 2);
209 +
210 + final LinkResourceRequest request =
211 + DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
212 + ImmutableSet.of(link))
213 + .build();
214 + final ResourceAllocation allocation =
215 + new BandwidthResourceAllocation(Bandwidth.valueOf(900000.0));
216 + final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
217 +
218 + final LinkResourceAllocations allocations =
219 + new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
220 +
221 + store.allocateResources(allocations);
222 + }
223 +
224 + /**
225 + * Tests a unsuccessful bandwidth allocation.
226 + */
227 + @Test
228 + public void testUnsuccessfulBandwidthAllocation() {
229 + final Link link = newLink("of:1", 1, "of:2", 2);
230 +
231 + final LinkResourceRequest request =
232 + DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
233 + ImmutableSet.of(link))
234 + .build();
235 + final ResourceAllocation allocation =
236 + new BandwidthResourceAllocation(Bandwidth.valueOf(9000000.0));
237 + final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
238 +
239 + final LinkResourceAllocations allocations =
240 + new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
241 +
242 + boolean gotException = false;
243 + try {
244 + store.allocateResources(allocations);
245 + } catch (ResourceAllocationException rae) {
246 + assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
247 + gotException = true;
248 + }
249 + assertEquals(true, gotException);
250 + }
251 +
252 + /**
253 + * Tests a successful bandwidth allocation.
254 + */
255 + @Test
256 + public void testSuccessfulLambdaAllocation() {
257 + final Link link = newLink("of:1", 1, "of:2", 2);
258 +
259 + final LinkResourceRequest request =
260 + DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
261 + ImmutableSet.of(link))
262 + .build();
263 + final ResourceAllocation allocation =
264 + new BandwidthResourceAllocation(Bandwidth.valueOf(900000.0));
265 + final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
266 +
267 + final LinkResourceAllocations allocations =
268 + new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
269 +
270 + store.allocateResources(allocations);
271 + }
272 +
273 + /**
274 + * Tests a unsuccessful bandwidth allocation.
275 + */
276 + @Test
277 + public void testUnsuccessfulLambdaAllocation() {
278 + final Link link = newLink("of:1", 1, "of:2", 2);
279 +
280 + final LinkResourceRequest request =
281 + DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
282 + ImmutableSet.of(link))
283 + .build();
284 + final ResourceAllocation allocation =
285 + new LambdaResourceAllocation(Lambda.valueOf(33));
286 + final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
287 +
288 + final LinkResourceAllocations allocations =
289 + new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
290 + store.allocateResources(allocations);
291 +
292 + boolean gotException = false;
293 + try {
294 + store.allocateResources(allocations);
295 + } catch (ResourceAllocationException rae) {
296 + assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
297 + gotException = true;
298 + }
299 + assertEquals(true, gotException);
300 + }
194 } 301 }
......
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -26,6 +26,7 @@ import org.apache.felix.scr.annotations.Activate; ...@@ -26,6 +26,7 @@ import org.apache.felix.scr.annotations.Activate;
26 import org.apache.felix.scr.annotations.Component; 26 import org.apache.felix.scr.annotations.Component;
27 import org.apache.felix.scr.annotations.Deactivate; 27 import org.apache.felix.scr.annotations.Deactivate;
28 import org.apache.felix.scr.annotations.Service; 28 import org.apache.felix.scr.annotations.Service;
29 +import org.onlab.util.PositionalParameterStringFormatter;
29 import org.onosproject.net.AnnotationKeys; 30 import org.onosproject.net.AnnotationKeys;
30 import org.onosproject.net.Annotations; 31 import org.onosproject.net.Annotations;
31 import org.onosproject.net.Link; 32 import org.onosproject.net.Link;
...@@ -38,6 +39,7 @@ import org.onosproject.net.resource.LinkResourceAllocations; ...@@ -38,6 +39,7 @@ import org.onosproject.net.resource.LinkResourceAllocations;
38 import org.onosproject.net.resource.LinkResourceEvent; 39 import org.onosproject.net.resource.LinkResourceEvent;
39 import org.onosproject.net.resource.LinkResourceStore; 40 import org.onosproject.net.resource.LinkResourceStore;
40 import org.onosproject.net.resource.ResourceAllocation; 41 import org.onosproject.net.resource.ResourceAllocation;
42 +import org.onosproject.net.resource.ResourceAllocationException;
41 import org.onosproject.net.resource.ResourceType; 43 import org.onosproject.net.resource.ResourceType;
42 import org.slf4j.Logger; 44 import org.slf4j.Logger;
43 45
...@@ -143,13 +145,30 @@ public class SimpleLinkResourceStore implements LinkResourceStore { ...@@ -143,13 +145,30 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
143 double requestedBandwidth = 145 double requestedBandwidth =
144 ((BandwidthResourceAllocation) res).bandwidth().toDouble(); 146 ((BandwidthResourceAllocation) res).bandwidth().toDouble();
145 double newBandwidth = ba.bandwidth().toDouble() - requestedBandwidth; 147 double newBandwidth = ba.bandwidth().toDouble() - requestedBandwidth;
146 - checkState(newBandwidth >= 0.0); 148 + if (newBandwidth < 0.0) {
149 + throw new ResourceAllocationException(
150 + PositionalParameterStringFormatter.format(
151 + "Unable to allocate bandwidth for link {} "
152 + + "requested amount is {} current allocation is {}",
153 + link,
154 + requestedBandwidth,
155 + ba));
156 + }
147 freeRes.remove(ba); 157 freeRes.remove(ba);
148 freeRes.add(new BandwidthResourceAllocation( 158 freeRes.add(new BandwidthResourceAllocation(
149 Bandwidth.valueOf(newBandwidth))); 159 Bandwidth.valueOf(newBandwidth)));
150 break; 160 break;
151 case LAMBDA: 161 case LAMBDA:
152 - checkState(freeRes.remove(res)); 162 + final boolean lambdaAvailable = freeRes.remove(res);
163 + if (!lambdaAvailable) {
164 + int requestedLambda =
165 + ((LambdaResourceAllocation) res).lambda().toInt();
166 + throw new ResourceAllocationException(
167 + PositionalParameterStringFormatter.format(
168 + "Unable to allocate lambda for link {} lambda is {}",
169 + link,
170 + requestedLambda));
171 + }
153 break; 172 break;
154 default: 173 default:
155 break; 174 break;
......
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.store.trivial.impl; 16 package org.onosproject.store.trivial.impl;
17 17
18 +import java.util.Collection;
18 import java.util.HashSet; 19 import java.util.HashSet;
19 import java.util.Set; 20 import java.util.Set;
20 21
...@@ -27,15 +28,21 @@ import org.onosproject.net.ConnectPoint; ...@@ -27,15 +28,21 @@ import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.DefaultAnnotations; 28 import org.onosproject.net.DefaultAnnotations;
28 import org.onosproject.net.DefaultLink; 29 import org.onosproject.net.DefaultLink;
29 import org.onosproject.net.Link; 30 import org.onosproject.net.Link;
31 +import org.onosproject.net.intent.IntentId;
30 import org.onosproject.net.provider.ProviderId; 32 import org.onosproject.net.provider.ProviderId;
31 import org.onosproject.net.resource.Bandwidth; 33 import org.onosproject.net.resource.Bandwidth;
32 import org.onosproject.net.resource.BandwidthResourceAllocation; 34 import org.onosproject.net.resource.BandwidthResourceAllocation;
35 +import org.onosproject.net.resource.Lambda;
33 import org.onosproject.net.resource.LambdaResourceAllocation; 36 import org.onosproject.net.resource.LambdaResourceAllocation;
34 import org.onosproject.net.resource.LinkResourceAllocations; 37 import org.onosproject.net.resource.LinkResourceAllocations;
35 import org.onosproject.net.resource.LinkResourceStore; 38 import org.onosproject.net.resource.LinkResourceStore;
36 import org.onosproject.net.resource.ResourceAllocation; 39 import org.onosproject.net.resource.ResourceAllocation;
40 +import org.onosproject.net.resource.ResourceAllocationException;
41 +import org.onosproject.net.resource.ResourceRequest;
37 import org.onosproject.net.resource.ResourceType; 42 import org.onosproject.net.resource.ResourceType;
38 43
44 +import com.google.common.collect.ImmutableSet;
45 +
39 import static org.junit.Assert.assertEquals; 46 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertFalse; 47 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertNotNull; 48 import static org.junit.Assert.assertNotNull;
...@@ -63,7 +70,7 @@ public class SimpleLinkResourceStoreTest { ...@@ -63,7 +70,7 @@ public class SimpleLinkResourceStoreTest {
63 * @param port2 destination port 70 * @param port2 destination port
64 * @return created {@link Link} object 71 * @return created {@link Link} object
65 */ 72 */
66 - private Link newLink(String dev1, int port1, String dev2, int port2) { 73 + private static Link newLink(String dev1, int port1, String dev2, int port2) {
67 Annotations annotations = DefaultAnnotations.builder() 74 Annotations annotations = DefaultAnnotations.builder()
68 .set(AnnotationKeys.OPTICAL_WAVES, "80") 75 .set(AnnotationKeys.OPTICAL_WAVES, "80")
69 .set(AnnotationKeys.BANDWIDTH, "1000000") 76 .set(AnnotationKeys.BANDWIDTH, "1000000")
...@@ -167,4 +174,133 @@ public class SimpleLinkResourceStoreTest { ...@@ -167,4 +174,133 @@ public class SimpleLinkResourceStoreTest {
167 assertNotNull(res); 174 assertNotNull(res);
168 assertEquals(80, res.size()); 175 assertEquals(80, res.size());
169 } 176 }
177 +
178 + public static class MockLinkResourceBandwidthAllocations implements LinkResourceAllocations {
179 + final double allocationAmount;
180 +
181 + MockLinkResourceBandwidthAllocations(Double allocationAmount) {
182 + this.allocationAmount = allocationAmount;
183 + }
184 + @Override
185 + public Set<ResourceAllocation> getResourceAllocation(Link link) {
186 + final ResourceAllocation allocation =
187 + new BandwidthResourceAllocation(Bandwidth.valueOf(allocationAmount));
188 + final Set<ResourceAllocation> allocations = new HashSet<>();
189 + allocations.add(allocation);
190 + return allocations;
191 + }
192 +
193 + @Override
194 + public IntentId intendId() {
195 + return null;
196 + }
197 +
198 + @Override
199 + public Collection<Link> links() {
200 + return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
201 + }
202 +
203 + @Override
204 + public Set<ResourceRequest> resources() {
205 + return null;
206 + }
207 +
208 + @Override
209 + public ResourceType type() {
210 + return null;
211 + }
212 + }
213 +
214 + public static class MockLinkResourceLambdaAllocations implements LinkResourceAllocations {
215 + final int allocatedLambda;
216 +
217 + MockLinkResourceLambdaAllocations(int allocatedLambda) {
218 + this.allocatedLambda = allocatedLambda;
219 + }
220 + @Override
221 + public Set<ResourceAllocation> getResourceAllocation(Link link) {
222 + final ResourceAllocation allocation =
223 + new LambdaResourceAllocation(Lambda.valueOf(allocatedLambda));
224 + final Set<ResourceAllocation> allocations = new HashSet<>();
225 + allocations.add(allocation);
226 + return allocations;
227 + }
228 +
229 + @Override
230 + public IntentId intendId() {
231 + return null;
232 + }
233 +
234 + @Override
235 + public Collection<Link> links() {
236 + return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
237 + }
238 +
239 + @Override
240 + public Set<ResourceRequest> resources() {
241 + return null;
242 + }
243 +
244 + @Override
245 + public ResourceType type() {
246 + return null;
247 + }
248 + }
249 +
250 + /**
251 + * Tests a successful bandwidth allocation.
252 + */
253 + @Test
254 + public void testSuccessfulBandwidthAllocation() {
255 + final LinkResourceAllocations allocations =
256 + new MockLinkResourceBandwidthAllocations(900.0);
257 + store.allocateResources(allocations);
258 + }
259 +
260 + /**
261 + * Tests an unsuccessful bandwidth allocation.
262 + */
263 + @Test
264 + public void testUnsuccessfulBandwidthAllocation() {
265 + final LinkResourceAllocations allocations =
266 + new MockLinkResourceBandwidthAllocations(2000000000.0);
267 + boolean gotException = false;
268 + try {
269 + store.allocateResources(allocations);
270 + } catch (ResourceAllocationException rae) {
271 + assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
272 + gotException = true;
273 + }
274 + assertEquals(true, gotException);
275 + }
276 +
277 + /**
278 + * Tests a successful lambda allocation.
279 + */
280 + @Test
281 + public void testSuccessfulLambdaAllocation() {
282 + final LinkResourceAllocations allocations =
283 + new MockLinkResourceLambdaAllocations(1);
284 + store.allocateResources(allocations);
285 + }
286 +
287 + /**
288 + * Tests an unsuccessful lambda allocation.
289 + */
290 + @Test
291 + public void testUnsuccessfulLambdaAllocation() {
292 + final LinkResourceAllocations allocations =
293 + new MockLinkResourceLambdaAllocations(1);
294 + store.allocateResources(allocations);
295 +
296 + boolean gotException = false;
297 +
298 + try {
299 + store.allocateResources(allocations);
300 + } catch (ResourceAllocationException rae) {
301 + assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
302 + gotException = true;
303 + }
304 + assertEquals(true, gotException);
305 + }
170 } 306 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onlab.util;
17 +
18 +/**
19 + * Allows slf4j style formatting of parameters into a string.
20 + */
21 +public final class PositionalParameterStringFormatter {
22 +
23 + /**
24 + * Hide default constructor.
25 + */
26 + private PositionalParameterStringFormatter() {
27 + }
28 +
29 + /**
30 + * Formats a string using slf4j style positional parameter replacement.
31 + * Instances of "{}" in the source string are replaced in order by the
32 + * specified parameter values as strings.
33 + *
34 + * @param source original string to format
35 + * @param parameters list of parameters that will be substituted
36 + * @return formatted string
37 + */
38 + public static String format(String source, Object... parameters) {
39 + String current = source;
40 + for (Object parameter : parameters) {
41 + if (!current.contains("{}")) {
42 + return current;
43 + }
44 + current = current.replaceFirst("\\{\\}", parameter.toString());
45 + }
46 + return current;
47 + }
48 +}