blob: 49ae74874f71affddd0906b08da38afdf2380179 [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;
Aaron Kruglikov66855212015-06-22 12:29:02 -070020import org.onlab.util.AbstractAccumulator;
21import org.onlab.util.Accumulator;
22import org.onlab.util.SharedExecutors;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070023import org.onosproject.cfg.ComponentConfigEvent;
24import org.onosproject.cfg.ComponentConfigService;
25import org.onosproject.cfg.ComponentConfigStore;
26import org.onosproject.cfg.ComponentConfigStoreDelegate;
27import org.onosproject.cfg.ConfigProperty;
28import org.osgi.service.cm.Configuration;
29import org.osgi.service.cm.ConfigurationAdmin;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070035import org.slf4j.Logger;
36
37import java.io.IOException;
38import java.io.InputStream;
39import java.util.Dictionary;
Aaron Kruglikov66855212015-06-22 12:29:02 -070040import java.util.HashSet;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070041import java.util.Hashtable;
Aaron Kruglikov66855212015-06-22 12:29:02 -070042import java.util.List;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070043import java.util.Map;
Thomas Vachuska9f6bd522018-02-12 15:53:01 -080044import java.util.Objects;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070045import java.util.Set;
46
47import static com.google.common.base.Preconditions.checkArgument;
48import static com.google.common.base.Preconditions.checkNotNull;
Changhoon Yoon541ef712015-05-23 17:18:34 +090049import static org.onosproject.security.AppGuard.checkPermission;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050import static org.onosproject.security.AppPermission.Type.CONFIG_READ;
51import static org.onosproject.security.AppPermission.Type.CONFIG_WRITE;
Thomas Vachuska92af89f2015-06-22 15:16:23 -070052import static org.slf4j.LoggerFactory.getLogger;
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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058@Component(immediate = true, service = ComponentConfigService.class)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070059public class ComponentConfigManager implements ComponentConfigService {
60
61 private static final String COMPONENT_NULL = "Component name cannot be null";
62 private static final String PROPERTY_NULL = "Property name cannot be null";
Brian O'Connor57e83e82015-09-22 15:45:37 -070063 private static final String COMPONENT_MISSING = "Component %s is not registered";
64 private static final String PROPERTY_MISSING = "Property %s does not exist for %s";
65
Thomas Vachuska6d697f12015-03-08 20:59:50 -070066
Aaron Kruglikov66855212015-06-22 12:29:02 -070067 //Symbolic constants for use with the accumulator
68 private static final int MAX_ITEMS = 100;
69 private static final int MAX_BATCH_MILLIS = 1000;
70 private static final int MAX_IDLE_MILLIS = 250;
71
Thomas Vachuska6d697f12015-03-08 20:59:50 -070072 private static final String RESOURCE_EXT = ".cfgdef";
73
74 private final Logger log = getLogger(getClass());
75
76 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
Carmelo Cascone4d5607e2021-01-28 18:25:59 -080077 private InternalAccumulator accumulator;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070078
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070080 protected ComponentConfigStore store;
81
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070083 protected ConfigurationAdmin cfgAdmin;
84
85 // Locally maintained catalog of definitions.
Thomas Vachuska9f6bd522018-02-12 15:53:01 -080086 private final Map<String, Map<String, ConfigProperty>> properties =
Thomas Vachuska6d697f12015-03-08 20:59:50 -070087 Maps.newConcurrentMap();
88
Aaron Kruglikov66855212015-06-22 12:29:02 -070089
Thomas Vachuska6d697f12015-03-08 20:59:50 -070090 @Activate
91 public void activate() {
Carmelo Cascone4d5607e2021-01-28 18:25:59 -080092 // Accumulator depends on SharedExecutors, we create a new one at each
93 // activation to avoid using executors that have been terminated, which
94 // can happen during core bundle refresh.
95 accumulator = new InternalAccumulator();
Thomas Vachuska6d697f12015-03-08 20:59:50 -070096 store.setDelegate(delegate);
97 log.info("Started");
98 }
99
100 @Deactivate
101 public void deactivate() {
Carmelo Cascone4d5607e2021-01-28 18:25:59 -0800102 accumulator = null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700103 store.unsetDelegate(delegate);
104 log.info("Stopped");
105 }
106
107 @Override
108 public Set<String> getComponentNames() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900109 checkPermission(CONFIG_READ);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700110 return ImmutableSet.copyOf(properties.keySet());
111 }
112
113 @Override
114 public void registerProperties(Class<?> componentClass) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900115 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900116
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700117 String componentName = componentClass.getName();
118 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
Carmelo Casconeafdddc12019-07-15 18:51:32 -0700119 if (componentClass.getResource(resourceName) == null) {
120 // Try looking in my/package/name/MyClass.cfgdef
121 resourceName = componentClass.getCanonicalName().replace('.', '/') + RESOURCE_EXT;
122 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700123 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
124 checkArgument(ris != null, "Property definitions not found at resource %s",
125 resourceName);
126
127 // Read the definitions
128 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
129
130 // Produce a new map of the properties and register it.
131 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
132 defs.forEach(p -> map.put(p.name(), p));
133
134 properties.put(componentName, map);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800135 loadExistingValues(componentName, map);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700136 } catch (IOException e) {
137 log.error("Unable to read property definitions from resource " + resourceName, e);
138 }
139 }
140
141 @Override
142 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900143 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900144
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700145 String componentName = componentClass.getName();
146 checkNotNull(componentName, COMPONENT_NULL);
147 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700148 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700149 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700150 clearExistingValues(componentName);
151 }
152 }
153
154 // Clears any existing values that may have been set.
155 private void clearExistingValues(String componentName) {
156 triggerUpdate(componentName);
157 }
158
159 @Override
160 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900161 checkPermission(CONFIG_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900162
Brian O'Connor85c29262015-03-10 20:53:43 -0700163 Map<String, ConfigProperty> map = properties.get(componentName);
164 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700165 }
166
167 @Override
168 public void setProperty(String componentName, String name, String value) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900169 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900170
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700171 checkNotNull(componentName, COMPONENT_NULL);
172 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700173
174 checkArgument(properties.containsKey(componentName),
175 COMPONENT_MISSING, componentName);
176 checkArgument(properties.get(componentName).containsKey(name),
177 PROPERTY_MISSING, name, componentName);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530178 checkValidity(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700179 store.setProperty(componentName, name, value);
180 }
181
182 @Override
andreadd106a62015-10-02 13:39:48 -0700183 public void preSetProperty(String componentName, String name, String value) {
Charles Chan8b488de2019-04-10 17:12:20 -0700184 preSetProperty(componentName, name, value, true);
185 }
186
187 @Override
188 public void preSetProperty(String componentName, String name, String value, boolean override) {
andreadd106a62015-10-02 13:39:48 -0700189 checkPermission(CONFIG_WRITE);
190
191 checkNotNull(componentName, COMPONENT_NULL);
192 checkNotNull(name, PROPERTY_NULL);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530193 checkValidity(componentName, name, value);
Charles Chan8b488de2019-04-10 17:12:20 -0700194 store.setProperty(componentName, name, value, override);
andreadd106a62015-10-02 13:39:48 -0700195 }
196
197 @Override
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700198 public void unsetProperty(String componentName, String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900199 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900200
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700201 checkNotNull(componentName, COMPONENT_NULL);
202 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700203
204 checkArgument(properties.containsKey(componentName),
205 COMPONENT_MISSING, componentName);
206 checkArgument(properties.get(componentName).containsKey(name),
207 PROPERTY_MISSING, name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700208 store.unsetProperty(componentName, name);
209 }
210
psnehaf31ac6f2018-08-02 01:16:46 -0400211 @Override
212 public ConfigProperty getProperty(String componentName, String attribute) {
213 checkPermission(CONFIG_READ);
214
215 Map<String, ConfigProperty> map = properties.get(componentName);
216 if (map != null) {
217 return map.get(attribute);
218 } else {
219 log.error("Attribute {} not present in component {}", attribute, componentName);
220 return null;
221 }
222 }
223
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700224 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
225
226 @Override
227 public void notify(ComponentConfigEvent event) {
228 String componentName = event.subject();
229 String name = event.name();
230 String value = event.value();
231
232 switch (event.type()) {
233 case PROPERTY_SET:
234 set(componentName, name, value);
235 break;
236 case PROPERTY_UNSET:
237 reset(componentName, name);
238 break;
239 default:
240 break;
241 }
242 }
243 }
244
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700245 // Buffers multiple subsequent configuration updates into one notification.
246 private class InternalAccumulator extends AbstractAccumulator<String>
247 implements Accumulator<String> {
Aaron Kruglikov66855212015-06-22 12:29:02 -0700248
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700249 protected InternalAccumulator() {
250 super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700251 }
252
253 @Override
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700254 public void processItems(List<String> items) {
255 // Conversion to set removes duplicates
256 Set<String> componentSet = new HashSet<>(items);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700257 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700258 }
259 }
260
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700261 // Locates the property in the component map and replaces it with an
262 // updated copy.
263 private void set(String componentName, String name, String value) {
264 Map<String, ConfigProperty> map = properties.get(componentName);
265 if (map != null) {
266 ConfigProperty prop = map.get(name);
267 if (prop != null) {
268 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700269 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700270 return;
271 }
272 }
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800273
274 // If definition doesn't exist in local catalog, cache the property.
275 preSet(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700276 }
277
278 // Locates the property in the component map and replaces it with an
279 // reset copy.
280 private void reset(String componentName, String name) {
281 Map<String, ConfigProperty> map = properties.get(componentName);
282 if (map != null) {
283 ConfigProperty prop = map.get(name);
284 if (prop != null) {
285 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700286 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700287 return;
288 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700289 log.warn("Unable to reset non-existent property {} for component {}",
290 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700291 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700292 }
293
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800294 // Stores non-existent property so that loadExistingValues() can load in future.
295 private void preSet(String componentName, String name, String value) {
296 try {
297 Configuration config = cfgAdmin.getConfiguration(componentName, null);
298 Dictionary<String, Object> props = config.getProperties();
299 if (props == null) {
300 props = new Hashtable<>();
301 }
302 props.put(name, value);
303 config.update(props);
304 } catch (IOException e) {
305 log.error("Failed to preset configuration for {}", componentName);
306 }
307 }
308
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530309 // Checks whether the value of the specified configuration property is a valid one or not.
310 private void checkValidity(String componentName, String name, String newValue) {
311 Map<String, ConfigProperty> map = properties.get(componentName);
312 if (map == null) {
313 return;
314 }
315 ConfigProperty prop = map.get(name);
316 ConfigProperty.Type type = prop.type();
317 try {
318 switch (type) {
319 case INTEGER:
320 Integer.parseInt(newValue);
321 break;
322 case LONG:
323 Long.parseLong(newValue);
324 break;
325 case FLOAT:
326 Float.parseFloat(newValue);
327 break;
328 case DOUBLE:
329 Double.parseDouble(newValue);
330 break;
331 case BOOLEAN:
332 if (!((newValue != null) && (newValue.equalsIgnoreCase("true") ||
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800333 newValue.equalsIgnoreCase("false")))) {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530334 throw new IllegalArgumentException("Invalid " + type + " value");
335 }
336 break;
337 case STRING:
338 case BYTE:
339 //do nothing
340 break;
341 default:
342 log.warn("Not a valid config property type");
343 break;
344
345 }
346 } catch (NumberFormatException e) {
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800347 throw new NumberFormatException("Invalid " + type + " value");
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530348 }
349 }
350
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800351 // Loads existing property values that may have been learned from other
352 // nodes in the cluster before the local property registration.
353 private void loadExistingValues(String componentName,
354 Map<String, ConfigProperty> map) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700355 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700356 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800357 Dictionary<String, Object> localProperties = cfg.getProperties();
358
359 // Iterate over all registered config properties...
360 for (ConfigProperty p : ImmutableSet.copyOf(map.values())) {
361 String name = p.name();
362 String globalValue = store.getProperty(componentName, name);
363 String localValue = localProperties != null ? (String) localProperties.get(name) : null;
Thomas Vachuska611f4662018-02-27 11:30:09 -0800364
365 log.debug("Processing {} property {}; global {}; local {}",
366 componentName, name, globalValue, localValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800367 try {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800368 // 1) If the global value is the same as default, or
369 // 2) if it is not set explicitly, but the local value is
370 // not the same as the default, reset local value to default.
371 if (Objects.equals(globalValue, p.defaultValue()) ||
372 (globalValue == null &&
373 !Objects.equals(localValue, p.defaultValue()))) {
374 log.debug("Resetting {} property {}; value same as default",
375 componentName, name);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800376 reset(componentName, name);
377
378 } else if (!Objects.equals(globalValue, localValue)) {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800379 // If the local value is different from global value
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800380 // validate the global value and apply it locally.
Ray Milkey397caca2019-04-01 16:27:50 -0700381 String newValue;
382 if (globalValue != null) {
383 newValue = globalValue;
384 } else {
385 newValue = localValue;
386 }
Thomas Vachuska611f4662018-02-27 11:30:09 -0800387 log.debug("Setting {} property {} to {}",
Ray Milkey397caca2019-04-01 16:27:50 -0700388 componentName, name, newValue);
389 checkValidity(componentName, name, newValue);
390 set(componentName, name, newValue);
Thomas Vachuska611f4662018-02-27 11:30:09 -0800391
392 } else {
393 // Otherwise, simply update the registered property
394 // with the global value.
395 log.debug("Syncing {} property {} to {}",
396 componentName, name, globalValue);
397 map.put(name, ConfigProperty.setProperty(p, globalValue));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700398 }
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800399 } catch (IllegalArgumentException e) {
Ray Milkey397caca2019-04-01 16:27:50 -0700400 log.warn("Value {} for property {} is not a valid {}; using default",
401 globalValue, name, p.type());
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800402 reset(componentName, name);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700403 }
404 }
405 } catch (IOException e) {
406 log.error("Unable to get configuration for " + componentName, e);
407 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700408 }
409
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700410 private void triggerUpdate(String componentName) {
411 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700412 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700413 Map<String, ConfigProperty> map = properties.get(componentName);
Ray Milkey7981a322018-08-16 14:55:46 -0700414 if (map == null) {
415 // Prevent NPE if the component isn't there
416 log.warn("Component not found for " + componentName);
417 return;
418 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700419 Dictionary<String, Object> props = new Hashtable<>();
Thomas Vachuska611f4662018-02-27 11:30:09 -0800420 map.values().stream().filter(p -> p.value() != null)
421 .forEach(p -> props.put(p.name(), p.value()));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700422 cfg.update(props);
423 } catch (IOException e) {
424 log.warn("Unable to update configuration for " + componentName, e);
425 }
426 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700427}