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