Thomas Vachuska

Starting to add initial configuration loading. WIP

Change-Id: I2c3ddb02ddc46f6ce9a4bfb531b8087a9463019d
......@@ -29,10 +29,20 @@ public class NetworkConfigEvent extends AbstractEvent<NetworkConfigEvent.Type, O
*/
public enum Type {
/**
* Signifies that network configuration was added.
* 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.
*/
......
/*
* 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)));
}
}
......@@ -103,11 +103,15 @@ public class DistributedNetworkConfigStore
@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
......
......@@ -49,5 +49,9 @@ TDEF_FILE=/tmp/${remote}.tablets.json
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
......
/* 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" : {
......