ONOS-3931: BasicDeviceConfig to allow configuration of  DeviceKeyId

Change-Id: I75d37709f19913f3382fa7614291e5a837ecf1a1
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
index 77073cb..097dc77 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
@@ -17,7 +17,7 @@
 
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
-
+import org.onosproject.net.key.DeviceKeyId;
 /**
  * Basic configuration for network infrastructure devices.
  */
@@ -30,12 +30,13 @@
     private static final String HW_VERSION = "hwVersion";
     private static final String SW_VERSION = "swVersion";
     private static final String SERIAL = "serial";
+    private static final String DEVICE_KEY_ID = "deviceKeyId";
 
     @Override
     public boolean isValid() {
         return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, RACK_ADDRESS, OWNER,
                              TYPE, DRIVER, MANUFACTURER, HW_VERSION, SW_VERSION, SERIAL,
-                             MANAGEMENT_ADDRESS);
+                             MANAGEMENT_ADDRESS, DEVICE_KEY_ID);
     }
 
     /**
@@ -162,7 +163,7 @@
     }
 
     /**
-     * Sets the driver name.
+     * Sets the device management ip (ip:port).
      *
      * @param managementAddress new device management address (ip:port); null to clear
      * @return self
@@ -171,6 +172,27 @@
         return (BasicDeviceConfig) setOrClear(MANAGEMENT_ADDRESS, managementAddress);
     }
 
+    /**
+     * Returns the device key id.
+     *
+     * @return device key id or null if not set
+     */
+    public DeviceKeyId deviceKeyId() {
+        String s = get(DEVICE_KEY_ID, null);
+        return s == null ? null : DeviceKeyId.deviceKeyId(s);
+    }
+
+    /**
+     * Sets the device key id.
+     *
+     * @param deviceKeyId new device key id; null to clear
+     * @return self
+     */
+    public BasicDeviceConfig deviceKeyId(DeviceKeyId deviceKeyId) {
+        return (BasicDeviceConfig) setOrClear(DEVICE_KEY_ID,
+                                              deviceKeyId != null ? deviceKeyId.id() : null);
+    }
+
     // TODO: device port meta-data to be configured via BasicPortsConfig
     // TODO: device credentials/keys; in a separate config
 
diff --git a/core/api/src/test/java/org/onosproject/net/config/basics/BasicDeviceConfigTest.java b/core/api/src/test/java/org/onosproject/net/config/basics/BasicDeviceConfigTest.java
index adb4354..6c3a789 100644
--- a/core/api/src/test/java/org/onosproject/net/config/basics/BasicDeviceConfigTest.java
+++ b/core/api/src/test/java/org/onosproject/net/config/basics/BasicDeviceConfigTest.java
@@ -20,6 +20,7 @@
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import org.junit.Before;
 import org.junit.Test;
+import org.onosproject.net.key.DeviceKeyId;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.config.ConfigApplyDelegate;
 
@@ -39,12 +40,14 @@
     private static final String SW_VERSION = "0.0";
     private static final String SERIAL = "1234";
     private static final String MANAGEMENT_ADDRESS = "12.34.56.78:99";
+    private static final DeviceKeyId DEVICE_KEY_ID = DeviceKeyId.deviceKeyId("fooDeviceKeyId");
     private static final String DRIVER_NEW = "barDriver";
     private static final String MANUFACTURER_NEW = "barManufacturer";
     private static final String HW_VERSION_NEW = "1.1";
     private static final String SW_VERSION_NEW = "1.1";
     private static final String SERIAL_NEW = "5678";
     private static final String MANAGEMENT_ADDRESS_NEW = "99.87.65.43:12";
+    private static final DeviceKeyId DEVICE_KEY_ID_NEW = DeviceKeyId.deviceKeyId("barDeviceKeyId");
 
     private static final String NAME1 = "fooProtocol:fooIP:fooPort";
 
@@ -58,7 +61,8 @@
     public void setUp() {
         SW_BDC.init(DeviceId.deviceId(NAME1), NAME1, JsonNodeFactory.instance.objectNode(), mapper, delegate);
         SW_BDC.type(SWITCH).manufacturer(MANUFACTURER).hwVersion(HW_VERSION)
-                .swVersion(SW_VERSION).serial(SERIAL).managementAddress(MANAGEMENT_ADDRESS).driver(DRIVER);
+                .swVersion(SW_VERSION).serial(SERIAL).managementAddress(MANAGEMENT_ADDRESS).driver(DRIVER)
+                .deviceKeyId(DEVICE_KEY_ID);
     }
 
     @Test
@@ -71,6 +75,7 @@
         assertEquals("Incorrect swVersion", SW_VERSION, SW_BDC.swVersion());
         assertEquals("Incorrect serial", SERIAL, SW_BDC.serial());
         assertEquals("Incorrect management Address", MANAGEMENT_ADDRESS, SW_BDC.managementAddress());
+        assertEquals("Incorrect deviceKeyId", DEVICE_KEY_ID, SW_BDC.deviceKeyId());
     }
 
 
@@ -119,4 +124,14 @@
         SW_BDC.managementAddress(MANAGEMENT_ADDRESS_NEW);
         assertEquals("Incorrect managementAddress", MANAGEMENT_ADDRESS_NEW, SW_BDC.managementAddress());
     }
+
+    @Test
+    public void testSetDeviceKeyId() {
+        // change device key id
+        SW_BDC.deviceKeyId(DEVICE_KEY_ID_NEW);
+        assertEquals("Incorrect deviceKeyId", DEVICE_KEY_ID_NEW, SW_BDC.deviceKeyId());
+        // clear device key id
+        SW_BDC.deviceKeyId(null);
+        assertEquals("Incorrect deviceKeyId", null, SW_BDC.deviceKeyId());
+    }
 }
\ No newline at end of file