Update gRPC

- Update gRPC and it's dependencies to 0.13.2
- Use pre-defined default port if not specified by URI
- Cosmetic fixes

Change-Id: Iac8c3ba4c6fe1b5925ea8832e61d313adfad6f71
diff --git a/incubator/rpc-grpc/features.xml b/incubator/rpc-grpc/features.xml
index 20bda8f..5b7f618 100644
--- a/incubator/rpc-grpc/features.xml
+++ b/incubator/rpc-grpc/features.xml
@@ -14,24 +14,27 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License.
   -->
-<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.1" name="${project.artifactId}-${project.version}">
     <feature name="${project.artifactId}" version="${project.version}"
              description="${project.description}">
         <feature>onos-api</feature>
-        <bundle>mvn:com.google.protobuf/protobuf-java/3.0.0-beta-1</bundle>
-        <bundle>mvn:io.netty/netty-common/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-buffer/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-transport/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-handler/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-codec/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-codec-http/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-codec-http2/4.1.0.Beta6</bundle>
-        <bundle>mvn:io.netty/netty-resolver/4.1.0.Beta6</bundle>
-        <bundle>mvn:com.twitter/hpack/0.11.0</bundle>
+        <bundle>mvn:com.google.protobuf/protobuf-java/3.0.0-beta-2</bundle>
+        <bundle>mvn:io.netty/netty-common/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-buffer/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-transport/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-handler/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-codec/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-codec-http/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-codec-http2/4.1.0.CR3</bundle>
+        <bundle>mvn:io.netty/netty-resolver/4.1.0.CR3</bundle>
         <!-- TODO: Create shaded jar for these. -->
         <bundle>wrap:mvn:com.google.auth/google-auth-library-credentials/0.3.0$Bundle-SymbolicName=com.google.auth.google-auth-library-credentials&amp;Bundle-Version=0.3.0</bundle>
         <bundle>wrap:mvn:com.google.auth/google-auth-library-oauth2-http/0.3.0$Bundle-SymbolicName=com.google.auth.google-auth-library-oauth2-http&amp;Bundle-Version=0.3.0</bundle>
-        <bundle>wrap:mvn:io.grpc/grpc-all/0.9.0$Bundle-SymbolicName=io.grpc.grpc-all&amp;Bundle-Version=0.9.0&amp;Import-Package=io.netty.*;version=4.1.0.Beta6,javax.net.ssl,com.google.protobuf.nano;resolution:=optional,okio;resolution:=optional,*</bundle>
+        <bundle>wrap:mvn:io.grpc/grpc-core/0.13.2$Bundle-SymbolicName=io.grpc.grpc-core&amp;Bundle-Version=0.13.2&amp;</bundle>
+        <bundle>wrap:mvn:io.grpc/grpc-protobuf/0.13.2$Bundle-SymbolicName=io.grpc.grpc-protobuf&amp;Bundle-Version=0.13.2&amp;</bundle>
+        <bundle>wrap:mvn:io.grpc/grpc-stub/0.13.2$Bundle-SymbolicName=io.grpc.grpc-stub&amp;Bundle-Version=0.13.2&amp;</bundle>
+        <bundle>wrap:mvn:io.grpc/grpc-netty/0.13.2$Bundle-SymbolicName=io.grpc.grpc-netty&amp;Bundle-Version=0.13.2&amp;Import-Package=io.netty.*;version=4.1.0.CR3,*</bundle>
+        <bundle>wrap:mvn:io.grpc/grpc-auth/0.13.2$Bundle-SymbolicName=io.grpc.grpc-auth&amp;Bundle-Version=0.13.2&amp;Import-Package=javax.net.ssl,*</bundle>
         <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
     </feature>
 </features>
