Add test application for node/mastership based remote Java proxies

Change-Id: I90a7d03016d331f07c7f05ba355514c46a5e4b4f
diff --git a/apps/test/pom.xml b/apps/test/pom.xml
index 7d47886..f731fcd 100644
--- a/apps/test/pom.xml
+++ b/apps/test/pom.xml
@@ -41,6 +41,7 @@
         <module>demo</module>
         <module>distributed-primitives</module>
         <module>netcfg-monitor</module>
+        <module>proxy</module>
     </modules>
 
 </project>
diff --git a/apps/test/proxy/BUCK b/apps/test/proxy/BUCK
new file mode 100644
index 0000000..4b443d3
--- /dev/null
+++ b/apps/test/proxy/BUCK
@@ -0,0 +1,20 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:org.apache.karaf.shell.console',
+    '//cli:onos-cli',
+    '//utils/rest:onlab-rest',
+    '//lib:javax.ws.rs-api',
+    '//core/store/serializers:onos-core-serializers',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+)
+
+onos_app (
+    app_name = 'org.onosproject.proxytest',
+    title = 'Proxy Test',
+    category = 'Test Utility',
+    url = 'http://onosproject.org',
+    description = 'Proxy test application.',
+)
diff --git a/apps/test/proxy/pom.xml b/apps/test/proxy/pom.xml
new file mode 100644
index 0000000..60435f2
--- /dev/null
+++ b/apps/test/proxy/pom.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2018-present Open Networking Foundation
+  ~
+  ~ 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.13.2-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-app-proxy-test</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Proxy test application</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.proxytest</onos.app.name>
+        <onos.app.title>Proxy Test App</onos.app.title>
+        <onos.app.category>Test</onos.app.category>
+        <onos.app.url>http://onosproject.org</onos.app.url>
+        <onos.app.readme>Proxy test application.</onos.app.readme>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-cli</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-serializers</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+        <!-- Required for javadoc generation -->
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>
diff --git a/apps/test/proxy/src/main/java/org/onosproject/proxytest/ProxyTest.java b/apps/test/proxy/src/main/java/org/onosproject/proxytest/ProxyTest.java
new file mode 100644
index 0000000..1edc4a6
--- /dev/null
+++ b/apps/test/proxy/src/main/java/org/onosproject/proxytest/ProxyTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.proxytest;
+
+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.cluster.NodeId;
+import org.onosproject.cluster.ProxyFactory;
+import org.onosproject.cluster.ProxyService;
+import org.onosproject.core.CoreService;
+import org.onosproject.mastership.MastershipProxyFactory;
+import org.onosproject.mastership.MastershipProxyService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.Serializer;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Proxy test application.
+ */
+@Component(immediate = true)
+@Service(value = ProxyTest.class)
+public class ProxyTest {
+
+    private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String APP_NAME = "org.onosproject.proxytest";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ProxyService proxyService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MastershipProxyService mastershipProxyService;
+
+    private ProxyFactory<TestProxy> proxyFactory;
+    private MastershipProxyFactory<TestProxy> mastershipProxyFactory;
+
+    @Activate
+    protected void activate() {
+        coreService.registerApplication(APP_NAME);
+        proxyService.registerProxyService(TestProxy.class, new TestProxyImpl(), SERIALIZER);
+        mastershipProxyService.registerProxyService(TestProxy.class, new TestProxyImpl(), SERIALIZER);
+        proxyFactory = proxyService.getProxyFactory(TestProxy.class, SERIALIZER);
+        mastershipProxyFactory = mastershipProxyService.getProxyFactory(TestProxy.class, SERIALIZER);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        proxyService.unregisterProxyService(TestProxy.class);
+        mastershipProxyService.unregisterProxyService(TestProxy.class);
+        log.info("Stopped");
+    }
+
+    /**
+     * Returns a proxy for the given node.
+     *
+     * @param nodeId the node for which to return the proxy
+     * @return the proxy for the given node
+     */
+    public TestProxy getProxyFor(NodeId nodeId) {
+        return proxyFactory.getProxyFor(nodeId);
+    }
+
+    /**
+     * Returns a mastership-based proxy for the given device.
+     *
+     * @param deviceId the device for which to return the proxy
+     * @return the proxy for the given device
+     */
+    public TestProxy getProxyFor(DeviceId deviceId) {
+        return mastershipProxyFactory.getProxyFor(deviceId);
+    }
+}
diff --git a/apps/test/proxy/src/main/java/org/onosproject/proxytest/ProxyTestCommand.java b/apps/test/proxy/src/main/java/org/onosproject/proxytest/ProxyTestCommand.java
new file mode 100644
index 0000000..1cb64e8
--- /dev/null
+++ b/apps/test/proxy/src/main/java/org/onosproject/proxytest/ProxyTestCommand.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.proxytest;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Proxy test command.
+ */
+@Command(scope = "onos", name = "proxy-test", description = "Manipulate a distributed proxy")
+public class ProxyTestCommand extends AbstractShellCommand {
+
+    @Argument(
+        index = 0,
+        name = "operation",
+        description = "Operation name",
+        required = true,
+        multiValued = false)
+    String operation;
+
+    @Argument(
+        index = 1,
+        name = "type",
+        description = "Operation type, either \"node\" or \"master\"",
+        required = true,
+        multiValued = false)
+    String type;
+
+    @Argument(
+        index = 2,
+        name = "arg1",
+        description = "Operation argument, either a device or node identifier",
+        required = true,
+        multiValued = false)
+    String arg1;
+
+    @Argument(
+        index = 3,
+        name = "arg2",
+        description = "Operation argument",
+        required = true,
+        multiValued = false)
+    String arg2;
+
+    @Override
+    protected void execute() {
+        ProxyTest proxyTest = get(ProxyTest.class);
+        TestProxy proxy;
+        if ("node".equals(type)) {
+            NodeId nodeId = NodeId.nodeId(arg1);
+            proxy = proxyTest.getProxyFor(nodeId);
+        } else if ("master".equals(type)) {
+            DeviceId deviceId = DeviceId.deviceId(arg1);
+            proxy = proxyTest.getProxyFor(deviceId);
+        } else {
+            throw new IllegalArgumentException("Unknown operation type " + type);
+        }
+
+        if ("sync".equals(operation)) {
+            print("%s", proxy.testSync(arg2));
+        } else if ("async".equals(operation)) {
+            try {
+                print("%s", proxy.testAsync(arg2).get(10, TimeUnit.SECONDS));
+            } catch (InterruptedException | ExecutionException | TimeoutException e) {
+                throw new IllegalStateException(e);
+            }
+        } else {
+            throw new IllegalArgumentException("Unknown operation " + operation);
+        }
+    }
+}
diff --git a/apps/test/proxy/src/main/java/org/onosproject/proxytest/TestProxy.java b/apps/test/proxy/src/main/java/org/onosproject/proxytest/TestProxy.java
new file mode 100644
index 0000000..2c2393c
--- /dev/null
+++ b/apps/test/proxy/src/main/java/org/onosproject/proxytest/TestProxy.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.proxytest;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Test proxy interface.
+ */
+public interface TestProxy {
+
+    /**
+     * Synchronous test method.
+     *
+     * @param arg the test argument
+     * @return the test result
+     */
+    String testSync(String arg);
+
+    /**
+     * Asynchronous test method.
+     *
+     * @param arg the test argument
+     * @return the test result
+     */
+    CompletableFuture<String> testAsync(String arg);
+
+}
diff --git a/apps/test/proxy/src/main/java/org/onosproject/proxytest/TestProxyImpl.java b/apps/test/proxy/src/main/java/org/onosproject/proxytest/TestProxyImpl.java
new file mode 100644
index 0000000..fb078c2
--- /dev/null
+++ b/apps/test/proxy/src/main/java/org/onosproject/proxytest/TestProxyImpl.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.proxytest;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Test proxy implementation.
+ */
+public class TestProxyImpl implements TestProxy {
+    @Override
+    public String testSync(String arg) {
+        return arg;
+    }
+
+    @Override
+    public CompletableFuture<String> testAsync(String arg) {
+        return CompletableFuture.completedFuture(arg);
+    }
+}
diff --git a/apps/test/proxy/src/main/java/org/onosproject/proxytest/package-info.java b/apps/test/proxy/src/main/java/org/onosproject/proxytest/package-info.java
new file mode 100644
index 0000000..9eea8ca
--- /dev/null
+++ b/apps/test/proxy/src/main/java/org/onosproject/proxytest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * Proxy test application.
+ */
+package org.onosproject.proxytest;
diff --git a/apps/test/proxy/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/test/proxy/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..9519234
--- /dev/null
+++ b/apps/test/proxy/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,22 @@
+<!--
+  ~ Copyright 2018-present Open Networking Foundation
+  ~
+  ~ 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.proxytest.ProxyTestCommand"/>
+        </command>
+    </command-bundle>
+</blueprint>
diff --git a/modules.defs b/modules.defs
index f142a4b..16e8d8d 100644
--- a/modules.defs
+++ b/modules.defs
@@ -196,6 +196,7 @@
     '//apps/test/netcfg-monitor:onos-apps-test-netcfg-monitor-oar',
     '//apps/test/messaging-perf:onos-apps-test-messaging-perf-oar',
     '//apps/test/primitive-perf:onos-apps-test-primitive-perf-oar',
+    '//apps/test/proxy:onos-apps-test-proxy-oar',
     '//apps/test/transaction-perf:onos-apps-test-transaction-perf-oar',
     '//apps/virtualbng:onos-apps-virtualbng-oar',
     '//apps/vpls:onos-apps-vpls-oar',