Search in sources :

Example 1 with ConsistentMap

use of org.onosproject.store.service.ConsistentMap in project onos by opennetworkinglab.

the class DefaultConsistentMapTest method testBehavior.

/**
 * Tests the behavior of public APIs of the default consistent map
 * implmentation.
 */
@Test
public void testBehavior() {
    Map<String, String> baseMap = new HashMap<>();
    AsyncConsistentMapMock<String, String> asyncMap = new AsyncConsistentMapMock<>(baseMap);
    ConsistentMap<String, String> newMap = new DefaultConsistentMap<>(asyncMap, 11);
    assertThat(newMap.size(), is(0));
    assertThat(newMap.isEmpty(), is(true));
    newMap.put(KEY1, VALUE1);
    assertThat(newMap.size(), is(1));
    assertThat(newMap.get(KEY1).value(), is(VALUE1));
    assertThat(newMap.containsKey(KEY1), is(true));
    assertThat(newMap.containsKey(VALUE1), is(false));
    assertThat(newMap.containsValue(VALUE1), is(true));
    assertThat(newMap.containsValue(KEY1), is(false));
    assertThat(newMap.keySet(), hasSize(1));
    assertThat(newMap.keySet(), hasItem(KEY1));
    assertThat(newMap.values(), hasSize(1));
    assertThat(newMap.values(), hasItem(new Versioned<>(VALUE1, 0, 0)));
    assertThat(newMap.entrySet(), hasSize(1));
    Map.Entry<String, Versioned<String>> entry = newMap.entrySet().iterator().next();
    assertThat(entry.getKey(), is(KEY1));
    assertThat(entry.getValue().value(), is(VALUE1));
    newMap.putIfAbsent(KEY2, VALUE2);
    assertThat(newMap.entrySet(), hasSize(2));
    assertThat(newMap.get(KEY2).value(), is(VALUE2));
    newMap.putIfAbsent(KEY2, VALUE1);
    assertThat(newMap.entrySet(), hasSize(2));
    assertThat(newMap.get(KEY2).value(), is(VALUE2));
    newMap.putAndGet(KEY3, VALUE3);
    assertThat(newMap.entrySet(), hasSize(3));
    assertThat(newMap.get(KEY3).value(), is(VALUE3));
    newMap.putIfAbsent(KEY3, VALUE1);
    assertThat(newMap.entrySet(), hasSize(3));
    assertThat(newMap.get(KEY3).value(), is(VALUE3));
    assertThat(newMap.computeIfAbsent(KEY4, this::computeFunction).value(), is(VALUE4));
    assertThat(computeFunctionCalls, is(1));
    assertThat(newMap.entrySet(), hasSize(4));
    assertThat(newMap.computeIfAbsent(KEY4, this::computeFunction).value(), is(VALUE4));
    assertThat(computeFunctionCalls, is(1));
    Map javaMap = newMap.asJavaMap();
    assertThat(javaMap.size(), is(newMap.size()));
    assertThat(javaMap.get(KEY1), is(VALUE1));
    assertThat(newMap.toString(), containsString(KEY4 + "=" + VALUE4));
    assertThat(newMap.remove(KEY4).value(), is(VALUE4));
    assertThat(newMap.entrySet(), hasSize(3));
    assertThat(newMap.remove(KEY4).value(), nullValue());
    assertThat(newMap.entrySet(), hasSize(3));
    assertThat(newMap.remove(KEY3, DEFAULT_VERSION), is(true));
    assertThat(newMap.entrySet(), hasSize(2));
    assertThat(newMap.remove(KEY3, DEFAULT_VERSION), is(false));
    assertThat(newMap.entrySet(), hasSize(2));
    assertThat(newMap.remove(KEY2, VALUE2), is(true));
    assertThat(newMap.entrySet(), hasSize(1));
    assertThat(newMap.remove(KEY2, VALUE2), is(false));
    assertThat(newMap.entrySet(), hasSize(1));
    assertThat(newMap.replace(KEY1, VALUE4).value(), is(VALUE1));
    assertThat(newMap.get(KEY1).value(), is(VALUE4));
    assertThat(newMap.replace(KEY1, VALUE4, VALUE2), is(true));
    assertThat(newMap.get(KEY1).value(), is(VALUE2));
    assertThat(newMap.replace(KEY1, DEFAULT_VERSION, VALUE1), is(true));
    assertThat(newMap.get(KEY1).value(), is(VALUE1));
    newMap.clear();
    assertThat(newMap.size(), is(0));
    newMap.compute(KEY1, (a, b) -> VALUE1);
    assertThat(newMap.get(KEY1).value(), is(VALUE1));
    newMap.computeIfPresent(KEY1, (a, b) -> VALUE2);
    assertThat(newMap.get(KEY1).value(), is(VALUE2));
    Listener listener1 = new Listener(1);
    newMap.addListener(listener1, null);
    assertThat(asyncMap.listeners, hasSize(1));
    assertThat(asyncMap.listeners, hasItem(listener1));
    newMap.removeListener(listener1);
    assertThat(asyncMap.listeners, hasSize(0));
    Consumer<DistributedPrimitive.Status> consumer = status -> {
    };
    newMap.addStatusChangeListener(consumer);
    assertThat(newMap.statusChangeListeners(), hasSize(1));
    assertThat(newMap.statusChangeListeners(), hasItem(consumer));
    newMap.removeStatusChangeListener(consumer);
    assertThat(newMap.statusChangeListeners(), hasSize(0));
    assertThat(newMap.statusChangeListeners(), not(hasItem(consumer)));
}
Also used : ConsistentMap(org.onosproject.store.service.ConsistentMap) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) BiFunction(java.util.function.BiFunction) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DistributedPrimitive(org.onosproject.store.service.DistributedPrimitive) ArrayList(java.util.ArrayList) MapEventListener(org.onosproject.store.service.MapEventListener) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Versioned(org.onosproject.store.service.Versioned) List(java.util.List) MapEvent(org.onosproject.store.service.MapEvent) AsyncConsistentMapAdapter(org.onosproject.store.service.AsyncConsistentMapAdapter) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) MapEventListener(org.onosproject.store.service.MapEventListener) Versioned(org.onosproject.store.service.Versioned) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ConsistentMap(org.onosproject.store.service.ConsistentMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ConsistentMap

use of org.onosproject.store.service.ConsistentMap in project onos by opennetworkinglab.

the class ConsistentMapTestCommand method doExecute.

@Override
protected void doExecute() {
    StorageService storageService = get(StorageService.class);
    map = storageService.<String, String>consistentMapBuilder().withName(name).withSerializer(Serializer.using(KryoNamespaces.BASIC)).withVersion(Version.version("1.0.0")).withCompatibilityFunction((value, version) -> version + ":" + value).build();
    if ("get".equals(operation)) {
        print(map.get(arg1));
    } else if ("put".equals(operation)) {
        print(map.put(arg1, arg2));
    } else if ("size".equals(operation)) {
        print("%d", map.size());
    } else if ("isEmpty".equals(operation)) {
        print("%b", map.isEmpty());
    } else if ("putIfAbsent".equals(operation)) {
        print(map.putIfAbsent(arg1, arg2));
    } else if ("putAndGet".equals(operation)) {
        print(map.putAndGet(arg1, arg2));
    } else if ("clear".equals(operation)) {
        map.clear();
    } else if ("remove".equals(operation)) {
        if (arg2 == null) {
            print(map.remove(arg1));
        } else {
            print("%b", map.remove(arg1, arg2));
        }
    } else if ("containsKey".equals(operation)) {
        print("%b", map.containsKey(arg1));
    } else if ("containsValue".equals(operation)) {
        print("%b", map.containsValue(arg1));
    } else if ("replace".equals(operation)) {
        if (arg3 == null) {
            print(map.replace(arg1, arg2));
        } else {
            print("%b", map.replace(arg1, arg2, arg3));
        }
    } else if ("compatiblePut".equals(operation)) {
        ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder().withName(name).withSerializer(Serializer.using(KryoNamespaces.BASIC)).withCompatibilityFunction((value, version) -> version + ":" + value).withVersion(Version.version("2.0.0")).build();
        print(map.put(arg1, arg2));
    } else if ("compatibleGet".equals(operation)) {
        ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder().withName(name).withSerializer(Serializer.using(KryoNamespaces.BASIC)).withCompatibilityFunction((value, version) -> version + ":" + value).withVersion(Version.version("2.0.0")).build();
        print(map.get(arg1));
    }
}
Also used : ConsistentMap(org.onosproject.store.service.ConsistentMap) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) Serializer(org.onosproject.store.service.Serializer) Versioned(org.onosproject.store.service.Versioned) StorageService(org.onosproject.store.service.StorageService) Service(org.apache.karaf.shell.api.action.lifecycle.Service) Version(org.onosproject.core.Version) Argument(org.apache.karaf.shell.api.action.Argument) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) Command(org.apache.karaf.shell.api.action.Command) ConsistentMap(org.onosproject.store.service.ConsistentMap) StorageService(org.onosproject.store.service.StorageService)