diff --git a/incubator/rpc-grpc/pom.xml b/incubator/rpc-grpc/pom.xml
index dd320bc..c9191e0 100644
--- a/incubator/rpc-grpc/pom.xml
+++ b/incubator/rpc-grpc/pom.xml
@@ -32,18 +32,10 @@
         <onos.app.name>org.onosproject.incubator.rpc.grpc</onos.app.name>
         <onos.app.requires>org.onosproject.incubator.rpc</onos.app.requires>
         <!-- Note: update feature.xml when updating -->
-        <grpc.version>0.9.0</grpc.version>
-        <grpc.netty.version>4.1.0.Beta6</grpc.netty.version>
+        <grpc.version>0.13.2</grpc.version>
+        <grpc.netty.version>4.1.0.CR3</grpc.netty.version>
     </properties>
 
-    <pluginRepositories>
-        <pluginRepository>
-            <id>protoc-plugin</id>
-            <url>https://dl.bintray.com/sergei-ivanov/maven/</url>
-        </pluginRepository>
-    </pluginRepositories>
-
-
     <dependencies>
         <dependency>
             <groupId>org.onosproject</groupId>
@@ -117,10 +109,21 @@
             <extension>
                 <groupId>kr.motd.maven</groupId>
                 <artifactId>os-maven-plugin</artifactId>
-                <version>1.4.0.Final</version>
+                <version>1.4.1.Final</version>
             </extension>
         </extensions>
 
+        <pluginManagement>
+            <plugins>
+                <plugin>
+                    <groupId>org.apache.karaf.tooling</groupId>
+                    <artifactId>karaf-maven-plugin</artifactId>
+                    <version>3.0.5</version>
+                    <extensions>true</extensions>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+
         <plugins>
             <!-- TODO This is included to suppress the generation of javadocs for
                 this package. There is a problem when we try to package the
@@ -196,16 +199,17 @@
             </plugin>
 
             <plugin>
-                <groupId>com.google.protobuf.tools</groupId>
-                <artifactId>maven-protoc-plugin</artifactId>
-                <version>0.4.2</version>
+                <groupId>org.xolstice.maven.plugins</groupId>
+                <artifactId>protobuf-maven-plugin</artifactId>
+                <version>0.5.0</version>
                 <configuration>
                     <!-- The version of protoc must match protobuf-java. If you don't 
                         depend on protobuf-java directly, you will be transitively depending on the 
                         protobuf-java version that grpc depends on. -->
-                    <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-1:exe:${os.detected.classifier}</protocArtifact>
+                    <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact>
                     <pluginId>grpc-java</pluginId>
                     <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
+                    <checkStaleness>true</checkStaleness>
                 </configuration>
                 <executions>
                     <execution>
@@ -220,7 +224,7 @@
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>build-helper-maven-plugin</artifactId>
-                <version>1.9.1</version>
+                <version>1.10</version>
                 <executions>
                     <execution>
                         <id>add-source</id>
diff --git a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderRegistryClientProxy.java b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderRegistryClientProxy.java
index 630b64d..766dd1a 100644
--- a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderRegistryClientProxy.java
+++ b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/DeviceProviderRegistryClientProxy.java
@@ -55,7 +55,7 @@
 
         // Create session
         DeviceProviderServiceClientProxy pService = new DeviceProviderServiceClientProxy(provider, channel);
