Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 18 | import java.util.Collection; |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 19 | import java.util.HashSet; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 25 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.AnnotationKeys; |
| 27 | import org.onosproject.net.Annotations; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.DefaultLink; |
| 31 | import org.onosproject.net.Link; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.provider.ProviderId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 34 | import org.onosproject.net.resource.link.BandwidthResource; |
| 35 | import org.onosproject.net.resource.link.BandwidthResourceAllocation; |
| 36 | import org.onosproject.net.resource.link.LambdaResource; |
| 37 | import org.onosproject.net.resource.link.LambdaResourceAllocation; |
| 38 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 39 | import org.onosproject.net.resource.link.LinkResourceStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.resource.ResourceAllocation; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 41 | import org.onosproject.net.resource.ResourceAllocationException; |
| 42 | import org.onosproject.net.resource.ResourceRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 44 | |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 45 | import com.google.common.collect.ImmutableSet; |
| 46 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertFalse; |
| 49 | import static org.junit.Assert.assertNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import static org.onosproject.net.DeviceId.deviceId; |
| 51 | import static org.onosproject.net.Link.Type.DIRECT; |
| 52 | import static org.onosproject.net.PortNumber.portNumber; |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 53 | |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Test of the simple LinkResourceStore implementation. |
| 56 | */ |
| 57 | public class SimpleLinkResourceStoreTest { |
| 58 | |
| 59 | private LinkResourceStore store; |
| 60 | private SimpleLinkResourceStore simpleStore; |
| 61 | private Link link1; |
| 62 | private Link link2; |
| 63 | private Link link3; |
| 64 | |
| 65 | /** |
| 66 | * Returns {@link Link} object. |
| 67 | * |
| 68 | * @param dev1 source device |
| 69 | * @param port1 source port |
| 70 | * @param dev2 destination device |
| 71 | * @param port2 destination port |
| 72 | * @return created {@link Link} object |
| 73 | */ |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 74 | private static Link newLink(String dev1, int port1, String dev2, int port2) { |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 75 | Annotations annotations = DefaultAnnotations.builder() |
| 76 | .set(AnnotationKeys.OPTICAL_WAVES, "80") |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 77 | .set(AnnotationKeys.BANDWIDTH, "1000") |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 78 | .build(); |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 79 | return DefaultLink.builder() |
| 80 | .providerId(new ProviderId("of", "foo")) |
| 81 | .src(new ConnectPoint(deviceId(dev1), portNumber(port1))) |
| 82 | .dst(new ConnectPoint(deviceId(dev2), portNumber(port2))) |
| 83 | .type(DIRECT) |
| 84 | .annotations(annotations) |
| 85 | .build(); |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Before |
| 89 | public void setUp() throws Exception { |
| 90 | simpleStore = new SimpleLinkResourceStore(); |
| 91 | simpleStore.activate(); |
| 92 | store = simpleStore; |
| 93 | |
| 94 | link1 = newLink("of:1", 1, "of:2", 2); |
| 95 | link2 = newLink("of:2", 1, "of:3", 2); |
| 96 | link3 = newLink("of:3", 1, "of:4", 2); |
| 97 | } |
| 98 | |
| 99 | @After |
| 100 | public void tearDown() throws Exception { |
| 101 | simpleStore.deactivate(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests constructor and activate method. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testConstructorAndActivate() { |
| 109 | final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations(); |
| 110 | assertNotNull(allAllocations); |
| 111 | assertFalse(allAllocations.iterator().hasNext()); |
| 112 | |
| 113 | final Iterable<LinkResourceAllocations> linkAllocations = |
| 114 | store.getAllocations(link1); |
| 115 | assertNotNull(linkAllocations); |
| 116 | assertFalse(linkAllocations.iterator().hasNext()); |
| 117 | |
| 118 | final Set<ResourceAllocation> res = store.getFreeResources(link2); |
| 119 | assertNotNull(res); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Picks up and returns one of bandwidth allocations from a given set. |
| 124 | * |
| 125 | * @param resources the set of {@link ResourceAllocation}s |
| 126 | * @return {@link BandwidthResourceAllocation} object if found, null |
| 127 | * otherwise |
| 128 | */ |
| 129 | private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) { |
| 130 | for (ResourceAllocation res : resources) { |
| 131 | if (res.type() == ResourceType.BANDWIDTH) { |
| 132 | return ((BandwidthResourceAllocation) res); |
| 133 | } |
| 134 | } |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns all lambda allocations from a given set. |
| 140 | * |
| 141 | * @param resources the set of {@link ResourceAllocation}s |
| 142 | * @return a set of {@link LambdaResourceAllocation} objects |
| 143 | */ |
| 144 | private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) { |
| 145 | Set<LambdaResourceAllocation> lambdaResources = new HashSet<>(); |
| 146 | for (ResourceAllocation res : resources) { |
| 147 | if (res.type() == ResourceType.LAMBDA) { |
| 148 | lambdaResources.add((LambdaResourceAllocation) res); |
| 149 | } |
| 150 | } |
| 151 | return lambdaResources; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Tests initial free bandwidth for a link. |
| 156 | */ |
| 157 | @Test |
| 158 | public void testInitialBandwidth() { |
| 159 | final Set<ResourceAllocation> freeRes = store.getFreeResources(link1); |
| 160 | assertNotNull(freeRes); |
| 161 | |
| 162 | final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes); |
| 163 | assertNotNull(alloc); |
| 164 | |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 165 | assertEquals(new BandwidthResource(Bandwidth.mbps(1000.0)), alloc.bandwidth()); |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests initial free lambda for a link. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testInitialLambdas() { |
| 173 | final Set<ResourceAllocation> freeRes = store.getFreeResources(link3); |
| 174 | assertNotNull(freeRes); |
| 175 | |
| 176 | final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes); |
| 177 | assertNotNull(res); |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 178 | assertEquals(80, res.size()); |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 179 | } |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 180 | |
| 181 | public static class MockLinkResourceBandwidthAllocations implements LinkResourceAllocations { |
| 182 | final double allocationAmount; |
| 183 | |
| 184 | MockLinkResourceBandwidthAllocations(Double allocationAmount) { |
| 185 | this.allocationAmount = allocationAmount; |
| 186 | } |
| 187 | @Override |
| 188 | public Set<ResourceAllocation> getResourceAllocation(Link link) { |
| 189 | final ResourceAllocation allocation = |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 190 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(allocationAmount))); |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 191 | final Set<ResourceAllocation> allocations = new HashSet<>(); |
| 192 | allocations.add(allocation); |
| 193 | return allocations; |
| 194 | } |
| 195 | |
| 196 | @Override |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 197 | public IntentId intentId() { |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Collection<Link> links() { |
| 203 | return ImmutableSet.of(newLink("of:1", 1, "of:2", 2)); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public Set<ResourceRequest> resources() { |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public ResourceType type() { |
| 213 | return null; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public static class MockLinkResourceLambdaAllocations implements LinkResourceAllocations { |
| 218 | final int allocatedLambda; |
| 219 | |
| 220 | MockLinkResourceLambdaAllocations(int allocatedLambda) { |
| 221 | this.allocatedLambda = allocatedLambda; |
| 222 | } |
| 223 | @Override |
| 224 | public Set<ResourceAllocation> getResourceAllocation(Link link) { |
| 225 | final ResourceAllocation allocation = |
Sho SHIMIZU | 94b7ff4 | 2015-05-06 17:51:49 -0700 | [diff] [blame] | 226 | new LambdaResourceAllocation(LambdaResource.valueOf(allocatedLambda)); |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 227 | final Set<ResourceAllocation> allocations = new HashSet<>(); |
| 228 | allocations.add(allocation); |
| 229 | return allocations; |
| 230 | } |
| 231 | |
| 232 | @Override |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 233 | public IntentId intentId() { |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 234 | return null; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public Collection<Link> links() { |
| 239 | return ImmutableSet.of(newLink("of:1", 1, "of:2", 2)); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Set<ResourceRequest> resources() { |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public ResourceType type() { |
| 249 | return null; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Tests a successful bandwidth allocation. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testSuccessfulBandwidthAllocation() { |
| 258 | final LinkResourceAllocations allocations = |
| 259 | new MockLinkResourceBandwidthAllocations(900.0); |
| 260 | store.allocateResources(allocations); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Tests an unsuccessful bandwidth allocation. |
| 265 | */ |
| 266 | @Test |
| 267 | public void testUnsuccessfulBandwidthAllocation() { |
| 268 | final LinkResourceAllocations allocations = |
| 269 | new MockLinkResourceBandwidthAllocations(2000000000.0); |
| 270 | boolean gotException = false; |
| 271 | try { |
| 272 | store.allocateResources(allocations); |
| 273 | } catch (ResourceAllocationException rae) { |
| 274 | assertEquals(true, rae.getMessage().contains("Unable to allocate bandwidth for link")); |
| 275 | gotException = true; |
| 276 | } |
| 277 | assertEquals(true, gotException); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Tests a successful lambda allocation. |
| 282 | */ |
| 283 | @Test |
| 284 | public void testSuccessfulLambdaAllocation() { |
| 285 | final LinkResourceAllocations allocations = |
| 286 | new MockLinkResourceLambdaAllocations(1); |
| 287 | store.allocateResources(allocations); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Tests an unsuccessful lambda allocation. |
| 292 | */ |
| 293 | @Test |
| 294 | public void testUnsuccessfulLambdaAllocation() { |
| 295 | final LinkResourceAllocations allocations = |
| 296 | new MockLinkResourceLambdaAllocations(1); |
| 297 | store.allocateResources(allocations); |
| 298 | |
| 299 | boolean gotException = false; |
| 300 | |
| 301 | try { |
| 302 | store.allocateResources(allocations); |
| 303 | } catch (ResourceAllocationException rae) { |
| 304 | assertEquals(true, rae.getMessage().contains("Unable to allocate lambda for link")); |
| 305 | gotException = true; |
| 306 | } |
| 307 | assertEquals(true, gotException); |
| 308 | } |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 309 | } |