Initial stc tests for distributed primitives

Change-Id: I82a2911df9a4852cb851732e220d498f9332e005
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/ConsistentMapTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/ConsistentMapTestCommand.java
new file mode 100644
index 0000000..d63a46c
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/ConsistentMapTestCommand.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2016-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.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+
+/**
+ * CLI command to manipulate a distributed value.
+ */
+@Command(scope = "onos", name = "map-test",
+        description = "Manipulate a consistent map")
+public class ConsistentMapTestCommand 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;
+
+    ConsistentMap<String, String> map;
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        map = storageService.<String, String>consistentMapBuilder()
+                                    .withName(name)
+                                    .withSerializer(Serializer.using(KryoNamespaces.BASIC))
+                                    .build();
+        if (operation.equals("get")) {
+            print(map.get(arg1));
+        } else if (operation.equals("put")) {
+            print(map.put(arg1, arg2));
+        } else if (operation.equals("size")) {
+            print("%d", map.size());
+        } else if (operation.equals("isEmpty")) {
+            print("%b", map.isEmpty());
+        } else if (operation.equals("putIfAbsent")) {
+            print(map.putIfAbsent(arg1, arg2));
+        } else if (operation.equals("putAndGet")) {
+            print(map.putAndGet(arg1, arg2));
+        } else if (operation.equals("clear")) {
+            map.clear();
+        } else if (operation.equals("remove")) {
+            if (arg2 == null) {
+                print(map.remove(arg1));
+            } else {
+                print("%b", map.remove(arg1, arg2));
+            }
+        } else if (operation.equals("containsKey")) {
+            print("%b", map.containsKey(arg1));
+        } else if (operation.equals("containsValue")) {
+            print("%b", map.containsValue(arg1));
+        } else if (operation.equals("replace")) {
+            if (arg3 == null) {
+                print(map.replace(arg1, arg2));
+            } else {
+                print("%b", map.replace(arg1, arg2, arg3));
+            }
+        }
+    }
+
+    void print(Versioned<String> value) {
+        if (value == null) {
+            print("null");
+        } else {
+            print("%s", value.value());
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestCommand.java
new file mode 100644
index 0000000..992e77e
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestCommand.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016-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.store.service.AtomicCounter;
+import org.onosproject.store.service.StorageService;
+
+/**
+ * CLI command to increment a distributed counter.
+ */
+@Command(scope = "onos", name = "counter-test",
+        description = "Manipulate a distributed counter")
+public class CounterTestCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "counter",
+            description = "Counter name",
+            required = true, multiValued = false)
+    String counter = null;
+
+    @Argument(index = 1, name = "operation",
+            description = "operation name",
+            required = true, multiValued = false)
+    String operation = null;
+
+    @Argument(index = 2, name = "value1",
+            description = "first arg",
+            required = false, multiValued = false)
+    Long value1 = null;
+
+    @Argument(index = 3, name = "value2",
+            description = "second arg",
+            required = false, multiValued = false)
+    Long value2 = null;
+
+    AtomicCounter atomicCounter;
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        atomicCounter = storageService.getAsyncAtomicCounter(counter).asAtomicCounter();
+        if (operation.equals("get")) {
+            print("%d", atomicCounter.get());
+        } else if (operation.equals("set")) {
+            atomicCounter.set(value1);
+        } else if (operation.equals("incrementAndGet")) {
+            print("%d", atomicCounter.incrementAndGet());
+        } else if (operation.equals("getAndIncrement")) {
+            print("%d", atomicCounter.getAndIncrement());
+        } else if (operation.equals("getAndAdd")) {
+            print("%d", atomicCounter.getAndAdd(value1));
+        } else if (operation.equals("addAndGet")) {
+            print("%d", atomicCounter.addAndGet(value1));
+        } else if (operation.equals("compareAndSet")) {
+            print("%b", atomicCounter.compareAndSet(value1, value2));
+        } else if (operation.equals("destroy")) {
+            atomicCounter.destroy();
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/LeaderElectorTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/LeaderElectorTestCommand.java
new file mode 100644
index 0000000..b7464c2
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/LeaderElectorTestCommand.java
@@ -0,0 +1,64 @@
+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.cluster.ClusterService;
+import org.onosproject.cluster.Leadership;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.store.service.LeaderElector;
+import org.onosproject.store.service.StorageService;
+
+import com.google.common.base.Joiner;
+
+@Command(scope = "onos", name = "leader-test",
+description = "LeaderElector test cli fixture")
+public class LeaderElectorTestCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "name",
+            description = "leader elector name",
+            required = true, multiValued = false)
+    String name = null;
+
+    @Argument(index = 1, name = "operation",
+            description = "operation",
+            required = true, multiValued = false)
+    String operation = null;
+
+
+    @Argument(index = 2, name = "topic",
+            description = "topic name",
+            required = false, multiValued = false)
+    String topic = null;
+
+    LeaderElector leaderElector;
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        ClusterService clusterService = get(ClusterService.class);
+        NodeId localNodeId = clusterService.getLocalNode().id();
+        leaderElector = storageService.leaderElectorBuilder()
+                .withName(name)
+                .build()
+                .asLeaderElector();
+        if (operation.equals("run")) {
+            print(leaderElector.run(topic, localNodeId));
+        } else if (operation.equals("withdraw")) {
+            leaderElector.withdraw(topic);
+        } else if (operation.equals("show")) {
+            print(leaderElector.getLeadership(topic));
+        }
+    }
+
+    private void print(Leadership leadership) {
+        if (leadership.leader() != null) {
+            print("leader=%s#term=%d#candidates=%s",
+                    leadership.leaderNodeId(),
+                    leadership.leader().term(),
+                    leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
+        } else {
+            print("leader=none#candidates=%s",
+                    leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/ValueTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/ValueTestCommand.java
new file mode 100644
index 0000000..fb736eb
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/ValueTestCommand.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2016-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.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.AtomicValue;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+
+/**
+ * CLI command to manipulate a distributed value.
+ */
+@Command(scope = "onos", name = "value-test",
+        description = "Manipulate a distributed value")
+public class ValueTestCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "value",
+            description = "Value name",
+            required = true, multiValued = false)
+    String value = null;
+
+    @Argument(index = 1, name = "operation",
+            description = "operation name",
+            required = true, multiValued = false)
+    String operation = null;
+
+    @Argument(index = 2, name = "value1",
+            description = "first arg",
+            required = false, multiValued = false)
+    String value1 = null;
+
+    @Argument(index = 3, name = "value2",
+            description = "second arg",
+            required = false, multiValued = false)
+    String value2 = null;
+
+    AtomicValue<String> atomicValue;
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        atomicValue = storageService.<String>atomicValueBuilder()
+                                    .withName(value)
+                                    .withSerializer(Serializer.using(KryoNamespaces.BASIC))
+                                    .build()
+                                    .asAtomicValue();
+        if (operation.equals("get")) {
+            print("%s", atomicValue.get());
+        } else if (operation.equals("set")) {
+            atomicValue.set(value1);
+        } else if (operation.equals("compareAndSet")) {
+            print("%b", atomicValue.compareAndSet(value1, value2));
+        } else if (operation.equals("destroy")) {
+            atomicValue.destroy();
+        }
+    }
+}