blob: 1e0ca6bad2a4545d8caae5f12b4894d3de9f3852 [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -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.cfg.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onosproject.cfg.ComponentConfigEvent;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.cfg.ComponentConfigStore;
29import org.onosproject.cfg.ComponentConfigStoreDelegate;
30import org.onosproject.cfg.ConfigProperty;
31import org.osgi.service.cm.Configuration;
32import org.osgi.service.cm.ConfigurationAdmin;
33import org.slf4j.Logger;
34
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.Dictionary;
38import java.util.Enumeration;
39import java.util.Hashtable;
40import java.util.Map;
41import java.util.Set;
42
43import static com.google.common.base.Preconditions.checkArgument;
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Implementation of the centralized component configuration service.
49 */
50@Component(immediate = true)
51@Service
52public class ComponentConfigManager implements ComponentConfigService {
53
54 private static final String COMPONENT_NULL = "Component name cannot be null";
55 private static final String PROPERTY_NULL = "Property name cannot be null";
56
57 private static final String RESOURCE_EXT = ".cfgdef";
58
59 private final Logger log = getLogger(getClass());
60
61 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ComponentConfigStore store;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ConfigurationAdmin cfgAdmin;
68
69 // Locally maintained catalog of definitions.
70 private final Map<String, Map<String, ConfigProperty>> properties =
71 Maps.newConcurrentMap();
72
73 @Activate
74 public void activate() {
75 store.setDelegate(delegate);
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 store.unsetDelegate(delegate);
82 log.info("Stopped");
83 }
84
85 @Override
86 public Set<String> getComponentNames() {
87 return ImmutableSet.copyOf(properties.keySet());
88 }
89
90 @Override
91 public void registerProperties(Class<?> componentClass) {
92 String componentName = componentClass.getName();
93 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
94 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
95 checkArgument(ris != null, "Property definitions not found at resource %s",
96 resourceName);
97
98 // Read the definitions
99 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
100
101 // Produce a new map of the properties and register it.
102 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
103 defs.forEach(p -> map.put(p.name(), p));
104
105 properties.put(componentName, map);
106 loadExistingValues(componentName);
107 } catch (IOException e) {
108 log.error("Unable to read property definitions from resource " + resourceName, e);
109 }
110 }
111
112 @Override
113 public void unregisterProperties(Class<?> componentClass, boolean clear) {
114 String componentName = componentClass.getName();
115 checkNotNull(componentName, COMPONENT_NULL);
116 Map<String, ConfigProperty> cps = properties.remove(componentName);
117 if (cps != null) {
118 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
119 }
120
121 if (clear) {
122 clearExistingValues(componentName);
123 }
124 }
125
126 // Clears any existing values that may have been set.
127 private void clearExistingValues(String componentName) {
128 triggerUpdate(componentName);
129 }
130
131 @Override
132 public Set<ConfigProperty> getProperties(String componentName) {
Brian O'Connor85c29262015-03-10 20:53:43 -0700133 Map<String, ConfigProperty> map = properties.get(componentName);
134 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700135 }
136
137 @Override
138 public void setProperty(String componentName, String name, String value) {
139 checkNotNull(componentName, COMPONENT_NULL);
140 checkNotNull(name, PROPERTY_NULL);
141 store.setProperty(componentName, name, value);
142 }
143
144 @Override
145 public void unsetProperty(String componentName, String name) {
146 checkNotNull(componentName, COMPONENT_NULL);
147 checkNotNull(name, PROPERTY_NULL);
148 store.unsetProperty(componentName, name);
149 }
150
151 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
152
153 @Override
154 public void notify(ComponentConfigEvent event) {
155 String componentName = event.subject();
156 String name = event.name();
157 String value = event.value();
158
159 switch (event.type()) {
160 case PROPERTY_SET:
161 set(componentName, name, value);
162 break;
163 case PROPERTY_UNSET:
164 reset(componentName, name);
165 break;
166 default:
167 break;
168 }
169 }
170 }
171
172 // Locates the property in the component map and replaces it with an
173 // updated copy.
174 private void set(String componentName, String name, String value) {
175 Map<String, ConfigProperty> map = properties.get(componentName);
176 if (map != null) {
177 ConfigProperty prop = map.get(name);
178 if (prop != null) {
179 map.put(name, ConfigProperty.setProperty(prop, value));
180 triggerUpdate(componentName);
181 return;
182 }
183 }
184 log.warn("Unable to set non-existent property {} for component {}",
185 name, componentName);
186 }
187
188 // Locates the property in the component map and replaces it with an
189 // reset copy.
190 private void reset(String componentName, String name) {
191 Map<String, ConfigProperty> map = properties.get(componentName);
192 if (map != null) {
193 ConfigProperty prop = map.get(name);
194 if (prop != null) {
195 map.put(name, ConfigProperty.resetProperty(prop));
196 triggerUpdate(componentName);
197 return;
198 }
199 }
200 log.warn("Unable to reset non-existent property {} for component {}",
201 name, componentName);
202 }
203
204 // Loads existing property values that may have been set.
205 private void loadExistingValues(String componentName) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700206 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700207 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700208 Map<String, ConfigProperty> map = properties.get(componentName);
209 Dictionary<String, Object> props = cfg.getProperties();
210 if (props != null) {
211 Enumeration<String> it = props.keys();
212 while (it.hasMoreElements()) {
213 String name = it.nextElement();
214 ConfigProperty p = map.get(name);
215 if (p != null) {
216 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
217 }
218 }
219 }
220 } catch (IOException e) {
221 log.error("Unable to get configuration for " + componentName, e);
222 }
223
224 }
225
226 // FIXME: This should be a slightly deferred execution to allow changing
227 // values just once per component when a number of updates arrive shortly
228 // after each other.
229 private void triggerUpdate(String componentName) {
230 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700231 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700232 Map<String, ConfigProperty> map = properties.get(componentName);
233 Dictionary<String, Object> props = new Hashtable<>();
234 map.values().forEach(p -> props.put(p.name(), p.value()));
235 cfg.update(props);
236 } catch (IOException e) {
237 log.warn("Unable to update configuration for " + componentName, e);
238 }
239 }
240
241}