Refactor Link gRPC NB service to use static registry binding

Change-Id: I881ee5edf616b589b0f019e771256b5bfd4834a4
diff --git a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkService.java b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkService.java
index d6d7d61..54ab184 100644
--- a/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkService.java
+++ b/incubator/protobuf/services/nb/src/main/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkService.java
@@ -15,12 +15,9 @@
 */
 
 package org.onosproject.incubator.protobuf.services.nb;
-import org.onosproject.incubator.protobuf.models.net.ConnectPointProtoTranslator;
-import org.onosproject.incubator.protobuf.models.net.LinkProtoTranslator;
 
 import com.google.common.annotations.Beta;
-import io.grpc.Server;
-import io.grpc.ServerBuilder;
+import io.grpc.BindableService;
 import io.grpc.stub.StreamObserver;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
@@ -28,30 +25,31 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onosproject.grpc.nb.net.link.LinkServiceGrpc.LinkServiceImplBase;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getActiveLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getActiveLinksRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceEgressLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceEgressLinksRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceIngressLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceIngressLinksRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceLinksRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getEgressLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getEgressLinksRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getIngressLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getIngressLinksRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkCountReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkCountRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkRequest;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinksReply;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinksRequest;
+import org.onosproject.incubator.protobuf.models.net.ConnectPointProtoTranslator;
+import org.onosproject.incubator.protobuf.models.net.LinkProtoTranslator;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.link.LinkService;
-
-import java.io.IOException;
-
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkCountRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkCountReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinkReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getActiveLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getActiveLinksReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceLinksReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceEgressLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceEgressLinksReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceIngressLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getDeviceIngressLinksReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getLinksReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getEgressLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getEgressLinksReply;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getIngressLinksRequest;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb.getIngressLinksReply;
+import org.onosproject.protobuf.api.GrpcServiceRegistry;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,32 +62,44 @@
 
     private static final Logger log = LoggerFactory.getLogger(GrpcNbLinkService.class);
 
-
-    private Server server;
-    private LinkServiceNBServerInternal innerClassInstance = null;
-
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GrpcServiceRegistry registry;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected LinkService linkService;
 
+    private LinkServiceNbServerInternal innerClassInstance;
+
     @Activate
     public void activate() {
-        server = ServerBuilder.forPort(64000).addService(getInnerClassInstance()).build();
-        try {
-            server.start();
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-        }
+
+        registry.register(getInnerInstance());
+        log.info("Started.");
     }
 
     @Deactivate
     public void deactivate() {
-        server.shutdown();
+
+        registry.unregister(getInnerInstance());
+        log.info("Stopped.");
     }
 
-    public class LinkServiceNBServerInternal extends LinkServiceImplBase {
+    /**
+     * Register Link Service, used for unit testing purposes.
+     *
+     * @return An instance of binding Link service
+     */
+    public InProcessServer<BindableService> registerInProcessServer() {
+        InProcessServer<BindableService> inprocessServer =
+                new InProcessServer(LinkServiceNb.class);
+        inprocessServer.addServiceToBind(getInnerInstance());
 
-        public LinkServiceNBServerInternal() {
+        return inprocessServer;
+    }
+
+    private class LinkServiceNbServerInternal extends LinkServiceImplBase {
+
+        public LinkServiceNbServerInternal() {
             super();
         }
 
@@ -204,9 +214,9 @@
         }
     }
 
-    public LinkServiceNBServerInternal getInnerClassInstance() {
+    private LinkServiceNbServerInternal getInnerInstance() {
         if (innerClassInstance == null) {
-            innerClassInstance = new LinkServiceNBServerInternal();
+            innerClassInstance = new LinkServiceNbServerInternal();
         }
         return innerClassInstance;
     }
diff --git a/incubator/protobuf/services/nb/src/test/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkServiceTest.java b/incubator/protobuf/services/nb/src/test/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkServiceTest.java
index d734e50..d45b985 100644
--- a/incubator/protobuf/services/nb/src/test/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkServiceTest.java
+++ b/incubator/protobuf/services/nb/src/test/java/org/onosproject/incubator/protobuf/services/nb/GrpcNbLinkServiceTest.java
@@ -17,23 +17,23 @@
 package org.onosproject.incubator.protobuf.services.nb;
 
 import com.google.common.collect.FluentIterable;
-import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableList;
-import org.onosproject.grpc.net.models.LinkProtoOuterClass;
-
-import static org.junit.Assert.assertTrue;
-import static org.onosproject.net.DeviceId.deviceId;
-import static org.onosproject.net.PortNumber.portNumber;
-
+import com.google.common.collect.ImmutableSet;
+import io.grpc.BindableService;
 import io.grpc.ManagedChannel;
 import io.grpc.inprocess.InProcessChannelBuilder;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onosproject.grpc.nb.net.link.LinkServiceGrpc;
+import org.onosproject.grpc.nb.net.link.LinkServiceGrpc.LinkServiceBlockingStub;
+import org.onosproject.grpc.nb.net.link.LinkServiceNb;
+import org.onosproject.grpc.net.models.LinkProtoOuterClass;
 import org.onosproject.incubator.protobuf.models.net.ConnectPointProtoTranslator;
 import org.onosproject.incubator.protobuf.models.net.LinkProtoTranslator;
 import org.onosproject.net.ConnectPoint;
-
 import org.onosproject.net.DefaultLink;
 import org.onosproject.net.DeviceId;
-
 import org.onosproject.net.Link;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.link.LinkService;
@@ -45,17 +45,12 @@
 import java.util.List;
 import java.util.Set;
 
-import org.junit.Test;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-
-import org.onosproject.grpc.nb.net.link.LinkServiceGrpc;
-import org.onosproject.grpc.nb.net.link.LinkServiceGrpc.LinkServiceBlockingStub;
-import org.onosproject.grpc.nb.net.link.LinkServiceNb;
-import org.onosproject.incubator.protobuf.services.nb.GrpcNbLinkService.LinkServiceNBServerInternal;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.net.PortNumber.portNumber;
 
 public class GrpcNbLinkServiceTest {
-    private static InProcessServer<LinkServiceNBServerInternal> inprocessServer;
+    private static InProcessServer<BindableService> inprocessServer;
     private static ManagedChannel channel;
     private static LinkServiceBlockingStub blockingStub;
 
@@ -353,14 +348,11 @@
 
     @BeforeClass
     public static void beforeClass() throws InstantiationException, IllegalAccessException, IOException {
-        inprocessServer = new InProcessServer<LinkServiceNBServerInternal>(LinkServiceNBServerInternal.class);
-
-        GrpcNbLinkService outer = new GrpcNbLinkService();
-        GrpcNbLinkService.LinkServiceNBServerInternal inner = outer.getInnerClassInstance();
-        outer.linkService = MOCK_LINK;
-        inprocessServer.addServiceToBind(inner);
-
+        GrpcNbLinkService linkService = new GrpcNbLinkService();
+        linkService.linkService = MOCK_LINK;
+        inprocessServer = linkService.registerInProcessServer();
         inprocessServer.start();
+
         channel = InProcessChannelBuilder.forName("test").directExecutor()
                 // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
                 // needing certificates.