Committed by
Gerrit Code Review
HazelcastLinkResourceStore
Change-Id: Ic5d6bf9b54b023368a883e3665484900ccda44e7
Showing
3 changed files
with
405 additions
and
0 deletions
| 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.hz; | ||
| 17 | + | ||
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 19 | + | ||
| 20 | +import java.util.ArrayList; | ||
| 21 | +import java.util.Collection; | ||
| 22 | +import java.util.HashSet; | ||
| 23 | +import java.util.Set; | ||
| 24 | +import java.util.concurrent.TimeUnit; | ||
| 25 | + | ||
| 26 | +import org.onlab.onos.store.serializers.StoreSerializer; | ||
| 27 | + | ||
| 28 | +import com.hazelcast.core.TransactionalMap; | ||
| 29 | +import com.hazelcast.query.Predicate; | ||
| 30 | + | ||
| 31 | +// TODO: implement Predicate, etc. if we need them. | ||
| 32 | +/** | ||
| 33 | + * Wrapper around TransactionalMap<byte[], byte[]> which serializes/deserializes | ||
| 34 | + * key and value using StoreSerializer. | ||
| 35 | + * | ||
| 36 | + * @param <K> key type | ||
| 37 | + * @param <V> value type | ||
| 38 | + */ | ||
| 39 | +public class STxMap<K, V> implements TransactionalMap<K, V> { | ||
| 40 | + | ||
| 41 | + private final TransactionalMap<byte[], byte[]> m; | ||
| 42 | + private final StoreSerializer serializer; | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * Creates a STxMap instance. | ||
| 46 | + * | ||
| 47 | + * @param baseMap base IMap to use | ||
| 48 | + * @param serializer serializer to use for both key and value | ||
| 49 | + */ | ||
| 50 | + public STxMap(TransactionalMap<byte[], byte[]> baseMap, StoreSerializer serializer) { | ||
| 51 | + this.m = checkNotNull(baseMap); | ||
| 52 | + this.serializer = checkNotNull(serializer); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Override | ||
| 56 | + public int size() { | ||
| 57 | + return m.size(); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Override | ||
| 61 | + public boolean isEmpty() { | ||
| 62 | + return m.isEmpty(); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Deprecated | ||
| 66 | + @Override | ||
| 67 | + public Object getId() { | ||
| 68 | + return m.getId(); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + public String getPartitionKey() { | ||
| 73 | + return m.getPartitionKey(); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + @Override | ||
| 77 | + public String getName() { | ||
| 78 | + return m.getName(); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public String getServiceName() { | ||
| 83 | + return m.getServiceName(); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Override | ||
| 87 | + public void destroy() { | ||
| 88 | + m.destroy(); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + @Override | ||
| 92 | + public boolean containsKey(Object key) { | ||
| 93 | + return m.containsKey(serializeKey(key)); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + @Override | ||
| 97 | + public V get(Object key) { | ||
| 98 | + return deserializeVal(m.get(serializeKey(key))); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + @Override | ||
| 102 | + public V getForUpdate(Object key) { | ||
| 103 | + // TODO Auto-generated method stub | ||
| 104 | + return deserializeVal(m.getForUpdate(serializeKey(key))); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + @Override | ||
| 108 | + public V put(K key, V value) { | ||
| 109 | + return deserializeVal(m.put(serializeKey(key), serializeVal(value))); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + @Override | ||
| 113 | + public V remove(Object key) { | ||
| 114 | + return deserializeVal(m.remove(serializeKey(key))); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Override | ||
| 118 | + public boolean remove(Object key, Object value) { | ||
| 119 | + return m.remove(serializeKey(key), serializeVal(value)); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Override | ||
| 123 | + public void delete(Object key) { | ||
| 124 | + m.delete(serializeKey(key)); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + @Override | ||
| 128 | + public V put(K key, V value, long ttl, TimeUnit timeunit) { | ||
| 129 | + return deserializeVal(m.put(serializeKey(key), serializeVal(value), ttl, timeunit)); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + @Override | ||
| 133 | + public V putIfAbsent(K key, V value) { | ||
| 134 | + return deserializeVal(m.putIfAbsent(serializeKey(key), serializeVal(value))); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + @Override | ||
| 138 | + public boolean replace(K key, V oldValue, V newValue) { | ||
| 139 | + return m.replace(serializeKey(key), serializeVal(oldValue), serializeVal(newValue)); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + @Override | ||
| 143 | + public V replace(K key, V value) { | ||
| 144 | + return deserializeVal(m.replace(serializeKey(key), serializeVal(value))); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + @Override | ||
| 148 | + public void set(K key, V value) { | ||
| 149 | + m.set(serializeKey(key), serializeVal(value)); | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + | ||
| 153 | + @Override | ||
| 154 | + public Set<K> keySet() { | ||
| 155 | + return deserializeKeySet(m.keySet()); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + @Override | ||
| 159 | + public Collection<V> values() { | ||
| 160 | + return deserializeVals(m.values()); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + @Deprecated // marking method not implemented | ||
| 164 | + @SuppressWarnings("rawtypes") | ||
| 165 | + @Override | ||
| 166 | + public Set<K> keySet(Predicate predicate) { | ||
| 167 | + throw new UnsupportedOperationException(); | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + @Deprecated // marking method not implemented | ||
| 171 | + @SuppressWarnings("rawtypes") | ||
| 172 | + @Override | ||
| 173 | + public Collection<V> values(Predicate predicate) { | ||
| 174 | + throw new UnsupportedOperationException(); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + private byte[] serializeKey(Object key) { | ||
| 178 | + return serializer.encode(key); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + private K deserializeKey(byte[] key) { | ||
| 182 | + return serializer.decode(key); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + private byte[] serializeVal(Object val) { | ||
| 186 | + return serializer.encode(val); | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + private V deserializeVal(byte[] val) { | ||
| 190 | + if (val == null) { | ||
| 191 | + return null; | ||
| 192 | + } | ||
| 193 | + return serializer.decode(val.clone()); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + private Set<K> deserializeKeySet(Set<byte[]> keys) { | ||
| 197 | + Set<K> dsk = new HashSet<>(keys.size()); | ||
| 198 | + for (byte[] key : keys) { | ||
| 199 | + dsk.add(deserializeKey(key)); | ||
| 200 | + } | ||
| 201 | + return dsk; | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + private Collection<V> deserializeVals(Collection<byte[]> vals) { | ||
| 205 | + Collection<V> dsl = new ArrayList<>(vals.size()); | ||
| 206 | + for (byte[] val : vals) { | ||
| 207 | + dsl.add(deserializeVal(val)); | ||
| 208 | + } | ||
| 209 | + return dsl; | ||
| 210 | + } | ||
| 211 | +} |
core/store/dist/src/main/java/org/onlab/onos/store/resource/impl/HazelcastLinkResourceStore.java
0 → 100644
This diff is collapsed. Click to expand it.
core/store/dist/src/test/java/org/onlab/onos/store/resource/impl/HazelcastLinkResourceStoreTest.java
0 → 100644
| 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.resource.impl; | ||
| 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; | ||
| 24 | +import org.onlab.onos.net.AnnotationKeys; | ||
| 25 | +import org.onlab.onos.net.Annotations; | ||
| 26 | +import org.onlab.onos.net.ConnectPoint; | ||
| 27 | +import org.onlab.onos.net.DefaultAnnotations; | ||
| 28 | +import org.onlab.onos.net.DefaultLink; | ||
| 29 | +import org.onlab.onos.net.Link; | ||
| 30 | +import org.onlab.onos.net.provider.ProviderId; | ||
| 31 | +import org.onlab.onos.net.resource.Bandwidth; | ||
| 32 | +import org.onlab.onos.net.resource.BandwidthResourceAllocation; | ||
| 33 | +import org.onlab.onos.net.resource.LambdaResourceAllocation; | ||
| 34 | +import org.onlab.onos.net.resource.LinkResourceAllocations; | ||
| 35 | +import org.onlab.onos.net.resource.LinkResourceStore; | ||
| 36 | +import org.onlab.onos.net.resource.ResourceAllocation; | ||
| 37 | +import org.onlab.onos.net.resource.ResourceType; | ||
| 38 | +import org.onlab.onos.store.hz.StoreService; | ||
| 39 | +import org.onlab.onos.store.hz.TestStoreManager; | ||
| 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; | ||
| 47 | +import static org.onlab.onos.net.DeviceId.deviceId; | ||
| 48 | +import static org.onlab.onos.net.Link.Type.DIRECT; | ||
| 49 | +import static org.onlab.onos.net.PortNumber.portNumber; | ||
| 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 | +} |
-
Please register or login to post a comment