ONOS-3760 Injection of mfg/hw/sw/serial in BasicDeviceConfig

Change-Id: I7f2269039e0cdabbee85cdad178c4ca27cdc2dce
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 ea40829..77073cb 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
@@ -26,11 +26,16 @@
     private static final String TYPE = "type";
     private static final String DRIVER = "driver";
     private static final String MANAGEMENT_ADDRESS = "managementAddress";
+    private static final String MANUFACTURER = "manufacturer";
+    private static final String HW_VERSION = "hwVersion";
+    private static final String SW_VERSION = "swVersion";
+    private static final String SERIAL = "serial";
 
     @Override
     public boolean isValid() {
         return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, RACK_ADDRESS, OWNER,
-                             TYPE, DRIVER, MANAGEMENT_ADDRESS);
+                             TYPE, DRIVER, MANUFACTURER, HW_VERSION, SW_VERSION, SERIAL,
+                             MANAGEMENT_ADDRESS);
     }
 
     /**
@@ -55,7 +60,7 @@
     /**
      * Returns the device driver name.
      *
-     * @return driver name of null if not set
+     * @return driver name or null if not set
      */
     public String driver() {
         return get(DRIVER, null);
@@ -67,8 +72,84 @@
      * @param driverName new driver name; null to clear
      * @return self
      */
-    public BasicElementConfig driver(String driverName) {
-        return (BasicElementConfig) setOrClear(DRIVER, driverName);
+    public BasicDeviceConfig driver(String driverName) {
+        return (BasicDeviceConfig) setOrClear(DRIVER, driverName);
+    }
+
+    /**
+     * Returns the device manufacturer.
+     *
+     * @return manufacturer or null if not set
+     */
+    public String manufacturer() {
+        return get(MANUFACTURER, null);
+    }
+
+    /**
+     * Sets the device manufacturer.
+     *
+     * @param manufacturerName new manufacturer; null to clear
+     * @return self
+     */
+    public BasicDeviceConfig manufacturer(String manufacturerName) {
+        return (BasicDeviceConfig) setOrClear(MANUFACTURER, manufacturerName);
+    }
+
+    /**
+     * Returns the device hardware version.
+     *
+     * @return hardware version or null if not set
+     */
+    public String hwVersion() {
+        return get(HW_VERSION, null);
+    }
+
+    /**
+     * Sets the device hardware version.
+     *
+     * @param hwVersion new hardware version; null to clear
+     * @return self
+     */
+    public BasicDeviceConfig hwVersion(String hwVersion) {
+        return (BasicDeviceConfig) setOrClear(HW_VERSION, hwVersion);
+    }
+
+    /**
+     * Returns the device software version.
+     *
+     * @return software version or null if not set
+     */
+    public String swVersion() {
+        return get(SW_VERSION, null);
+    }
+
+    /**
+     * Sets the device software version.
+     *
+     * @param swVersion new software version; null to clear
+     * @return self
+     */
+    public BasicDeviceConfig swVersion(String swVersion) {
+        return (BasicDeviceConfig) setOrClear(SW_VERSION, swVersion);
+    }
+
+    /**
+     * Returns the device serial number.
+     *
+     * @return serial number or null if not set
+     */
+    public String serial() {
+        return get(SERIAL, null);
+    }
+
+    /**
+     * Sets the device serial number.
+     *
+     * @param serial new serial number; null to clear
+     * @return self
+     */
+    public BasicDeviceConfig serial(String serial) {
+        return (BasicDeviceConfig) setOrClear(SERIAL, serial);
     }
 
     /**
@@ -86,8 +167,8 @@
      * @param managementAddress new device management address (ip:port); null to clear
      * @return self
      */
-    public BasicElementConfig managementAddress(String managementAddress) {
-        return (BasicElementConfig) setOrClear(MANAGEMENT_ADDRESS, managementAddress);
+    public BasicDeviceConfig managementAddress(String managementAddress) {
+        return (BasicDeviceConfig) setOrClear(MANAGEMENT_ADDRESS, managementAddress);
     }
 
     // TODO: device port meta-data to be configured via BasicPortsConfig
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
new file mode 100644
index 0000000..adb4354
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/config/basics/BasicDeviceConfigTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.net.config.basics;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.ConfigApplyDelegate;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.Device.Type.OTN;
+import static org.onosproject.net.Device.Type.SWITCH;
+
+/**
+ * Test class for BasicDeviceConfig.
+ */
+public class BasicDeviceConfigTest {
+
+    private static final String DRIVER = "fooDriver";
+    private static final String MANUFACTURER = "fooManufacturer";
+    private static final String HW_VERSION = "0.0";
+    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 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 String NAME1 = "fooProtocol:fooIP:fooPort";
+
+    private final ConfigApplyDelegate delegate = config -> {
+    };
+    private final ObjectMapper mapper = new ObjectMapper();
+
+    private static final BasicDeviceConfig SW_BDC = new BasicDeviceConfig();
+
+    @Before
+    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);
+    }
+
+    @Test
+    public void testCorrectConfiguration() {
+        assertTrue("Configuration contains not valid fields", SW_BDC.isValid());
+        assertEquals("Incorrect type", SWITCH, SW_BDC.type());
+        assertEquals("Incorrect driver", DRIVER, SW_BDC.driver());
+        assertEquals("Incorrect manufacturer", MANUFACTURER, SW_BDC.manufacturer());
+        assertEquals("Incorrect HwVersion", HW_VERSION, SW_BDC.hwVersion());
+        assertEquals("Incorrect swVersion", SW_VERSION, SW_BDC.swVersion());
+        assertEquals("Incorrect serial", SERIAL, SW_BDC.serial());
+        assertEquals("Incorrect management Address", MANAGEMENT_ADDRESS, SW_BDC.managementAddress());
+    }
+
+
+    @Test
+    public void testSetType() {
+        SW_BDC.type(OTN);
+        assertEquals("Incorrect type", OTN, SW_BDC.type());
+    }
+
+
+    @Test
+    public void testSetDriver() {
+        SW_BDC.driver(DRIVER_NEW);
+        assertEquals("Incorrect driver", DRIVER_NEW, SW_BDC.driver());
+    }
+
+
+    @Test
+    public void testSetManufacturer() {
+        SW_BDC.manufacturer(MANUFACTURER_NEW);
+        assertEquals("Incorrect manufacturer", MANUFACTURER_NEW, SW_BDC.manufacturer());
+    }
+
+
+    @Test
+    public void testSetHwVersion() {
+        SW_BDC.hwVersion(HW_VERSION_NEW);
+        assertEquals("Incorrect HwVersion", HW_VERSION_NEW, SW_BDC.hwVersion());
+    }
+
+
+    @Test
+    public void testSetSwVersion() {
+        SW_BDC.swVersion(SW_VERSION_NEW);
+        assertEquals("Incorrect swVersion", SW_VERSION_NEW, SW_BDC.swVersion());
+    }
+
+    @Test
+    public void testSetSerial() {
+        SW_BDC.serial(SERIAL_NEW);
+        assertEquals("Incorrect serial", SERIAL_NEW, SW_BDC.serial());
+    }
+
+    @Test
+    public void testSetManagementAddress() {
+        SW_BDC.managementAddress(MANAGEMENT_ADDRESS_NEW);
+        assertEquals("Incorrect managementAddress", MANAGEMENT_ADDRESS_NEW, SW_BDC.managementAddress());
+    }
+}
\ No newline at end of file
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java b/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
index 108d68d..c4a4291 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
@@ -59,11 +59,27 @@
         if (bdc.type() != null && bdc.type() != type) {
             type = bdc.type();
         }
+        String manufacturer = descr.manufacturer();
+        if (bdc.manufacturer() != null && !bdc.manufacturer().equals(manufacturer)) {
+            manufacturer = bdc.manufacturer();
+        }
+        String hwVersion = descr.hwVersion();
+        if (bdc.hwVersion() != null && !bdc.hwVersion().equals(hwVersion)) {
+            hwVersion = bdc.hwVersion();
+        }
+        String swVersion = descr.swVersion();
+        if (bdc.swVersion() != null && !bdc.swVersion().equals(swVersion)) {
+            swVersion = bdc.swVersion();
+        }
+        String serial = descr.serialNumber();
+        if (bdc.serial() != null && !bdc.serial().equals(serial)) {
+            serial = bdc.serial();
+        }
 
         SparseAnnotations sa = combine(bdc, descr.annotations());
-        return new DefaultDeviceDescription(descr.deviceUri(), type, descr.manufacturer(),
-                                            descr.hwVersion(), descr.swVersion(),
-                                            descr.serialNumber(), descr.chassisId(), sa);
+        return new DefaultDeviceDescription(descr.deviceUri(), type, manufacturer,
+                                            hwVersion, swVersion,
+                                            serial, descr.chassisId(), sa);
     }
 
     /**
diff --git a/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java b/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java
index 2be0df7..138283c 100644
--- a/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java
@@ -15,26 +15,25 @@
  */
 package org.onosproject.net.device.impl;
 
