Refactoring Netconf Device Provider to simplify device connection and avoid calling behaviours when the session is not yet established

Change-Id: I7d3b1efc6194629ba77918dfd759781f7ccf1b60
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 19197e7..1ff38db 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
@@ -21,6 +21,7 @@
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.onlab.packet.IpAddress;
 import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
@@ -104,6 +105,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY)
     protected NetworkConfigRegistry netCfgService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected MastershipService mastershipService;
+
     public static final Logger log = LoggerFactory
             .getLogger(NetconfControllerImpl.class);
 
@@ -283,7 +287,7 @@
     @Override
     public void disconnectDevice(DeviceId deviceId, boolean remove) {
         if (!netconfDeviceMap.containsKey(deviceId)) {
-            log.warn("Device {} is not present", deviceId);
+            log.debug("Device {} is not present", deviceId);
         } else {
             stopDevice(deviceId, remove);
         }
@@ -356,7 +360,8 @@
         @Override
         public void event(NetconfDeviceOutputEvent event) {
             DeviceId did = event.getDeviceInfo().getDeviceId();
-            if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
+            if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED) ||
+                    !mastershipService.isLocalMaster(did)) {
                 removeDevice(did);
             } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) {
                 log.info("Trying to reestablish connection with device {}", did);
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java
index 9a70597..e73a4c6 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/ctl/impl/NetconfSessionMinaImpl.java
@@ -35,6 +35,7 @@
 import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onlab.osgi.ServiceDirectory;
+import org.onlab.util.ItemNotFoundException;
 import org.onlab.util.SharedExecutors;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.driver.Driver;
@@ -188,17 +189,22 @@
     public Set<String> getClientCapabilites(DeviceId deviceId) {
         Set<String> capabilities = new LinkedHashSet<>();
         DriverService driverService = directory.get(DriverService.class);
-        Driver driver = driverService.getDriver(deviceId);
-        if (driver == null) {
+        try {
+            Driver driver = driverService.getDriver(deviceId);
+            if (driver == null) {
+                return capabilities;
+            }
+            String clientCapabilities = driver.getProperty(NETCONF_CLIENT_CAPABILITY);
+            if (clientCapabilities == null) {
+                return capabilities;
+            }
+            String[] textStr = clientCapabilities.split("\\|");
+            capabilities.addAll(Arrays.asList(textStr));
+            return capabilities;
+        } catch (ItemNotFoundException e) {
+            log.warn("Driver for device {} currently not available", deviceId);
             return capabilities;
         }
-        String clientCapabilities = driver.getProperty(NETCONF_CLIENT_CAPABILITY);
-        if (clientCapabilities == null) {
-            return capabilities;
-        }
-        String[] textStr = clientCapabilities.split("\\|");
-        capabilities.addAll(Arrays.asList(textStr));
-        return capabilities;
     }
 
     private void startConnection() throws NetconfException {
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfControllerImplTest.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfControllerImplTest.java
index e503584..bbac123 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfControllerImplTest.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/impl/NetconfControllerImplTest.java
@@ -26,6 +26,8 @@
 import org.onlab.packet.IpAddress;
 import org.onosproject.cfg.ComponentConfigAdapter;
 import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.mastership.MastershipServiceAdapter;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.config.Config;
 import org.onosproject.net.config.ConfigApplyDelegate;
@@ -117,6 +119,7 @@
     private static DeviceService deviceService = new NetconfDeviceServiceMock();
     private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
     private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
+    private final MastershipService mastershipService = new MockmastershipService();
 
     private final ComponentContext context = new MockComponentContext();
 
@@ -128,6 +131,7 @@
         ctrl.deviceService = deviceService;
         ctrl.deviceKeyService = deviceKeyService;
         ctrl.netCfgService = netCfgService;
+        ctrl.mastershipService = mastershipService;
         NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
         NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
         NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
@@ -536,4 +540,11 @@
         public void onApply(Config configFile) {
         }
     }
+
+    private class MockmastershipService extends MastershipServiceAdapter {
+        @Override
+        public boolean isLocalMaster(DeviceId deviceId) {
+            return true;
+        }
+    }
 }