Thomas Vachuska

Starting to add initial configuration loading. WIP

Change-Id: I2c3ddb02ddc46f6ce9a4bfb531b8087a9463019d
...@@ -29,10 +29,20 @@ public class NetworkConfigEvent extends AbstractEvent<NetworkConfigEvent.Type, O ...@@ -29,10 +29,20 @@ public class NetworkConfigEvent extends AbstractEvent<NetworkConfigEvent.Type, O
29 */ 29 */
30 public enum Type { 30 public enum Type {
31 /** 31 /**
32 - * Signifies that network configuration was added. 32 + * Signifies that a network configuration was registered.
33 + */
34 + CONFIG_REGISTERED,
35 +
36 + /**
37 + * Signifies that a network configuration was unregistered.
33 */ 38 */
39 + CONFIG_UNREGISTERED,
34 40
41 + /**
42 + * Signifies that network configuration was added.
43 + */
35 CONFIG_ADDED, 44 CONFIG_ADDED,
45 +
36 /** 46 /**
37 * Signifies that network configuration was updated. 47 * Signifies that network configuration was updated.
38 */ 48 */
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.incubator.net.config.impl;
17 +
18 +import com.fasterxml.jackson.databind.ObjectMapper;
19 +import com.fasterxml.jackson.databind.node.ObjectNode;
20 +import org.apache.felix.scr.annotations.Activate;
21 +import org.apache.felix.scr.annotations.Component;
22 +import org.apache.felix.scr.annotations.Reference;
23 +import org.apache.felix.scr.annotations.ReferenceCardinality;
24 +import org.onosproject.incubator.net.config.NetworkConfigService;
25 +import org.onosproject.incubator.net.config.SubjectFactory;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import java.io.File;
30 +
31 +/**
32 + * Component for loading the initial network configuration.
33 + */
34 +@Component(immediate = true)
35 +public class NetworkConfigLoader {
36 +
37 + private static final File CFG_FILE = new File("../config/network-cfg.json");
38 +
39 + private final Logger log = LoggerFactory.getLogger(getClass());
40 +
41 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 + protected NetworkConfigService networkConfigService;
43 +
44 + // FIXME: Add mutual exclusion to make sure this happens only once per startup.
45 +
46 + // TODO: add a field to track the collection of pending JSONS
47 +
48 + @Activate
49 + public void activate() {
50 + // Add listener to net config events
51 + try {
52 + if (CFG_FILE.exists()) {
53 + ObjectNode root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE);
54 + // Parse this JSON structure and accumulate a collection of all leaf config JSONs
55 +
56 + // Perform initial iteration over all leaf configs and attempt to apply them,
57 + // but do this only if they are valid.
58 +// networkConfigService.getConfigClass("foo");
59 +
60 + // This code can be used for building the collection of jsons
61 + root.fieldNames().forEachRemaining(sk ->
62 + consumeJson(networkConfigService, (ObjectNode) root.path(sk),
63 + networkConfigService.getSubjectFactory(sk)));
64 + log.info("Loaded initial network configuration from {}", CFG_FILE);
65 + }
66 + } catch (Exception e) {
67 + log.warn("Unable to load initial network configuration from {}",
68 + CFG_FILE, e);
69 + }
70 + }
71 +
72 +
73 + // TODO: add deactivate which will remove listener
74 +
75 + // TODO: implement event listener and as each config is registered,
76 + // sweep through pending config jsons and try to add them
77 +
78 + /**
79 + * Consumes configuration JSON for the specified subject factory.
80 + *
81 + * @param service network configuration service
82 + * @param classNode subject class JSON node
83 + * @param subjectFactory subject factory
84 + */
85 + static void consumeJson(NetworkConfigService service, ObjectNode classNode,
86 + SubjectFactory subjectFactory) {
87 + classNode.fieldNames().forEachRemaining(s ->
88 + consumeSubjectJson(service, (ObjectNode) classNode.path(s),
89 + subjectFactory.createSubject(s)));
90 + }
91 +
92 + private static void consumeSubjectJson(NetworkConfigService service,
93 + ObjectNode subjectNode, Object subject) {
94 + subjectNode.fieldNames().forEachRemaining(c ->
95 + service.applyConfig(subject, service.getConfigClass(c),
96 + (ObjectNode) subjectNode.path(c)));
97 + }
98 +
99 +}
...@@ -103,11 +103,15 @@ public class DistributedNetworkConfigStore ...@@ -103,11 +103,15 @@ public class DistributedNetworkConfigStore
103 @Override 103 @Override
104 public void addConfigFactory(ConfigFactory configFactory) { 104 public void addConfigFactory(ConfigFactory configFactory) {
105 factoriesByConfig.put(configFactory.configClass().getName(), configFactory); 105 factoriesByConfig.put(configFactory.configClass().getName(), configFactory);
106 + notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(),
107 + configFactory.configClass()));
106 } 108 }
107 109
108 @Override 110 @Override
109 public void removeConfigFactory(ConfigFactory configFactory) { 111 public void removeConfigFactory(ConfigFactory configFactory) {
110 factoriesByConfig.remove(configFactory.configClass().getName()); 112 factoriesByConfig.remove(configFactory.configClass().getName());
113 + notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
114 + configFactory.configClass()));
111 } 115 }
112 116
113 @Override 117 @Override
......
...@@ -49,5 +49,9 @@ TDEF_FILE=/tmp/${remote}.tablets.json ...@@ -49,5 +49,9 @@ TDEF_FILE=/tmp/${remote}.tablets.json
49 onos-gen-partitions $TDEF_FILE 49 onos-gen-partitions $TDEF_FILE
50 scp -q $TDEF_FILE $remote:$ONOS_INSTALL_DIR/config/tablets.json 50 scp -q $TDEF_FILE $remote:$ONOS_INSTALL_DIR/config/tablets.json
51 51
52 -# copy tools/package/config/ to remote 52 +# Copy tools/package/config/ to remote
53 scp -qr ${ONOS_ROOT}/tools/package/config/ $remote:$ONOS_INSTALL_DIR/ 53 scp -qr ${ONOS_ROOT}/tools/package/config/ $remote:$ONOS_INSTALL_DIR/
54 +
55 +# Copy the desired initial network configuration to remote if needed
56 +[ -n $ONOS_CFG -a -f $ONOS_CFG -a "${1:-$OCI}" = "$OC1" ] && \
57 + scp $ONOS_CFG $remote:$ONOS_INSTALL_DIR/config/network-cfg.json
......
1 -/* 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 */
2 { 1 {
3 "devices": { 2 "devices": {
4 "of:001122334455667788" : { 3 "of:001122334455667788" : {
......