blob: bab7ac1aa57f543c41ca98b602dd5fc07d273851 [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;
31import org.osgi.service.cm.Configuration;
32import org.osgi.service.cm.ConfigurationAdmin;
33import org.slf4j.Logger;
34
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.Dictionary;
38import java.util.Enumeration;
39import java.util.Hashtable;
40import java.util.Map;
41import java.util.Set;
42
43import static com.google.common.base.Preconditions.checkArgument;
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Implementation of the centralized component configuration service.
49 */
50@Component(immediate = true)
51@Service
52public class ComponentConfigManager implements ComponentConfigService {
53
54 private static final String COMPONENT_NULL = "Component name cannot be null";
55 private static final String PROPERTY_NULL = "Property name cannot be null";
56
57 private static final String RESOURCE_EXT = ".cfgdef";
58
59 private final Logger log = getLogger(getClass());
60
61 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ComponentConfigStore store;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ConfigurationAdmin cfgAdmin;
68
69 // Locally maintained catalog of definitions.
70 private final Map<String, Map<String, ConfigProperty>> properties =
71 Maps.newConcurrentMap();
72
73 @Activate
74 public void activate() {
75 store.setDelegate(delegate);
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 store.unsetDelegate(delegate);
82 log.info("Stopped");
83 }
84
85 @Override
86 public Set<String> getComponentNames() {
87 return ImmutableSet.copyOf(properties.keySet());
88 }
89
90 @Override
91 public void registerProperties(Class<?> componentClass) {
92 String componentName = componentClass.getName();
93 String resourceName = componentClass.getSimpleName() + RESOURCE_EXT;
94 try (InputStream ris = componentClass.getResourceAsStream(resourceName)) {
95 checkArgument(ris != null, "Property definitions not found at resource %s",
96 resourceName);
97
98 // Read the definitions
99 Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris);
100
101 // Produce a new map of the properties and register it.
102 Map<String, ConfigProperty> map = Maps.newConcurrentMap();
103 defs.forEach(p -> map.put(p.name(), p));
104
105 properties.put(componentName, map);
106 loadExistingValues(componentName);
107 } catch (IOException e) {
108 log.error("Unable to read property definitions from resource " + resourceName, e);
109 }
110 }
111
112 @Override
113 public void unregisterProperties(Class<?> componentClass, boolean clear) {
114 String componentName = componentClass.getName();
115 checkNotNull(componentName, COMPONENT_NULL);
116 Map<String, ConfigProperty> cps = properties.remove(componentName);
117 if (cps != null) {
118 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
119 }
120
121 if (clear) {
122 clearExistingValues(componentName);
123 }
124 }
125
126 // Clears any existing values that may have been set.
127 private void clearExistingValues(String componentName) {
128 triggerUpdate(componentName);
129 }
130
131 @Override
132 public Set<ConfigProperty> getProperties(String componentName) {
133 return ImmutableSet.copyOf(properties.get(componentName).values());
134 }
135
136 @Override
137 public void setProperty(String componentName, String name, String value) {
138 checkNotNull(componentName, COMPONENT_NULL);
139 checkNotNull(name, PROPERTY_NULL);
140 store.setProperty(componentName, name, value);
141 }
142
143 @Override
144 public void unsetProperty(String componentName, String name) {
145 checkNotNull(componentName, COMPONENT_NULL);
146 checkNotNull(name, PROPERTY_NULL);
147 store.unsetProperty(componentName, name);
148 }
149
150 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
151
152 @Override
153 public void notify(ComponentConfigEvent event) {
154 String componentName = event.subject();
155 String name = event.name();
156 String value = event.value();
157
158 switch (event.type()) {
159 case PROPERTY_SET:
160 set(componentName, name, value);
161 break;
162 case PROPERTY_UNSET:
163 reset(componentName, name);
164 break;
165 default:
166 break;
167 }
168 }
169 }
170
171 // Locates the property in the component map and replaces it with an
172 // updated copy.
173 private void set(String componentName, String name, String value) {
174 Map<String, ConfigProperty> map = properties.get(componentName);
175 if (map != null) {
176 ConfigProperty prop = map.get(name);
177 if (prop != null) {
178 map.put(name, ConfigProperty.setProperty(prop, value));
179 triggerUpdate(componentName);
180 return;
181 }
182 }
183 log.warn("Unable to set non-existent property {} for component {}",
184 name, componentName);
185 }
186
187 // Locates the property in the component map and replaces it with an
188 // reset copy.
189 private void reset(String componentName, String name) {
190 Map<String, ConfigProperty> map = properties.get(componentName);
191 if (map != null) {
192 ConfigProperty prop = map.get(name);
193 if (prop != null) {
194 map.put(name, ConfigProperty.resetProperty(prop));
195 triggerUpdate(componentName);
196 return;
197 }
198 }
199 log.warn("Unable to reset non-existent property {} for component {}",
200 name, componentName);
201 }
202
203 // Loads existing property values that may have been set.
204 private void loadExistingValues(String componentName) {
205 // FIXME: implement this by talking to the config admin.
206 try {
207 Configuration cfg = cfgAdmin.getConfiguration(componentName);
208 Map<String, ConfigProperty> map = properties.get(componentName);
209 Dictionary<String, Object> props = cfg.getProperties();
210 if (props != null) {
211 Enumeration<String> it = props.keys();
212 while (it.hasMoreElements()) {
213 String name = it.nextElement();
214 ConfigProperty p = map.get(name);
215 if (p != null) {
216 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
217 }
218 }
219 }
220 } catch (IOException e) {
221 log.error("Unable to get configuration for " + componentName, e);
222 }
223
224 }
225
226 // FIXME: This should be a slightly deferred execution to allow changing
227 // values just once per component when a number of updates arrive shortly
228 // after each other.
229 private void triggerUpdate(String componentName) {
230 try {
231 Configuration cfg = cfgAdmin.getConfiguration(componentName);
232 Map<String, ConfigProperty> map = properties.get(componentName);
233 Dictionary<String, Object> props = new Hashtable<>();
234 map.values().forEach(p -> props.put(p.name(), p.value()));
235 cfg.update(props);
236 } catch (IOException e) {
237 log.warn("Unable to update configuration for " + componentName, e);
238 }
239 }
240
241}