andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.cfg.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigService; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.io.File; |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 29 | |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Component responsible for automatically loading configuration file from |
| 34 | * configuration directory. |
| 35 | */ |
| 36 | @Component(immediate = true) |
| 37 | public class ComponentConfigLoader { |
| 38 | |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 39 | private static final String CFG_JSON = "../config/component-cfg.json"; |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 40 | static File cfgFile = new File(CFG_JSON); |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected ComponentConfigService configService; |
| 46 | |
| 47 | private ObjectNode root; |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 48 | |
| 49 | @Activate |
| 50 | protected void activate() { |
| 51 | this.loadConfigs(); |
| 52 | log.info("Started"); |
| 53 | } |
andrea | 73f0378 | 2015-10-01 14:01:35 -0700 | [diff] [blame] | 54 | |
| 55 | // Loads the configurations for each component from the file in |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 56 | // ../config/component-cfg.json, using the preSetProperty method. |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 57 | private void loadConfigs() { |
| 58 | try { |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 59 | if (cfgFile.exists()) { |
| 60 | root = (ObjectNode) new ObjectMapper().readTree(cfgFile); |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 61 | 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()))); |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 67 | log.info("Loaded initial component configuration from {}", cfgFile); |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 68 | } |
| 69 | } catch (Exception e) { |
| 70 | log.warn("Unable to load initial component configuration from {}", |
andrea | 33279c8 | 2015-09-30 15:30:48 -0700 | [diff] [blame] | 71 | cfgFile, e); |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
andrea | 7837576 | 2015-09-30 11:52:42 -0700 | [diff] [blame] | 74 | } |