-        log.debug("Created DeviceProviderServiceClientProxy", pService);
+        log.debug("Created DeviceProviderServiceClientProxy {}", pService);
 
         DeviceProviderServiceClientProxy old = pServices.put(provider, pService);
         if (old != null) {
@@ -68,7 +68,7 @@
     @Override
     public synchronized void unregister(DeviceProvider provider) {
         DeviceProviderServiceClientProxy pService = pServices.remove(provider);
-        log.debug("Unregistering DeviceProviderServiceClientProxy", pService);
+        log.debug("Unregistering DeviceProviderServiceClientProxy {}", pService);
         super.unregister(provider);
         if (pService != null) {
             pService.shutdown();
diff --git a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceProvider.java b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceProvider.java
index 71a83c9..955a657 100644
--- a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceProvider.java
+++ b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceProvider.java
@@ -58,7 +58,7 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected RemoteServiceProviderRegistry rpcRegistry;
 
-    private Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>();
+    private final Map<URI, ManagedChannel> channels = new ConcurrentHashMap<>();
 
     private RemoteServiceContextProviderService providerService;
 
@@ -67,8 +67,8 @@
     protected void activate() {
         providerService = rpcRegistry.register(this);
 
-        // FIXME remove me. test code to see if gRPC loads in karaf
-        //getChannel(URI.create("grpc://localhost:8080"));
+        // Uncomment to test if gRPC can be loaded in karaf
+        //getChannel(URI.create("grpc://localhost:11984"));
 
         log.info("Started");
     }
@@ -111,7 +111,11 @@
 
     private ManagedChannel createChannel(URI uri) {
         log.debug("Creating channel for {}", uri);
-        return NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort())
+        int port = GrpcRemoteServiceServer.DEFAULT_LISTEN_PORT;
+        if (uri.getPort() != -1) {
+            port = uri.getPort();
+        }
+        return NettyChannelBuilder.forAddress(uri.getHost(), port)
                 .negotiationType(NegotiationType.PLAINTEXT)
                 .build();
     }
diff --git a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java
index 77d1cbe..f14b4ef 100644
--- a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java
+++ b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/GrpcRemoteServiceServer.java
@@ -165,6 +165,7 @@
      * Unregisters all registered LinkProviders.
      */
     private synchronized void unregisterLinkProviders() {
+        // TODO remove all links registered by these providers
         linkProviders.values().forEach(linkProviderRegistry::unregister);
         linkProviders.clear();
         linkProviderServices.clear();
@@ -250,6 +251,7 @@
                 // TODO Do we care about provider name?
                 pairedProvider.setProviderId(new ProviderId(registerProvider.getProviderScheme(), RPC_PROVIDER_NAME));
                 registeredProviders.add(pairedProvider);
+                log.info("registering DeviceProvider {} via gRPC", pairedProvider.id());
                 deviceProviderService = deviceProviderRegistry.register(pairedProvider);
                 break;
 
@@ -319,8 +321,13 @@
         @Override
         public void onError(Throwable e) {
             log.error("DeviceProviderServiceServerProxy#onError", e);
-            deviceProviderRegistry.unregister(pairedProvider);
-            registeredProviders.remove(pairedProvider);
+            if (pairedProvider != null) {
+                // TODO call deviceDisconnected against all devices
+                // registered for this provider scheme
+                log.info("unregistering DeviceProvider {} via gRPC", pairedProvider.id());
+                deviceProviderRegistry.unregister(pairedProvider);
+                registeredProviders.remove(pairedProvider);
+            }
             // TODO What is the proper clean up for bi-di stream on error?
             // sample suggests no-op
             toDeviceProvider.onError(e);
@@ -364,6 +371,7 @@
 
         /**
          * Registers RPC stream in other direction.
+         *
          * @param deviceProviderServiceProxy {@link DeviceProviderServiceServerProxy}
          */
         void pair(DeviceProviderServiceServerProxy deviceProviderServiceProxy) {
diff --git a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/LinkProviderServiceServerProxy.java b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/LinkProviderServiceServerProxy.java
index 80322dc..bf35ed2 100644
--- a/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/LinkProviderServiceServerProxy.java
+++ b/incubator/rpc-grpc/src/main/java/org/onosproject/incubator/rpc/grpc/LinkProviderServiceServerProxy.java
@@ -51,6 +51,9 @@
 
     private final GrpcRemoteServiceServer server;
 
+    // TODO implement aging mechanism to automatically remove
+    // stale links reported by dead client, etc.
+
     LinkProviderServiceServerProxy(GrpcRemoteServiceServer server) {
         this.server = checkNotNull(server);
     }