ONOS-3732 Bandwidth resource registration using netcfg.

- Uses netcfg defined value as available resource if defined,
  else uses port speed as available Bandwidth resource

Change-Id: I2dde9a9194025194ed8785b4608f064debab182b
diff --git a/core/api/src/main/java/org/onosproject/net/config/Config.java b/core/api/src/main/java/org/onosproject/net/config/Config.java
index aac1320..1e2ee12 100644
--- a/core/api/src/main/java/org/onosproject/net/config/Config.java
+++ b/core/api/src/main/java/org/onosproject/net/config/Config.java
@@ -191,6 +191,17 @@
     }
 
     /**
+     * Clears the specified property.
+     *
+     * @param name  property name
+     * @return self
+     */
+    protected Config<S> clear(String name) {
+        object.remove(name);
+        return this;
+    }
+
+    /**
      * Sets the specified property as a boolean or clears it if null value given.
      *
      * @param name  property name
@@ -437,7 +448,38 @@
      */
     protected boolean isNumber(String field, FieldPresence presence, long... minMax) {
         JsonNode node = object.path(field);
-        return isValid(node, presence, (node.isLong() || node.isInt()) &&
+        return isValid(node, presence, node.isNumber() &&
+                (minMax.length > 0 && minMax[0] <= node.asLong() || minMax.length < 1) &&
+                (minMax.length > 1 && minMax[1] > node.asLong() || minMax.length < 2));
+    }
+    /**
+     * Indicates whether the specified field holds a valid number.
+     *
+     * @param field    JSON field name
+     * @param presence specifies if field is optional or mandatory
+     * @param minMax   optional min/max values
+     * @return true if valid; false otherwise
+     * @throws IllegalArgumentException if field is present, but not valid
+     */
+    protected boolean isNumber(String field, FieldPresence presence, double... minMax) {
+        JsonNode node = object.path(field);
+        return isValid(node, presence, node.isNumber() &&
+                (minMax.length > 0 && minMax[0] <= node.asDouble() || minMax.length < 1) &&
+                (minMax.length > 1 && minMax[1] > node.asDouble() || minMax.length < 2));
+    }
+
+    /**
+     * Indicates whether the specified field holds a valid integer.
+     *
+     * @param field    JSON field name
+     * @param presence specifies if field is optional or mandatory
+     * @param minMax   optional min/max values
+     * @return true if valid; false otherwise
+     * @throws IllegalArgumentException if field is present, but not valid
+     */
+    protected boolean isIntegralNumber(String field, FieldPresence presence, long... minMax) {
+        JsonNode node = object.path(field);
+        return isValid(node, presence, node.isIntegralNumber() &&
                 (minMax.length > 0 && minMax[0] <= node.asLong() || minMax.length < 1) &&
                 (minMax.length > 1 && minMax[1] > node.asLong() || minMax.length < 2));
     }
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/BandwidthCapacity.java b/core/api/src/main/java/org/onosproject/net/newresource/BandwidthCapacity.java
new file mode 100644
index 0000000..f7cb3c4
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/newresource/BandwidthCapacity.java
@@ -0,0 +1,85 @@
+/*
+ * 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.newresource;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.onlab.util.Bandwidth;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.annotations.Beta;
+
+/**
+ * Configuration to specify maximum available bandwidth resource (Capacity) on a port.
+ */
+@Beta
+public class BandwidthCapacity extends Config<ConnectPoint> {
+
+    /**
+     * netcfg ConfigKey for {@link BandwidthCapacity}.
+     */
+    public static final String CONFIG_KEY = "bandwidthCapacity";
+
+    // JSON key
+    private static final String CAPACITY = "capacityMbps";
+
+    private static final Logger log = LoggerFactory.getLogger(BandwidthCapacity.class);
+
+    @Override
+    public boolean isValid() {
+        // Open for extension (adding fields) in the future,
+        // must have CAPACITY field.
+        return isNumber(CAPACITY, FieldPresence.MANDATORY);
+    }
+
+    /**
+     * Sets the Available Bandwidth resource (Capacity).
+     *
+     * @param bandwidth value to set.
+     * @return self
+     */
+    public BandwidthCapacity capacity(Bandwidth bandwidth) {
+        checkNotNull(bandwidth);
+
+        // TODO current Bandwidth API end up value converted to double
+        setOrClear(CAPACITY, bandwidth.bps());
+        return this;
+    }
+
+    /**
+     * Available Bandwidth resource (Capacity).
+     *
+     * @return {@link Bandwidth}
+     */
+    public Bandwidth capacity() {
+        JsonNode v = object.path(CAPACITY);
+
+        if (v.isIntegralNumber()) {
+
+            return Bandwidth.mbps(v.asLong());
+        } else if (v.isFloatingPointNumber()) {
+
+            return Bandwidth.mbps(v.asDouble());
+        } else {
+            log.warn("Unexpected JsonNode for {}: {}", CAPACITY, v);
+            return Bandwidth.mbps(v.asDouble());
+        }
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
index da80f77..e3c5c47 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
@@ -16,6 +16,7 @@
 package org.onosproject.net.newresource;
 
 import com.google.common.annotations.Beta;
+
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;