Creating a registry for gRPC services, early version restarts on each modification of the set of services.

Change-Id: Icf1c0cabef2d718cf3728c90cdf30855d54e65df
diff --git a/incubator/api/BUCK b/incubator/api/BUCK
index eb7b036..c606814 100644
--- a/incubator/api/BUCK
+++ b/incubator/api/BUCK
@@ -1,6 +1,7 @@
 COMPILE_DEPS = [
     '//lib:CORE_DEPS',
     '//lib:JACKSON',
+    '//lib:grpc-core'
 ]
 
 TEST_DEPS = [
diff --git a/incubator/api/pom.xml b/incubator/api/pom.xml
index fe86161..391ad3a 100644
--- a/incubator/api/pom.xml
+++ b/incubator/api/pom.xml
@@ -43,6 +43,11 @@
             <artifactId>easymock</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>io.grpc</groupId>
+            <artifactId>grpc-core</artifactId>
+            <version>1.2.0</version>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/grpc/GrpcServiceRegistry.java b/incubator/api/src/main/java/org/onosproject/incubator/grpc/GrpcServiceRegistry.java
new file mode 100644
index 0000000..0e33f2f
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/grpc/GrpcServiceRegistry.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2017-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.incubator.grpc;
+
+import io.grpc.BindableService;
+
+/**
+ * A service that allows for de/registration of gRPC services, and determining
+ * whether a service is present.
+ */
+public interface GrpcServiceRegistry {
+    /**
+     * Register a gRPC service with this registry.
+     * @param service the service to be registered
+     * @return true if the service was added and server successfully started,
+     * false otherwise
+     */
+    boolean register(BindableService service);
+
+    /**
+     * Unregister a gRPC service with this registry.
+     * @param service the service to be unregistered
+     * @return true if the service was removed and the server successfully
+     * started, false otherwise
+     */
+    boolean unregister(BindableService service);
+
+    /**
+     * Checks if an instance of the  provided serviceClass is currently
+     * registered with this registry.
+     * @param serviceClass the class being queries
+     * @return true if an instance of this specified class has been registered,
+     * false otherwise
+     */
+    boolean containsService(Class<BindableService> serviceClass);
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/grpc/package-info.java b/incubator/api/src/main/java/org/onosproject/incubator/grpc/package-info.java
new file mode 100644
index 0000000..eac386d
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/grpc/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * Abstractions for interacting with the gRPC subsystem.
+ */
+package org.onosproject.incubator.grpc;
diff --git a/incubator/protobuf/pom.xml b/incubator/protobuf/pom.xml
index 303c1ee..5d2c530 100644
--- a/incubator/protobuf/pom.xml
+++ b/incubator/protobuf/pom.xml
@@ -16,6 +16,18 @@
   -->
 <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/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
     <parent>
         <artifactId>onos-incubator-grpc-dependencies</artifactId>
         <groupId>org.onosproject</groupId>
@@ -54,6 +66,17 @@
             <scope>provided</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>compile</scope>
+        </dependency>
+
     </dependencies>
 
     <modules>
diff --git a/incubator/protobuf/src/main/java/org/onosproject/incubator/protobuf/net/GrpcServiceRegistryImpl.java b/incubator/protobuf/src/main/java/org/onosproject/incubator/protobuf/net/GrpcServiceRegistryImpl.java
new file mode 100644
index 0000000..8b7fd0f
--- /dev/null
+++ b/incubator/protobuf/src/main/java/org/onosproject/incubator/protobuf/net/GrpcServiceRegistryImpl.java
@@ -0,0 +1,207 @@
+/*
+ * 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.incubator.protobuf.net;
+
+import com.google.common.collect.Maps;
+import io.grpc.BindableService;
+import io.grpc.Server;
+import io.grpc.ServerBuilder;
+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.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.incubator.grpc.GrpcServiceRegistry;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Map;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+
+import static org.onlab.util.Tools.get;
+
+/**
+ * A basic implementation of {@link GrpcServiceRegistry} designed for use with
+ * built in gRPC services.
+ *
+ * NOTE: this is an early implementation in which the addition of any new
+ * service forces a restart of the server, this is sufficient for testing but
+ * inappropriate for deployment.
+ */
+@Service
+@Component(immediate = false)
+public class GrpcServiceRegistryImpl implements GrpcServiceRegistry {
+
+    private static final int DEFAULT_SERVER_PORT = 64000;
+    private static final int DEFAULT_SHUTDOWN_TIME = 1;
+    private static final AtomicBoolean servicesModifiedSinceStart = new AtomicBoolean(false);
+
+    private static final String PORT_PROPERTY_NAME = "listeningPort";
+
+    private final Map<Class<? extends BindableService>, BindableService> registeredServices =
+            Maps.newHashMap();
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private Server server;
+
+    /* It is currently the responsibility of the administrator to notify
+    clients of nonstandard port usage as there is no mechanism available to
+    discover the port hosting gRPC services.
+     */
+    @Property(name = PORT_PROPERTY_NAME, intValue = DEFAULT_SERVER_PORT,
+             label = "The port number which ONOS will use to host gRPC services.")
+    private int listeningPort = DEFAULT_SERVER_PORT;
+
+    @Activate
+    public void activate() {
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        attemptGracefulShutdownThenForce(DEFAULT_SHUTDOWN_TIME);
+        log.info("Stopped");
+    }
+
+    @Modified
+    public void modified(ComponentContext context) {
+        if (context != null) {
+            setProperties(context);
+        }
+        log.info("Connection was restarted to allow service to be added, " +
+                         "this is a temporary workaround");
+        restartServer(listeningPort);
+    }
+
+    @Override
+    public boolean register(BindableService service) {
+        synchronized (registeredServices) {
+            if (!registeredServices.containsKey(service.getClass())) {
+                registeredServices.put(service.getClass(), service);
+            } else {
+                log.warn("The specified class \"{}\" was not added becuase an " +
+                                 "instance of the class is already registered.",
+                         service.getClass().toString());
+                return false;
+            }
+        }
+        return restartServer(listeningPort);
+    }
+
+    @Override
+    public boolean unregister(BindableService service) {
+        synchronized (registeredServices) {
+            if (registeredServices.containsKey(service.getClass())) {
+                registeredServices.remove(service.getClass());
+            } else {
+                log.warn("The specified class \"{}\" was not removed because it " +
+                                 "was not present.", service.getClass().toString());
+                return false;
+            }
+        }
+        return restartServer(listeningPort);
+    }
+
+    @Override
+    public boolean containsService(Class<BindableService> serviceClass) {
+        return registeredServices.containsKey(serviceClass);
+    }
+
+    private void setProperties(ComponentContext context) {
+        Dictionary<String, Object> properties = context.getProperties();
+        String listeningPort = get(properties, PORT_PROPERTY_NAME);
+        this.listeningPort = listeningPort == null ? DEFAULT_SERVER_PORT :
+                Integer.parseInt(listeningPort.trim());
+    }
+
+    /**
+     * Attempts a graceful shutdown allowing {@code timeLimitSeconds} to elapse
+     * before forcing a shutdown.
+     *
+     * @param timeLimitSeconds time before a shutdown is forced in seconds
+     * @return true if the server is terminated, false otherwise
+     */
+    private boolean attemptGracefulShutdownThenForce(int timeLimitSeconds) {
+        if (!server.isShutdown()) {
+            server.shutdown();
+        }
+        try {
+            /*This is not conditional in case the server is shutdown but
+            handling requests submitted before shutdown was called.*/
+            server.awaitTermination(timeLimitSeconds, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            log.error("Awaiting server termination failed with error {}",
+                      e.getMessage());
+        }
+        if (!server.isTerminated()) {
+            server.shutdownNow();
+            try {
+                server.awaitTermination(10, TimeUnit.MILLISECONDS);
+            } catch (InterruptedException e) {
+                log.error("Server failed to terminate as expected with error" +
+                                  " {}", e.getMessage());
+            }
+        }
+        return server.isTerminated();
+    }
+
+    private boolean restartServer(int port) {
+        if (!attemptGracefulShutdownThenForce(DEFAULT_SHUTDOWN_TIME)) {
+            log.error("Shutdown failed, the previous server may still be" +
+                              " active.");
+        }
+        return createServerAndStart(port);
+    }
+
+    /**
+     * Creates a server with the set of registered services on the specified
+     * port.
+     *
+     * @param port the port on which this server will listen
+     * @return true if the server was started successfully, false otherwise
+     */
+    private boolean createServerAndStart(int port) {
+
+        ServerBuilder serverBuilder =
+                ServerBuilder.forPort(port);
+        synchronized (registeredServices) {
+            registeredServices.values().forEach(
+                    service -> serverBuilder.addService(service));
+        }
+        server = serverBuilder.build();
+        try {
+            server.start();
+        } catch (IllegalStateException e) {
+            log.error("The server could not be started because an existing " +
+                              "server is already running: {}", e.getMessage());
+            return false;
+        } catch (IOException e) {
+            log.error("The server could not be started due to a failure to " +
+                              "bind: {} ", e.getMessage());
+            return false;
+        }
+        return true;
+    }
+}
diff --git a/lib/BUCK b/lib/BUCK
index ce7e0a6..77dc5bf 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -1,4 +1,4 @@
-# ***** This file was auto-generated at Thu, 24 Aug 2017 21:35:03 GMT. Do not edit this file manually. *****
+# ***** This file was auto-generated at Fri, 25 Aug 2017 00:23:12 GMT. Do not edit this file manually. *****
 # ***** Use onos-lib-gen *****
 
 pass_thru_pom(
@@ -910,6 +910,42 @@
 )
 
 remote_jar (
+  name = 'catalyst-concurrent',
+  out = 'catalyst-concurrent-1.2.0.jar',
+  url = 'mvn:io.atomix.catalyst:catalyst-concurrent:jar:1.2.0',
+  sha1 = 'ba91527a1c0a68c8f46cc591ef0dded3d2d0c298',
+  maven_coords = 'io.atomix.catalyst:catalyst-concurrent:1.2.0',
+  visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
+  name = 'catalyst-netty',
+  out = 'catalyst-netty-1.2.0.jar',
+  url = 'mvn:io.atomix.catalyst:catalyst-netty:jar:1.2.0',
+  sha1 = 'abb694b6fe835eb66d30ae6979ec0f7e4ac2e738',
+  maven_coords = 'io.atomix.catalyst:catalyst-netty:1.2.0',
+  visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
+  name = 'catalyst-transport',
+  out = 'catalyst-transport-1.2.0.jar',
+  url = 'mvn:io.atomix.catalyst:catalyst-transport:jar:1.2.0',
+  sha1 = '1469017e168a5e611fa4c251273184a763e0cd7f',
+  maven_coords = 'io.atomix.catalyst:catalyst-transport:1.2.0',
+  visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
+  name = 'grpc-core',
+  out = 'grpc-core-1.2.0.jar',
+  url = 'mvn:io.grpc:grpc-core:jar:1.2.0',
+  sha1 = 'f12a213e2b59a0615df2cc9bed35dc15fd2fee37',
+  maven_coords = 'io.grpc:grpc-core:jar:NON-OSGI:1.2.0',
+  visibility = [ 'PUBLIC' ],
+)
+
+remote_jar (
   name = 'objenesis',
   out = 'objenesis-2.2.jar',
   url = 'mvn:org.objenesis:objenesis:jar:2.2',
diff --git a/lib/deps.json b/lib/deps.json
index 640f630..74e4b1e 100644
--- a/lib/deps.json
+++ b/lib/deps.json
@@ -196,6 +196,10 @@
     "netty-resolver": "mvn:io.netty:netty-resolver:4.1.8.Final",
     "netty-codec-http2": "mvn:io.netty:netty-codec-http2:4.1.8.Final",
     "netty-codec-http": "mvn:io.netty:netty-codec-http:4.1.8.Final",
+    "catalyst-concurrent": "mvn:io.atomix.catalyst:catalyst-concurrent:1.2.0",
+    "catalyst-netty": "mvn:io.atomix.catalyst:catalyst-netty:1.2.0",
+    "catalyst-transport": "mvn:io.atomix.catalyst:catalyst-transport:1.2.0",
+    "grpc-core": "mvn:io.grpc:grpc-core:1.2.0",
     "objenesis": "mvn:org.objenesis:objenesis:2.2",
     "openflowj": "mvn:org.onosproject:openflowj:3.2.0.onos",
     "org.apache.felix.scr": "mvn:org.apache.felix:org.apache.felix.scr:1.8.2",