blob: 5d5d73f485e4f10be4c2cb8528495cbb9f49bbb0 [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;
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;
41import java.util.Enumeration;
Aaron Kruglikov66855212015-06-22 12:29:02 -070042import java.util.HashSet;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070043import java.util.Hashtable;
Aaron Kruglikov66855212015-06-22 12:29:02 -070044import java.util.List;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070045import java.util.Map;
46import 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.
Aaron Kruglikov66855212015-06-22 12:29:02 -070087 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);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900106
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700107 return ImmutableSet.copyOf(properties.keySet());
108 }
109
110 @Override
111 public void registerProperties(Class<?> componentClass) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900112 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900113
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700114 String componentName = componentClass.getName();
115 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
116 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
117 checkArgument(ris != null, "Property definitions not found at resource %s",
118 resourceName);
119
120 // Read the definitions
121 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
122
123 // Produce a new map of the properties and register it.
124 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
125 defs.forEach(p -> map.put(p.name(), p));
126
127 properties.put(componentName, map);
128 loadExistingValues(componentName);
129 } catch (IOException e) {
130 log.error("Unable to read property definitions from resource " + resourceName, e);
131 }
132 }
133
134 @Override
135 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900136 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900137
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700138 String componentName = componentClass.getName();
139 checkNotNull(componentName, COMPONENT_NULL);
140 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700141 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700142 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700143 clearExistingValues(componentName);
144 }
145 }
146
147 // Clears any existing values that may have been set.
148 private void clearExistingValues(String componentName) {
149 triggerUpdate(componentName);
150 }
151
152 @Override
153 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900154 checkPermission(CONFIG_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900155
Brian O'Connor85c29262015-03-10 20:53:43 -0700156 Map<String, ConfigProperty> map = properties.get(componentName);
157 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700158 }
159
160 @Override
161 public void setProperty(String componentName, String name, String value) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900162 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900163
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700164 checkNotNull(componentName, COMPONENT_NULL);
165 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700166
167 checkArgument(properties.containsKey(componentName),
168 COMPONENT_MISSING, componentName);
169 checkArgument(properties.get(componentName).containsKey(name),
170 PROPERTY_MISSING, name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700171 store.setProperty(componentName, name, value);
172 }
173
174 @Override
175 public void unsetProperty(String componentName, String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900176 checkPermission(CONFIG_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900177
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700178 checkNotNull(componentName, COMPONENT_NULL);
179 checkNotNull(name, PROPERTY_NULL);
Brian O'Connor57e83e82015-09-22 15:45:37 -0700180
181 checkArgument(properties.containsKey(componentName),
182 COMPONENT_MISSING, componentName);
183 checkArgument(properties.get(componentName).containsKey(name),
184 PROPERTY_MISSING, name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700185 store.unsetProperty(componentName, name);
186 }
187
188 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
189
190 @Override
191 public void notify(ComponentConfigEvent event) {
192 String componentName = event.subject();
193 String name = event.name();
194 String value = event.value();
195
196 switch (event.type()) {
197 case PROPERTY_SET:
198 set(componentName, name, value);
199 break;
200 case PROPERTY_UNSET:
201 reset(componentName, name);
202 break;
203 default:
204 break;
205 }
206 }
207 }
208
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700209 // Buffers multiple subsequent configuration updates into one notification.
210 private class InternalAccumulator extends AbstractAccumulator<String>
211 implements Accumulator<String> {
Aaron Kruglikov66855212015-06-22 12:29:02 -0700212
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700213 protected InternalAccumulator() {
214 super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700215 }
216
217 @Override
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700218 public void processItems(List<String> items) {
219 // Conversion to set removes duplicates
220 Set<String> componentSet = new HashSet<>(items);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700221 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700222 }
223 }
224
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700225 // Locates the property in the component map and replaces it with an
226 // updated copy.
227 private void set(String componentName, String name, String value) {
228 Map<String, ConfigProperty> map = properties.get(componentName);
229 if (map != null) {
230 ConfigProperty prop = map.get(name);
231 if (prop != null) {
232 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700233 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700234 return;
235 }
236 }
237 log.warn("Unable to set non-existent property {} for component {}",
238 name, componentName);
239 }
240
241 // Locates the property in the component map and replaces it with an
242 // reset copy.
243 private void reset(String componentName, String name) {
244 Map<String, ConfigProperty> map = properties.get(componentName);
245 if (map != null) {
246 ConfigProperty prop = map.get(name);
247 if (prop != null) {
248 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700249 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700250 return;
251 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700252 log.warn("Unable to reset non-existent property {} for component {}",
253 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700254 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700255 }
256
257 // Loads existing property values that may have been set.
258 private void loadExistingValues(String componentName) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700259 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700260 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700261 Map<String, ConfigProperty> map = properties.get(componentName);
262 Dictionary<String, Object> props = cfg.getProperties();
263 if (props != null) {
264 Enumeration<String> it = props.keys();
265 while (it.hasMoreElements()) {
266 String name = it.nextElement();
267 ConfigProperty p = map.get(name);
268 if (p != null) {
269 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
270 }
271 }
272 }
273 } catch (IOException e) {
274 log.error("Unable to get configuration for " + componentName, e);
275 }
276
277 }
278
279 // FIXME: This should be a slightly deferred execution to allow changing
280 // values just once per component when a number of updates arrive shortly
281 // after each other.
282 private void triggerUpdate(String componentName) {
283 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700284 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700285 Map<String, ConfigProperty> map = properties.get(componentName);
286 Dictionary<String, Object> props = new Hashtable<>();
287 map.values().forEach(p -> props.put(p.name(), p.value()));
288 cfg.update(props);
289 } catch (IOException e) {
290 log.warn("Unable to update configuration for " + componentName, e);
291 }
292 }
293
294}