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