blob: 884d0284dcac874ae698c6e8a657f068abed7ff8 [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;
Changhoon Yoon541ef712015-05-23 17:18:34 +090034import org.onosproject.core.Permission;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070035import org.osgi.service.cm.Configuration;
36import org.osgi.service.cm.ConfigurationAdmin;
37import org.slf4j.Logger;
38
39import java.io.IOException;
40import java.io.InputStream;
41import java.util.Dictionary;
42import java.util.Enumeration;
Aaron Kruglikov66855212015-06-22 12:29:02 -070043import java.util.HashSet;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070044import java.util.Hashtable;
Aaron Kruglikov66855212015-06-22 12:29:02 -070045import java.util.List;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070046import java.util.Map;
47import java.util.Set;
48
49import static com.google.common.base.Preconditions.checkArgument;
50import static com.google.common.base.Preconditions.checkNotNull;
Changhoon Yoon541ef712015-05-23 17:18:34 +090051import static org.onosproject.security.AppGuard.checkPermission;
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 */
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";
64
Aaron Kruglikov66855212015-06-22 12:29:02 -070065 //Symbolic constants for use with the accumulator
66 private static final int MAX_ITEMS = 100;
67 private static final int MAX_BATCH_MILLIS = 1000;
68 private static final int MAX_IDLE_MILLIS = 250;
69
Thomas Vachuska6d697f12015-03-08 20:59:50 -070070 private static final String RESOURCE_EXT = ".cfgdef";
71
72 private final Logger log = getLogger(getClass());
73
74 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
Thomas Vachuska92af89f2015-06-22 15:16:23 -070075 private final InternalAccumulator accumulator = new InternalAccumulator();
Thomas Vachuska6d697f12015-03-08 20:59:50 -070076
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected ComponentConfigStore store;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected ConfigurationAdmin cfgAdmin;
82
83 // Locally maintained catalog of definitions.
Aaron Kruglikov66855212015-06-22 12:29:02 -070084 private final Map<String, Map<String, ConfigProperty>> properties =
Thomas Vachuska6d697f12015-03-08 20:59:50 -070085 Maps.newConcurrentMap();
86
Aaron Kruglikov66855212015-06-22 12:29:02 -070087
Thomas Vachuska6d697f12015-03-08 20:59:50 -070088 @Activate
89 public void activate() {
90 store.setDelegate(delegate);
91 log.info("Started");
92 }
93
94 @Deactivate
95 public void deactivate() {
96 store.unsetDelegate(delegate);
97 log.info("Stopped");
98 }
99
100 @Override
101 public Set<String> getComponentNames() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900102 checkPermission(Permission.CONFIG_READ);
103
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700104 return ImmutableSet.copyOf(properties.keySet());
105 }
106
107 @Override
108 public void registerProperties(Class<?> componentClass) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900109 checkPermission(Permission.CONFIG_WRITE);
110
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700111 String componentName = componentClass.getName();
112 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
113 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
114 checkArgument(ris != null, "Property definitions not found at resource %s",
115 resourceName);
116
117 // Read the definitions
118 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
119
120 // Produce a new map of the properties and register it.
121 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
122 defs.forEach(p -> map.put(p.name(), p));
123
124 properties.put(componentName, map);
125 loadExistingValues(componentName);
126 } catch (IOException e) {
127 log.error("Unable to read property definitions from resource " + resourceName, e);
128 }
129 }
130
131 @Override
132 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900133 checkPermission(Permission.CONFIG_WRITE);
134
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700135 String componentName = componentClass.getName();
136 checkNotNull(componentName, COMPONENT_NULL);
137 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700138 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700139 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700140 clearExistingValues(componentName);
141 }
142 }
143
144 // Clears any existing values that may have been set.
145 private void clearExistingValues(String componentName) {
146 triggerUpdate(componentName);
147 }
148
149 @Override
150 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900151 checkPermission(Permission.CONFIG_READ);
152
Brian O'Connor85c29262015-03-10 20:53:43 -0700153 Map<String, ConfigProperty> map = properties.get(componentName);
154 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700155 }
156
157 @Override
158 public void setProperty(String componentName, String name, String value) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900159 checkPermission(Permission.CONFIG_WRITE);
160
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700161 checkNotNull(componentName, COMPONENT_NULL);
162 checkNotNull(name, PROPERTY_NULL);
163 store.setProperty(componentName, name, value);
164 }
165
166 @Override
167 public void unsetProperty(String componentName, String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900168 checkPermission(Permission.CONFIG_WRITE);
169
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700170 checkNotNull(componentName, COMPONENT_NULL);
171 checkNotNull(name, PROPERTY_NULL);
172 store.unsetProperty(componentName, name);
173 }
174
175 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
176
177 @Override
178 public void notify(ComponentConfigEvent event) {
179 String componentName = event.subject();
180 String name = event.name();
181 String value = event.value();
182
183 switch (event.type()) {
184 case PROPERTY_SET:
185 set(componentName, name, value);
186 break;
187 case PROPERTY_UNSET:
188 reset(componentName, name);
189 break;
190 default:
191 break;
192 }
193 }
194 }
195
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700196 // Buffers multiple subsequent configuration updates into one notification.
197 private class InternalAccumulator extends AbstractAccumulator<String>
198 implements Accumulator<String> {
Aaron Kruglikov66855212015-06-22 12:29:02 -0700199
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700200 protected InternalAccumulator() {
201 super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700202 }
203
204 @Override
Thomas Vachuska92af89f2015-06-22 15:16:23 -0700205 public void processItems(List<String> items) {
206 // Conversion to set removes duplicates
207 Set<String> componentSet = new HashSet<>(items);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700208 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
Aaron Kruglikov66855212015-06-22 12:29:02 -0700209 }
210 }
211
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700212 // Locates the property in the component map and replaces it with an
213 // updated copy.
214 private void set(String componentName, String name, String value) {
215 Map<String, ConfigProperty> map = properties.get(componentName);
216 if (map != null) {
217 ConfigProperty prop = map.get(name);
218 if (prop != null) {
219 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700220 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700221 return;
222 }
223 }
224 log.warn("Unable to set non-existent property {} for component {}",
225 name, componentName);
226 }
227
228 // Locates the property in the component map and replaces it with an
229 // reset copy.
230 private void reset(String componentName, String name) {
231 Map<String, ConfigProperty> map = properties.get(componentName);
232 if (map != null) {
233 ConfigProperty prop = map.get(name);
234 if (prop != null) {
235 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700236 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700237 return;
238 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700239 log.warn("Unable to reset non-existent property {} for component {}",
240 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700241 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700242 }
243
244 // Loads existing property values that may have been set.
245 private void loadExistingValues(String componentName) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700246 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700247 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700248 Map<String, ConfigProperty> map = properties.get(componentName);
249 Dictionary<String, Object> props = cfg.getProperties();
250 if (props != null) {
251 Enumeration<String> it = props.keys();
252 while (it.hasMoreElements()) {
253 String name = it.nextElement();
254 ConfigProperty p = map.get(name);
255 if (p != null) {
256 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
257 }
258 }
259 }
260 } catch (IOException e) {
261 log.error("Unable to get configuration for " + componentName, e);
262 }
263
264 }
265
266 // FIXME: This should be a slightly deferred execution to allow changing
267 // values just once per component when a number of updates arrive shortly
268 // after each other.
269 private void triggerUpdate(String componentName) {
270 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700271 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700272 Map<String, ConfigProperty> map = properties.get(componentName);
273 Dictionary<String, Object> props = new Hashtable<>();
274 map.values().forEach(p -> props.put(p.name(), p.value()));
275 cfg.update(props);
276 } catch (IOException e) {
277 log.warn("Unable to update configuration for " + componentName, e);
278 }
279 }
280
281}