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 fe3b376..4227a0e 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
@@ -1359,12 +1359,15 @@
     protected void channelIdle(ChannelHandlerContext ctx,
                                IdleStateEvent e)
             throws Exception {
-        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);
     }