a5f2facb8be53eb2293a6137949aba46838a3dfb
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
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  */
16 package org.onosproject.store.resource.impl;
17
18 /**
19  * Test of the simple LinkResourceStore implementation.
20  */
21 public class HazelcastLinkResourceStoreTest {
22 /*
23     private LinkResourceStore store;
24     private HazelcastLinkResourceStore storeImpl;
25     private Link link1;
26     private Link link2;
27     private Link link3;
28     private TestStoreManager storeMgr;
29
30     /**
31      * Returns {@link Link} object.
32      *
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
38      * /
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")
43                 .build();
44         return new DefaultLink(
45                 new ProviderId("of", "foo"),
46                 new ConnectPoint(deviceId(dev1), portNumber(port1)),
47                 new ConnectPoint(deviceId(dev2), portNumber(port2)),
48                 DIRECT, annotations);
49     }
50
51     @Before
52     public void setUp() throws Exception {
53
54         TestStoreManager testStoreMgr = new TestStoreManager();
55         testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance());
56         storeMgr = testStoreMgr;
57         storeMgr.activate();
58
59
60         storeImpl = new TestHazelcastLinkResourceStore(storeMgr);
61         storeImpl.activate();
62         store = storeImpl;
63
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);
67     }
68
69     @After
70     public void tearDown() throws Exception {
71         storeImpl.deactivate();
72
73         storeMgr.deactivate();
74     }
75
76     @Test
77     public void testConstructorAndActivate() {
78         final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
79         assertNotNull(allAllocations);
80         assertFalse(allAllocations.iterator().hasNext());
81
82         final Iterable<LinkResourceAllocations> linkAllocations =
83                 store.getAllocations(link1);
84         assertNotNull(linkAllocations);
85         assertFalse(linkAllocations.iterator().hasNext());
86
87         final Set<ResourceAllocation> res = store.getFreeResources(link2);
88         assertNotNull(res);
89     }
90
91     private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
92         for (ResourceAllocation res : resources) {
93             if (res.type() == ResourceType.BANDWIDTH) {
94                 return ((BandwidthResourceAllocation) res);
95             }
96         }
97         return null;
98     }
99
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);
105             }
106         }
107         return lambdaResources;
108     }
109
110     @Test
111     public void testInitialBandwidth() {
112         final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
113         assertNotNull(freeRes);
114
115         final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
116         assertNotNull(alloc);
117
118         assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth());
119     }
120
121     @Test
122     public void testInitialLambdas() {
123         final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
124         assertNotNull(freeRes);
125
126         final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
127         assertNotNull(res);
128         assertEquals(80, res.size());
129     }
130
131     public static final class TestHazelcastLinkResourceStore
132             extends HazelcastLinkResourceStore {
133
134         public TestHazelcastLinkResourceStore(StoreService storeMgr) {
135             super.storeService = storeMgr;
136         }
137
138     }
139
140     @Test
141     public void testSuccessfulBandwidthAllocation() {
142         final Link link = newLink("of:1", 1, "of:2", 2);
143
144         final LinkResourceRequest request =
145                 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
146                         ImmutableSet.of(link))
147                 .build();
148         final ResourceAllocation allocation =
149                 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0)));
150         final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
151
152         final LinkResourceAllocations allocations =
153                 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
154
155         store.allocateResources(allocations);
156     }
157
158     @Test
159     public void testUnsuccessfulBandwidthAllocation() {
160         final Link link = newLink("of:1", 1, "of:2", 2);
161
162         final LinkResourceRequest request =
163                 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
164                         ImmutableSet.of(link))
165                         .build();
166         final ResourceAllocation allocation =
167                 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(9000.0)));
168         final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
169
170         final LinkResourceAllocations allocations =
171                 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
172
173         boolean gotException = false;
174         try {
175             store.allocateResources(allocations);
176         } catch (ResourceAllocationException rae) {
177             assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link"));
178             gotException = true;
179         }
180         assertEquals(true, gotException);
181     }
182
183     @Test
184     public void testSuccessfulLambdaAllocation() {
185         final Link link = newLink("of:1", 1, "of:2", 2);
186
187         final LinkResourceRequest request =
188                 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
189                         ImmutableSet.of(link))
190                         .build();
191         final ResourceAllocation allocation =
192                 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0)));
193         final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
194
195         final LinkResourceAllocations allocations =
196                 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
197
198         store.allocateResources(allocations);
199     }
200
201     @Test
202     public void testUnsuccessfulLambdaAllocation() {
203         final Link link = newLink("of:1", 1, "of:2", 2);
204
205         final LinkResourceRequest request =
206                 DefaultLinkResourceRequest.builder(IntentId.valueOf(1),
207                         ImmutableSet.of(link))
208                         .build();
209         final ResourceAllocation allocation =
210                 new LambdaResourceAllocation(LambdaResource.valueOf(33));
211         final Set<ResourceAllocation> allocationSet = ImmutableSet.of(allocation);
212
213         final LinkResourceAllocations allocations =
214                 new DefaultLinkResourceAllocations(request, ImmutableMap.of(link, allocationSet));
215         store.allocateResources(allocations);
216
217         boolean gotException = false;
218         try {
219             store.allocateResources(allocations);
220         } catch (ResourceAllocationException rae) {
221             assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link"));
222             gotException = true;
223         }
224         assertEquals(true, gotException);
225     }
226     */
227 }