blob: 1622df1a9c4f028abede133b5295bf32feeb6e2f [file] [log] [blame]
andrea78375762015-09-30 11:52:42 -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 */
16
17package org.onosproject.cfg.impl;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.Sets;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.onlab.util.SharedExecutors;
28import org.onosproject.cfg.ComponentConfigService;
29import org.slf4j.Logger;
30
31import java.io.File;
32import java.util.Set;
33import java.util.TimerTask;
34
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Component responsible for automatically loading configuration file from
39 * configuration directory.
40 */
41@Component(immediate = true)
42public class ComponentConfigLoader {
43
andrea78375762015-09-30 11:52:42 -070044 private static final int RETRY_DELAY = 5_000; // millis between retries
andrea33279c82015-09-30 15:30:48 -070045 private static final String CFG_JSON = "../config/component-cfg.json";
46
47 static File cfgFile = new File(CFG_JSON);
andrea78375762015-09-30 11:52:42 -070048
49 private final Logger log = getLogger(getClass());
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected ComponentConfigService configService;
53
54 private ObjectNode root;
55 private final Set<String> pendingComponents = Sets.newHashSet();
56
57
58 /* TimerTask object that calls the load configuration for each component in the
59 pending components set and cancels itself if the set is mpty.
60 */
61 private final TimerTask loader = new TimerTask() {
62 @Override
63 public void run() {
64 ImmutableSet.copyOf(pendingComponents)
65 .forEach(k -> loadConfig(k, (ObjectNode) root.path(k)));
66 if (pendingComponents.isEmpty()) {
67 this.cancel();
68 }
69 }
70 };
71
72 @Activate
73 protected void activate() {
74 this.loadConfigs();
75 log.info("Started");
76 }
77 /* loads the configurations for each component from the file in
78 ../config/component-cfg.json, adds them to a set and schedules a task to try
79 and load them.
80 */
81 private void loadConfigs() {
82 try {
andrea33279c82015-09-30 15:30:48 -070083 if (cfgFile.exists()) {
84 root = (ObjectNode) new ObjectMapper().readTree(cfgFile);
andrea78375762015-09-30 11:52:42 -070085 root.fieldNames().forEachRemaining(pendingComponents::add);
andrea33279c82015-09-30 15:30:48 -070086 SharedExecutors.getTimer().schedule(loader, 0, RETRY_DELAY);
87 log.info("Loaded initial component configuration from {}", cfgFile);
andrea78375762015-09-30 11:52:42 -070088 }
89 } catch (Exception e) {
90 log.warn("Unable to load initial component configuration from {}",
andrea33279c82015-09-30 15:30:48 -070091 cfgFile, e);
andrea78375762015-09-30 11:52:42 -070092 }
93 }
94 /*
95 * loads a configuration for a single component and removes it from the
96 * components set
97 */
98 private void loadConfig(String component, ObjectNode config) {
99 if (configService.getComponentNames().contains(component)) {
100 config.fieldNames()
101 .forEachRemaining(k -> configService.setProperty(component, k,
102 config.path(k).asText()));
103 pendingComponents.remove(component);
104 }
105 }
106}