[ONOS-4045]Adding mastership service to NetconfProvider

Change-Id: Id39cbef54a079ab6e080a9d3f60770c4bea90b3f
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
index 931bfd4..03bb53e 100644
--- a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfControllerImplTest.java
@@ -22,6 +22,8 @@
 import org.junit.Test;
 import org.onlab.packet.IpAddress;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.key.DeviceKeyService;
 import org.onosproject.netconf.NetconfDevice;
 import org.onosproject.netconf.NetconfDeviceFactory;
 import org.onosproject.netconf.NetconfDeviceInfo;
@@ -44,12 +46,14 @@
  * Unit tests for the Netconf controller implementation test.
  */
 public class NetconfControllerImplTest {
+
     NetconfControllerImpl ctrl;
 
     //DeviceInfo
     NetconfDeviceInfo deviceInfo1;
     NetconfDeviceInfo deviceInfo2;
     NetconfDeviceInfo badDeviceInfo3;
+    NetconfDeviceInfo deviceInfoIpV6;
 
     //Devices & DeviceId
     NetconfDevice device1;
@@ -68,21 +72,29 @@
     private static final String DEVICE_1_IP = "10.10.10.11";
     private static final String DEVICE_2_IP = "10.10.10.12";
     private static final String BAD_DEVICE_IP = "10.10.10.13";
+    private static final String DEVICE_IPV6 = "2001:db8::1";
 
     private static final int DEVICE_1_PORT = 11;
     private static final int DEVICE_2_PORT = 12;
     private static final int BAD_DEVICE_PORT = 13;
+    private static final int IPV6_DEVICE_PORT = 14;
+
+    private static DeviceService deviceService = new NetconfDeviceServiceMock();
+    private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
 
 
     @Before
     public void setUp() throws Exception {
         ctrl = new NetconfControllerImpl();
         ctrl.deviceFactory = new TestNetconfDeviceFactory();
+        ctrl.deviceService = deviceService;
+        ctrl.deviceKeyService = deviceKeyService;
 
         //Creating mock devices
         deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
         deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
         badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
+        deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
 
         device1 = new TestNetconfDevice(deviceInfo1);
         deviceId1 = deviceInfo1.getDeviceId();
@@ -102,9 +114,9 @@
         reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
 
         eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
-                null, Optional.of(1), deviceInfo1);
+                                                           null, Optional.of(1), deviceInfo1);
         eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
-                null, Optional.of(2), deviceInfo2);
+                                                           null, Optional.of(2), deviceInfo2);
     }
 
     @After
@@ -128,7 +140,7 @@
         ctrl.addDeviceListener(deviceListener3);
         assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
         assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
-                deviceListener2, deviceListener3));
+                                                                                   deviceListener2, deviceListener3));
 
         ctrl.removeDeviceListener(deviceListener1);
         assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
@@ -168,7 +180,7 @@
     public void testConnectBadDevice() throws Exception {
         reflectedDeviceMap.clear();
         try {
-            ctrl.connectDevice(badDeviceInfo3);
+            ctrl.connectDevice(badDeviceInfo3.getDeviceId());
         } finally {
             assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
         }
@@ -180,25 +192,37 @@
     @Test
     public void testConnectCorrectDevice() throws Exception {
         reflectedDeviceMap.clear();
-        ctrl.connectDevice(deviceInfo1);
-        ctrl.connectDevice(deviceInfo2);
+        ctrl.connectDevice(deviceInfo1.getDeviceId());
+        ctrl.connectDevice(deviceInfo2.getDeviceId());
         assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
         assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
         assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
     }
 
+    /**
+     * Check for correct ipv6 device connection. In this case the device map get modified.
+     */
+    @Test
+    public void testConnectCorrectIpv6Device() throws Exception {
+        reflectedDeviceMap.clear();
+        ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
+        assertTrue("Incorrect device connection", ctrl.getDevicesMap()
+                .containsKey(deviceInfoIpV6.getDeviceId()));
+        assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
+    }
+
 
     /**
      * Check for connect devices already added to the map.
      */
     @Test
     public void testConnectAlreadyExistingDevice() throws Exception {
-        NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1);
-        NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2);
+        NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
+        NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
         assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
-                deviceInfo1.getDeviceId());
+                     deviceInfo1.getDeviceId());
         assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
-                deviceInfo2.getDeviceId());
+                     deviceInfo2.getDeviceId());
     }
 
     /**
@@ -206,7 +230,7 @@
      */
     @Test
     public void testDisconnectDevice() throws Exception {
-        ctrl.disconnectDevice(deviceInfo1);
+        ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
         assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
     }
 
@@ -215,7 +239,7 @@
      */
     @Test
     public void testRemoveDevice() throws Exception {
-        ctrl.removeDevice(deviceInfo1);
+        ctrl.removeDevice(deviceInfo1.getDeviceId());
         assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
     }
 
@@ -234,13 +258,13 @@
     @Test
     public void testDeviceDownEventListener() throws Exception {
         reflectedDeviceMap.clear();
-        ctrl.connectDevice(deviceInfo1);
+        ctrl.connectDevice(deviceInfo1.getDeviceId());
         boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
         assertFalse("Irrelevant Device Event", result1);
         assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
         reflectedDownListener.event(eventForDeviceInfo1);
         assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
-        ctrl.connectDevice(deviceInfo2);
+        ctrl.connectDevice(deviceInfo2.getDeviceId());
         boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
         assertTrue("Irrelevant Device Event", result2);
         assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
@@ -269,7 +293,7 @@
 
         public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
             netconfDeviceInfo = deviceInfo;
-            if (netconfDeviceInfo.ip() != badDeviceInfo3.ip()) {
+            if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
                 netconfSession = EasyMock.createMock(NetconfSession.class);
                 deviceState = true;
             } else {