Cleaner warning if a NETCONF session to Juniper device not yet established.

Old version gave in logs ...
2019-06-06T09:34:46,730 | WARN  | FlowRuleDriverProvider-0 | FlowRuleDriverProvider           | 191 - org.onosproject.onos-core-net - 2.2.0.SNAPSHOT | Exception thrown while polling netconf:172.26.138.38:830
java.lang.NullPointerException: null
	at org.onosproject.drivers.juniper.FlowRuleJuniperImpl.getFlowEntries(FlowRuleJuniperImpl.java:92) ~[?:?]
	at org.onosproject.net.flow.impl.FlowRuleDriverProvider.pollDeviceFlowEntries(FlowRuleDriverProvider.java:193) ~[?:?]
	at org.onosproject.net.flow.impl.FlowRuleDriverProvider.access$500(FlowRuleDriverProvider.java:63) ~[?:?]
	at org.onosproject.net.flow.impl.FlowRuleDriverProvider$InternalDeviceListener.handleEvent(FlowRuleDriverProvider.java:249) ~[?:?]
	at org.onosproject.net.flow.impl.FlowRuleDriverProvider$InternalDeviceListener.lambda$event$0(FlowRuleDriverProvider.java:233) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:?]
	at java.lang.Thread.run(Thread.java:745) [?:?]

New version gives ...
2019-06-07T09:36:07,924 | WARN  | FlowRuleDriverProvider-0 | JuniperAbstractHandlerBehaviour  | 218 - org.onosproject.onos-drivers-juniper - 2.2.0.SNAPSHOT | NETCONF session to device netconf:172.26.138.68:830 not yet established, will be retried

Note in new version this lookup code refactored to be in one place.

Change-Id: Ic78c5ecc67e8e98d14da845d0541d980296750a5
diff --git a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/DeviceDiscoveryJuniperImpl.java b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/DeviceDiscoveryJuniperImpl.java
index 3c254c5..bd87be8 100644
--- a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/DeviceDiscoveryJuniperImpl.java
+++ b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/DeviceDiscoveryJuniperImpl.java
@@ -23,15 +23,12 @@
 import org.onosproject.net.device.DeviceDescription;
 import org.onosproject.net.device.DeviceDescriptionDiscovery;
 import org.onosproject.net.device.PortDescription;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
-import org.onosproject.netconf.NetconfController;
 import org.onosproject.netconf.NetconfException;
 import org.onosproject.netconf.NetconfSession;
 import org.slf4j.Logger;
 
 import java.util.List;
 
-import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.drivers.juniper.JuniperUtils.REQ_IF_INFO;
 import static org.onosproject.drivers.juniper.JuniperUtils.REQ_MAC_ADD_INFO;
 import static org.onosproject.drivers.juniper.JuniperUtils.REQ_SYS_INFO;
@@ -44,7 +41,7 @@
  * Tested with MX240 junos 14.2
  */
 @Beta
