blob: 9b80bcc285a96f40eab08a9a8a44a20ee1ff0aa2 [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;
26import org.onosproject.cfg.ComponentConfigEvent;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.cfg.ComponentConfigStore;
29import org.onosproject.cfg.ComponentConfigStoreDelegate;
30import org.onosproject.cfg.ConfigProperty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090031import org.onosproject.core.Permission;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070032import org.osgi.service.cm.Configuration;
33import org.osgi.service.cm.ConfigurationAdmin;
34import org.slf4j.Logger;
35
36import java.io.IOException;
37import java.io.InputStream;
38import java.util.Dictionary;
39import java.util.Enumeration;
40import java.util.Hashtable;
41import java.util.Map;
42import java.util.Set;
43
44import static com.google.common.base.Preconditions.checkArgument;
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090047import static org.onosproject.security.AppGuard.checkPermission;
48
Thomas Vachuska6d697f12015-03-08 20:59:50 -070049
50/**
51 * Implementation of the centralized component configuration service.
52 */
53@Component(immediate = true)
54@Service
55public class ComponentConfigManager implements ComponentConfigService {
56
57 private static final String COMPONENT_NULL = "Component name cannot be null";
58 private static final String PROPERTY_NULL = "Property name cannot be null";
59
60 private static final String RESOURCE_EXT = ".cfgdef";
61
62 private final Logger log = getLogger(getClass());
63
64 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ComponentConfigStore store;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected ConfigurationAdmin cfgAdmin;
71
72 // Locally maintained catalog of definitions.
73 private final Map<String, Map<String, ConfigProperty>> properties =
74 Maps.newConcurrentMap();
75
76 @Activate
77 public void activate() {
78 store.setDelegate(delegate);
79 log.info("Started");
80 }
81
82 @Deactivate
83 public void deactivate() {
84 store.unsetDelegate(delegate);
85 log.info("Stopped");
86 }
87
88 @Override
89 public Set<String> getComponentNames() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090090 checkPermission(Permission.CONFIG_READ);
91
Thomas Vachuska6d697f12015-03-08 20:59:50 -070092 return ImmutableSet.copyOf(properties.keySet());
93 }
94
95 @Override
96 public void registerProperties(Class<?> componentClass) {
Changhoon Yoon541ef712015-05-23 17:18:34 +090097 checkPermission(Permission.CONFIG_WRITE);
98
Thomas Vachuska6d697f12015-03-08 20:59:50 -070099 String componentName = componentClass.getName();
100 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
101 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
102 checkArgument(ris != null, "Property definitions not found at resource %s",
103 resourceName);
104
105 // Read the definitions
106 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
107
108 // Produce a new map of the properties and register it.
109 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
110 defs.forEach(p -> map.put(p.name(), p));
111
112 properties.put(componentName, map);
113 loadExistingValues(componentName);
114 } catch (IOException e) {
115 log.error("Unable to read property definitions from resource " + resourceName, e);
116 }
117 }
118
119 @Override
120 public void unregisterProperties(Class<?> componentClass, boolean clear) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900121 checkPermission(Permission.CONFIG_WRITE);
122
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700123 String componentName = componentClass.getName();
124 checkNotNull(componentName, COMPONENT_NULL);
125 Map<String, ConfigProperty> cps = properties.remove(componentName);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700126 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700127 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700128 clearExistingValues(componentName);
129 }
130 }
131
132 // Clears any existing values that may have been set.
133 private void clearExistingValues(String componentName) {
134 triggerUpdate(componentName);
135 }
136
137 @Override
138 public Set<ConfigProperty> getProperties(String componentName) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900139 checkPermission(Permission.CONFIG_READ);
140
Brian O'Connor85c29262015-03-10 20:53:43 -0700141 Map<String, ConfigProperty> map = properties.get(componentName);
142 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700143 }
144
145 @Override
146 public void setProperty(String componentName, String name, String value) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900147 checkPermission(Permission.CONFIG_WRITE);
148
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700149 checkNotNull(componentName, COMPONENT_NULL);
150 checkNotNull(name, PROPERTY_NULL);
151 store.setProperty(componentName, name, value);
152 }
153
154 @Override
155 public void unsetProperty(String componentName, String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900156 checkPermission(Permission.CONFIG_WRITE);
157
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700158 checkNotNull(componentName, COMPONENT_NULL);
159 checkNotNull(name, PROPERTY_NULL);
160 store.unsetProperty(componentName, name);
161 }
162
163 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
164
165 @Override
166 public void notify(ComponentConfigEvent event) {
167 String componentName = event.subject();
168 String name = event.name();
169 String value = event.value();
170
171 switch (event.type()) {
172 case PROPERTY_SET:
173 set(componentName, name, value);
174 break;
175 case PROPERTY_UNSET:
176 reset(componentName, name);
177 break;
178 default:
179 break;
180 }
181 }
182 }
183
184 // Locates the property in the component map and replaces it with an
185 // updated copy.
186 private void set(String componentName, String name, String value) {
187 Map<String, ConfigProperty> map = properties.get(componentName);
188 if (map != null) {
189 ConfigProperty prop = map.get(name);
190 if (prop != null) {
191 map.put(name, ConfigProperty.setProperty(prop, value));
192 triggerUpdate(componentName);
193 return;
194 }
195 }
196 log.warn("Unable to set non-existent property {} for component {}",
197 name, componentName);
198 }
199
200 // Locates the property in the component map and replaces it with an
201 // reset copy.
202 private void reset(String componentName, String name) {
203 Map<String, ConfigProperty> map = properties.get(componentName);
204 if (map != null) {
205 ConfigProperty prop = map.get(name);
206 if (prop != null) {
207 map.put(name, ConfigProperty.resetProperty(prop));
208 triggerUpdate(componentName);
209 return;
210 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700211 log.warn("Unable to reset non-existent property {} for component {}",
212 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700213 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700214 }
215
216 // Loads existing property values that may have been set.
217 private void loadExistingValues(String componentName) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700218 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700219 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700220 Map<String, ConfigProperty> map = properties.get(componentName);
221 Dictionary<String, Object> props = cfg.getProperties();
222 if (props != null) {
223 Enumeration<String> it = props.keys();
224 while (it.hasMoreElements()) {
225 String name = it.nextElement();
226 ConfigProperty p = map.get(name);
227 if (p != null) {
228 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
229 }
230 }
231 }
232 } catch (IOException e) {
233 log.error("Unable to get configuration for " + componentName, e);
234 }
235
236 }
237
238 // FIXME: This should be a slightly deferred execution to allow changing
239 // values just once per component when a number of updates arrive shortly
240 // after each other.
241 private void triggerUpdate(String componentName) {
242 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700243 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700244 Map<String, ConfigProperty> map = properties.get(componentName);
245 Dictionary<String, Object> props = new Hashtable<>();
246 map.values().forEach(p -> props.put(p.name(), p.value()));
247 cfg.update(props);
248 } catch (IOException e) {
249 log.warn("Unable to update configuration for " + componentName, e);
250 }
251 }
252
253}