Protect channelIdle from NPE

Change-Id: I9a802f2686ee9ec70ddfd52d8df2a9a2293201b9
diff --git a/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OFChannelHandler.java b/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OFChannelHandler.java
index 5f2c13f..ac046d9 100644
--- a/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OFChannelHandler.java
+++ b/protocols/openflow/ctl/src/main/java/org/onosproject/openflow/controller/impl/OFChannelHandler.java
@@ -1351,12 +1351,15 @@
     private void channelIdle(ChannelHandlerContext ctx,
                                IdleStateEvent e)
             throws IOException {
-        OFMessage m = factory.buildEchoRequest().build();
-        log.debug("Sending Echo Request on idle channel: {}",
-                  ctx.channel());
-        ctx.write(Collections.singletonList(m), ctx.voidPromise());
-        // XXX S some problems here -- echo request has no transaction id, and
-        // echo reply is not correlated to the echo request.
+        // Factory can be null if the channel goes idle during initial handshake. Since the switch
+        // is not even initialized properly, we just skip this and disconnect the channel.
+        if (factory != null) {
+            OFMessage m = factory.buildEchoRequest().build();
+            log.debug("Sending Echo Request on idle channel: {}", ctx.channel());
+            ctx.write(Collections.singletonList(m), ctx.voidPromise());
+            // XXX S some problems here -- echo request has no transaction id, and
+            // echo reply is not correlated to the echo request.
+        }
         state.processIdle(this);
     }