ONOS-3460 - Link provider that enforces strict configuration

This provider uses the "links" configuration of the network
config and only allows discovery of links that are in
the config.

Refactoring will be done in a subsequent patch set - the DiscoveryContext and
LinkDiscovery classes are duplicates, they need to be refactored so the
LLDP provider and the Network Config provider can share them.

Change-Id: I4de12fc1c4ffa05e3cac7767b8a31f48ba379f6c
diff --git a/core/store/dist/src/main/java/org/onosproject/store/link/impl/CoreConfig.java b/core/store/dist/src/main/java/org/onosproject/store/link/impl/CoreConfig.java
new file mode 100644
index 0000000..dba3106
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/link/impl/CoreConfig.java
@@ -0,0 +1,37 @@
+/*
+ * 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.store.link.impl;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.Config;
+
+public class CoreConfig extends Config<ApplicationId> {
+
+    private static final String LINK_DISCOVERY_MODE = "linkDiscoveryMode";
+
+    protected static final String DEFAULT_LINK_DISCOVERY_MODE = "PERMISSIVE";
+
+    /**
+     * Returns the link discovery mode.
+     *
+     * @return link discovery mode
+     */
+    public ECLinkStore.LinkDiscoveryMode linkDiscoveryMode() {
+        return ECLinkStore.LinkDiscoveryMode
+                .valueOf(get(LINK_DISCOVERY_MODE, DEFAULT_LINK_DISCOVERY_MODE));
+    }
+}
+
diff --git a/core/store/dist/src/main/java/org/onosproject/store/link/impl/ECLinkStore.java b/core/store/dist/src/main/java/org/onosproject/store/link/impl/ECLinkStore.java
index 8e77ac8..845518e 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/link/impl/ECLinkStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/link/impl/ECLinkStore.java
@@ -15,21 +15,6 @@
  */
 package org.onosproject.store.link.impl;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-import static org.onosproject.net.DefaultAnnotations.merge;
-import static org.onosproject.net.DefaultAnnotations.union;
-import static org.onosproject.net.Link.State.ACTIVE;
-import static org.onosproject.net.Link.State.INACTIVE;
-import static org.onosproject.net.Link.Type.DIRECT;
-import static org.onosproject.net.Link.Type.INDIRECT;
-import static org.onosproject.net.LinkKey.linkKey;
-import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
-import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
-import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
-import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
-import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
-import static org.slf4j.LoggerFactory.getLogger;
-
 import java.util.Collection;
 import java.util.Map;
 import java.util.Objects;
@@ -48,6 +33,8 @@
 import org.onlab.util.SharedExecutors;
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.NodeId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
 import org.onosproject.mastership.MastershipService;
 import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.AnnotationsUtil;
@@ -56,8 +43,12 @@
 import org.onosproject.net.DefaultLink;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
-import org.onosproject.net.LinkKey;
 import org.onosproject.net.Link.Type;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
 import org.onosproject.net.device.DeviceClockService;
 import org.onosproject.net.link.DefaultLinkDescription;
 import org.onosproject.net.link.LinkDescription;
@@ -82,6 +73,22 @@
 import com.google.common.collect.Maps;
 import com.google.common.util.concurrent.Futures;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.DefaultAnnotations.merge;
+import static org.onosproject.net.DefaultAnnotations.union;
+import static org.onosproject.net.Link.State.ACTIVE;
+import static org.onosproject.net.Link.State.INACTIVE;
+import static org.onosproject.net.Link.Type.DIRECT;
+import static org.onosproject.net.Link.Type.INDIRECT;
+import static org.onosproject.net.LinkKey.linkKey;
+import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
+import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
+import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
+import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
+import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
+import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
+import static org.slf4j.LoggerFactory.getLogger;
+
 /**
  * Manages the inventory of links using a {@code EventuallyConsistentMap}.
  */
@@ -91,11 +98,29 @@
     extends AbstractStore<LinkEvent, LinkStoreDelegate>
     implements LinkStore {
 
+    /**
+     * Modes for dealing with newly discovered links.
+     */
+    protected enum LinkDiscoveryMode {
+        /**
+         * Permissive mode - all newly discovered links are valid.
+         */
+        PERMISSIVE,
+
+        /**
+         * Strict mode - all newly discovered links must be defined in
+         * the network config.
+         */
+        STRICT
+    }
+
     private final Logger log = getLogger(getClass());
 
     private final Map<LinkKey, Link> links = Maps.newConcurrentMap();
     private EventuallyConsistentMap<Provided<LinkKey>, LinkDescription> linkDescriptions;
 
+    private ApplicationId appId;
+
     private static final MessageSubject LINK_INJECT_MESSAGE = new MessageSubject("inject-link-request");
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -113,9 +138,20 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ClusterService clusterService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigRegistry netCfgService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
     private EventuallyConsistentMapListener<Provided<LinkKey>, LinkDescription> linkTracker =
             new InternalLinkTracker();
 
+    // Listener for config changes
+    private final InternalConfigListener cfgListener = new InternalConfigListener();
+
+    protected LinkDiscoveryMode linkDiscoveryMode = LinkDiscoveryMode.STRICT;
+
     protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
         @Override
         protected void setupKryoPool() {
@@ -129,6 +165,12 @@
 
     @Activate
     public void activate() {
+        appId = coreService.registerApplication("org.onosproject.core");
+        netCfgService.registerConfigFactory(factory);
+        netCfgService.addListener(cfgListener);
+
+        cfgListener.reconfigure(netCfgService.getConfig(appId, CoreConfig.class));
+
         KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
                 .register(KryoNamespaces.API)
                 .register(MastershipBasedTimestamp.class)
@@ -162,6 +204,8 @@
         linkDescriptions.destroy();
         links.clear();
         clusterCommunicator.removeSubscriber(LINK_INJECT_MESSAGE);
+        netCfgService.removeListener(cfgListener);
+        netCfgService.unregisterConfigFactory(factory);
 
         log.info("Stopped");
     }
@@ -255,6 +299,7 @@
                         current.src(),
                         current.dst(),
                         current.type() == DIRECT ? DIRECT : updated.type(),
+                        current.isExpected(),
                         union(current.annotations(), updated.annotations()));
         }
         return updated;
