[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 {
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfDeviceKeyServiceMock.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfDeviceKeyServiceMock.java
new file mode 100644
index 0000000..ace87fb
--- /dev/null
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfDeviceKeyServiceMock.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.netconf.ctl;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.key.DeviceKey;
+import org.onosproject.net.key.DeviceKeyId;
+import org.onosproject.net.key.DeviceKeyListener;
+import org.onosproject.net.key.DeviceKeyService;
+import org.onosproject.netconf.NetconfDeviceInfo;
+
+import java.util.Collection;
+
+/**
+ * Mock DeviceKey service to return device keys.
+ */
+class NetconfDeviceKeyServiceMock implements DeviceKeyService {
+
+    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;
+
+    //DeviceInfo
+    private NetconfDeviceInfo deviceInfo1 =
+            new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP),
+                                  DEVICE_1_PORT);
+    private NetconfDeviceInfo deviceInfo2 =
+            new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP),
+                                  DEVICE_2_PORT);
+    private NetconfDeviceInfo badDeviceInfo3 =
+            new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP),
+                                  BAD_DEVICE_PORT);
+    private NetconfDeviceInfo deviceInfoIpV6 =
+            new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
+
+    @Override
+    public Collection<DeviceKey> getDeviceKeys() {
+        return null;
+    }
+
+    @Override
+    public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) {
+        if (deviceKeyId.toString().equals(deviceInfo1.getDeviceId().toString())) {
+            return DeviceKey.createDeviceKeyUsingUsernamePassword(
+                    DeviceKeyId.deviceKeyId(deviceInfo1.getDeviceId().toString()),
+                    null, deviceInfo1.name(), deviceInfo1.password());
+        } else if (deviceKeyId.toString().equals(deviceInfo2.getDeviceId().toString())) {
+            return DeviceKey.createDeviceKeyUsingUsernamePassword(
+                    DeviceKeyId.deviceKeyId(deviceInfo2.getDeviceId().toString()),
+                    null, deviceInfo2.name(), deviceInfo2.password());
+        } else if (deviceKeyId.toString().equals(badDeviceInfo3.getDeviceId().toString())) {
+            return DeviceKey.createDeviceKeyUsingUsernamePassword(
+                    DeviceKeyId.deviceKeyId(badDeviceInfo3.getDeviceId().toString()),
+                    null, badDeviceInfo3.name(), badDeviceInfo3.password());
+        } else if (deviceKeyId.toString().equals(deviceInfoIpV6.getDeviceId().toString())) {
+            return DeviceKey.createDeviceKeyUsingUsernamePassword(
+                    DeviceKeyId.deviceKeyId(deviceInfoIpV6.getDeviceId().toString()),
+                    null, deviceInfoIpV6.name(), deviceInfoIpV6.password());
+        }
+        return null;
+    }
+
+    @Override
+    public void addListener(DeviceKeyListener listener) {
+
+    }
+
+    @Override
+    public void removeListener(DeviceKeyListener listener) {
+
+    }
+}
diff --git a/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfDeviceServiceMock.java b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfDeviceServiceMock.java
new file mode 100644
index 0000000..e355bbd
--- /dev/null
+++ b/protocols/netconf/ctl/src/test/java/org/onosproject/netconf/ctl/NetconfDeviceServiceMock.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * 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.netconf.ctl;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.MastershipRole;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortStatistics;
+
+import java.util.List;
+
+/**
+ * Mock device Service.
+ */
+class NetconfDeviceServiceMock implements DeviceService {
+
+    @Override
+    public int getDeviceCount() {
+        return 0;
+    }
+
+    @Override
+    public Iterable<Device> getDevices() {
+        return null;
+    }
+
+    @Override
+    public Iterable<Device> getDevices(Device.Type type) {
+        return null;
+    }
+
+    @Override
+    public Iterable<Device> getAvailableDevices() {
+        return null;
+    }
+
+    @Override
+    public Iterable<Device> getAvailableDevices(Device.Type type) {
+        return null;
+    }
+
+    @Override
+    public Device getDevice(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
+    public MastershipRole getRole(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
+    public List<Port> getPorts(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
+    public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
+    public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
+        return null;
+    }
+
+    @Override
+    public Port getPort(DeviceId deviceId, PortNumber portNumber) {
+        return null;
+    }
+
+    @Override
+    public boolean isAvailable(DeviceId deviceId) {
+        return false;
+    }
+
+    @Override
+    public void addListener(DeviceListener listener) {
+
+    }
+
+    @Override
+    public void removeListener(DeviceListener listener) {
+
+    }
+}
\ No newline at end of file