Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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.onlab.onos.store.trivial.impl; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 22 | import static org.onlab.onos.net.Link.Type.DIRECT; |
| 23 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 24 | |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame^] | 31 | import org.onlab.onos.net.AnnotationKeys; |
| 32 | import org.onlab.onos.net.Annotations; |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.ConnectPoint; |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame^] | 34 | import org.onlab.onos.net.DefaultAnnotations; |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.DefaultLink; |
| 36 | import org.onlab.onos.net.Link; |
| 37 | import org.onlab.onos.net.provider.ProviderId; |
| 38 | import org.onlab.onos.net.resource.Bandwidth; |
| 39 | import org.onlab.onos.net.resource.BandwidthResourceAllocation; |
| 40 | import org.onlab.onos.net.resource.LambdaResourceAllocation; |
| 41 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 42 | import org.onlab.onos.net.resource.LinkResourceStore; |
| 43 | import org.onlab.onos.net.resource.ResourceAllocation; |
| 44 | import org.onlab.onos.net.resource.ResourceType; |
| 45 | |
| 46 | /** |
| 47 | * Test of the simple LinkResourceStore implementation. |
| 48 | */ |
| 49 | public class SimpleLinkResourceStoreTest { |
| 50 | |
| 51 | private LinkResourceStore store; |
| 52 | private SimpleLinkResourceStore simpleStore; |
| 53 | private Link link1; |
| 54 | private Link link2; |
| 55 | private Link link3; |
| 56 | |
| 57 | /** |
| 58 | * Returns {@link Link} object. |
| 59 | * |
| 60 | * @param dev1 source device |
| 61 | * @param port1 source port |
| 62 | * @param dev2 destination device |
| 63 | * @param port2 destination port |
| 64 | * @return created {@link Link} object |
| 65 | */ |
| 66 | private Link newLink(String dev1, int port1, String dev2, int port2) { |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame^] | 67 | Annotations annotations = DefaultAnnotations.builder() |
| 68 | .set(AnnotationKeys.OPTICAL_WAVES, "80") |
| 69 | .set(AnnotationKeys.BANDWIDTH, "1000000") |
| 70 | .build(); |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 71 | return new DefaultLink( |
| 72 | new ProviderId("of", "foo"), |
| 73 | new ConnectPoint(deviceId(dev1), portNumber(port1)), |
| 74 | new ConnectPoint(deviceId(dev2), portNumber(port2)), |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame^] | 75 | DIRECT, annotations); |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Before |
| 79 | public void setUp() throws Exception { |
| 80 | simpleStore = new SimpleLinkResourceStore(); |
| 81 | simpleStore.activate(); |
| 82 | store = simpleStore; |
| 83 | |
| 84 | link1 = newLink("of:1", 1, "of:2", 2); |
| 85 | link2 = newLink("of:2", 1, "of:3", 2); |
| 86 | link3 = newLink("of:3", 1, "of:4", 2); |
| 87 | } |
| 88 | |
| 89 | @After |
| 90 | public void tearDown() throws Exception { |
| 91 | simpleStore.deactivate(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests constructor and activate method. |
| 96 | */ |
| 97 | @Test |
| 98 | public void testConstructorAndActivate() { |
| 99 | final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations(); |
| 100 | assertNotNull(allAllocations); |
| 101 | assertFalse(allAllocations.iterator().hasNext()); |
| 102 | |
| 103 | final Iterable<LinkResourceAllocations> linkAllocations = |
| 104 | store.getAllocations(link1); |
| 105 | assertNotNull(linkAllocations); |
| 106 | assertFalse(linkAllocations.iterator().hasNext()); |
| 107 | |
| 108 | final Set<ResourceAllocation> res = store.getFreeResources(link2); |
| 109 | assertNotNull(res); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Picks up and returns one of bandwidth allocations from a given set. |
| 114 | * |
| 115 | * @param resources the set of {@link ResourceAllocation}s |
| 116 | * @return {@link BandwidthResourceAllocation} object if found, null |
| 117 | * otherwise |
| 118 | */ |
| 119 | private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) { |
| 120 | for (ResourceAllocation res : resources) { |
| 121 | if (res.type() == ResourceType.BANDWIDTH) { |
| 122 | return ((BandwidthResourceAllocation) res); |
| 123 | } |
| 124 | } |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns all lambda allocations from a given set. |
| 130 | * |
| 131 | * @param resources the set of {@link ResourceAllocation}s |
| 132 | * @return a set of {@link LambdaResourceAllocation} objects |
| 133 | */ |
| 134 | private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) { |
| 135 | Set<LambdaResourceAllocation> lambdaResources = new HashSet<>(); |
| 136 | for (ResourceAllocation res : resources) { |
| 137 | if (res.type() == ResourceType.LAMBDA) { |
| 138 | lambdaResources.add((LambdaResourceAllocation) res); |
| 139 | } |
| 140 | } |
| 141 | return lambdaResources; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tests initial free bandwidth for a link. |
| 146 | */ |
| 147 | @Test |
| 148 | public void testInitialBandwidth() { |
| 149 | final Set<ResourceAllocation> freeRes = store.getFreeResources(link1); |
| 150 | assertNotNull(freeRes); |
| 151 | |
| 152 | final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes); |
| 153 | assertNotNull(alloc); |
| 154 | |
| 155 | assertEquals(Bandwidth.valueOf(1000000.0), alloc.bandwidth()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Tests initial free lambda for a link. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testInitialLambdas() { |
| 163 | final Set<ResourceAllocation> freeRes = store.getFreeResources(link3); |
| 164 | assertNotNull(freeRes); |
| 165 | |
| 166 | final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes); |
| 167 | assertNotNull(res); |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame^] | 168 | assertEquals(80, res.size()); |
Toshio Koide | 3233618 | 2014-10-30 13:02:47 -0700 | [diff] [blame] | 169 | } |
| 170 | } |