-public class DeviceDiscoveryJuniperImpl extends AbstractHandlerBehaviour
+public class DeviceDiscoveryJuniperImpl extends JuniperAbstractHandlerBehaviour
         implements DeviceDescriptionDiscovery {
 
     private final Logger log = getLogger(getClass());
@@ -52,8 +49,7 @@
     @Override
     public DeviceDescription discoverDeviceDetails() {
         DeviceId devId = handler().data().deviceId();
-        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap().get(devId).getSession();
+        NetconfSession session = lookupNetconfSession(devId);
         String sysInfo;
         String chassisMacAddresses;
         try {
@@ -75,8 +71,7 @@
     @Override
     public List<PortDescription> discoverPortDetails() {
         DeviceId devId = handler().data().deviceId();
-        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap().get(devId).getSession();
+        NetconfSession session = lookupNetconfSession(devId);
         String reply;
         try {
             reply = session.get(requestBuilder(REQ_IF_INFO));
diff --git a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/FlowRuleJuniperImpl.java b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/FlowRuleJuniperImpl.java
index 075d3dc..a07effe 100644
--- a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/FlowRuleJuniperImpl.java
+++ b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/FlowRuleJuniperImpl.java
@@ -27,7 +27,6 @@
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.FlowRuleProgrammable;
@@ -38,7 +37,6 @@
 import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
 import org.onosproject.net.link.LinkService;
 import org.onosproject.netconf.DatastoreId;
-import org.onosproject.netconf.NetconfController;
 import org.onosproject.netconf.NetconfException;
 import org.onosproject.netconf.NetconfSession;
 
@@ -77,7 +75,7 @@
  * to find the next hop IP address.
  */
 @Beta
-public class FlowRuleJuniperImpl extends AbstractHandlerBehaviour
+public class FlowRuleJuniperImpl extends JuniperAbstractHandlerBehaviour
         implements FlowRuleProgrammable {
 
     private static final String OK = "<ok/>";
@@ -87,10 +85,8 @@
     public Collection<FlowEntry> getFlowEntries() {
 
         DeviceId devId = checkNotNull(this.data().deviceId());
-        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap().get(devId).getSession();
+        NetconfSession session = lookupNetconfSession(devId);
         if (session == null) {
-            log.warn("Device {} is not registered in netconf", devId);
             return Collections.emptyList();
         }
 
@@ -176,10 +172,7 @@
         DeviceId deviceId = this.data().deviceId();
 
         log.debug("{} flow entries to NETCONF device {}", type, deviceId);
-        NetconfController controller = checkNotNull(handler()
-                .get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap().get(deviceId)
-                .getSession();
+        NetconfSession session = lookupNetconfSession(deviceId);
         Collection<FlowRule> managedRules = new HashSet<>();
 
         for (FlowRule flowRule : rules) {
@@ -324,10 +317,7 @@
     }
 
     private boolean commit() {
-        NetconfController controller = checkNotNull(handler()
-                .get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap()
-                .get(handler().data().deviceId()).getSession();
+        NetconfSession session = lookupNetconfSession(handler().data().deviceId());
 
         String replay;
         try {
@@ -341,10 +331,7 @@
     }
 
     private boolean rollback() {
-        NetconfController controller = checkNotNull(handler()
-                .get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap()
-                .get(handler().data().deviceId()).getSession();
+        NetconfSession session = lookupNetconfSession(handler().data().deviceId());
 
         String replay;
         try {
diff --git a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperAbstractHandlerBehaviour.java b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperAbstractHandlerBehaviour.java
new file mode 100644
index 0000000..c2ded32
--- /dev/null
+++ b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/JuniperAbstractHandlerBehaviour.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.juniper;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class JuniperAbstractHandlerBehaviour extends AbstractHandlerBehaviour {
+
+    private static final Logger log = getLogger(JuniperAbstractHandlerBehaviour.class);
+
+    /**
+     * Lookup the current NETCONF session for specified device.
+     * @param deviceId id of device
+     * @return the current session (which may be null)
+     */
+    NetconfSession lookupNetconfSession(final DeviceId deviceId) {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        final NetconfDevice netconfDevice = controller.getDevicesMap().get(deviceId);
+
+        if (netconfDevice == null) {
+            log.warn("NETCONF session to device {} not yet established, can be retried", deviceId);
+            return null;
+        }
+
+        return netconfDevice.getSession();
+    }
+
+}
diff --git a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/LinkDiscoveryJuniperImpl.java b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/LinkDiscoveryJuniperImpl.java
index 95c02a1..441def0 100644
--- a/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/LinkDiscoveryJuniperImpl.java
+++ b/drivers/juniper/src/main/java/org/onosproject/drivers/juniper/LinkDiscoveryJuniperImpl.java
@@ -25,9 +25,7 @@
 import org.onosproject.net.Port;
 import org.onosproject.net.behaviour.LinkDiscovery;
 import org.onosproject.net.device.DeviceService;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.net.link.LinkDescription;
-import org.onosproject.netconf.NetconfController;
 import org.onosproject.netconf.NetconfException;
 import org.onosproject.netconf.NetconfSession;
 import org.slf4j.Logger;
@@ -36,7 +34,6 @@
 import java.util.Optional;
 import java.util.Set;
 
-import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction;
 import static org.onosproject.drivers.juniper.JuniperUtils.parseJuniperLldp;
 import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder;
@@ -51,7 +48,7 @@
  * Tested with MX240 junos 14.2
  */
 @Beta
-public class LinkDiscoveryJuniperImpl extends AbstractHandlerBehaviour
+public class LinkDiscoveryJuniperImpl extends JuniperAbstractHandlerBehaviour
         implements LinkDiscovery {
 
     private final Logger log = getLogger(getClass());
@@ -59,10 +56,7 @@
     @Override
     public Set<LinkDescription> getLinks() {
         DeviceId localDeviceId = this.handler().data().deviceId();
-        NetconfController controller =
-                checkNotNull(handler().get(NetconfController.class));
-        NetconfSession session =
-                controller.getDevicesMap().get(localDeviceId).getSession();
+        NetconfSession session = lookupNetconfSession(localDeviceId);
 
         String reply;
         try {