[ONOS-7842] Implement host annotation using netcfg

Change-Id: I2c5c516d642b34f11cae994b3fea31ebd7d76219
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) {