blob: 6f6d853e424d21f2b1a3190f77cff09a07295cbf [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();
Ray Milkey2693bda2016-01-22 16:08:14 -080079 return DefaultLink.builder()
80 .providerId(new ProviderId("of", "foo"))
81 .src(new ConnectPoint(deviceId(dev1), portNumber(port1)))
82 .dst(new ConnectPoint(deviceId(dev2), portNumber(port2)))
83 .type(DIRECT)
84 .annotations(annotations)
85 .build();
Toshio Koide32336182014-10-30 13:02:47 -070086 }
87
88 @Before
89 public void setUp() throws Exception {
90 simpleStore = new SimpleLinkResourceStore();
91 simpleStore.activate();
92 store = simpleStore;
93
94 link1 = newLink("of:1", 1, "of:2", 2);
95 link2 = newLink("of:2", 1, "of:3", 2);
96 link3 = newLink("of:3", 1, "of:4", 2);
97 }
98
99 @After
100 public void tearDown() throws Exception {
101 simpleStore.deactivate();
102 }
103
104 /**
105 * Tests constructor and activate method.
106 */
107 @Test
108 public void testConstructorAndActivate() {
109 final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
110 assertNotNull(allAllocations);
111 assertFalse(allAllocations.iterator().hasNext());
112
113 final Iterable<LinkResourceAllocations> linkAllocations =
114 store.getAllocations(link1);
115 assertNotNull(linkAllocations);
116 assertFalse(linkAllocations.iterator().hasNext());
117
118 final Set<ResourceAllocation> res = store.getFreeResources(link2);
119 assertNotNull(res);
120 }
121
122 /**
123 * Picks up and returns one of bandwidth allocations from a given set.
124 *
125 * @param resources the set of {@link ResourceAllocation}s
126 * @return {@link BandwidthResourceAllocation} object if found, null
127 * otherwise
128 */
129 private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
130 for (ResourceAllocation res : resources) {
131 if (res.type() == ResourceType.BANDWIDTH) {
132 return ((BandwidthResourceAllocation) res);
133 }
134 }
135 return null;
136 }
137
138 /**
139 * Returns all lambda allocations from a given set.
140 *
141 * @param resources the set of {@link ResourceAllocation}s
142 * @return a set of {@link LambdaResourceAllocation} objects
143 */
144 private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
145 Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
146 for (ResourceAllocation res : resources) {
147 if (res.type() == ResourceType.LAMBDA) {
148 lambdaResources.add((LambdaResourceAllocation) res);
149 }
150 }
151 return lambdaResources;
152 }
153
154 /**
155 * Tests initial free bandwidth for a link.
156 */
157 @Test
158 public void testInitialBandwidth() {
159 final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
160 assertNotNull(freeRes);
161
162 final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
163 assertNotNull(alloc);
164
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700165 assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
Toshio Koide32336182014-10-30 13:02:47 -0700166 }
167
168 /**
169 * Tests initial free lambda for a link.
170 */
171 @Test
172 public void testInitialLambdas() {
173 final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
174 assertNotNull(freeRes);
175
176 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
177 assertNotNull(res);
Toshio Koide8e5e91e2014-11-13 12:27:12 -0800178 assertEquals(80, res.size());
Toshio Koide32336182014-10-30 13:02:47 -0700179 }
Ray Milkey9a39eca2015-01-05 09:41:01 -0800180
181 public static class MockLinkResourceBandwidthAllocations implements LinkResourceAllocations {
182 final double allocationAmount;
183
184 MockLinkResourceBandwidthAllocations(Double allocationAmount) {
185 this.allocationAmount = allocationAmount;
186 }
187 @Override
188 public Set<ResourceAllocation> getResourceAllocation(Link link) {
189 final ResourceAllocation allocation =
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700190 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(allocationAmount)));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800191 final Set<ResourceAllocation> allocations = new HashSet<>();
192 allocations.add(allocation);
193 return allocations;
194 }
195
196 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700197 public IntentId intentId() {
Ray Milkey9a39eca2015-01-05 09:41:01 -0800198 return null;
199 }
200
201 @Override
202 public Collection<Link> links() {
203 return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
204 }
205
206 @Override
207 public Set<ResourceRequest> resources() {
208 return null;
209 }
210
211 @Override
212 public ResourceType type() {
213 return null;
214 }
215 }
216
217 public static class MockLinkResourceLambdaAllocations implements LinkResourceAllocations {
218 final int allocatedLambda;
219
220 MockLinkResourceLambdaAllocations(int allocatedLambda) {
221 this.allocatedLambda = allocatedLambda;
222 }
223 @Override
224 public Set<ResourceAllocation> getResourceAllocation(Link link) {
225 final ResourceAllocation allocation =
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700226 new LambdaResourceAllocation(LambdaResource.valueOf(allocatedLambda));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800227 final Set<ResourceAllocation> allocations = new HashSet<>();
228 allocations.add(allocation);
229 return allocations;
230 }
231
232 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -0700233 public IntentId intentId() {
Ray Milkey9a39eca2015-01-05 09:41:01 -0800234 return null;
235 }
236
237 @Override
238 public Collection<Link> links() {
239 return ImmutableSet.of(newLink("of:1", 1, "of:2", 2));
240 }
241
242 @Override
243 public Set<ResourceRequest> resources() {
244 return null;
245 }
246
247 @Override
248 public ResourceType type() {
249 return null;
250 }
251 }
252
253 /**
254 * Tests a successful bandwidth allocation.
255 */
256 @Test
257 public void testSuccessfulBandwidthAllocation() {
258 final LinkResourceAllocations allocations =
259 new MockLinkResourceBandwidthAllocations(900.0);
260 store.allocateResources(allocations);
261 }
262
263 /**
264 * Tests an unsuccessful bandwidth allocation.
265 */
266 @Test
267 public void testUnsuccessfulBandwidthAllocation() {
268 final LinkResourceAllocations allocations =
269 new MockLinkResourceBandwidthAllocations(2000000000.0);
270 boolean gotException = false;
271 try {
272 store.allocateResources(allocations);
273 } catch (ResourceAllocationException rae) {
274 assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
275 gotException = true;
276 }
277 assertEquals(true, gotException);
278 }
279
280 /**
281 * Tests a successful lambda allocation.
282 */
283 @Test
284 public void testSuccessfulLambdaAllocation() {
285 final LinkResourceAllocations allocations =
286 new MockLinkResourceLambdaAllocations(1);
287 store.allocateResources(allocations);
288 }
289
290 /**
291 * Tests an unsuccessful lambda allocation.
292 */
293 @Test
294 public void testUnsuccessfulLambdaAllocation() {
295 final LinkResourceAllocations allocations =
296 new MockLinkResourceLambdaAllocations(1);
297 store.allocateResources(allocations);
298
299 boolean gotException = false;
300
301 try {
302 store.allocateResources(allocations);
303 } catch (ResourceAllocationException rae) {
304 assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
305 gotException = true;
306 }
307 assertEquals(true, gotException);
308 }
Toshio Koide32336182014-10-30 13:02:47 -0700309}