[ONOS-6443] Add eventually consistent map test commands
Change-Id: If6a1814bccbd95c5e01e344b2f39132760c6af89
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
index 850401f..3191696 100644
--- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
@@ -15,13 +15,21 @@
*/
package org.onosproject.distributedprimitives;
+import java.util.Map;
+
+import com.google.common.collect.Maps;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.EventuallyConsistentMap;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.WallClockTimestamp;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
@@ -31,6 +39,7 @@
* Simple application to test distributed primitives.
*/
@Component(immediate = true)
+@Service(value = DistributedPrimitivesTest.class)
public class DistributedPrimitivesTest {
private final Logger log = getLogger(getClass());
@@ -41,17 +50,33 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap();
@Activate
protected void activate() {
-
log.info("Distributed-Primitives-test app started");
appId = coreService.registerApplication(APP_NAME);
}
@Deactivate
protected void deactivate() {
-
log.info("Distributed-Primitives-test app Stopped");
}
+
+ /**
+ * Returns an eventually consistent test map by name.
+ *
+ * @param name the test map name
+ * @return the test map
+ */
+ public EventuallyConsistentMap<String, String> getEcMap(String name) {
+ return maps.computeIfAbsent(name, n -> storageService.<String, String>eventuallyConsistentMapBuilder()
+ .withName(name)
+ .withSerializer(KryoNamespaces.BASIC)
+ .withTimestampProvider((k, v) -> new WallClockTimestamp())
+ .build());
+ }
}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/EventuallyConsistentMapTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/EventuallyConsistentMapTestCommand.java
new file mode 100644
index 0000000..e0c5a11
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/EventuallyConsistentMapTestCommand.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2017-present 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.onosproject.distributedprimitives.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
+import org.onosproject.store.service.EventuallyConsistentMap;
+
+/**
+ * CLI command to manipulate a distributed map.
+ */
+@Command(scope = "onos", name = "ec-map-test",
+ description = "Manipulate an eventually consistent map")
+public class EventuallyConsistentMapTestCommand extends AbstractShellCommand {
+
+ @Argument(index = 0, name = "name",
+ description = "map name",
+ required = true, multiValued = false)
+ String name = null;
+
+ @Argument(index = 1, name = "operation",
+ description = "operation name",
+ required = true, multiValued = false)
+ String operation = null;
+
+ @Argument(index = 2, name = "key",
+ description = "first arg",
+ required = false, multiValued = false)
+ String arg1 = null;
+
+ @Argument(index = 3, name = "value1",
+ description = "second arg",
+ required = false, multiValued = false)
+ String arg2 = null;
+
+ @Argument(index = 4, name = "value2",
+ description = "third arg",
+ required = false, multiValued = false)
+ String arg3 = null;
+
+ EventuallyConsistentMap<String, String> map;
+
+ @Override
+ protected void execute() {
+ DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
+ map = test.getEcMap(name);
+
+ if ("get".equals(operation)) {
+ print(map.get(arg1));
+ } else if ("put".equals(operation)) {
+ map.put(arg1, arg2);
+ } else if ("size".equals(operation)) {
+ print("%d", map.size());
+ } else if ("isEmpty".equals(operation)) {
+ print("%b", map.isEmpty());
+ } else if ("clear".equals(operation)) {
+ map.clear();
+ } else if ("remove".equals(operation)) {
+ if (arg2 == null) {
+ print(map.remove(arg1));
+ } else {
+ map.remove(arg1, arg2);
+ }
+ } else if ("containsKey".equals(operation)) {
+ print("%b", map.containsKey(arg1));
+ } else if ("containsValue".equals(operation)) {
+ print("%b", map.containsValue(arg1));
+ }
+ }
+
+ void print(String value) {
+ if (value == null) {
+ print("null");
+ } else {
+ print("%s", value);
+ }
+ }
+}
diff --git a/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 310e8da..9d8b280 100644
--- a/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -44,6 +44,9 @@
<action class="org.onosproject.distributedprimitives.cli.ConsistentMapTestCommand"/>
</command>
<command>
+ <action class="org.onosproject.distributedprimitives.cli.EventuallyConsistentMapTestCommand"/>
+ </command>
+ <command>
<action class="org.onosproject.distributedprimitives.cli.ValueTestCommand"/>
</command>
<command>