Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.resource.impl; |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.HashSet; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 24 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.AnnotationKeys; |
| 26 | import org.onosproject.net.Annotations; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DefaultAnnotations; |
| 29 | import org.onosproject.net.DefaultLink; |
| 30 | import org.onosproject.net.Link; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 33 | import org.onosproject.net.resource.BandwidthResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.resource.BandwidthResourceAllocation; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 35 | import org.onosproject.net.resource.DefaultLinkResourceAllocations; |
| 36 | import org.onosproject.net.resource.DefaultLinkResourceRequest; |
Sho SHIMIZU | 94b7ff4 | 2015-05-06 17:51:49 -0700 | [diff] [blame] | 37 | import org.onosproject.net.resource.LambdaResource; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.resource.LambdaResourceAllocation; |
| 39 | import org.onosproject.net.resource.LinkResourceAllocations; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 40 | import org.onosproject.net.resource.LinkResourceRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.resource.LinkResourceStore; |
| 42 | import org.onosproject.net.resource.ResourceAllocation; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 43 | import org.onosproject.net.resource.ResourceAllocationException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.resource.ResourceType; |
| 45 | import org.onosproject.store.hz.StoreService; |
| 46 | import org.onosproject.store.hz.TestStoreManager; |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 47 | |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 48 | import com.google.common.collect.ImmutableMap; |
| 49 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 50 | import static org.junit.Assert.assertEquals; |
| 51 | import static org.junit.Assert.assertFalse; |
| 52 | import static org.junit.Assert.assertNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import static org.onosproject.net.DeviceId.deviceId; |
| 54 | import static org.onosproject.net.Link.Type.DIRECT; |
| 55 | import static org.onosproject.net.PortNumber.portNumber; |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Test of the simple LinkResourceStore implementation. |
| 59 | */ |
| 60 | public 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 SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 81 | .set(AnnotationKeys.BANDWIDTH, "1000") |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 82 | .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 HIGUCHI | 151cad8 | 2015-02-04 23:26:50 -0800 | [diff] [blame] | 93 | TestStoreManager testStoreMgr = new TestStoreManager(); |
| 94 | testStoreMgr.setHazelcastInstance(testStoreMgr.initSingleInstance()); |
| 95 | storeMgr = testStoreMgr; |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 96 | 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 SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 176 | assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth()); |
Yuta HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 177 | } |
| 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 Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 200 | |
| 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 SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 213 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0))); |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 214 | 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 SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 234 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(9000.0))); |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 235 | 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 SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 262 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.mbps(900.0))); |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 263 | 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 SHIMIZU | 94b7ff4 | 2015-05-06 17:51:49 -0700 | [diff] [blame] | 283 | new LambdaResourceAllocation(LambdaResource.valueOf(33)); |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 284 | 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 HIGUCHI | 3cc4d9b | 2014-11-29 18:17:17 -0800 | [diff] [blame] | 299 | } |