[ONOS-7842] Implement host annotation using netcfg

Change-Id: I2c5c516d642b34f11cae994b3fea31ebd7d76219
diff --git a/cli/src/main/java/org/onosproject/cli/net/AnnotateHostCommand.java b/cli/src/main/java/org/onosproject/cli/net/AnnotateHostCommand.java
new file mode 100644
index 0000000..f363583
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/AnnotateHostCommand.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2018-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.cli.net;
+
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cli.net.completer.AnnotationKeysCompleter;
+import org.onosproject.net.HostId;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.HostAnnotationConfig;
+import org.onosproject.net.provider.ProviderId;
+
+/**
+ * Annotates host model.
+ */
+@Service
+@Command(scope = "onos", name = "annotate-host",
+        description = "Annotates host")
+public class AnnotateHostCommand extends AbstractShellCommand {
+
+    static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
+
+    @Argument(index = 0, name = "uri", description = "Host ID",
+            required = true, multiValued = false)
+    @Completion(HostIdCompleter.class)
+    String uri = null;
+
+    @Argument(index = 1, name = "key", description = "Annotation key",
+            required = true, multiValued = false)
+    @Completion(AnnotationKeysCompleter.class)
+    String key = null;
+
+    @Argument(index = 2, name = "value",
+            description = "Annotation value (null to remove)",
+            required = false, multiValued = false)
+    String value = null;
+
+    @Option(name = "--remove-config",
+            description = "Remove annotation config")
+    private boolean removeCfg = false;
+
+    @Override
+    protected void doExecute() {
+        NetworkConfigService netcfgService = get(NetworkConfigService.class);
+        HostId hostId = HostId.hostId(uri);
+
+        if (key == null) {
+            print("[ERROR] Annotation key not specified.");
+            return;
+        }
+        HostAnnotationConfig cfg = netcfgService.getConfig(hostId, HostAnnotationConfig.class);
+        if (cfg == null) {
+            cfg = new HostAnnotationConfig(hostId);
+        }
+        if (removeCfg) {
+            // remove config about entry
+            cfg.annotation(key);
+        } else {
+            // add request config
+            cfg.annotation(key, value);
+        }
+        netcfgService.applyConfig(hostId, HostAnnotationConfig.class, cfg.node());
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/HostConfigOperator.java b/core/api/src/main/java/org/onosproject/net/config/HostConfigOperator.java
new file mode 100644
index 0000000..8134f49
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/HostConfigOperator.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2018-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 com.google.common.annotations.Beta;
+import org.onosproject.net.HostId;
+import org.onosproject.net.host.HostDescription;
+
+import java.util.Optional;
+
+
+/**
+ * {@link ConfigOperator} for Host.
+ * <p>
+ * Note: We currently assume {@link HostConfigOperator}s are commutative.
+ */
+@Beta
+public interface HostConfigOperator extends ConfigOperator {
+
+    /**
+     * Binds {@link NetworkConfigService} to use for retrieving configuration.
+     *
+     * @param networkConfigService the service to use
+     */
+    void bindService(NetworkConfigService networkConfigService);
+
+
+    /**
+     * Generates a HostDescription containing fields from a HostDescription and
+     * configuration.
+     *
+     * @param hostId {@link HostId} representing the port.
+     * @param descr input {@link HostDescription}
+     * @param prevConfig previous config {@link Config}
+     * @return Combined {@link HostDescription}
+     */
+    HostDescription combine(HostId hostId, HostDescription descr,
+                              Optional<Config> prevConfig);
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/HostAnnotationConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/HostAnnotationConfig.java
new file mode 100644
index 0000000..4cca68b
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/HostAnnotationConfig.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2018-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 com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.net.HostId;
+import org.onosproject.net.config.BaseConfig;
+import org.onosproject.net.config.InvalidFieldException;
+import org.slf4j.Logger;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Configuration to add extra annotations to a host via netcfg subsystem.
+ */
+public class HostAnnotationConfig
+        extends BaseConfig<HostId> {
+
+    /**
+     * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link HostAnnotationConfig}.
+     */
+    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 Host.
+     *
+     * @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 Host.
+     *
+     * @param replace annotations to be added by this configuration.
+     *                null value represent key removal request
+     * @return self
+     */
+    public HostAnnotationConfig annotations(Map<String, String> replace) {
+        ObjectNode anns = object.objectNode();
+        if (replace != null) {
+            replace.forEach(anns::put);
+        }
+        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 HostAnnotationConfig 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 HostAnnotationConfig 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 HostAnnotationConfig}.
+     * <p>
+     * Note: created instance needs to be initialized by #init(..) before using.
+     */
+    public HostAnnotationConfig() {
+        super();
+    }
+
+    /**
+     * Create a detached {@link HostAnnotationConfig} for specified host.
+     * <p>
+     * Note: created instance is not bound to NetworkConfigService,
+     * thus cannot use {@link #apply()}. Must be passed to the service
+     * using NetworkConfigService#applyConfig
+     *
+     * @param hostId Host id
+     */
+    public HostAnnotationConfig(HostId hostId) {
+        ObjectMapper mapper = new ObjectMapper();
+        init(hostId, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java b/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java
index 1992283..c83c2c1 100644
--- a/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java
+++ b/core/api/src/main/java/org/onosproject/net/host/DefaultHostDescription.java
@@ -167,6 +167,28 @@
         this.configured = configured;
     }
 
+    /**
+     * Creates a host description using the supplied information.
+     * @param base HostDescription to basic information
+     * @param annotations Annotations to use.
+     */
+    public DefaultHostDescription(HostDescription base, SparseAnnotations annotations) {
+        this(base.hwAddress(), base.vlan(), base.locations(), base.ipAddress(), base.innerVlan(), base.tpid(),
+                base.configured(), annotations);
+    }
+
+    /**
+     * Creates a host description using the supplied information.
+     *
+     * @param base base
+     * @param annotations annotations
+     * @return host description
+     */
+    public static DefaultHostDescription copyReplacingAnnotation(HostDescription base,
+                                                                 SparseAnnotations annotations) {
+        return new DefaultHostDescription(base, annotations);
+    }
+
     @Override
     public MacAddress hwAddress() {
         return mac;
diff --git a/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java b/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java
index a5d06a8..3cf0c0c 100644
--- a/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java
+++ b/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java
@@ -17,7 +17,6 @@
 
 import com.google.common.collect.ImmutableSet;
 import org.onosproject.core.CoreService;
-import org.onosproject.net.config.basics.PortDescriptionsConfig;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.HostId;
@@ -31,8 +30,10 @@
 import org.onosproject.net.config.basics.BasicRegionConfig;
 import org.onosproject.net.config.basics.BasicUiTopoLayoutConfig;
 import org.onosproject.net.config.basics.DeviceAnnotationConfig;
+import org.onosproject.net.config.basics.HostAnnotationConfig;
 import org.onosproject.net.config.basics.InterfaceConfig;
 import org.onosproject.net.config.basics.PortAnnotationConfig;
+import org.onosproject.net.config.basics.PortDescriptionsConfig;
 import org.onosproject.net.config.basics.SubjectFactories;
 import org.onosproject.net.region.RegionId;
 import org.onosproject.ui.model.topo.UiTopoLayoutId;
@@ -138,6 +139,14 @@
                 public DeviceAnnotationConfig createConfig() {
                     return new DeviceAnnotationConfig();
                 }
+            },
+            new ConfigFactory<HostId, HostAnnotationConfig>(HOST_SUBJECT_FACTORY,
+                    HostAnnotationConfig.class,
+                    HostAnnotationConfig.CONFIG_KEY) {
+                @Override
+                public HostAnnotationConfig createConfig() {
+                    return new HostAnnotationConfig();
+                }
             }
 
     );
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/HostAnnotationOperator.java b/core/net/src/main/java/org/onosproject/net/host/impl/HostAnnotationOperator.java
new file mode 100644
index 0000000..06ce4d6
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/HostAnnotationOperator.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2018-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.host.impl;
+
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultAnnotations.Builder;
+import org.onosproject.net.HostId;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.HostConfigOperator;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.HostAnnotationConfig;
+import org.onosproject.net.host.DefaultHostDescription;
+import org.onosproject.net.host.HostDescription;
+
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Implementations of {@link HostConfigOperator} to weave
+ * annotations added via {@link HostAnnotationConfig}.
+ */
+public class HostAnnotationOperator implements HostConfigOperator {
+
+    private NetworkConfigService networkConfigService;
+
+    /**
+     * Creates {@link HostAnnotationOperator} instance.
+     */
+    public HostAnnotationOperator() {
+    }
+
+    HostAnnotationOperator(NetworkConfigService networkConfigService) {
+        bindService(networkConfigService);
+    }
+
+    @Override
+    public void bindService(NetworkConfigService networkConfigService) {
+        this.networkConfigService = networkConfigService;
+    }
+
+    private HostAnnotationConfig lookupConfig(HostId hostId) {
+        if (networkConfigService == null) {
+            return null;
+        }
+        return networkConfigService.getConfig(hostId, HostAnnotationConfig.class);
+    }
+
+    @Override
+    public HostDescription combine(HostId hostId, HostDescription descr,
+                                   Optional<Config> prevConfig) {
+        HostAnnotationConfig cfg = lookupConfig(hostId);
+        if (cfg == null) {
+            return descr;
+        }
+        Map<String, String> annotations = cfg.annotations();
+
+        Builder builder = DefaultAnnotations.builder();
+        builder.putAll(descr.annotations());
+        if (prevConfig.isPresent()) {
+            HostAnnotationConfig prevHostAnnotationConfig = (HostAnnotationConfig) prevConfig.get();
+            for (String key : prevHostAnnotationConfig.annotations().keySet()) {
+                if (!annotations.containsKey(key)) {
+                    builder.remove(key);
+                }
+            }
+        }
+        builder.putAll(annotations);
+
+        return DefaultHostDescription.copyReplacingAnnotation(descr, builder.build());
+    }
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java b/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
index 1136951..97e3cb4 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
@@ -26,10 +26,12 @@
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
 import org.onosproject.net.HostLocation;
+import org.onosproject.net.config.Config;
 import org.onosproject.net.config.NetworkConfigEvent;
 import org.onosproject.net.config.NetworkConfigListener;
 import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.config.basics.BasicHostConfig;
+import org.onosproject.net.config.basics.HostAnnotationConfig;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.edge.EdgePortService;
 import org.onosproject.net.host.HostAdminService;
@@ -57,6 +59,7 @@
 import org.slf4j.Logger;
 
 import java.util.Dictionary;
+import java.util.Optional;
 import java.util.Set;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -138,10 +141,12 @@
     private boolean greedyLearningIpv6 = HM_GREEDY_LEARNING_IPV6_DEFAULT;
 
     private HostMonitor monitor;
+    private HostAnnotationOperator hostAnnotationOperator;
 
 
     @Activate
     public void activate(ComponentContext context) {
+        hostAnnotationOperator = new HostAnnotationOperator(networkConfigService);
         store.setDelegate(delegate);
         eventDispatcher.addSink(HostEvent.class, listenerRegistry);
         cfgService.registerProperties(getClass());
@@ -352,6 +357,19 @@
             if (!allowDuplicateIps) {
                 removeDuplicates(hostId, hostDescription);
             }
+
+            BasicHostConfig cfg = networkConfigService.getConfig(hostId, BasicHostConfig.class);
+            if (!isAllowed(cfg)) {
+                log.warn("Host {} is not allowed to be added into the contol domain", hostId);
+                return;
+            }
+
+            hostDescription = BasicHostOperator.combine(cfg, hostDescription);
+            HostAnnotationConfig annoConfig = networkConfigService.getConfig(hostId, HostAnnotationConfig.class);
+            if (annoConfig != null) {
+                hostDescription = hostAnnotationOperator.combine(hostId, hostDescription, Optional.of(annoConfig));
+            }
+
             store.createOrUpdateHost(provider().id(), hostId,
                                      hostDescription, replaceIps);
 
@@ -507,7 +525,8 @@
         public boolean isRelevant(NetworkConfigEvent event) {
             return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
                     || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
-                    && (event.configClass().equals(BasicHostConfig.class));
+                    && (event.configClass().equals(BasicHostConfig.class)
+                    || event.configClass().equals(HostAnnotationConfig.class));
         }
 
         @Override
@@ -521,7 +540,7 @@
 
             if (!isAllowed(cfg)) {
                 kickOutBadHost(hostId);
-            } else {
+            } else if (event.configClass().equals(BasicHostConfig.class)) {
                 Host host = getHost(hostId);
                 HostDescription desc =
                         (host == null) ? null : BasicHostOperator.descriptionOf(host);
@@ -529,6 +548,20 @@
                 if (desc != null) {
                     he = store.createOrUpdateHost(host.providerId(), hostId, desc, false);
                 }
+            } else if (event.configClass().equals(HostAnnotationConfig.class)) {
+                Host host = getHost(hostId);
+                HostProvider hp = getProvider(host.providerId());
+                HostDescription desc = (host == null) ? null : BasicHostOperator.descriptionOf(host);
+                Optional<Config> prevConfig = event.prevConfig();
+                log.debug("Host annotations: {} prevconfig {} desc {}", hostId, prevConfig, desc);
+                desc = hostAnnotationOperator.combine(hostId, desc, prevConfig);
+                if (desc != null && hp != null) {
+                    log.debug("Host annotations update - updated host description :{}", desc.toString());
+                    he = store.createOrUpdateHost(hp.id(), hostId, desc, false);
+                    if (he != null && he.subject() != null) {
+                        log.debug("Host annotations update - Host Event : {}", he.subject().annotations());
+                    }
+                }
             }
 
             if (he != null) {