blob: 8c8d03e15f81530b821a9c829e6eee993032d36d [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;
51import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090052import static org.onosproject.security.AppGuard.checkPermission;
53
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();
Aaron Kruglikov66855212015-06-22 12:29:02 -070075 //TODO make accumulator properties configurable
76 private final InternalAccumulator accumulator = new InternalAccumulator(SharedExecutors.getTimer(),
77 MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
Thomas Vachuska6d697f12015-03-08 20:59:50 -070078
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected ComponentConfigStore store;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected ConfigurationAdmin cfgAdmin;
84
85 // Locally maintained catalog of definitions.
Aaron Kruglikov66855212015-06-22 12:29:02 -070086 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 Yoon541ef712015-05-23 17:18:34 +0900104 checkPermission(Permission.CONFIG_READ);
105
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700106 return ImmutableSet.copyOf(properties.keySet());
107 }
108
109 @Override
110 public void registerProperties(Class<?> componentClass) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900111 checkPermission(Permission.CONFIG_WRITE);
112
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700113 String componentName = componentClass.getName();
114 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
115 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
116 checkArgument(ris != null, "Property definitions not found at resource %s",
117 resourceName);
118
119 // Read the definitions
120 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
121
122 // Produce a new map of the properties and register it.
123 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
124 defs.forEach(p -> map.put(p.name(), p));
125
126 properties.put(componentName, map);
127 loadExistingValues(componentName);
128 } catch (IOException e) {
129 log.error("Unable to read property definitions from resource " + resourceName, e);
130 }
131 }
132
133 @Override
134 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900135 checkPermission(Permission.CONFIG_WRITE);
136
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700137 String componentName = componentClass.getName();
138 checkNotNull(componentName, COMPONENT_NULL);
139 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700140 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700141 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700142 clearExistingValues(componentName);
143 }
144 }
145
146 // Clears any existing values that may have been set.
147 private void clearExistingValues(String componentName) {
148 triggerUpdate(componentName);
149 }
150
151 @Override
152 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900153 checkPermission(Permission.CONFIG_READ);
154
Brian O'Connor85c29262015-03-10 20:53:43 -0700155 Map<String, ConfigProperty> map = properties.get(componentName);
156 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700157 }
158
159 @Override
160 public void setProperty(String componentName, String name, String value) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900161 checkPermission(Permission.CONFIG_WRITE);
162
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700163 checkNotNull(componentName, COMPONENT_NULL);
164 checkNotNull(name, PROPERTY_NULL);
165 store.setProperty(componentName, name, value);
166 }
167
168 @Override
169 public void unsetProperty(String componentName, String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900170 checkPermission(Permission.CONFIG_WRITE);
171
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700172 checkNotNull(componentName, COMPONENT_NULL);
173 checkNotNull(name, PROPERTY_NULL);
174 store.unsetProperty(componentName, name);
175 }
176
177 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
178
179 @Override
180 public void notify(ComponentConfigEvent event) {
181 String componentName = event.subject();
182 String name = event.name();
183 String value = event.value();
184
185 switch (event.type()) {
186 case PROPERTY_SET:
187 set(componentName, name, value);
188 break;
189 case PROPERTY_UNSET:
190 reset(componentName, name);
191 break;
192 default:
193 break;
194 }
195 }
196 }
197
Aaron Kruglikov66855212015-06-22 12:29:02 -0700198 private class InternalAccumulator extends AbstractAccumulator<String> implements Accumulator<String> {
199
200 protected InternalAccumulator(java.util.Timer timer, int maxItems, int maxBatchMillis, int maxIdleMillis) {
201 super(timer, maxItems, maxBatchMillis, maxIdleMillis);
202 }
203
204 @Override
205 public void processItems(List items) {
206 //Conversion to hashset removes duplicates
207 Set<String> componentSet = new HashSet<String>(items);
208 componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
209
210
211 }
212 }
213
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700214 // Locates the property in the component map and replaces it with an
215 // updated copy.
216 private void set(String componentName, String name, String value) {
217 Map<String, ConfigProperty> map = properties.get(componentName);
218 if (map != null) {
219 ConfigProperty prop = map.get(name);
220 if (prop != null) {
221 map.put(name, ConfigProperty.setProperty(prop, value));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700222 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700223 return;
224 }
225 }
226 log.warn("Unable to set non-existent property {} for component {}",
227 name, componentName);
228 }
229
230 // Locates the property in the component map and replaces it with an
231 // reset copy.
232 private void reset(String componentName, String name) {
233 Map<String, ConfigProperty> map = properties.get(componentName);
234 if (map != null) {
235 ConfigProperty prop = map.get(name);
236 if (prop != null) {
237 map.put(name, ConfigProperty.resetProperty(prop));
Aaron Kruglikov66855212015-06-22 12:29:02 -0700238 accumulator.add(componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700239 return;
240 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700241 log.warn("Unable to reset non-existent property {} for component {}",
242 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700243 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700244 }
245
246 // Loads existing property values that may have been set.
247 private void loadExistingValues(String componentName) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700248 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700249 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700250 Map<String, ConfigProperty> map = properties.get(componentName);
251 Dictionary<String, Object> props = cfg.getProperties();
252 if (props != null) {
253 Enumeration<String> it = props.keys();
254 while (it.hasMoreElements()) {
255 String name = it.nextElement();
256 ConfigProperty p = map.get(name);
257 if (p != null) {
258 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
259 }
260 }
261 }
262 } catch (IOException e) {
263 log.error("Unable to get configuration for " + componentName, e);
264 }
265
266 }
267
268 // FIXME: This should be a slightly deferred execution to allow changing
269 // values just once per component when a number of updates arrive shortly
270 // after each other.
271 private void triggerUpdate(String componentName) {
272 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700273 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700274 Map<String, ConfigProperty> map = properties.get(componentName);
275 Dictionary<String, Object> props = new Hashtable<>();
276 map.values().forEach(p -> props.put(p.name(), p.value()));
277 cfg.update(props);
278 } catch (IOException e) {
279 log.warn("Unable to update configuration for " + componentName, e);
280 }
281 }
282
283}