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

Change-Id: I4ee69873508127ad5362563bc7f0e1c7ac4996aa
diff --git a/incubator/protobuf/api/BUCK b/incubator/protobuf/api/BUCK
new file mode 100644
index 0000000..9ae1ccf
--- /dev/null
+++ b/incubator/protobuf/api/BUCK
@@ -0,0 +1,15 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//incubator/grpc-dependencies:grpc-core-repkg-1.3.0'
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+]
+
+osgi_jar_with_tests (
+    name = 'onos-grpc-api',
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+    visibility = ['PUBLIC'],
+)
diff --git a/incubator/protobuf/api/src/main/java/org/onosproject/protobuf/api/GrpcServiceRegistry.java b/incubator/protobuf/api/src/main/java/org/onosproject/protobuf/api/GrpcServiceRegistry.java
new file mode 100644
index 0000000..5e7e4bc
--- /dev/null
+++ b/incubator/protobuf/api/src/main/java/org/onosproject/protobuf/api/GrpcServiceRegistry.java
@@ -0,0 +1,52 @@
+/*
+ * 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.protobuf.api;
+
+import com.google.common.annotations.Beta;
+import io.grpc.BindableService;
+
+/**
+ * A service that allows for de/registration of gRPC services, and determining
+ * whether a service is present.
+ */
+@Beta
+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/protobuf/api/src/main/java/org/onosproject/protobuf/api/package-info.java b/incubator/protobuf/api/src/main/java/org/onosproject/protobuf/api/package-info.java
new file mode 100644
index 0000000..0541b49
--- /dev/null
+++ b/incubator/protobuf/api/src/main/java/org/onosproject/protobuf/api/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.protobuf.api;
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/registry/BUCK b/incubator/protobuf/registry/BUCK
new file mode 100644
index 0000000..e6a339d
--- /dev/null
+++ b/incubator/protobuf/registry/BUCK
@@ -0,0 +1,21 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:NETTY',
+    '//lib:GRPC_1.3',
+    '//incubator/grpc-dependencies:grpc-core-repkg-1.3.0',
+    '//incubator/protobuf/api:onos-grpc-api'
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+)
+
+onos_app (
+    title = 'Grpc Service Registry',
+    app_name = "org.onosproject.grpc.registry",
+    included_bundles = ['//incubator/grpc-dependencies:grpc-core-repkg-1.3.0',
+        '//lib:google-instrumentation-0.3.0'],
+    category = 'TODO',
+    url = 'http://onosproject.org',
+    description = 'Service providing connections for remote apps communicating with the ONOS core via gRPC.',
+)
diff --git a/incubator/protobuf/registry/src/main/java/org/onosproject/protobuf/registry/GrpcServiceRegistryImpl.java b/incubator/protobuf/registry/src/main/java/org/onosproject/protobuf/registry/GrpcServiceRegistryImpl.java
new file mode 100644
index 0000000..2304f0e
--- /dev/null
+++ b/incubator/protobuf/registry/src/main/java/org/onosproject/protobuf/registry/GrpcServiceRegistryImpl.java
@@ -0,0 +1,202 @@
+/*
+ * 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.protobuf.registry;
+
+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.protobuf.api.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.TimeUnit;
+
+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 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/incubator/protobuf/registry/src/main/java/org/onosproject/protobuf/registry/package-info.java b/incubator/protobuf/registry/src/main/java/org/onosproject/protobuf/registry/package-info.java
new file mode 100644
index 0000000..818c4e8
--- /dev/null
+++ b/incubator/protobuf/registry/src/main/java/org/onosproject/protobuf/registry/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.
+ */
+
+/**
+ * Services for interacting remotely with the gRPC subsystem.
+ */
+package org.onosproject.protobuf.registry;
diff --git a/incubator/protobuf/services/nb/BUCK b/incubator/protobuf/services/nb/BUCK
index 6f582e0..1ce40c2 100644
--- a/incubator/protobuf/services/nb/BUCK
+++ b/incubator/protobuf/services/nb/BUCK
@@ -7,7 +7,8 @@
     '//lib:protobuf-java-3.2.0',
     '//lib:GRPC_1.3',
     '//incubator/grpc-dependencies:grpc-core-repkg-1.3.0',
-    '//lib:grpc-protobuf-lite-1.3.0'
+    '//lib:grpc-protobuf-lite-1.3.0',
+    '//incubator/protobuf/api:onos-grpc-api'
 ]
 
 GRPC_DEPS = [
@@ -15,7 +16,7 @@
     '//incubator/grpc-dependencies:grpc-core-repkg-1.3.0',
     '//incubator/protobuf/models:onos-incubator-protobuf-models-proto',
     '//lib:protobuf-java-3.2.0',
-    '//lib:guava'
+    '//lib:guava',
 ]
 
 BUNDLES = [
diff --git a/incubator/protobuf/services/nb/pom.xml b/incubator/protobuf/services/nb/pom.xml
index 43be7ce..5316c89 100644
--- a/incubator/protobuf/services/nb/pom.xml
+++ b/incubator/protobuf/services/nb/pom.xml
@@ -41,6 +41,11 @@
             <artifactId>onos-incubator-protobuf-models</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-protobuf</artifactId>
+            <version>${project.version}</version>
+        </dependency>
 
     </dependencies>
 
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java
index df85f18..0ba9e48 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbDeviceService.java
@@ -28,6 +28,7 @@
 import org.onosproject.grpc.nb.net.device.DeviceServiceGrpc.DeviceServiceImplBase;
 import org.onosproject.grpc.net.models.PortProtoOuterClass.PortProto;
 import org.onosproject.grpc.net.device.models.DeviceEnumsProto;
+import org.onosproject.protobuf.api.GrpcServiceRegistry;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.MastershipRole;
@@ -47,26 +48,39 @@
 @Component(immediate = true)
 public class GrpcNbDeviceService {
 
+    private static DeviceServiceNbServerInternal instance = null;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GrpcServiceRegistry registry;
+
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DeviceService deviceService;
 
     @Activate
     public void activate() {
         //TODO this should contact the registry service and register an instance
-        // of this service.
+        // of this service
+        registry.register(getInnerInstance());
     }
 
     @Deactivate
     public void deactivate() {
+        registry.unregister(getInnerInstance());
     }
 
-    private class DeviceServiceNbServerInternal extends DeviceServiceImplBase {
+    public DeviceServiceNbServerInternal getInnerInstance() {
+        if (instance == null) {
+            instance = new DeviceServiceNbServerInternal();
+        }
+        return instance;
+    }
 
-        public DeviceServiceNbServerInternal() {
+    private final class DeviceServiceNbServerInternal extends DeviceServiceImplBase {
+
+        private DeviceServiceNbServerInternal() {
             super();
         }
 
-
         @Override
         public void getDeviceCount(
                 getDeviceCountRequest request,