Example 3 with ConsistentMap

use of org.onosproject.store.service.ConsistentMap in project onos by opennetworkinglab.

the class ECFlowRuleStoreTest method setUp.

@Before
public void setUp() throws Exception {
    flowStoreImpl = new ECFlowRuleStore();
    flowStoreImpl.storageService = new TestStorageService() {

        @Override
        public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
            return new ConsistentMapBuilder<K, V>() {

                @Override
                public AsyncConsistentMap<K, V> buildAsyncMap() {
                    return new AsyncConsistentMapAdapter<K, V>();
                }

                @Override
                public ConsistentMap<K, V> build() {
                    return null;
                }
            };
        }
    };
    ReplicaInfoManager replicaInfoManager = new ReplicaInfoManager();
    replicaInfoManager.mastershipService = new MasterOfAll();
    flowStoreImpl.replicaInfoManager = replicaInfoManager;
    mockClusterService = createMock(ClusterService.class);
    flowStoreImpl.clusterService = mockClusterService;
    nodeId = new NodeId("1");
    mockControllerNode = new MockControllerNode(nodeId);
    expect(mockClusterService.getLocalNode()).andReturn(mockControllerNode).anyTimes();
    replay(mockClusterService);
    flowStoreImpl.clusterCommunicator = new ClusterCommunicationServiceAdapter();
    flowStoreImpl.mastershipService = new MasterOfAll();
    flowStoreImpl.deviceService = new DeviceServiceAdapter();
    flowStoreImpl.coreService = new CoreServiceAdapter();
    flowStoreImpl.configService = new ComponentConfigAdapter();
    flowStoreImpl.persistenceService = new PersistenceServiceAdapter();
    flowStoreImpl.activate(context);
}
Also used : PersistenceServiceAdapter(org.onosproject.store.persistence.PersistenceServiceAdapter) TestStorageService(org.onosproject.store.service.TestStorageService) ClusterCommunicationServiceAdapter(org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter) ComponentConfigAdapter(org.onosproject.cfg.ComponentConfigAdapter) ClusterService(org.onosproject.cluster.ClusterService) CoreServiceAdapter(org.onosproject.core.CoreServiceAdapter) ConsistentMap(org.onosproject.store.service.ConsistentMap) AsyncConsistentMap(org.onosproject.store.service.AsyncConsistentMap) NodeId(org.onosproject.cluster.NodeId) AsyncConsistentMap(org.onosproject.store.service.AsyncConsistentMap) ConsistentMapBuilder(org.onosproject.store.service.ConsistentMapBuilder) DeviceServiceAdapter(org.onosproject.net.device.DeviceServiceAdapter) Before(org.junit.Before)

Aggregations

ConsistentMap (org.onosproject.store.service.ConsistentMap)3 Versioned (org.onosproject.store.service.Versioned)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Executor (java.util.concurrent.Executor)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Argument (org.apache.karaf.shell.api.action.Argument)1 Command (org.apache.karaf.shell.api.action.Command)1 Service (org.apache.karaf.shell.api.action.lifecycle.Service)1 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)1 CoreMatchers.not (org.hamcrest.CoreMatchers.not)1 CoreMatchers.nullValue (org.hamcrest.CoreMatchers.nullValue)1