Fixed an issue in basic host and basic link config validations.

Added validation for basic device config.

Made all concrete configurations final (as they should be).

Changed stc to use the new onos-netcfg as a method to re-locate and name devices and hosts.

Change-Id: I372e6c7e6c0fa6fa52301568af73342aaae6347b
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java
index 6e6663c..ee4042c 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java
@@ -23,7 +23,7 @@
  */
 public abstract class AllowedEntityConfig<S> extends Config<S> {
 
-    private static final String ALLOWED = "allowed";
+    protected static final String ALLOWED = "allowed";
 
     /**
      * Indicates whether the element is allowed for admission into the control
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 afde9a9..add8cf3 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
@@ -21,11 +21,17 @@
 /**
  * Basic configuration for network infrastructure devices.
  */
-public class BasicDeviceConfig extends BasicElementConfig<DeviceId> {
+public final class BasicDeviceConfig extends BasicElementConfig<DeviceId> {
 
-    public static final String TYPE = "type";
-    public static final String DRIVER = "driver";
-    public static final String MANAGEMENT_ADDRESS = "managementAddress";
+    private static final String TYPE = "type";
+    private static final String DRIVER = "driver";
+    private static final String MANAGEMENT_ADDRESS = "managementAddress";
+
+    @Override
+    public boolean isValid() {
+        return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, RACK_ADDRESS, OWNER,
+                             TYPE, DRIVER, MANAGEMENT_ADDRESS);
+    }
 
     /**
      * Returns the device type.
@@ -85,6 +91,6 @@
     }
 
     // TODO: device port meta-data to be configured via BasicPortsConfig
-    // TODO: device credentials/keys
+    // TODO: device credentials/keys; in a separate config
 
 }
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
index 7b3248c..10d753b 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
@@ -22,13 +22,13 @@
  */
 public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
 
-    public static final String NAME = "name";
+    protected static final String NAME = "name";
 
-    public static final String LATITUDE = "latitude";
-    public static final String LONGITUDE = "longitude";
+    protected static final String LATITUDE = "latitude";
+    protected static final String LONGITUDE = "longitude";
 
-    public static final String RACK_ADDRESS = "rackAddress";
-    public static final String OWNER = "owner";
+    protected static final String RACK_ADDRESS = "rackAddress";
+    protected static final String OWNER = "owner";
 
     protected static final double DEFAULT_COORD = -1.0;
 
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
index 9294631..8429630 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
@@ -19,68 +19,62 @@
 import org.onlab.packet.IpAddress;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.HostId;
+
 import java.util.HashSet;
 import java.util.Set;
 
+import static org.onosproject.net.config.basics.AllowedEntityConfig.ALLOWED;
+
 /**
  * Basic configuration for network end-station hosts.
  */
-public class BasicHostConfig extends BasicElementConfig<HostId> {
+public final class BasicHostConfig extends BasicElementConfig<HostId> {
+
     private static final String IPS = "ips";
     private static final String LOCATION = "location";
 
     @Override
     public boolean isValid() {
-        return hasOnlyFields(IPS, LOCATION) &&
-                this.location() != null &&
-                this.ipAddresses() != null;
+        // Location and IP addresses can be absent, but if present must be valid.
+        this.location();
+        this.ipAddresses();
+        return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, RACK_ADDRESS, OWNER,
+                             IPS, LOCATION);
     }
 
     /**
-     * Gets location of the host.
+     * Returns location of the host.
      *
-     * @return location of the host. Or null if not specified with correct format.
+     * @return location of the host or null if not set
+     * @throws IllegalArgumentException if not specified with correct format
      */
     public ConnectPoint location() {
         String location = get(LOCATION, null);
-
-        if (location != null) {
-            try {
-                return ConnectPoint.deviceConnectPoint(location);
-            } catch (Exception e) {
-                return null;
-            }
-        }
-        return null;
+        return location != null ? ConnectPoint.deviceConnectPoint(location) : null;
     }
 
     /**
      * Sets the location of the host.
      *
-     * @param location location of the host.
-     * @return the config of the host.
+     * @param location location of the host or null to unset
+     * @return the config of the host
      */
     public BasicHostConfig setLocation(String location) {
         return (BasicHostConfig) setOrClear(LOCATION, location);
     }
 
     /**
-     * Gets IP addresses of the host.
+     * Returns IP addresses of the host.
      *
-     * @return IP addresses of the host. Or null if not specified with correct format.
+     * @return IP addresses of the host or null if not set
+     * @throws IllegalArgumentException if not specified with correct format
      */
     public Set<IpAddress> ipAddresses() {
         HashSet<IpAddress> ipAddresses = new HashSet<>();
         if (object.has(IPS)) {
             ArrayNode ipNodes = (ArrayNode) object.path(IPS);
-            try {
-                ipNodes.forEach(ipNode -> {
-                    ipAddresses.add(IpAddress.valueOf(ipNode.asText()));
-                });
-                return ipAddresses;
-            } catch (Exception e) {
-                return null;
-            }
+            ipNodes.forEach(n -> ipAddresses.add(IpAddress.valueOf(n.asText())));
+            return ipAddresses;
         }
         return null;
     }
@@ -88,8 +82,8 @@
     /**
      * Sets the IP addresses of the host.
      *
-     * @param ipAddresses IP addresses of the host.
-     * @return the config of the host.
+     * @param ipAddresses IP addresses of the host or null to unset
+     * @return the config of the host
      */
     public BasicHostConfig setIps(Set<IpAddress> ipAddresses) {
         return (BasicHostConfig) setOrClear(IPS, ipAddresses);
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java
index ed807b8..0ad851a 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java
@@ -26,19 +26,19 @@
 /**
  * Basic configuration for network infrastructure link.
  */
-public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
+public final class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
 
-    public static final String TYPE = "type";
-    public static final String METRIC = "metric";
-    public static final String LATENCY = "latency";
-    public static final String BANDWIDTH = "bandwidth";
-    public static final String IS_DURABLE = "durable";
+    private static final String TYPE = "type";
+    private static final String METRIC = "metric";
+    private static final String LATENCY = "latency";
+    private static final String BANDWIDTH = "bandwidth";
+    private static final String IS_DURABLE = "durable";
 
     @Override
     public boolean isValid() {
-        return hasOnlyFields(TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE) &&
-                isNumber(METRIC, OPTIONAL) && isNumber(LATENCY, OPTIONAL) &&
-                isNumber(BANDWIDTH, OPTIONAL);
+        return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE) &&
+                isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
+                isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL);
     }
 
     /**
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java
index dfb494d..7a1b801 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java
@@ -23,11 +23,13 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 
+import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
+
 
 /**
  * Configurations for an optical port on a device.
  */
-public class OpticalPortConfig extends Config<ConnectPoint> {
+public final class OpticalPortConfig extends Config<ConnectPoint> {
     // optical type {OMS, OCH, ODUClt, fiber}
     public static final String TYPE = "type";
 
@@ -42,6 +44,12 @@
     // **Linc-OE : remove if it's not needed after all.**
     public static final String SPEED = "speed";
 
+    @Override
+    public boolean isValid() {
+        return hasOnlyFields(TYPE, NAME, PORT, STATIC_PORT, STATIC_LAMBDA, SPEED) &&
+                isNumber(STATIC_LAMBDA, OPTIONAL) && isNumber(SPEED, OPTIONAL);
+    }
+
     /**
      * Returns the Enum value representing the type of port.
      *