Allow enabling gRPC message logging for existing channels

Change-Id: Ic7586266e6a264ceeb9b55d7dae0db5e9a34c0c1
diff --git a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcChannelControllerImpl.java b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcChannelControllerImpl.java
index 87c9426..96a1671 100644
--- a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcChannelControllerImpl.java
+++ b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcChannelControllerImpl.java
@@ -92,9 +92,7 @@
             enableMessageLog.set(Tools.isPropertyEnabled(
                     properties, ENABLE_MESSAGE_LOG, ENABLE_MESSAGE_LOG_DEFAULT));
             log.info("Configured. Logging of gRPC messages is {}",
-                     enableMessageLog.get()
-                             ? "ENABLED for new channels"
-                             : "DISABLED for new and existing channels");
+                     enableMessageLog.get() ? "ENABLED" : "DISABLED");
         }
     }
 
@@ -125,28 +123,23 @@
                         "A channel with ID '%s' already exists", channelId));
             }
 
-            GrpcLoggingInterceptor interceptor = null;
-            if (enableMessageLog.get()) {
-                interceptor = new GrpcLoggingInterceptor(channelId, enableMessageLog);
-                channelBuilder.intercept(interceptor);
-            }
+            final GrpcLoggingInterceptor interceptor = new GrpcLoggingInterceptor(
+                    channelId, enableMessageLog);
+            channelBuilder.intercept(interceptor);
+
             ManagedChannel channel = channelBuilder.build();
             // Forced connection API is still experimental. Use workaround...
             // channel.getState(true);
             try {
                 doDummyMessage(channel);
             } catch (StatusRuntimeException e) {
-                if (interceptor != null) {
-                    interceptor.close();
-                }
+                interceptor.close();
                 shutdownNowAndWait(channel, channelId);
                 throw e;
             }
             // If here, channel is open.
             channels.put(channelId, channel);
-            if (interceptor != null) {
-                interceptors.put(channelId, interceptor);
-            }
+            interceptors.put(channelId, interceptor);
             return channel;
         } finally {
             lock.unlock();
diff --git a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcLoggingInterceptor.java b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcLoggingInterceptor.java
index 774152e..45ec5ba 100644
--- a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcLoggingInterceptor.java
+++ b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcLoggingInterceptor.java
@@ -63,22 +63,25 @@
     GrpcLoggingInterceptor(GrpcChannelId channelId, AtomicBoolean enabled) {
         this.channelId = channelId;
         this.enabled = enabled;
-        try {
-            writer = initWriter();
-            write(format("GRPC CALL LOG - %s\n\n", channelId));
-        } catch (IOException e) {
-            log.error("Unable to initialize gRPC call log writer", e);
-        }
     }
 
-    private FileWriter initWriter() throws IOException {
+    private boolean initWriter() {
+        if (writer != null) {
+            return true;
+        }
         final String safeChName = channelId.id()
                 .replaceAll("[^A-Za-z0-9]", "_");
         final String fileName = format("grpc_%s_", safeChName).toLowerCase();
-        final File tmpFile = File.createTempFile(fileName, ".log");
-        log.info("Created gRPC call log file for channel {}: {}",
-                 channelId, tmpFile.getAbsolutePath());
-        return new FileWriter(tmpFile);
+        try {
+            final File tmpFile = File.createTempFile(fileName, ".log");
+            this.writer = new FileWriter(tmpFile);
+            log.info("Created gRPC call log file for channel {}: {}",
+                     channelId, tmpFile.getAbsolutePath());
+            return true;
+        } catch (IOException e) {
+            log.error("Unable to initialize gRPC call log writer", e);
+        }
+        return false;
     }
 
     void close() {
@@ -98,19 +101,20 @@
 
     private void write(String message) {
         synchronized (this) {
-            if (writer != null) {
-                if (message.length() > 4096) {
-                    message = message.substring(0, 256) + "... TRUNCATED!\n\n";
-                }
-                try {
-                    writer.write(format(
-                            "*** %s - %s",
-                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
-                                    .format(new Date()),
-                            message));
-                } catch (IOException e) {
-                    log.error("Unable to write gRPC call log", e);
-                }
+            if (!initWriter()) {
+                return;
+            }
+            if (message.length() > 4096) {
+                message = message.substring(0, 256) + "... TRUNCATED!\n\n";
+            }
+            try {
+                writer.write(format(
+                        "*** %s - %s",
+                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
+                                .format(new Date()),
+                        message));
+            } catch (IOException e) {
+                log.error("Unable to write gRPC call log", e);
             }
         }
     }