[ONOS-8129] Bug fixed for duplicate events when netconf device goes down along with some fixes for NPE in NetconfControllerImpl

Change-Id: I4ea6100ce4c4e56ff8219cf846f107f49c50cd2d
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
index 90f6d14..c32004c 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfControllerImpl.java
@@ -558,6 +558,27 @@
         }
     }
 
+    @Override
+    public <T> boolean pingDevice(DeviceId deviceId) {
+        NetconfProxyMessage proxyMessage = new DefaultNetconfProxyMessage(
+                NetconfProxyMessage.SubjectType.GET_DEVICE_CAPABILITIES_SET, deviceId, null, localNodeId);
+        CompletableFuture<T> reply;
+        if (deviceService.getRole(deviceId).equals(MastershipRole.MASTER)) {
+            reply = handleProxyMessage(proxyMessage);
+        } else {
+            reply = relayMessageToMaster(proxyMessage);
+        }
+        try {
+            T deviceCapabilities = reply.get();
+            log.debug("Get device capabilities from device : {} -> {}", deviceId, deviceCapabilities);
+        } catch (InterruptedException | ExecutionException e) {
+            log.error("Error while getting device capabilities for device : {}", deviceId);
+            log.error("Error details : ", e);
+            return false;
+        }
+        return true;
+    }
+
     public <T> CompletableFuture<T> relayMessageToMaster(NetconfProxyMessage proxyMessage) {
         DeviceId deviceId = proxyMessage.deviceId();
 
@@ -592,6 +613,7 @@
     }
 
     private <T> CompletableFuture<T> handleProxyMessage(NetconfProxyMessage proxyMessage) {
+        countDownLatch = new CountDownLatch(1);
         try {
             switch (proxyMessage.subjectType()) {
                 case GET_DEVICE_CAPABILITIES_SET:
@@ -639,7 +661,7 @@
             NetconfProxyMessage.SubjectType subjectType = proxyMessage.subjectType();
             NetconfSession secureTransportSession;
 
-            if (netconfDeviceMap.get(deviceId).isMasterSession()) {
+            if (netconfDeviceMap.get(deviceId) != null && netconfDeviceMap.get(deviceId).isMasterSession()) {
                 secureTransportSession = netconfDeviceMap.get(deviceId).getSession();
             } else {
                 throw new NetconfException("Ssh session not present");
@@ -709,7 +731,7 @@
             NetconfProxyMessage.SubjectType subjectType = proxyMessage.subjectType();
             NetconfSession secureTransportSession;
 
-            if (netconfDeviceMap.get(deviceId).isMasterSession()) {
+            if (netconfDeviceMap.get(deviceId) != null && netconfDeviceMap.get(deviceId).isMasterSession()) {
                 secureTransportSession = netconfDeviceMap.get(deviceId).getSession();
             } else {
                 throw new NetconfException("SSH session not present");