@@ -268,6 +313,7 @@
                 eventType.set(LINK_ADDED);
                 return newLink;
             } else if (existingLink.state() != newLink.state() ||
+                       existingLink.isExpected() != newLink.isExpected() ||
                         (existingLink.type() == INDIRECT && newLink.type() == DIRECT) ||
                         !AnnotationsUtil.isEqual(existingLink.annotations(), newLink.annotations())) {
                     eventType.set(LINK_UPDATED);
@@ -316,14 +362,28 @@
                                                           linkDescriptions.get(key).annotations()));
         });
 
-        boolean isDurable = Objects.equals(annotations.get().value(AnnotationKeys.DURABLE), "true");
+        Link.State initialLinkState;
+
+        boolean isExpected;
+        if (linkDiscoveryMode == LinkDiscoveryMode.PERMISSIVE) {
+            initialLinkState = ACTIVE;
+            isExpected =
+                    Objects.equals(annotations.get().value(AnnotationKeys.DURABLE), "true");
+        } else {
+            initialLinkState = base.isExpected() ? ACTIVE : INACTIVE;
+            isExpected = base.isExpected();
+        }
+
+
+
+
         return DefaultLink.builder()
                 .providerId(baseProviderId)
                 .src(src)
                 .dst(dst)
                 .type(type)
-                .state(ACTIVE)
-                .isExpected(isDurable)
+                .state(initialLinkState)
+                .isExpected(isExpected)
                 .annotations(annotations.get())
                 .build();
     }
@@ -350,7 +410,7 @@
             return null;
         }
 
-        if (link.isDurable()) {
+        if (linkDiscoveryMode == LinkDiscoveryMode.PERMISSIVE && link.isExpected()) {
             // FIXME: this will not sync link state!!!
             return link.state() == INACTIVE ? null :
                     updateLink(linkKey(link.src(), link.dst()), link,
@@ -421,4 +481,47 @@
             }
         }
     }
+
+    private class InternalConfigListener implements NetworkConfigListener {
+
+        void reconfigure(CoreConfig coreConfig) {
+            if (coreConfig == null) {
+                linkDiscoveryMode = LinkDiscoveryMode.PERMISSIVE;
+            } else {
+                linkDiscoveryMode = coreConfig.linkDiscoveryMode();
+            }
+            if (linkDiscoveryMode == LinkDiscoveryMode.STRICT) {
+                // Remove any previous links to force them to go through the strict
+                // discovery process
+                linkDescriptions.clear();
+                links.clear();
+            }
+            log.debug("config set link discovery mode to {}",
+                      linkDiscoveryMode.name());
+        }
+
+        @Override
+        public void event(NetworkConfigEvent event) {
+
+            if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
+                    event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
+                    event.configClass().equals(CoreConfig.class)) {
+
+                CoreConfig cfg = netCfgService.getConfig(appId, CoreConfig.class);
+                reconfigure(cfg);
+                log.info("Reconfigured");
+            }
+        }
+    }
+
+    // Configuration properties factory
+    private final ConfigFactory factory =
+            new ConfigFactory<ApplicationId, CoreConfig>(APP_SUBJECT_FACTORY,
+                                                        CoreConfig.class,
+                                                        "core") {
+                @Override
+                public CoreConfig createConfig() {
+                    return new CoreConfig();
+                }
+            };
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/link/impl/GossipLinkStore.java b/core/store/dist/src/main/java/org/onosproject/store/link/impl/GossipLinkStore.java
index cf46b15..551e731 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/link/impl/GossipLinkStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/link/impl/GossipLinkStore.java
@@ -15,12 +15,20 @@
  */
 package org.onosproject.store.link.impl;
 
-import com.google.common.base.Function;
-import com.google.common.collect.FluentIterable;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Multimaps;
-import com.google.common.collect.SetMultimap;
-import com.google.common.collect.Sets;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -32,7 +40,6 @@
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.mastership.MastershipService;
-import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.AnnotationsUtil;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultAnnotations;
@@ -60,20 +67,12 @@
 import org.onosproject.store.serializers.custom.DistributedStoreSerializers;
 import org.slf4j.Logger;
 
-import java.io.IOException;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Objects;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
+import com.google.common.collect.Sets;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Predicates.notNull;
@@ -433,7 +432,9 @@
                     new DefaultLinkDescription(
                         linkDescription.value().src(),
                         linkDescription.value().dst(),
-                        newType, merged),
+                        newType,
+                        existingLinkDescription.value().isExpected(),
+                        merged),
                     linkDescription.timestamp());
         }
         return descs.put(providerId, newLinkDescription);
@@ -608,14 +609,17 @@
             annotations = merge(annotations, e.getValue().value().annotations());
         }
 
-        boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
+        //boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
+
+        // TEMP
+        Link.State initialLinkState = base.value().isExpected() ? ACTIVE : INACTIVE;
         return DefaultLink.builder()
                 .providerId(baseProviderId)
                 .src(src)
                 .dst(dst)
                 .type(type)
-                .state(ACTIVE)
-                .isExpected(isDurable)
+                .state(initialLinkState)
+                .isExpected(base.value().isExpected())
                 .annotations(annotations)
                 .build();
     }