blob: cc577fe70cbd4c8bb742151f0f4da26030d54bd1 [file] [log] [blame]
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -08001/*
Ray Milkey9a39eca2015-01-05 09:41:01 -08002 * Copyright 2014-2015 Open Networking Laboratory
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.resource.impl;
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -080017
18import java.util.HashSet;
19import java.util.Set;
20
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.AnnotationKeys;
25import org.onosproject.net.Annotations;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.DefaultLink;
29import org.onosproject.net.Link;
Ray Milkey9a39eca2015-01-05 09:41:01 -080030import org.onosproject.net.intent.IntentId;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.provider.ProviderId;
32import org.onosproject.net.resource.Bandwidth;
33import org.onosproject.net.resource.BandwidthResourceAllocation;
Ray Milkey9a39eca2015-01-05 09:41:01 -080034import org.onosproject.net.resource.DefaultLinkResourceAllocations;
35import org.onosproject.net.resource.DefaultLinkResourceRequest;
36import org.onosproject.net.resource.Lambda;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.resource.LambdaResourceAllocation;
38import org.onosproject.net.resource.LinkResourceAllocations;
Ray Milkey9a39eca2015-01-05 09:41:01 -080039import org.onosproject.net.resource.LinkResourceRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.resource.LinkResourceStore;
41import org.onosproject.net.resource.ResourceAllocation;
Ray Milkey9a39eca2015-01-05 09:41:01 -080042import org.onosproject.net.resource.ResourceAllocationException;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.resource.ResourceType;
44import org.onosproject.store.hz.StoreService;
45import org.onosproject.store.hz.TestStoreManager;
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -080046
Ray Milkey9a39eca2015-01-05 09:41:01 -080047import com.google.common.collect.ImmutableMap;
48import com.google.common.collect.ImmutableSet;
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -080049import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertFalse;
51import static org.junit.Assert.assertNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import static org.onosproject.net.DeviceId.deviceId;
53import static org.onosproject.net.Link.Type.DIRECT;
54import static org.onosproject.net.PortNumber.portNumber;
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -080055
56/**
57 * Test of the simple LinkResourceStore implementation.
58 */
59public class HazelcastLinkResourceStoreTest {
60
61 private LinkResourceStore store;
62 private HazelcastLinkResourceStore storeImpl;
63 private Link link1;
64 private Link link2;
65 private Link link3;
66 private TestStoreManager storeMgr;
67
68 /**
69 * Returns {@link Link} object.
70 *
71 * @param dev1 source device
72 * @param port1 source port
73 * @param dev2 destination device
74 * @param port2 destination port
75 * @return created {@link Link} object
76 */
77 private Link newLink(String dev1, int port1, String dev2, int port2) {
78 Annotations annotations = DefaultAnnotations.builder()
79 .set(AnnotationKeys.OPTICAL_WAVES, "80")
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080080 .set(AnnotationKeys.BANDWIDTH, "1000")
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -080081 .build();
82 return new DefaultLink(
83 new ProviderId("of", "foo"),
84 new ConnectPoint(deviceId(dev1), portNumber(port1)),
85 new ConnectPoint(deviceId(dev2), portNumber(port2)),
86 DIRECT, annotations);
87 }
88
89 @Before
90 public void setUp() throws Exception {
91
Yuta HIGUCHI151cad82015-02-04 23:26:50 -080092 TestStoreManager testStoreMgr = new TestStoreManager();
93 testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
94 storeMgr = testStoreMgr;
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -080095 storeMgr.activate();
96
97
98 storeImpl = new TestHazelcastLinkResourceStore(storeMgr);
99 storeImpl.activate();
100 store = storeImpl;
101
102 link1 = newLink("of:1", 1, "of:2", 2);
103 link2 = newLink("of:2", 1, "of:3", 2);
104 link3 = newLink("of:3", 1, "of:4", 2);
105 }
106
107 @After
108 public void tearDown() throws Exception {
109 storeImpl.deactivate();
110
111 storeMgr.deactivate();
112 }
113
114 /**
115 * Tests constructor and activate method.
116 */
117 @Test
118 public void testConstructorAndActivate() {
119 final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
120 assertNotNull(allAllocations);
121 assertFalse(allAllocations.iterator().hasNext());
122
123 final Iterable<LinkResourceAllocations> linkAllocations =
124 store.getAllocations(link1);
125 assertNotNull(linkAllocations);
126 assertFalse(linkAllocations.iterator().hasNext());
127
128 final Set<ResourceAllocation> res = store.getFreeResources(link2);
129 assertNotNull(res);
130 }
131
132 /**
133 * Picks up and returns one of bandwidth allocations from a given set.
134 *
135 * @param resources the set of {@link ResourceAllocation}s
136 * @return {@link BandwidthResourceAllocation} object if found, null
137 * otherwise
138 */
139 private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
140 for (ResourceAllocation res : resources) {
141 if (res.type() == ResourceType.BANDWIDTH) {
142 return ((BandwidthResourceAllocation) res);
143 }
144 }
145 return null;
146 }
147
148 /**
149 * Returns all lambda allocations from a given set.
150 *
151 * @param resources the set of {@link ResourceAllocation}s
152 * @return a set of {@link LambdaResourceAllocation} objects
153 */
154 private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
155 Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
156 for (ResourceAllocation res : resources) {
157 if (res.type() == ResourceType.LAMBDA) {
158 lambdaResources.add((LambdaResourceAllocation) res);
159 }
160 }
161 return lambdaResources;
162 }
163
164 /**
165 * Tests initial free bandwidth for a link.
166 */
167 @Test
168 public void testInitialBandwidth() {
169 final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
170 assertNotNull(freeRes);
171
172 final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
173 assertNotNull(alloc);
174
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800175 assertEquals(Bandwidth.mbps(1000.0), alloc.bandwidth());
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -0800176 }
177
178 /**
179 * Tests initial free lambda for a link.
180 */
181 @Test
182 public void testInitialLambdas() {
183 final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
184 assertNotNull(freeRes);
185
186 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
187 assertNotNull(res);
188 assertEquals(80, res.size());
189 }
190
191 public static final class TestHazelcastLinkResourceStore
192 extends HazelcastLinkResourceStore {
193
194 public TestHazelcastLinkResourceStore(StoreService storeMgr) {
195 super.storeService = storeMgr;
196 }
197
198 }
Ray Milkey9a39eca2015-01-05 09:41:01 -0800199
200 /**
201 * Tests a successful bandwidth allocation.
202 */
203 @Test
204 public void testSuccessfulBandwidthAllocation() {
205 final Link link = newLink("of:1", 1, "of:2", 2);
206
207 final LinkResourceRequest request =
208 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
209 ImmutableSet.of(link))
210 .build();
211 final ResourceAllocation allocation =
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800212 new BandwidthResourceAllocation(Bandwidth.mbps(900.0));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800213 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
214
215 final LinkResourceAllocations allocations =
216 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
217
218 store.allocateResources(allocations);
219 }
220
221 /**
222 * Tests a unsuccessful bandwidth allocation.
223 */
224 @Test
225 public void testUnsuccessfulBandwidthAllocation() {
226 final Link link = newLink("of:1", 1, "of:2", 2);
227
228 final LinkResourceRequest request =
229 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
230 ImmutableSet.of(link))
231 .build();
232 final ResourceAllocation allocation =
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800233 new BandwidthResourceAllocation(Bandwidth.mbps(9000.0));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800234 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
235
236 final LinkResourceAllocations allocations =
237 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
238
239 boolean gotException = false;
240 try {
241 store.allocateResources(allocations);
242 } catch (ResourceAllocationException rae) {
243 assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
244 gotException = true;
245 }
246 assertEquals(true, gotException);
247 }
248
249 /**
250 * Tests a successful bandwidth allocation.
251 */
252 @Test
253 public void testSuccessfulLambdaAllocation() {
254 final Link link = newLink("of:1", 1, "of:2", 2);
255
256 final LinkResourceRequest request =
257 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
258 ImmutableSet.of(link))
259 .build();
260 final ResourceAllocation allocation =
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800261 new BandwidthResourceAllocation(Bandwidth.mbps(900.0));
Ray Milkey9a39eca2015-01-05 09:41:01 -0800262 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
263
264 final LinkResourceAllocations allocations =
265 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
266
267 store.allocateResources(allocations);
268 }
269
270 /**
271 * Tests a unsuccessful bandwidth allocation.
272 */
273 @Test
274 public void testUnsuccessfulLambdaAllocation() {
275 final Link link = newLink("of:1", 1, "of:2", 2);
276
277 final LinkResourceRequest request =
278 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
279 ImmutableSet.of(link))
280 .build();
281 final ResourceAllocation allocation =
282 new LambdaResourceAllocation(Lambda.valueOf(33));
283 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
284
285 final LinkResourceAllocations allocations =
286 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
287 store.allocateResources(allocations);
288
289 boolean gotException = false;
290 try {
291 store.allocateResources(allocations);
292 } catch (ResourceAllocationException rae) {
293 assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
294 gotException = true;
295 }
296 assertEquals(true, gotException);
297 }
Yuta HIGUCHI3cc4d9b2014-11-29 18:17:17 -0800298}