2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.resource.impl;
19 * Test of the simple LinkResourceStore implementation.
21 public class HazelcastLinkResourceStoreTest {
23 private LinkResourceStore store;
24 private HazelcastLinkResourceStore storeImpl;
28 private TestStoreManager storeMgr;
31 * Returns {@link Link} object.
33 * @param dev1 source device
34 * @param port1 source port
35 * @param dev2 destination device
36 * @param port2 destination port
37 * @return created {@link Link} object
39 private Link newLink(String dev1, int port1, String dev2, int port2) {
40 Annotations annotations = DefaultAnnotations.builder()
41 .set(AnnotationKeys.OPTICAL_WAVES, "80")
42 .set(AnnotationKeys.BANDWIDTH, "1000")
44 return new DefaultLink(
45 new ProviderId("of", "foo"),
46 new ConnectPoint(deviceId(dev1), portNumber(port1)),
47 new ConnectPoint(deviceId(dev2), portNumber(port2)),
52 public void setUp() throws Exception {
54 TestStoreManager testStoreMgr = new TestStoreManager();
55 testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
56 storeMgr = testStoreMgr;
60 storeImpl = new TestHazelcastLinkResourceStore(storeMgr);
64 link1 = newLink("of:1", 1, "of:2", 2);
65 link2 = newLink("of:2", 1, "of:3", 2);
66 link3 = newLink("of:3", 1, "of:4", 2);
70 public void tearDown() throws Exception {
71 storeImpl.deactivate();
73 storeMgr.deactivate();
77 public void testConstructorAndActivate() {
78 final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
79 assertNotNull(allAllocations);
80 assertFalse(allAllocations.iterator().hasNext());
82 final Iterable<LinkResourceAllocations> linkAllocations =
83 store.getAllocations(link1);
84 assertNotNull(linkAllocations);
85 assertFalse(linkAllocations.iterator().hasNext());
87 final Set<ResourceAllocation> res = store.getFreeResources(link2);
91 private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
92 for (ResourceAllocation res : resources) {
93 if (res.type() == ResourceType.BANDWIDTH) {
94 return ((BandwidthResourceAllocation) res);
100 private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
101 Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
102 for (ResourceAllocation res : resources) {
103 if (res.type() == ResourceType.LAMBDA) {
104 lambdaResources.add((LambdaResourceAllocation) res);
107 return lambdaResources;
111 public void testInitialBandwidth() {
112 final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
113 assertNotNull(freeRes);
115 final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
116 assertNotNull(alloc);
118 assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
122 public void testInitialLambdas() {
123 final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
124 assertNotNull(freeRes);
126 final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
128 assertEquals(80, res.size());
131 public static final class TestHazelcastLinkResourceStore
132 extends HazelcastLinkResourceStore {
134 public TestHazelcastLinkResourceStore(StoreService storeMgr) {
135 super.storeService = storeMgr;
141 public void testSuccessfulBandwidthAllocation() {
142 final Link link = newLink("of:1", 1, "of:2", 2);
144 final LinkResourceRequest request =
145 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
146 ImmutableSet.of(link))
148 final ResourceAllocation allocation =
149 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0)));
150 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
152 final LinkResourceAllocations allocations =
153 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
155 store.allocateResources(allocations);
159 public void testUnsuccessfulBandwidthAllocation() {
160 final Link link = newLink("of:1", 1, "of:2", 2);
162 final LinkResourceRequest request =
163 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
164 ImmutableSet.of(link))
166 final ResourceAllocation allocation =
167 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(9000.0)));
168 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
170 final LinkResourceAllocations allocations =
171 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
173 boolean gotException = false;
175 store.allocateResources(allocations);
176 } catch (ResourceAllocationException rae) {
177 assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
180 assertEquals(true, gotException);
184 public void testSuccessfulLambdaAllocation() {
185 final Link link = newLink("of:1", 1, "of:2", 2);
187 final LinkResourceRequest request =
188 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
189 ImmutableSet.of(link))
191 final ResourceAllocation allocation =
192 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0)));
193 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
195 final LinkResourceAllocations allocations =
196 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
198 store.allocateResources(allocations);
202 public void testUnsuccessfulLambdaAllocation() {
203 final Link link = newLink("of:1", 1, "of:2", 2);
205 final LinkResourceRequest request =
206 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
207 ImmutableSet.of(link))
209 final ResourceAllocation allocation =
210 new LambdaResourceAllocation(LambdaResource.valueOf(33));
211 final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
213 final LinkResourceAllocations allocations =
214 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
215 store.allocateResources(allocations);
217 boolean gotException = false;
219 store.allocateResources(allocations);
220 } catch (ResourceAllocationException rae) {
221 assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
224 assertEquals(true, gotException);