Yuta HIGUCHI
Committed by Gerrit Code Review

HazelcastLinkResourceStore

Change-Id: Ic5d6bf9b54b023368a883e3665484900ccda44e7
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.onos.store.hz;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.onlab.onos.store.serializers.StoreSerializer;
import com.hazelcast.core.TransactionalMap;
import com.hazelcast.query.Predicate;
// TODO: implement Predicate, etc. if we need them.
/**
* Wrapper around TransactionalMap<byte[], byte[]> which serializes/deserializes
* key and value using StoreSerializer.
*
* @param <K> key type
* @param <V> value type
*/
public class STxMap<K, V> implements TransactionalMap<K, V> {
private final TransactionalMap<byte[], byte[]> m;
private final StoreSerializer serializer;
/**
* Creates a STxMap instance.
*
* @param baseMap base IMap to use
* @param serializer serializer to use for both key and value
*/
public STxMap(TransactionalMap<byte[], byte[]> baseMap, StoreSerializer serializer) {
this.m = checkNotNull(baseMap);
this.serializer = checkNotNull(serializer);
}
@Override
public int size() {
return m.size();
}
@Override
public boolean isEmpty() {
return m.isEmpty();
}
@Deprecated
@Override
public Object getId() {
return m.getId();
}
@Override
public String getPartitionKey() {
return m.getPartitionKey();
}
@Override
public String getName() {
return m.getName();
}
@Override
public String getServiceName() {
return m.getServiceName();
}
@Override
public void destroy() {
m.destroy();
}
@Override
public boolean containsKey(Object key) {
return m.containsKey(serializeKey(key));
}
@Override
public V get(Object key) {
return deserializeVal(m.get(serializeKey(key)));
}
@Override
public V getForUpdate(Object key) {
// TODO Auto-generated method stub
return deserializeVal(m.getForUpdate(serializeKey(key)));
}
@Override
public V put(K key, V value) {
return deserializeVal(m.put(serializeKey(key), serializeVal(value)));
}
@Override
public V remove(Object key) {
return deserializeVal(m.remove(serializeKey(key)));
}
@Override
public boolean remove(Object key, Object value) {
return m.remove(serializeKey(key), serializeVal(value));
}
@Override
public void delete(Object key) {
m.delete(serializeKey(key));
}
@Override
public V put(K key, V value, long ttl, TimeUnit timeunit) {
return deserializeVal(m.put(serializeKey(key), serializeVal(value), ttl, timeunit));
}
@Override
public V putIfAbsent(K key, V value) {
return deserializeVal(m.putIfAbsent(serializeKey(key), serializeVal(value)));
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return m.replace(serializeKey(key), serializeVal(oldValue), serializeVal(newValue));
}
@Override
public V replace(K key, V value) {
return deserializeVal(m.replace(serializeKey(key), serializeVal(value)));
}
@Override
public void set(K key, V value) {
m.set(serializeKey(key), serializeVal(value));
}
@Override
public Set<K> keySet() {
return deserializeKeySet(m.keySet());
}
@Override
public Collection<V> values() {
return deserializeVals(m.values());
}
@Deprecated // marking method not implemented
@SuppressWarnings("rawtypes")
@Override
public Set<K> keySet(Predicate predicate) {
throw new UnsupportedOperationException();
}
@Deprecated // marking method not implemented
@SuppressWarnings("rawtypes")
@Override
public Collection<V> values(Predicate predicate) {
throw new UnsupportedOperationException();
}
private byte[] serializeKey(Object key) {
return serializer.encode(key);
}
private K deserializeKey(byte[] key) {
return serializer.decode(key);
}
private byte[] serializeVal(Object val) {
return serializer.encode(val);
}
private V deserializeVal(byte[] val) {
if (val == null) {
return null;
}
return serializer.decode(val.clone());
}
private Set<K> deserializeKeySet(Set<byte[]> keys) {
Set<K> dsk = new HashSet<>(keys.size());
for (byte[] key : keys) {
dsk.add(deserializeKey(key));
}
return dsk;
}
private Collection<V> deserializeVals(Collection<byte[]> vals) {
Collection<V> dsl = new ArrayList<>(vals.size());
for (byte[] val : vals) {
dsl.add(deserializeVal(val));
}
return dsl;
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.onos.store.resource.impl;
import java.util.HashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.onos.net.AnnotationKeys;
import org.onlab.onos.net.Annotations;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.DefaultAnnotations;
import org.onlab.onos.net.DefaultLink;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.onos.net.resource.Bandwidth;
import org.onlab.onos.net.resource.BandwidthResourceAllocation;
import org.onlab.onos.net.resource.LambdaResourceAllocation;
import org.onlab.onos.net.resource.LinkResourceAllocations;
import org.onlab.onos.net.resource.LinkResourceStore;
import org.onlab.onos.net.resource.ResourceAllocation;
import org.onlab.onos.net.resource.ResourceType;
import org.onlab.onos.store.hz.StoreService;
import org.onlab.onos.store.hz.TestStoreManager;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.onlab.onos.net.DeviceId.deviceId;
import static org.onlab.onos.net.Link.Type.DIRECT;
import static org.onlab.onos.net.PortNumber.portNumber;
/**
* Test of the simple LinkResourceStore implementation.
*/
public class HazelcastLinkResourceStoreTest {
private LinkResourceStore store;
private HazelcastLinkResourceStore storeImpl;
private Link link1;
private Link link2;
private Link link3;
private TestStoreManager storeMgr;
/**
* Returns {@link Link} object.
*
* @param dev1 source device
* @param port1 source port
* @param dev2 destination device
* @param port2 destination port
* @return created {@link Link} object
*/
private Link newLink(String dev1, int port1, String dev2, int port2) {
Annotations annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.OPTICAL_WAVES, "80")
.set(AnnotationKeys.BANDWIDTH, "1000000")
.build();
return new DefaultLink(
new ProviderId("of", "foo"),
new ConnectPoint(deviceId(dev1), portNumber(port1)),
new ConnectPoint(deviceId(dev2), portNumber(port2)),
DIRECT, annotations);
}
@Before
public void setUp() throws Exception {
Config config = TestStoreManager.getTestConfig();
storeMgr = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
storeMgr.activate();
storeImpl = new TestHazelcastLinkResourceStore(storeMgr);
storeImpl.activate();
store = storeImpl;
link1 = newLink("of:1", 1, "of:2", 2);
link2 = newLink("of:2", 1, "of:3", 2);
link3 = newLink("of:3", 1, "of:4", 2);
}
@After
public void tearDown() throws Exception {
storeImpl.deactivate();
storeMgr.deactivate();
}
/**
* Tests constructor and activate method.
*/
@Test
public void testConstructorAndActivate() {
final Iterable<LinkResourceAllocations> allAllocations = store.getAllocations();
assertNotNull(allAllocations);
assertFalse(allAllocations.iterator().hasNext());
final Iterable<LinkResourceAllocations> linkAllocations =
store.getAllocations(link1);
assertNotNull(linkAllocations);
assertFalse(linkAllocations.iterator().hasNext());
final Set<ResourceAllocation> res = store.getFreeResources(link2);
assertNotNull(res);
}
/**
* Picks up and returns one of bandwidth allocations from a given set.
*
* @param resources the set of {@link ResourceAllocation}s
* @return {@link BandwidthResourceAllocation} object if found, null
* otherwise
*/
private BandwidthResourceAllocation getBandwidthObj(Set<ResourceAllocation> resources) {
for (ResourceAllocation res : resources) {
if (res.type() == ResourceType.BANDWIDTH) {
return ((BandwidthResourceAllocation) res);
}
}
return null;
}
/**
* Returns all lambda allocations from a given set.
*
* @param resources the set of {@link ResourceAllocation}s
* @return a set of {@link LambdaResourceAllocation} objects
*/
private Set<LambdaResourceAllocation> getLambdaObjs(Set<ResourceAllocation> resources) {
Set<LambdaResourceAllocation> lambdaResources = new HashSet<>();
for (ResourceAllocation res : resources) {
if (res.type() == ResourceType.LAMBDA) {
lambdaResources.add((LambdaResourceAllocation) res);
}
}
return lambdaResources;
}
/**
* Tests initial free bandwidth for a link.
*/
@Test
public void testInitialBandwidth() {
final Set<ResourceAllocation> freeRes = store.getFreeResources(link1);
assertNotNull(freeRes);
final BandwidthResourceAllocation alloc = getBandwidthObj(freeRes);
assertNotNull(alloc);
assertEquals(Bandwidth.valueOf(1000000.0), alloc.bandwidth());
}
/**
* Tests initial free lambda for a link.
*/
@Test
public void testInitialLambdas() {
final Set<ResourceAllocation> freeRes = store.getFreeResources(link3);
assertNotNull(freeRes);
final Set<LambdaResourceAllocation> res = getLambdaObjs(freeRes);
assertNotNull(res);
assertEquals(80, res.size());
}
public static final class TestHazelcastLinkResourceStore
extends HazelcastLinkResourceStore {
public TestHazelcastLinkResourceStore(StoreService storeMgr) {
super.storeService = storeMgr;
}
}
}