blob: 42af52edd868f63140cc6373032e9fb82cb15c57 [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();
Thomas Vachuska92af89f2015-06-22 15:16:23 -070077 private final InternalAccumulator accumulator = new InternalAccumulator();
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() {
92 store.setDelegate(delegate);
93 log.info("Started");
94 }
95
96 @Deactivate
97 public void deactivate() {
98 store.unsetDelegate(delegate);
99 log.info("Stopped");
100 }
101
102 @Override
103 public Set<String> getComponentNames() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900104 checkPermission(CONFIG_READ);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700105 return ImmutableSet.copyOf(properties.keySet());
106 }
107
108 @Override
109 public void registerProperties(Class<?> componentClass) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900110 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900111
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700112 String componentName = componentClass.getName();
113 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
114 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
115 checkArgument(ris != null, "Property definitions not found at resource %s",
116 resourceName);
117
118 // Read the definitions
119 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
120
121 // Produce a new map of the properties and register it.
122 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
123 defs.forEach(p -> map.put(p.name(), p));
124
125 properties.put(componentName, map);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800126 loadExistingValues(componentName, map);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700127 } catch (IOException e) {
128 log.error("Unable to read property definitions from resource " + resourceName, e);
129 }
130 }
131
132 @Override
133 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900134 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900135
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700136 String componentName = componentClass.getName();
137 checkNotNull(componentName, COMPONENT_NULL);
138 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700139 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700140 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700141 clearExistingValues(componentName);
142 }
143 }
144
145 // Clears any existing values that may have been set.
146 private void clearExistingValues(String componentName) {
147 triggerUpdate(componentName);
148 }
149
150 @Override
151 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900152 checkPermission(CONFIG_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900153
Brian O'Connor85c29262015-03-10 20:53:43 -0700154 Map<String, ConfigProperty> map = properties.get(componentName);
155 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700156 }
157
158 @Override
159 public void setProperty(String componentName, String name, String value) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900160 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900161
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700162 checkNotNull(componentName, COMPONENT_NULL);
163 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700164
165 checkArgument(properties.containsKey(componentName),
166 COMPONENT_MISSING, componentName);
167 checkArgument(properties.get(componentName).containsKey(name),
168 PROPERTY_MISSING, name, componentName);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530169 checkValidity(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700170 store.setProperty(componentName, name, value);
171 }
172
173 @Override
andreadd106a62015-10-02 13:39:48 -0700174 public void preSetProperty(String componentName, String name, String value) {
andreadd106a62015-10-02 13:39:48 -0700175 checkPermission(CONFIG_WRITE);
176
177 checkNotNull(componentName, COMPONENT_NULL);
178 checkNotNull(name, PROPERTY_NULL);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530179 checkValidity(componentName, name, value);
andreadd106a62015-10-02 13:39:48 -0700180 store.setProperty(componentName, name, value);
181 }
182
183 @Override
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700184 public void unsetProperty(String componentName, String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900185 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900186
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700187 checkNotNull(componentName, COMPONENT_NULL);
188 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700189
190 checkArgument(properties.containsKey(componentName),
191 COMPONENT_MISSING, componentName);
192 checkArgument(properties.get(componentName).containsKey(name),
193 PROPERTY_MISSING, name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700194 store.unsetProperty(componentName, name);
195 }
196
psnehaf31ac6f2018-08-02 01:16:46 -0400197 @Override
198 public ConfigProperty getProperty(String componentName, String attribute) {
199 checkPermission(CONFIG_READ);
200
201 Map<String, ConfigProperty> map = properties.get(componentName);
202 if (map != null) {
203 return map.get(attribute);
204 } else {
205 log.error("Attribute {} not present in component {}", attribute, componentName);
206 return null;
207 }
208 }
209
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700210 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
211
212 @Override
213 public void notify(ComponentConfigEvent event) {
214 String componentName = event.subject();
215 String name = event.name();
216 String value = event.value();
217
218 switch (event.type()) {
219 case PROPERTY_SET:
220 set(componentName, name, value);
221 break;
222 case PROPERTY_UNSET:
223 reset(componentName, name);
224 break;
225 default:
226 break;
227 }
228 }
229 }
230
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700231 // Buffers multiple subsequent configuration updates into one notification.
232 private class InternalAccumulator extends AbstractAccumulator<String>
233 implements Accumulator<String> {
Aaron Kruglikov66855212015-06-22 12:29:02 -0700234
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700235 protected InternalAccumulator() {
236 super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700237 }
238
239 @Override
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700240 public void processItems(List<String> items) {
241 // Conversion to set removes duplicates
242 Set<String> componentSet = new HashSet<>(items);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700243 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700244 }
245 }
246
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700247 // Locates the property in the component map and replaces it with an
248 // updated copy.
249 private void set(String componentName, String name, String value) {
250 Map<String, ConfigProperty> map = properties.get(componentName);
251 if (map != null) {
252 ConfigProperty prop = map.get(name);
253 if (prop != null) {
254 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700255 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700256 return;
257 }
258 }
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800259
260 // If definition doesn't exist in local catalog, cache the property.
261 preSet(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700262 }
263
264 // Locates the property in the component map and replaces it with an
265 // reset copy.
266 private void reset(String componentName, String name) {
267 Map<String, ConfigProperty> map = properties.get(componentName);
268 if (map != null) {
269 ConfigProperty prop = map.get(name);
270 if (prop != null) {
271 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700272 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700273 return;
274 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700275 log.warn("Unable to reset non-existent property {} for component {}",
276 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700277 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700278 }
279
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800280 // Stores non-existent property so that loadExistingValues() can load in future.
281 private void preSet(String componentName, String name, String value) {
282 try {
283 Configuration config = cfgAdmin.getConfiguration(componentName, null);
284 Dictionary<String, Object> props = config.getProperties();
285 if (props == null) {
286 props = new Hashtable<>();
287 }
288 props.put(name, value);
289 config.update(props);
290 } catch (IOException e) {
291 log.error("Failed to preset configuration for {}", componentName);
292 }
293 }
294
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530295 // Checks whether the value of the specified configuration property is a valid one or not.
296 private void checkValidity(String componentName, String name, String newValue) {
297 Map<String, ConfigProperty> map = properties.get(componentName);
298 if (map == null) {
299 return;
300 }
301 ConfigProperty prop = map.get(name);
302 ConfigProperty.Type type = prop.type();
303 try {
304 switch (type) {
305 case INTEGER:
306 Integer.parseInt(newValue);
307 break;
308 case LONG:
309 Long.parseLong(newValue);
310 break;
311 case FLOAT:
312 Float.parseFloat(newValue);
313 break;
314 case DOUBLE:
315 Double.parseDouble(newValue);
316 break;
317 case BOOLEAN:
318 if (!((newValue != null) && (newValue.equalsIgnoreCase("true") ||
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800319 newValue.equalsIgnoreCase("false")))) {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530320 throw new IllegalArgumentException("Invalid " + type + " value");
321 }
322 break;
323 case STRING:
324 case BYTE:
325 //do nothing
326 break;
327 default:
328 log.warn("Not a valid config property type");
329 break;
330
331 }
332 } catch (NumberFormatException e) {
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800333 throw new NumberFormatException("Invalid " + type + " value");
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530334 }
335 }
336
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800337 // Loads existing property values that may have been learned from other
338 // nodes in the cluster before the local property registration.
339 private void loadExistingValues(String componentName,
340 Map<String, ConfigProperty> map) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700341 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700342 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800343 Dictionary<String, Object> localProperties = cfg.getProperties();
344
345 // Iterate over all registered config properties...
346 for (ConfigProperty p : ImmutableSet.copyOf(map.values())) {
347 String name = p.name();
348 String globalValue = store.getProperty(componentName, name);
349 String localValue = localProperties != null ? (String) localProperties.get(name) : null;
Thomas Vachuska611f4662018-02-27 11:30:09 -0800350
351 log.debug("Processing {} property {}; global {}; local {}",
352 componentName, name, globalValue, localValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800353 try {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800354 // 1) If the global value is the same as default, or
355 // 2) if it is not set explicitly, but the local value is
356 // not the same as the default, reset local value to default.
357 if (Objects.equals(globalValue, p.defaultValue()) ||
358 (globalValue == null &&
359 !Objects.equals(localValue, p.defaultValue()))) {
360 log.debug("Resetting {} property {}; value same as default",
361 componentName, name);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800362 reset(componentName, name);
363
364 } else if (!Objects.equals(globalValue, localValue)) {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800365 // If the local value is different from global value
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800366 // validate the global value and apply it locally.
Thomas Vachuska611f4662018-02-27 11:30:09 -0800367 log.debug("Setting {} property {} to {}",
368 componentName, name, globalValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800369 checkValidity(componentName, name, globalValue);
370 set(componentName, name, globalValue);
Thomas Vachuska611f4662018-02-27 11:30:09 -0800371
372 } else {
373 // Otherwise, simply update the registered property
374 // with the global value.
375 log.debug("Syncing {} property {} to {}",
376 componentName, name, globalValue);
377 map.put(name, ConfigProperty.setProperty(p, globalValue));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700378 }
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800379 } catch (IllegalArgumentException e) {
380 log.warn("Value {} is not a valid {}; using default",
381 globalValue, p.type());
382 reset(componentName, name);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700383 }
384 }
385 } catch (IOException e) {
386 log.error("Unable to get configuration for " + componentName, e);
387 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700388 }
389
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700390 private void triggerUpdate(String componentName) {
391 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700392 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700393 Map<String, ConfigProperty> map = properties.get(componentName);
Ray Milkey7981a322018-08-16 14:55:46 -0700394 if (map == null) {
395 // Prevent NPE if the component isn't there
396 log.warn("Component not found for " + componentName);
397 return;
398 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700399 Dictionary<String, Object> props = new Hashtable<>();
Thomas Vachuska611f4662018-02-27 11:30:09 -0800400 map.values().stream().filter(p -> p.value() != null)
401 .forEach(p -> props.put(p.name(), p.value()));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700402 cfg.update(props);
403 } catch (IOException e) {
404 log.warn("Unable to update configuration for " + componentName, e);
405 }
406 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700407}