Test application for counters and sets

Application to expose some of the distributed primitives to the cli for
system tests.

Change-Id: I275c8fb6790135c6150658ca3bb5ce356ea735a7
diff --git a/apps/test/distributed-primitives/pom.xml b/apps/test/distributed-primitives/pom.xml
new file mode 100644
index 0000000..1da9b6a
--- /dev/null
+++ b/apps/test/distributed-primitives/pom.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2015 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-apps-test</artifactId>
+        <version>1.2.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-distributed-primitives</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS app to test distributed primitives</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.distributedprimitives</onos.app.name>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+            <classifier>tests</classifier>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-cli</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-dist</artifactId>
+            <version>1.2.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+
+    </dependencies>
+
+</project>
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
new file mode 100644
index 0000000..f089228
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2015 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;
+
+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.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+
+/**
+ * Simple application to test distributed primitives.
+ */
+@Component(immediate = true)
+public class DistributedPrimitivesTest {
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String APP_NAME = "org.onosproject.distributedprimitives";
+    private ApplicationId appId;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+
+    @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");
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java
new file mode 100644
index 0000000..bf64f00
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2015 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.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.store.service.AsyncAtomicCounter;
+import org.onosproject.store.service.StorageService;
+import org.slf4j.Logger;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * CLI command to increment a distributed counter.
+ */
+@Command(scope = "onos", name = "counter-test-increment",
+        description = "Increment a distributed counter")
+public class CounterTestIncrementCommand extends AbstractShellCommand {
+
+    private final Logger log = getLogger(getClass());
+
+    @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
+            required = false, multiValued = false)
+    private boolean inMemory = false;
+
+    @Argument(index = 0, name = "counter",
+            description = "Counter name",
+            required = true, multiValued = false)
+    String counter = null;
+
+    AsyncAtomicCounter atomicCounter;
+
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        if (inMemory) {
+            atomicCounter = storageService.atomicCounterBuilder()
+                    .withName(counter)
+                    .withPartitionsDisabled()
+                    .buildAsyncCounter();
+        } else {
+            atomicCounter = storageService.atomicCounterBuilder()
+                    .withName(counter)
+                    .buildAsyncCounter();
+        }
+
+        CompletableFuture<Long> result = atomicCounter.incrementAndGet();
+        try {
+            print("%s was incremented to %d", counter, result.get(3, TimeUnit.SECONDS));
+        } catch (InterruptedException e) {
+            return;
+        } catch (ExecutionException e) {
+            e.printStackTrace();
+        } catch (TimeoutException e) {
+            e.printStackTrace();
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java
new file mode 100644
index 0000000..0ccc2d3
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2015 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.onlab.util.KryoNamespace;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * CLI command to add elements to a distributed set.
+ */
+@Command(scope = "onos", name = "set-test-add",
+        description = "Add to a distributed set")
+public class SetTestAddCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "setName",
+            description = "set name",
+            required = true, multiValued = false)
+    String setName = null;
+
+    @Argument(index = 1, name = "values",
+            description = "Value(s) to add to the set",
+            required = true, multiValued = true)
+    String[] values = null;
+
+    Set<String> set;
+    Set<String> toAdd = new HashSet<String>();
+
+
+    Serializer serializer = Serializer.using(
+            new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
+
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        set = storageService.<String>setBuilder()
+                .withName(setName)
+                .withSerializer(serializer)
+                .build();
+
+        // Add a single element to the set
+        if (values.length == 1) {
+            if (set.add(values[0])) {
+                print("[%s] was added to the set %s", values[0], setName);
+            } else {
+                print("[%s] was already in set %s", values[0], setName);
+            }
+        } else if (values.length >= 1) {
+            // Add multiple elements to a set
+            for (String value : values) {
+                toAdd.add(value);
+            }
+            if (set.addAll(toAdd)) {
+                print("%s was added to the set %s", toAdd, setName);
+            } else {
+                print("%s was already in set %s", toAdd, setName);
+            }
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java
new file mode 100644
index 0000000..19d3216
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2015 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.apache.karaf.shell.commands.Option;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * CLI command to get the elements in a distributed set.
+ */
+@Command(scope = "onos", name = "set-test-get",
+        description = "Get the elements in a distributed set")
+public class SetTestGetCommand extends AbstractShellCommand {
+
+    @Option(name = "-s", aliases = "--size", description = "Also show the size of the set?",
+            required = false, multiValued = false)
+    private boolean size = false;
+
+    @Argument(index = 0, name = "setName",
+            description = "set name",
+            required = true, multiValued = false)
+    String setName = null;
+
+    @Argument(index = 1, name = "values",
+            description = "Check if the set contains these value(s)",
+            required = false, multiValued = true)
+    String[] values = null;
+
+    Set<String> set;
+    Set<String> toCheck = new HashSet<String>();
+    String output = new String();
+
+    Serializer serializer = Serializer.using(
+            new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
+
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        set = storageService.<String>setBuilder()
+                .withName(setName)
+                .withSerializer(serializer)
+                .build();
+
+        // Print the set size
+        if (size) {
+            print("There are %d items in set %s:", set.size(), setName);
+        } else {
+            print("Items in set %s:", setName);
+        }
+        // Print the set
+        if (set.isEmpty()) {
+            print("[]");
+        } else {
+            for (String e : set.toArray(new String[0])) {
+                if (output.isEmpty()) {
+                    output += e;
+                } else {
+                    output += ", " + e;
+                }
+            }
+            print("[%s]", output);
+        }
+        // Check if given values are in the set
+        if (values.length == 1) {
+            // contains
+            if (set.contains(values[0])) {
+                print("Set %s contains the value %s", setName, values[0]);
+            } else {
+                print("Set %s did not contain the value %s", setName, values[0]);
+            }
+        } else if (values.length > 1) {
+            //containsAll
+            for (String value : values) {
+                toCheck.add(value);
+            }
+            if (set.containsAll(toCheck)) {
+                print("Set %s contains the the subset %s", setName, toCheck);
+            } else {
+                print("Set %s did not contain the the subset %s", setName, toCheck);
+            }
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java
new file mode 100644
index 0000000..7e3a3e8
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2015 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.apache.karaf.shell.commands.Option;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * CLI command to remove elements from a distributed set.
+ */
+@Command(scope = "onos", name = "set-test-remove",
+        description = "Remove from a distributed set")
+public class SetTestRemoveCommand extends AbstractShellCommand {
+
+    @Option(name = "-r", aliases = "--retain",
+            description = "Only keep the given values in the set (if they already exist in the set)",
+            required = false, multiValued = false)
+    private boolean retain = false;
+
+    @Option(name = "-c", aliases = "--clear", description = "Clear the set of all values",
+            required = false, multiValued = false)
+    private boolean clear = false;
+
+    @Argument(index = 0, name = "setName",
+            description = "set name",
+            required = true, multiValued = false)
+    String setName = null;
+
+    @Argument(index = 1, name = "values",
+            description = "Value(s) to remove from the set",
+            required = false, multiValued = true)
+    String[] values = null;
+
+    Set<String> set;
+    Set<String> givenValues = new HashSet<String>();
+    Serializer serializer = Serializer.using(
+            new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
+
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        set = storageService.<String>setBuilder()
+                .withName(setName)
+                .withSerializer(serializer)
+                .build();
+
+        if (clear) {
+            set.clear();
+            print("Set %s cleared", setName);
+            return;
+        }
+
+        if (values == null) {
+            print("Error executing command: No value given");
+            return;
+        }
+
+        if (retain) { // Keep only the given values
+            for (String value : values) {
+                givenValues.add(value);
+            }
+            if (set.retainAll(givenValues)) {
+                print("%s was pruned to contain only elements of set %s", setName, givenValues);
+            } else {
+                print("%s was not changed by retaining only elements of the set %s", setName, givenValues);
+            }
+        } else if (values.length == 1) {
+            // Remove a single element from the set
+            if (set.remove(values[0])) {
+                print("[%s] was removed from the set %s", values[0], setName);
+            } else {
+                print("[%s] was not in set %s", values[0], setName);
+            }
+        } else if (values.length >= 1) {
+            // Remove multiple elements from a set
+            for (String value : values) {
+                givenValues.add(value);
+            }
+            if (set.removeAll(givenValues)) {
+                print("%s was removed from the set %s", givenValues, setName);
+            } else {
+                print("No element of %s was in set %s", givenValues, setName);
+            }
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java
new file mode 100644
index 0000000..53ed805
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * Distributed Primitives test command-line handlers.
+ */
+package org.onosproject.distributedprimitives.cli;
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java
new file mode 100644
index 0000000..73c4181
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * Sample application for use in various experiments with distributed primitives.
+ */
+package org.onosproject.distributedprimitives;
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
new file mode 100644
index 0000000..00ad975
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,33 @@
+<!--
+  ~ Copyright 2015 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.
+  -->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+        <command>
+            <action class="org.onosproject.distributedprimitives.cli.CounterTestIncrementCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.distributedprimitives.cli.SetTestAddCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.distributedprimitives.cli.SetTestGetCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.distributedprimitives.cli.SetTestRemoveCommand"/>
+        </command>
+    </command-bundle>
+
+</blueprint>
diff --git a/apps/test/pom.xml b/apps/test/pom.xml
index bda05d0..f3b717e 100644
--- a/apps/test/pom.xml
+++ b/apps/test/pom.xml
@@ -36,6 +36,7 @@
         <module>intent-perf</module>
         <module>messaging-perf</module>
         <module>demo</module>
+        <module>distributed-primitives</module>
     </modules>
 
 </project>