blob: 2397c64a1c1c5967ec5f9c8ebdfc944ae5683c92 [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);
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700117 if (clear && cps != null) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700118 cps.keySet().forEach(name -> store.unsetProperty(componentName, name));
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700119 clearExistingValues(componentName);
120 }
121 }
122
123 // Clears any existing values that may have been set.
124 private void clearExistingValues(String componentName) {
125 triggerUpdate(componentName);
126 }
127
128 @Override
129 public Set<ConfigProperty> getProperties(String componentName) {
Brian O'Connor85c29262015-03-10 20:53:43 -0700130 Map<String, ConfigProperty> map = properties.get(componentName);
131 return map != null ? ImmutableSet.copyOf(map.values()) : null;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700132 }
133
134 @Override
135 public void setProperty(String componentName, String name, String value) {
136 checkNotNull(componentName, COMPONENT_NULL);
137 checkNotNull(name, PROPERTY_NULL);
138 store.setProperty(componentName, name, value);
139 }
140
141 @Override
142 public void unsetProperty(String componentName, String name) {
143 checkNotNull(componentName, COMPONENT_NULL);
144 checkNotNull(name, PROPERTY_NULL);
145 store.unsetProperty(componentName, name);
146 }
147
148 private class InternalStoreDelegate implements ComponentConfigStoreDelegate {
149
150 @Override
151 public void notify(ComponentConfigEvent event) {
152 String componentName = event.subject();
153 String name = event.name();
154 String value = event.value();
155
156 switch (event.type()) {
157 case PROPERTY_SET:
158 set(componentName, name, value);
159 break;
160 case PROPERTY_UNSET:
161 reset(componentName, name);
162 break;
163 default:
164 break;
165 }
166 }
167 }
168
169 // Locates the property in the component map and replaces it with an
170 // updated copy.
171 private void set(String componentName, String name, String value) {
172 Map<String, ConfigProperty> map = properties.get(componentName);
173 if (map != null) {
174 ConfigProperty prop = map.get(name);
175 if (prop != null) {
176 map.put(name, ConfigProperty.setProperty(prop, value));
177 triggerUpdate(componentName);
178 return;
179 }
180 }
181 log.warn("Unable to set non-existent property {} for component {}",
182 name, componentName);
183 }
184
185 // Locates the property in the component map and replaces it with an
186 // reset copy.
187 private void reset(String componentName, String name) {
188 Map<String, ConfigProperty> map = properties.get(componentName);
189 if (map != null) {
190 ConfigProperty prop = map.get(name);
191 if (prop != null) {
192 map.put(name, ConfigProperty.resetProperty(prop));
193 triggerUpdate(componentName);
194 return;
195 }
Thomas Vachuskadb320ab2015-04-22 09:03:33 -0700196 log.warn("Unable to reset non-existent property {} for component {}",
197 name, componentName);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700198 }
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700199 }
200
201 // Loads existing property values that may have been set.
202 private void loadExistingValues(String componentName) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700203 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700204 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700205 Map<String, ConfigProperty> map = properties.get(componentName);
206 Dictionary<String, Object> props = cfg.getProperties();
207 if (props != null) {
208 Enumeration<String> it = props.keys();
209 while (it.hasMoreElements()) {
210 String name = it.nextElement();
211 ConfigProperty p = map.get(name);
212 if (p != null) {
213 map.put(name, ConfigProperty.setProperty(p, (String) props.get(name)));
214 }
215 }
216 }
217 } catch (IOException e) {
218 log.error("Unable to get configuration for " + componentName, e);
219 }
220
221 }
222
223 // FIXME: This should be a slightly deferred execution to allow changing
224 // values just once per component when a number of updates arrive shortly
225 // after each other.
226 private void triggerUpdate(String componentName) {
227 try {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700228 Configuration cfg = cfgAdmin.getConfiguration(componentName, null);
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700229 Map<String, ConfigProperty> map = properties.get(componentName);
230 Dictionary<String, Object> props = new Hashtable<>();
231 map.values().forEach(p -> props.put(p.name(), p.value()));
232 cfg.update(props);
233 } catch (IOException e) {
234 log.warn("Unable to update configuration for " + componentName, e);
235 }
236 }
237
238}