-import static org.onosproject.net.Device.Type.SWITCH;
-import static org.onosproject.net.Device.Type.ROADM;
-import static org.junit.Assert.assertEquals;
-
-import java.net.URI;
-
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import org.junit.Before;
 import org.junit.Test;
 import org.onlab.packet.ChassisId;
-import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.net.config.basics.BasicDeviceConfig;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.config.ConfigApplyDelegate;
+import org.onosproject.net.config.basics.BasicDeviceConfig;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DeviceDescription;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import java.net.URI;
+
+import static org.junit.Assert.assertEquals;
+import static org.onosproject.net.Device.Type.ROADM;
+import static org.onosproject.net.Device.Type.SWITCH;
 
 public class BasicDeviceOperatorTest {
 
@@ -47,6 +46,12 @@
     private static final String SW = "3.9.1";
     private static final String SN = "43311-12345";
     private static final ChassisId CID = new ChassisId();
+    private static final String DRIVER = "fooDriver";
+    private static final String MANUFACTURER = "fooManufacturer";
+    private static final String HW_VERSION = "0.0";
+    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 SparseAnnotations SA = DefaultAnnotations.builder()
             .set(AnnotationKeys.DRIVER, NAME2).build();
@@ -65,7 +70,8 @@
         SW_BDC.init(DeviceId.deviceId(NAME1), NAME1, JsonNodeFactory.instance.objectNode(), mapper, delegate);
         SW_BDC.type(SWITCH).driver(NAME1).owner(OWNER);
         RD_BDC.init(DeviceId.deviceId(NAME2), NAME2, JsonNodeFactory.instance.objectNode(), mapper, delegate);
-        RD_BDC.type(ROADM);
+        RD_BDC.type(ROADM).manufacturer(MANUFACTURER).hwVersion(HW_VERSION)
+                .swVersion(SW_VERSION).serial(SERIAL).managementAddress(MANAGEMENT_ADDRESS).driver(DRIVER).owner(OWNER);
     }
 
     @Test
@@ -77,8 +83,14 @@
         desc = BasicDeviceOperator.combine(SW_BDC, DEV1);
         assertEquals(NAME1, desc.annotations().value(AnnotationKeys.DRIVER));
 
-        // override Device Type
+        // override Device Information
         desc = BasicDeviceOperator.combine(RD_BDC, DEV1);
-        assertEquals(ROADM, desc.type());
+        assertEquals("Wrong type", ROADM, desc.type());
+        assertEquals("Wrong manufacturer", MANUFACTURER, desc.manufacturer());
+        assertEquals("Wrong HwVersion", HW_VERSION, desc.hwVersion());
+        assertEquals("Wrong swVersion", SW_VERSION, desc.swVersion());
+        assertEquals("Wrong serial", SERIAL, desc.serialNumber());
+        assertEquals("Wrong management Address", MANAGEMENT_ADDRESS,
+                     desc.annotations().value(AnnotationKeys.MANAGEMENT_ADDRESS));
     }
 }