blob: 5958708d691f9819bc7ce39a1aa0eeae10c436cf [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) {
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700110 if (true) {
111 return;
112 }
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700113
Changhoon Yoonb856b812015-08-10 03:47:19 +0900114 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900115
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700116 String componentName = componentClass.getName();
117 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
118 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
119 checkArgument(ris != null, "Property definitions not found at resource %s",
120 resourceName);
121
122 // Read the definitions
123 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
124
125 // Produce a new map of the properties and register it.
126 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
127 defs.forEach(p -> map.put(p.name(), p));
128
129 properties.put(componentName, map);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800130 loadExistingValues(componentName, map);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700131 } catch (IOException e) {
132 log.error("Unable to read property definitions from resource " + resourceName, e);
133 }
134 }
135
136 @Override
137 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700138 if (true) {
139 return;
140 }
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700141
Changhoon Yoonb856b812015-08-10 03:47:19 +0900142 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900143
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700144 String componentName = componentClass.getName();
145 checkNotNull(componentName, COMPONENT_NULL);
146 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700147 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700148 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700149 clearExistingValues(componentName);
150 }
151 }
152
153 // Clears any existing values that may have been set.
154 private void clearExistingValues(String componentName) {
155 triggerUpdate(componentName);
156 }
157
158 @Override
159 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900160 checkPermission(CONFIG_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900161
Brian O'Connor85c29262015-03-10 20:53:43 -0700162 Map<String, ConfigProperty> map = properties.get(componentName);
163 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700164 }
165
166 @Override
167 public void setProperty(String componentName, String name, String value) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900168 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900169
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700170 checkNotNull(componentName, COMPONENT_NULL);
171 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700172
173 checkArgument(properties.containsKey(componentName),
174 COMPONENT_MISSING, componentName);
175 checkArgument(properties.get(componentName).containsKey(name),
176 PROPERTY_MISSING, name, componentName);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530177 checkValidity(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700178 store.setProperty(componentName, name, value);
179 }
180
181 @Override
andreadd106a62015-10-02 13:39:48 -0700182 public void preSetProperty(String componentName, String name, String value) {
andreadd106a62015-10-02 13:39:48 -0700183 checkPermission(CONFIG_WRITE);
184
185 checkNotNull(componentName, COMPONENT_NULL);
186 checkNotNull(name, PROPERTY_NULL);
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530187 checkValidity(componentName, name, value);
andreadd106a62015-10-02 13:39:48 -0700188 store.setProperty(componentName, name, value);
189 }
190
191 @Override
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700192 public void unsetProperty(String componentName, String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900193 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900194
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700195 checkNotNull(componentName, COMPONENT_NULL);
196 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700197
198 checkArgument(properties.containsKey(componentName),
199 COMPONENT_MISSING, componentName);
200 checkArgument(properties.get(componentName).containsKey(name),
201 PROPERTY_MISSING, name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700202 store.unsetProperty(componentName, name);
203 }
204
psnehaf31ac6f2018-08-02 01:16:46 -0400205 @Override
206 public ConfigProperty getProperty(String componentName, String attribute) {
207 checkPermission(CONFIG_READ);
208
209 Map<String, ConfigProperty> map = properties.get(componentName);
210 if (map != null) {
211 return map.get(attribute);
212 } else {
213 log.error("Attribute {} not present in component {}", attribute, componentName);
214 return null;
215 }
216 }
217
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700218 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
219
220 @Override
221 public void notify(ComponentConfigEvent event) {
222 String componentName = event.subject();
223 String name = event.name();
224 String value = event.value();
225
226 switch (event.type()) {
227 case PROPERTY_SET:
228 set(componentName, name, value);
229 break;
230 case PROPERTY_UNSET:
231 reset(componentName, name);
232 break;
233 default:
234 break;
235 }
236 }
237 }
238
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700239 // Buffers multiple subsequent configuration updates into one notification.
240 private class InternalAccumulator extends AbstractAccumulator<String>
241 implements Accumulator<String> {
Aaron Kruglikov66855212015-06-22 12:29:02 -0700242
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700243 protected InternalAccumulator() {
244 super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700245 }
246
247 @Override
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700248 public void processItems(List<String> items) {
249 // Conversion to set removes duplicates
250 Set<String> componentSet = new HashSet<>(items);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700251 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700252 }
253 }
254
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700255 // Locates the property in the component map and replaces it with an
256 // updated copy.
257 private void set(String componentName, String name, String value) {
258 Map<String, ConfigProperty> map = properties.get(componentName);
259 if (map != null) {
260 ConfigProperty prop = map.get(name);
261 if (prop != null) {
262 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700263 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700264 return;
265 }
266 }
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800267
268 // If definition doesn't exist in local catalog, cache the property.
269 preSet(componentName, name, value);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700270 }
271
272 // Locates the property in the component map and replaces it with an
273 // reset copy.
274 private void reset(String componentName, String name) {
275 Map<String, ConfigProperty> map = properties.get(componentName);
276 if (map != null) {
277 ConfigProperty prop = map.get(name);
278 if (prop != null) {
279 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700280 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700281 return;
282 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700283 log.warn("Unable to reset non-existent property {} for component {}",
284 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700285 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700286 }
287
Thomas Vachuska4b32fcb2018-02-20 12:35:19 -0800288 // Stores non-existent property so that loadExistingValues() can load in future.
289 private void preSet(String componentName, String name, String value) {
290 try {
291 Configuration config = cfgAdmin.getConfiguration(componentName, null);
292 Dictionary<String, Object> props = config.getProperties();
293 if (props == null) {
294 props = new Hashtable<>();
295 }
296 props.put(name, value);
297 config.update(props);
298 } catch (IOException e) {
299 log.error("Failed to preset configuration for {}", componentName);
300 }
301 }
302
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530303 // Checks whether the value of the specified configuration property is a valid one or not.
304 private void checkValidity(String componentName, String name, String newValue) {
305 Map<String, ConfigProperty> map = properties.get(componentName);
306 if (map == null) {
307 return;
308 }
309 ConfigProperty prop = map.get(name);
310 ConfigProperty.Type type = prop.type();
311 try {
312 switch (type) {
313 case INTEGER:
314 Integer.parseInt(newValue);
315 break;
316 case LONG:
317 Long.parseLong(newValue);
318 break;
319 case FLOAT:
320 Float.parseFloat(newValue);
321 break;
322 case DOUBLE:
323 Double.parseDouble(newValue);
324 break;
325 case BOOLEAN:
326 if (!((newValue != null) && (newValue.equalsIgnoreCase("true") ||
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800327 newValue.equalsIgnoreCase("false")))) {
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530328 throw new IllegalArgumentException("Invalid " + type + " value");
329 }
330 break;
331 case STRING:
332 case BYTE:
333 //do nothing
334 break;
335 default:
336 log.warn("Not a valid config property type");
337 break;
338
339 }
340 } catch (NumberFormatException e) {
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800341 throw new NumberFormatException("Invalid " + type + " value");
sivachidambaram subramanian8c9a7012017-04-26 14:59:02 +0530342 }
343 }
344
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800345 // Loads existing property values that may have been learned from other
346 // nodes in the cluster before the local property registration.
347 private void loadExistingValues(String componentName,
348 Map<String, ConfigProperty> map) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700349 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700350 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800351 Dictionary<String, Object> localProperties = cfg.getProperties();
352
353 // Iterate over all registered config properties...
354 for (ConfigProperty p : ImmutableSet.copyOf(map.values())) {
355 String name = p.name();
356 String globalValue = store.getProperty(componentName, name);
357 String localValue = localProperties != null ? (String) localProperties.get(name) : null;
Thomas Vachuska611f4662018-02-27 11:30:09 -0800358
359 log.debug("Processing {} property {}; global {}; local {}",
360 componentName, name, globalValue, localValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800361 try {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800362 // 1) If the global value is the same as default, or
363 // 2) if it is not set explicitly, but the local value is
364 // not the same as the default, reset local value to default.
365 if (Objects.equals(globalValue, p.defaultValue()) ||
366 (globalValue == null &&
367 !Objects.equals(localValue, p.defaultValue()))) {
368 log.debug("Resetting {} property {}; value same as default",
369 componentName, name);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800370 reset(componentName, name);
371
372 } else if (!Objects.equals(globalValue, localValue)) {
Thomas Vachuska611f4662018-02-27 11:30:09 -0800373 // If the local value is different from global value
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800374 // validate the global value and apply it locally.
Thomas Vachuska611f4662018-02-27 11:30:09 -0800375 log.debug("Setting {} property {} to {}",
376 componentName, name, globalValue);
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800377 checkValidity(componentName, name, globalValue);
378 set(componentName, name, globalValue);
Thomas Vachuska611f4662018-02-27 11:30:09 -0800379
380 } else {
381 // Otherwise, simply update the registered property
382 // with the global value.
383 log.debug("Syncing {} property {} to {}",
384 componentName, name, globalValue);
385 map.put(name, ConfigProperty.setProperty(p, globalValue));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700386 }
Thomas Vachuska9f6bd522018-02-12 15:53:01 -0800387 } catch (IllegalArgumentException e) {
388 log.warn("Value {} is not a valid {}; using default",
389 globalValue, p.type());
390 reset(componentName, name);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700391 }
392 }
393 } catch (IOException e) {
394 log.error("Unable to get configuration for " + componentName, e);
395 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700396 }
397
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700398 private void triggerUpdate(String componentName) {
399 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700400 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700401 Map<String, ConfigProperty> map = properties.get(componentName);
Ray Milkey7981a322018-08-16 14:55:46 -0700402 if (map == null) {
403 // Prevent NPE if the component isn't there
404 log.warn("Component not found for " + componentName);
405 return;
406 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700407 Dictionary<String, Object> props = new Hashtable<>();
Thomas Vachuska611f4662018-02-27 11:30:09 -0800408 map.values().stream().filter(p -> p.value() != null)
409 .forEach(p -> props.put(p.name(), p.value()));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700410 cfg.update(props);
411 } catch (IOException e) {
412 log.warn("Unable to update configuration for " + componentName, e);
413 }
414 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700415}