blob: b6e6a0f3f607e21940530cc885812f6c23221a35 [file] [log] [blame]
Thomas Vachuska6d697f12015-03-08 20:59:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska6d697f12015-03-08 20:59:50 -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 */
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;
Aaron Kruglikov66855212015-06-22 12:29:02 -070026import org.onlab.util.AbstractAccumulator;
27import org.onlab.util.Accumulator;
28import org.onlab.util.SharedExecutors;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070029import org.onosproject.cfg.ComponentConfigEvent;
30import org.onosproject.cfg.ComponentConfigService;
31import org.onosproject.cfg.ComponentConfigStore;
32import org.onosproject.cfg.ComponentConfigStoreDelegate;
33import org.onosproject.cfg.ConfigProperty;
34import org.osgi.service.cm.Configuration;
35import org.osgi.service.cm.ConfigurationAdmin;
36import org.slf4j.Logger;
37
38import java.io.IOException;
39import java.io.InputStream;
40import java.util.Dictionary;
Aaron Kruglikov66855212015-06-22 12:29:02 -070041import java.util.HashSet;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070042import java.util.Hashtable;
Aaron Kruglikov66855212015-06-22 12:29:02 -070043import java.util.List;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070044import java.util.Map;
Thomas Vachuska9f6bd522018-02-12 15:53:01 -080045import java.util.Objects;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070046import java.util.Set;
47
48import static com.google.common.base.Preconditions.checkArgument;
49import static com.google.common.base.Preconditions.checkNotNull;
Changhoon Yoon541ef712015-05-23 17:18:34 +090050import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska92af89f2015-06-22 15:16:23 -070051import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090052import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090053
Thomas Vachuska6d697f12015-03-08 20:59:50 -070054
55/**
56 * Implementation of the centralized component configuration service.
57 */
58@Component(immediate = true)
59@Service
60public class ComponentConfigManager implements ComponentConfigService {
61
62 private static final String COMPONENT_NULL = "Component name cannot be null";
63 private static final String PROPERTY_NULL = "Property name cannot be null";
Brian O'Connor57e83e82015-09-22 15:45:37 -070064 private static final String COMPONENT_MISSING = "Component %s is not registered";
65 private static final String PROPERTY_MISSING = "Property %s does not exist for %s";
66
Thomas Vachuska6d697f12015-03-08 20:59:50 -070067
Aaron Kruglikov66855212015-06-22 12:29:02 -070068 //Symbolic constants for use with the accumulator
69 private static final int MAX_ITEMS = 100;
70 private static final int MAX_BATCH_MILLIS = 1000;
71 private static final int MAX_IDLE_MILLIS = 250;
72
Thomas Vachuska6d697f12015-03-08 20:59:50 -070073 private static final String RESOURCE_EXT = ".cfgdef";
74
75 private final Logger log = getLogger(getClass());
76
77 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuska92af89f2015-06-22 15:16:23 -070078 private final InternalAccumulator accumulator = new InternalAccumulator();
Thomas Vachuska6d697f12015-03-08 20:59:50 -070079
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected ComponentConfigStore store;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected ConfigurationAdmin cfgAdmin;
85
86 // Locally maintained catalog of definitions.
Thomas Vachuska9f6bd522018-02-12 15:53:01 -080087 private final Map<String, Map<String, ConfigProperty>> properties =
Thomas Vachuska6d697f12015-03-08 20:59:50 -070088 Maps.newConcurrentMap();
89
Aaron Kruglikov66855212015-06-22 12:29:02 -070090
Thomas Vachuska6d697f12015-03-08 20:59:50 -070091 @Activate
92 public void activate() {
93 store.setDelegate(delegate);
94 log.info("Started");
95 }
96
97 @Deactivate
98 public void deactivate() {
99 store.unsetDelegate(delegate);
100 log.info("Stopped");
101 }
102
103 @Override
104 public Set<String> getComponentNames() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900105 checkPermission(CONFIG_READ);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700106 return ImmutableSet.copyOf(properties.keySet());
107 }
108
109 @Override
110 public void registerProperties(Class<?> componentClass) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900111 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900112
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700113 String componentName = componentClass.getName();
114 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
115 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
116 checkArgument(ris != null, "Property definitions not found at resource %s",
117 resourceName);
118
119 // Read the definitions
120 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
121
122 // Produce a new map of the properties and register it.
123 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
124 defs.forEach(p -> map.put(p.name(), p));
125
126 properties.put(componentName, map);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800127 loadExistingValues(componentName, map);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700128 } catch (IOException e) {
129 log.error("Unable to read property definitions from resource " + resourceName, e);
130 }
131 }
132
133 @Override
134 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900135 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900136
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700137 String componentName = componentClass.getName();
138 checkNotNull(componentName, COMPONENT_NULL);
139 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700140 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700141 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700142 clearExistingValues(componentName);
143 }
144 }
145
146 // Clears any existing values that may have been set.
147 private void clearExistingValues(String componentName) {
148 triggerUpdate(componentName);
149 }
150
151 @Override
152 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900153 checkPermission(CONFIG_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900154
Brian O'Connor85c29262015-03-10 20:53:43 -0700155 Map<String, ConfigProperty> map = properties.get(componentName);
156 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700157 }
158
159 @Override
160 public void setProperty(String componentName, String name, String value) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900161 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900162
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700163 checkNotNull(componentName, COMPONENT_NULL);
164 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700165
166 checkArgument(properties.containsKey(componentName),
167 COMPONENT_MISSING, componentName);
168 checkArgument(properties.get(componentName).containsKey(name),
169 PROPERTY_MISSING, name, componentName);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530170 checkValidity(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700171 store.setProperty(componentName, name, value);
172 }
173
174 @Override
andreadd106a62015-10-02 13:39:48 -0700175 public void preSetProperty(String componentName, String name, String value) {
andreadd106a62015-10-02 13:39:48 -0700176 checkPermission(CONFIG_WRITE);
177
178 checkNotNull(componentName, COMPONENT_NULL);
179 checkNotNull(name, PROPERTY_NULL);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530180 checkValidity(componentName, name, value);
andreadd106a62015-10-02 13:39:48 -0700181 store.setProperty(componentName, name, value);
182 }
183
184 @Override
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700185 public void unsetProperty(String componentName, String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900186 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900187
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700188 checkNotNull(componentName, COMPONENT_NULL);
189 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700190
191 checkArgument(properties.containsKey(componentName),
192 COMPONENT_MISSING, componentName);
193 checkArgument(properties.get(componentName).containsKey(name),
194 PROPERTY_MISSING, name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700195 store.unsetProperty(componentName, name);
196 }
197
psnehaf31ac6f2018-08-02 01:16:46 -0400198 @Override
199 public ConfigProperty getProperty(String componentName, String attribute) {
200 checkPermission(CONFIG_READ);
201
202 Map<String, ConfigProperty> map = properties.get(componentName);
203 if (map != null) {
204 return map.get(attribute);
205 } else {
206 log.error("Attribute {} not present in component {}", attribute, componentName);
207 return null;
208 }
209 }
210
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700211 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
212
213 @Override
214 public void notify(ComponentConfigEvent event) {
215 String componentName = event.subject();
216 String name = event.name();
217 String value = event.value();
218
219 switch (event.type()) {
220 case PROPERTY_SET:
221 set(componentName, name, value);
222 break;
223 case PROPERTY_UNSET:
224 reset(componentName, name);
225 break;
226 default:
227 break;
228 }
229 }
230 }
231
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700232 // Buffers multiple subsequent configuration updates into one notification.
233 private class InternalAccumulator extends AbstractAccumulator<String>
234 implements Accumulator<String> {
Aaron Kruglikov66855212015-06-22 12:29:02 -0700235
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700236 protected InternalAccumulator() {
237 super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700238 }
239
240 @Override
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700241 public void processItems(List<String> items) {
242 // Conversion to set removes duplicates
243 Set<String> componentSet = new HashSet<>(items);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700244 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700245 }
246 }
247
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700248 // Locates the property in the component map and replaces it with an
249 // updated copy.
250 private void set(String componentName, String name, String value) {
251 Map<String, ConfigProperty> map = properties.get(componentName);
252 if (map != null) {
253 ConfigProperty prop = map.get(name);
254 if (prop != null) {
255 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700256 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700257 return;
258 }
259 }
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800260
261 // If definition doesn't exist in local catalog, cache the property.
262 preSet(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700263 }
264
265 // Locates the property in the component map and replaces it with an
266 // reset copy.
267 private void reset(String componentName, String name) {
268 Map<String, ConfigProperty> map = properties.get(componentName);
269 if (map != null) {
270 ConfigProperty prop = map.get(name);
271 if (prop != null) {
272 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700273 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700274 return;
275 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700276 log.warn("Unable to reset non-existent property {} for component {}",
277 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700278 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700279 }
280
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800281 // Stores non-existent property so that loadExistingValues() can load in future.
282 private void preSet(String componentName, String name, String value) {
283 try {
284 Configuration config = cfgAdmin.getConfiguration(componentName, null);
285 Dictionary<String, Object> props = config.getProperties();
286 if (props == null) {
287 props = new Hashtable<>();
288 }
289 props.put(name, value);
290 config.update(props);
291 } catch (IOException e) {
292 log.error("Failed to preset configuration for {}", componentName);
293 }
294 }
295
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530296 // Checks whether the value of the specified configuration property is a valid one or not.
297 private void checkValidity(String componentName, String name, String newValue) {
298 Map<String, ConfigProperty> map = properties.get(componentName);
299 if (map == null) {
300 return;
301 }
302 ConfigProperty prop = map.get(name);
303 ConfigProperty.Type type = prop.type();
304 try {
305 switch (type) {
306 case INTEGER:
307 Integer.parseInt(newValue);
308 break;
309 case LONG:
310 Long.parseLong(newValue);
311 break;
312 case FLOAT:
313 Float.parseFloat(newValue);
314 break;
315 case DOUBLE:
316 Double.parseDouble(newValue);
317 break;
318 case BOOLEAN:
319 if (!((newValue != null) && (newValue.equalsIgnoreCase("true") ||
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800320 newValue.equalsIgnoreCase("false")))) {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530321 throw new IllegalArgumentException("Invalid " + type + " value");
322 }
323 break;
324 case STRING:
325 case BYTE:
326 //do nothing
327 break;
328 default:
329 log.warn("Not a valid config property type");
330 break;
331
332 }
333 } catch (NumberFormatException e) {
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800334 throw new NumberFormatException("Invalid " + type + " value");
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530335 }
336 }
337
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800338 // Loads existing property values that may have been learned from other
339 // nodes in the cluster before the local property registration.
340 private void loadExistingValues(String componentName,
341 Map<String, ConfigProperty> map) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700342 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700343 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800344 Dictionary<String, Object> localProperties = cfg.getProperties();
345
346 // Iterate over all registered config properties...
347 for (ConfigProperty p : ImmutableSet.copyOf(map.values())) {
348 String name = p.name();
349 String globalValue = store.getProperty(componentName, name);
350 String localValue = localProperties != null ? (String) localProperties.get(name) : null;
Thomas Vachuska611f4662018-02-27 11:30:09 -0800351
352 log.debug("Processing {} property {}; global {}; local {}",
353 componentName, name, globalValue, localValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800354 try {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800355 // 1) If the global value is the same as default, or
356 // 2) if it is not set explicitly, but the local value is
357 // not the same as the default, reset local value to default.
358 if (Objects.equals(globalValue, p.defaultValue()) ||
359 (globalValue == null &&
360 !Objects.equals(localValue, p.defaultValue()))) {
361 log.debug("Resetting {} property {}; value same as default",
362 componentName, name);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800363 reset(componentName, name);
364
365 } else if (!Objects.equals(globalValue, localValue)) {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800366 // If the local value is different from global value
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800367 // validate the global value and apply it locally.
Thomas Vachuska611f4662018-02-27 11:30:09 -0800368 log.debug("Setting {} property {} to {}",
369 componentName, name, globalValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800370 checkValidity(componentName, name, globalValue);
371 set(componentName, name, globalValue);
Thomas Vachuska611f4662018-02-27 11:30:09 -0800372
373 } else {
374 // Otherwise, simply update the registered property
375 // with the global value.
376 log.debug("Syncing {} property {} to {}",
377 componentName, name, globalValue);
378 map.put(name, ConfigProperty.setProperty(p, globalValue));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700379 }
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800380 } catch (IllegalArgumentException e) {
381 log.warn("Value {} is not a valid {}; using default",
382 globalValue, p.type());
383 reset(componentName, name);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700384 }
385 }
386 } catch (IOException e) {
387 log.error("Unable to get configuration for " + componentName, e);
388 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700389 }
390
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700391 private void triggerUpdate(String componentName) {
392 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700393 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700394 Map<String, ConfigProperty> map = properties.get(componentName);
Ray Milkey7981a322018-08-16 14:55:46 -0700395 if (map == null) {
396 // Prevent NPE if the component isn't there
397 log.warn("Component not found for " + componentName);
398 return;
399 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700400 Dictionary<String, Object> props = new Hashtable<>();
Thomas Vachuska611f4662018-02-27 11:30:09 -0800401 map.values().stream().filter(p -> p.value() != null)
402 .forEach(p -> props.put(p.name(), p.value()));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700403 cfg.update(props);
404 } catch (IOException e) {
405 log.warn("Unable to update configuration for " + componentName, e);
406 }
407 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700408}