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;
+ }
+}