blob: b6e24d5c2020b6e499c90c9089341bb6dbfe080c [file] [log] [blame]
Thomas Vachuskae6360222015-07-21 10:10:36 -07001/*
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 */
16package org.onosproject.incubator.net.config.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.incubator.net.config.NetworkConfigService;
25import org.onosproject.incubator.net.config.SubjectFactory;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.io.File;
30
31/**
32 * Component for loading the initial network configuration.
33 */
34@Component(immediate = true)
35public 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),
Jonathan Hart111b42b2015-07-14 13:28:05 -070089 subjectFactory.createSubject(s),
90 subjectFactory.subjectKey()));
Thomas Vachuskae6360222015-07-21 10:10:36 -070091 }
92
93 private static void consumeSubjectJson(NetworkConfigService service,
Jonathan Hart111b42b2015-07-14 13:28:05 -070094 ObjectNode subjectNode, Object subject, String subjectKey) {
Thomas Vachuskae6360222015-07-21 10:10:36 -070095 subjectNode.fieldNames().forEachRemaining(c ->
Jonathan Hart111b42b2015-07-14 13:28:05 -070096 service.applyConfig(subject,
97 service.getConfigClass(subjectKey, c),
Thomas Vachuskae6360222015-07-21 10:10:36 -070098 (ObjectNode) subjectNode.path(c)));
99 }
100
101}