Starting to add initial configuration loading. WIP

Change-Id: I2c3ddb02ddc46f6ce9a4bfb531b8087a9463019d
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/config/NetworkConfigEvent.java b/incubator/api/src/main/java/org/onosproject/incubator/net/config/NetworkConfigEvent.java
index e381f5a..1673f73 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/config/NetworkConfigEvent.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/config/NetworkConfigEvent.java
@@ -29,10 +29,20 @@
      */
     public enum Type {
         /**
+         * Signifies that a network configuration was registered.
+         */
+        CONFIG_REGISTERED,
+
+        /**
+         * Signifies that a network configuration was unregistered.
+         */
+        CONFIG_UNREGISTERED,
+
+        /**
          * Signifies that network configuration was added.
          */
-
         CONFIG_ADDED,
+
         /**
          * Signifies that network configuration was updated.
          */
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/config/impl/NetworkConfigLoader.java b/incubator/net/src/main/java/org/onosproject/incubator/net/config/impl/NetworkConfigLoader.java
new file mode 100644
index 0000000..19e36a5
--- /dev/null
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/config/impl/NetworkConfigLoader.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2015 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.incubator.net.config.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.incubator.net.config.NetworkConfigService;
+import org.onosproject.incubator.net.config.SubjectFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+
+/**
+ * Component for loading the initial network configuration.
+ */
+@Component(immediate = true)
+public class NetworkConfigLoader {
+
+    private static final File CFG_FILE = new File("../config/network-cfg.json");
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigService networkConfigService;
+
+    // FIXME: Add mutual exclusion to make sure this happens only once per startup.
+
+    // TODO: add a field to track the collection of pending JSONS
+
+    @Activate
+    public void activate() {
+        // Add listener to net config events
+        try {
+            if (CFG_FILE.exists()) {
+                ObjectNode root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE);
+                // Parse this JSON structure and accumulate a collection of all leaf config JSONs
+
+                // Perform initial iteration over all leaf configs and attempt to apply them,
+                // but do this only if they are valid.
+//                networkConfigService.getConfigClass("foo");
+
+                // This code can be used for building the collection of jsons
+                root.fieldNames().forEachRemaining(sk ->
+                       consumeJson(networkConfigService, (ObjectNode) root.path(sk),
+                                   networkConfigService.getSubjectFactory(sk)));
+                log.info("Loaded initial network configuration from {}", CFG_FILE);
+            }
+        } catch (Exception e) {
+            log.warn("Unable to load initial network configuration from {}",
+                     CFG_FILE, e);
+        }
+    }
+
+
+    // TODO: add deactivate which will remove listener
+
+    // TODO: implement event listener and as each config is registered,
+    // sweep through pending config jsons and try to add them
+
+    /**
+     * Consumes configuration JSON for the specified subject factory.
+     *
+     * @param service        network configuration service
+     * @param classNode      subject class JSON node
+     * @param subjectFactory subject factory
+     */
+    static void consumeJson(NetworkConfigService service, ObjectNode classNode,
+                            SubjectFactory subjectFactory) {
+        classNode.fieldNames().forEachRemaining(s ->
+                                                        consumeSubjectJson(service, (ObjectNode) classNode.path(s),
+                                                                           subjectFactory.createSubject(s)));
+    }
+
+    private static void consumeSubjectJson(NetworkConfigService service,
+                                           ObjectNode subjectNode, Object subject) {
+        subjectNode.fieldNames().forEachRemaining(c ->
+                                                          service.applyConfig(subject, service.getConfigClass(c),
+                                                                              (ObjectNode) subjectNode.path(c)));
+    }
+
+}
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java
index 7e1cc76..dc4d2e4 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/config/impl/DistributedNetworkConfigStore.java
@@ -103,11 +103,15 @@
     @Override
     public void addConfigFactory(ConfigFactory configFactory) {
         factoriesByConfig.put(configFactory.configClass().getName(), configFactory);
+        notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(),
+                                              configFactory.configClass()));
     }
 
     @Override
     public void removeConfigFactory(ConfigFactory configFactory) {
         factoriesByConfig.remove(configFactory.configClass().getName());
+        notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
+                                              configFactory.configClass()));
     }
 
     @Override
diff --git a/tools/test/bin/onos-config b/tools/test/bin/onos-config
index 732aa8f..4fe122a 100755
--- a/tools/test/bin/onos-config
+++ b/tools/test/bin/onos-config
@@ -49,5 +49,9 @@
 onos-gen-partitions $TDEF_FILE
 scp -q $TDEF_FILE $remote:$ONOS_INSTALL_DIR/config/tablets.json
 
-# copy tools/package/config/ to remote
+# Copy tools/package/config/ to remote
 scp -qr ${ONOS_ROOT}/tools/package/config/ $remote:$ONOS_INSTALL_DIR/
+
+# Copy the desired initial network configuration to remote if needed
+[ -n $ONOS_CFG -a -f $ONOS_CFG -a "${1:-$OCI}" = "$OC1" ] && \
+    scp $ONOS_CFG $remote:$ONOS_INSTALL_DIR/config/network-cfg.json
diff --git a/tools/test/configs/sample-basic.json b/tools/test/configs/sample-basic.json
index 430bce1..c3929db 100644
--- a/tools/test/configs/sample-basic.json
+++ b/tools/test/configs/sample-basic.json
@@ -1,4 +1,3 @@
-/* curl -X POST -H'Content-type:application/json' http://$OCI:8181/onos/v1/network/configuration -d @/users/tom/onos/tools/test/configs/sample-basic.json */
 {
   "devices": {
     "of:001122334455667788" : {