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