blob: 238e75d0e9f099f50b7ea5a478ba1d1af0d1fdcc [file] [log] [blame]
Toshio Koide32336182014-10-30 13:02:47 -07001/*
Ray Milkey9a39eca2015-01-05 09:41:01 -08002 * Copyright 2014-2015 Open Networking Laboratory
Toshio Koide32336182014-10-30 13:02:47 -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 */
Thomas Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Toshio Koide32336182014-10-30 13:02:47 -070017
Ray Milkey9a39eca2015-01-05 09:41:01 -080018import java.util.Collection;
Toshio Koide32336182014-10-30 13:02:47 -070019import java.util.HashSet;
20import java.util.Set;
21
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070025import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.AnnotationKeys;
27import org.onosproject.net.Annotations;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.DefaultLink;
31import org.onosproject.net.Link;
Ray Milkey9a39eca2015-01-05 09:41:01 -080032import org.onosproject.net.intent.IntentId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.provider.ProviderId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070034import org.onosproject.net.resource.link.BandwidthResource;
35import org.onosproject.net.resource.link.BandwidthResourceAllocation;
36import org.onosproject.net.resource.link.LambdaResource;
37import org.onosproject.net.resource.link.LambdaResourceAllocation;
38import org.onosproject.net.resource.link.LinkResourceAllocations;
39import org.onosproject.net.resource.link.LinkResourceStore;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.resource.ResourceAllocation;
Ray Milkey9a39eca2015-01-05 09:41:01 -080041import org.onosproject.net.resource.ResourceAllocationException;
42import org.onosproject.net.resource.ResourceRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.resource.ResourceType;
Toshio Koide32336182014-10-30 13:02:47 -070044
Ray Milkey9a39eca2015-01-05 09:41:01 -080045import com.google.common.collect.ImmutableSet;
46
Ray Milkeye97ede92014-11-20 10:43:12 -080047import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertFalse;
49import static org.junit.Assert.assertNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import static org.onosproject.net.DeviceId.deviceId;
51import static org.onosproject.net.Link.Type.DIRECT;
52import static org.onosproject.net.PortNumber.portNumber;
Ray Milkeye97ede92014-11-20 10:43:12 -080053
Toshio Koide32336182014-10-30 13:02:47 -070054/**
55 * Test of the simple LinkResourceStore implementation.
56 */
57public class SimpleLinkResourceStoreTest {
58
59 private LinkResourceStore store;
60 private SimpleLinkResourceStore simpleStore;
61 private Link link1;
62 private Link link2;
63 private Link link3;
64
65 /**
66 * Returns {@link Link} object.
67 *
68 * @param dev1 source device
69 * @param port1 source port
70 * @param dev2 destination device
71 * @param port2 destination port
72 * @return created {@link Link} object
73 */
Ray Milkey9a39eca2015-01-05 09:41:01 -080074 private static Link newLink(String dev1, int port1, String dev2, int port2) {
Toshio Koide8e5e91e2014-11-13 12:27:12 -080075 Annotations annotations = DefaultAnnotations.builder()
76 .set(AnnotationKeys.OPTICAL_WAVES, "80")
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080077 .set(AnnotationKeys.BANDWIDTH, "1000")
Toshio Koide8e5e91e2014-11-13 12:27:12 -080078 .build();
Toshio Koide32336182014-10-30 13:02:47 -070079 return new DefaultLink(
80 new ProviderId("of", "foo"),
81 new ConnectPoint(deviceId(dev1), portNumber(port1)),
82 new ConnectPoint(deviceId(dev2), portNumber(port2)),
Toshio Koide8e5e91e2014-11-13 12:27:12 -080083 DIRECT, annotations);
Toshio Koide32336182014-10-30 13:02:47 -070084 }
85
86 @Before
87 public void setUp() throws Exception {
88 simpleStore = new SimpleLinkResourceStore();
89 simpleStore.activate();
90 store = simpleStore;
91
92 link1 = newLink("of:1", 1, "of:2", 2);
93 link2 = newLink("of:2", 1, "of:3", 2);
94 link3 = newLink("of:3", 1, "of:4", 2);
95 }
96
97 @After
98 public void tearDown() throws Exception {
99 simpleStore.deactivate();
100 }
101
102 /**
103 * Tests constructor and activate method.
104 */
105 @Test
106 public void testConstructorAndActivate() {
107 final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
108 assertNotNull(allAllocations);
109 assertFalse(allAllocations.iterator().hasNext());
110
111 final Iterable<LinkResourceAllocations> linkAllocations =
112 store.getAllocations(link1);
113 assertNotNull(linkAllocations);
114 assertFalse(linkAllocations.iterator().hasNext());
115
116 final Set<ResourceAllocation> res = store.getFreeResources(link2);
117 assertNotNull(res);
118 }
119
120 /**
121 * Picks up and returns one of bandwidth allocations from a given set.
122 *
123 * @param resources the set of {@link ResourceAllocation}s
124 * @return {@link BandwidthResourceAllocation} object if found, null
125 * otherwise
126 */
127 private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
128 for (ResourceAllocation res : resources) {
129 if (res.type() == ResourceType.BANDWIDTH) {
130 return ((BandwidthResourceAllocation) res);
131 }
132 }
133 return null;
134 }
135
136 /**
137 * Returns all lambda allocations from a given set.
138 *
139 * @param resources the set of {@link ResourceAllocation}s
140 * @return a set of {@link LambdaResourceAllocation} objects
141 */
142 private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
143 Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
144 for (ResourceAllocation res : resources) {
145 if (res.type() == ResourceType.LAMBDA) {
146 lambdaResources.add((LambdaResourceAllocation) res);
147 }
148 }
149 return lambdaResources;
150 }
151
152 /**
153 * Tests initial free bandwidth for a link.
154 */
155 @Test
156 public void testInitialBandwidth() {
157 final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
158 assertNotNull(freeRes);
159
160 final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
161 assertNotNull(alloc);
162
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700163 assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
Toshio Koide32336182014-10-30 13:02:47 -0700164 }
165
166 /**
167 * Tests initial free lambda for a link.
168 */
169 @Test
170 public void testInitialLambdas() {
171 final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
172 assertNotNull(freeRes);
173
174 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
175 assertNotNull(res);
Toshio Koide8e5e91e2014-11-13 12:27:12 -0800176 assertEquals(80, res.size());
Toshio Koide32336182014-10-30 13:02:47 -0700177 }
Ray Milkey9a39eca2015-01-05 09:41:01 -0800178
179 public static class MockLinkResourceBandwidthAllocations implements LinkResourceAllocations {
180 final double allocationAmount;
181
182 MockLinkResourceBandwidthAllocations(Double allocationAmount) {
183 this.allocationAmount = allocationAmount;
184 }
185 @Override
186 public Set<ResourceAllocation> getResourceAllocation(Link link) {
187 final ResourceAllocation allocation =
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700188 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(allocationAmount)));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800189 final Set<ResourceAllocation> allocations = new HashSet<>();
190 allocations.add(allocation);
191 return allocations;
192 }
193
194 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700195 public IntentId intentId() {
Ray Milkey9a39eca2015-01-05 09:41:01 -0800196 return null;
197 }
198
199 @Override
200 public Collection<Link> links() {
201 return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
202 }
203
204 @Override
205 public Set<ResourceRequest> resources() {
206 return null;
207 }
208
209 @Override
210 public ResourceType type() {
211 return null;
212 }
213 }
214
215 public static class MockLinkResourceLambdaAllocations implements LinkResourceAllocations {
216 final int allocatedLambda;
217
218 MockLinkResourceLambdaAllocations(int allocatedLambda) {
219 this.allocatedLambda = allocatedLambda;
220 }
221 @Override
222 public Set<ResourceAllocation> getResourceAllocation(Link link) {
223 final ResourceAllocation allocation =
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700224 new LambdaResourceAllocation(LambdaResource.valueOf(allocatedLambda));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800225 final Set<ResourceAllocation> allocations = new HashSet<>();
226 allocations.add(allocation);
227 return allocations;
228 }
229
230 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700231 public IntentId intentId() {
Ray Milkey9a39eca2015-01-05 09:41:01 -0800232 return null;
233 }
234
235 @Override
236 public Collection<Link> links() {
237 return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
238 }
239
240 @Override
241 public Set<ResourceRequest> resources() {
242 return null;
243 }
244
245 @Override
246 public ResourceType type() {
247 return null;
248 }
249 }
250
251 /**
252 * Tests a successful bandwidth allocation.
253 */
254 @Test
255 public void testSuccessfulBandwidthAllocation() {
256 final LinkResourceAllocations allocations =
257 new MockLinkResourceBandwidthAllocations(900.0);
258 store.allocateResources(allocations);
259 }
260
261 /**
262 * Tests an unsuccessful bandwidth allocation.
263 */
264 @Test
265 public void testUnsuccessfulBandwidthAllocation() {
266 final LinkResourceAllocations allocations =
267 new MockLinkResourceBandwidthAllocations(2000000000.0);
268 boolean gotException = false;
269 try {
270 store.allocateResources(allocations);
271 } catch (ResourceAllocationException rae) {
272 assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
273 gotException = true;
274 }
275 assertEquals(true, gotException);
276 }
277
278 /**
279 * Tests a successful lambda allocation.
280 */
281 @Test
282 public void testSuccessfulLambdaAllocation() {
283 final LinkResourceAllocations allocations =
284 new MockLinkResourceLambdaAllocations(1);
285 store.allocateResources(allocations);
286 }
287
288 /**
289 * Tests an unsuccessful lambda allocation.
290 */
291 @Test
292 public void testUnsuccessfulLambdaAllocation() {
293 final LinkResourceAllocations allocations =
294 new MockLinkResourceLambdaAllocations(1);
295 store.allocateResources(allocations);
296
297 boolean gotException = false;
298
299 try {
300 store.allocateResources(allocations);
301 } catch (ResourceAllocationException rae) {
302 assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
303 gotException = true;
304 }
305 assertEquals(true, gotException);
306 }
Toshio Koide32336182014-10-30 13:02:47 -0700307}