ONOS-6950 Annotate device through network configuration

Change-Id: I39d5ca39667bb1478a090408ff3c1af33220a0b2
diff --git a/core/api/src/main/java/org/onosproject/net/config/DeviceConfigOperator.java b/core/api/src/main/java/org/onosproject/net/config/DeviceConfigOperator.java
new file mode 100644
index 0000000..f01abf4
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/DeviceConfigOperator.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * 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;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceDescription;
+
+import com.google.common.annotations.Beta;
+
+import java.util.Optional;
+
+
+/**
+ * {@link ConfigOperator} for Device.
+ * <p>
+ * Note: We currently assume {@link DeviceConfigOperator}s are commutative.
+ */
+@Beta
+public interface DeviceConfigOperator extends ConfigOperator {
+
+    /**
+     * Binds {@link NetworkConfigService} to use for retrieving configuration.
+     *
+     * @param networkConfigService the service to use
+     */
+    void bindService(NetworkConfigService networkConfigService);
+
+
+    /**
+     * Generates a DeviceDescription containing fields from a DeviceDescription and
+     * configuration.
+     *
+     * @param deviceId {@link DeviceId} representing the port.
+     * @param descr input {@link DeviceDescription}
+     * @param prevConfig previous config {@link Config}
+     * @return Combined {@link DeviceDescription}
+     */
+    DeviceDescription combine(DeviceId deviceId, DeviceDescription descr,
+                              Optional<Config> prevConfig);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/DeviceAnnotationConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/DeviceAnnotationConfig.java
new file mode 100644
index 0000000..14e6712
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/DeviceAnnotationConfig.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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 static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.BaseConfig;
+import org.onosproject.net.config.InvalidFieldException;
+import org.slf4j.Logger;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Configuration to add extra annotations to a device via netcfg subsystem.
+ */
+public class DeviceAnnotationConfig
+        extends BaseConfig<DeviceId> {
+
+    /**
+     * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link DeviceAnnotationConfig}.
+     */
+    public static final String CONFIG_KEY = "annotations";
+
+    /**
+     * JSON key for annotation entries.
+     * Value is a String.
+     */
+    private static final String ENTRIES = "entries";
+
+    private final Logger log = getLogger(getClass());
+
+    private static final int KEY_MAX_LENGTH = 1024;
+    private static final int VALUE_MAX_LENGTH = 1024;
+
+    @Override
+    public boolean isValid() {
+        JsonNode jsonNode = object.path(ENTRIES);
+        if (jsonNode.isObject()) {
+            jsonNode.fields().forEachRemaining(entry -> {
+                if (entry.getKey().length() > KEY_MAX_LENGTH) {
+                    throw new InvalidFieldException(entry.getKey(),
+                                                    entry.getKey() + " exceeds maximum length " + KEY_MAX_LENGTH);
+                }
+                if (entry.getValue().asText("").length() > VALUE_MAX_LENGTH) {
+                    throw new InvalidFieldException(entry.getKey(),
+                                                    entry.getKey() + " exceeds maximum length " + VALUE_MAX_LENGTH);
+                }
+            });
+        }
+        return hasField(ENTRIES) && object.get(ENTRIES).isObject();
+    }
+
+    /**
+     * Returns annotations to add to a Device.
+     *
+     * @return annotations as a map. null value represent key removal request
+     */
+    public Map<String, String> annotations() {
+        Map<String, String> map = new HashMap<>();
+
+        JsonNode jsonNode = object.path(ENTRIES);
+        if (!jsonNode.isObject()) {
+            return map;
+        }
+
+        jsonNode.fields().forEachRemaining(entry -> {
+            String key = entry.getKey();
+            JsonNode value = entry.getValue();
+            if (value.isTextual()) {
+                map.put(key, value.asText());
+            } else if (value.isNull()) {
+                map.put(key, null);
+            } else {
+                try {
+                    map.put(key, mapper().writeValueAsString(value));
+                } catch (JsonProcessingException e) {
+                    log.warn("Error processing JSON value for {}.", key, e);
+                }
+            }
+        });
+        return map;
+    }
+
+    /**
+     * Sets annotations to add to a Device.
+     *
+     * @param replace annotations to be added by this configuration.
+     *                null value represent key removal request
+     * @return self
+     */
+    public DeviceAnnotationConfig annotations(Map<String, String> replace) {
+        ObjectNode anns = object.objectNode();
+        if (replace != null) {
+            replace.forEach((k, v) -> {
+                anns.put(k, v);
+            });
+        }
+        object.set(ENTRIES, anns);
+        return this;
+    }
+
+    /**
+     * Add configuration to set or remove annotation entry.
+     *
+     * @param key annotations key
+     * @param value annotations value. specifying null removes the entry.
+     * @return self
+     */
+    public DeviceAnnotationConfig annotation(String key, String value) {
+        JsonNode ent = object.path(ENTRIES);
+        ObjectNode obj = (ent.isObject()) ? (ObjectNode) ent : object.objectNode();
+
+        obj.put(key, value);
+
+        object.set(ENTRIES, obj);
+        return this;
+    }
+
+    /**
+     * Remove configuration about specified key.
+     *
+     * @param key annotations key
+     * @return self
+     */
+    public DeviceAnnotationConfig annotation(String key) {
+        JsonNode ent = object.path(ENTRIES);
+        ObjectNode obj = (ent.isObject()) ? (ObjectNode) ent : object.objectNode();
+
+        obj.remove(key);
+
+        object.set(ENTRIES, obj);
+        return this;
+    }
+
+    /**
+     * Create a detached {@link DeviceAnnotationConfig}.
+     * <p>
+     * Note: created instance needs to be initialized by #init(..) before using.
+     */
+    public DeviceAnnotationConfig() {
+        super();
+    }
+
+    /**
+     * Create a detached {@link DeviceAnnotationConfig} for specified device.
+     * <p>
+     * Note: created instance is not bound to NetworkConfigService,
+     * thus cannot use {@link #apply()}. Must be passed to the service
+     * using NetworkConfigService#applyConfig
+     *
+     * @param deviceId Device id
+     */
+    public DeviceAnnotationConfig(DeviceId deviceId) {
+        ObjectMapper mapper = new ObjectMapper();
+        init(deviceId, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/device/DefaultDeviceDescription.java b/core/api/src/main/java/org/onosproject/net/device/DefaultDeviceDescription.java
index 577a2ec..a331f69 100644
--- a/core/api/src/main/java/org/onosproject/net/device/DefaultDeviceDescription.java
+++ b/core/api/src/main/java/org/onosproject/net/device/DefaultDeviceDescription.java
@@ -154,6 +154,18 @@
              base.chassisId(), defaultAvailable, annotations);
     }
 
+    /**
+     * Creates a device description using the supplied information.
+     *
+     * @param base base
+     * @param annotations annotations
+     * @return device description
+     */
+    public static DefaultDeviceDescription copyReplacingAnnotation(DeviceDescription base,
+                                                                   SparseAnnotations annotations) {
+        return new DefaultDeviceDescription(base, annotations);
+    }
+
     @Override
     public URI deviceUri() {
         return uri;