blob: a88738dece821607d48a4414c778abb38afa2589 [file] [log] [blame]
andrea78375762015-09-30 11:52:42 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
andrea78375762015-09-30 11:52:42 -07003 *
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.osgi.service.component.annotations.Activate;
22import org.osgi.service.component.annotations.Component;
23import org.osgi.service.component.annotations.Reference;
24import org.osgi.service.component.annotations.ReferenceCardinality;
andrea78375762015-09-30 11:52:42 -070025import org.onosproject.cfg.ComponentConfigService;
26import org.slf4j.Logger;
27
28import java.io.File;
andrea78375762015-09-30 11:52:42 -070029
30import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Component responsible for automatically loading configuration file from
34 * configuration directory.
35 */
36@Component(immediate = true)
37public class ComponentConfigLoader {
38
andrea33279c82015-09-30 15:30:48 -070039 private static final String CFG_JSON = "../config/component-cfg.json";
andrea33279c82015-09-30 15:30:48 -070040 static File cfgFile = new File(CFG_JSON);
andrea78375762015-09-30 11:52:42 -070041
42 private final Logger log = getLogger(getClass());
43
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044 @Reference(cardinality = ReferenceCardinality.MANDATORY)
andrea78375762015-09-30 11:52:42 -070045 protected ComponentConfigService configService;
46
47 private ObjectNode root;
andrea78375762015-09-30 11:52:42 -070048
49 @Activate
50 protected void activate() {
51 this.loadConfigs();
52 log.info("Started");
53 }
andrea73f03782015-10-01 14:01:35 -070054
55 // Loads the configurations for each component from the file in
andreadd106a62015-10-02 13:39:48 -070056 // ../config/component-cfg.json, using the preSetProperty method.
andrea78375762015-09-30 11:52:42 -070057 private void loadConfigs() {
58 try {
andrea33279c82015-09-30 15:30:48 -070059 if (cfgFile.exists()) {
60 root = (ObjectNode) new ObjectMapper().readTree(cfgFile);
andreadd106a62015-10-02 13:39:48 -070061 root.fieldNames().
62 forEachRemaining(component -> root.path(component).fieldNames()
63 .forEachRemaining(k -> configService
64 .preSetProperty(component, k,
65 root.path(component).path(k)
66 .asText())));
andrea33279c82015-09-30 15:30:48 -070067 log.info("Loaded initial component configuration from {}", cfgFile);
andrea78375762015-09-30 11:52:42 -070068 }
69 } catch (Exception e) {
70 log.warn("Unable to load initial component configuration from {}",
andrea33279c82015-09-30 15:30:48 -070071 cfgFile, e);
andrea78375762015-09-30 11:52:42 -070072 }
73 }
andrea78375762015-09-30 11:52:42 -070074}