Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cfg.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.cfg.ComponentConfigEvent; |
| 27 | import org.onosproject.cfg.ComponentConfigService; |
| 28 | import org.onosproject.cfg.ComponentConfigStore; |
| 29 | import org.onosproject.cfg.ComponentConfigStoreDelegate; |
| 30 | import org.onosproject.cfg.ConfigProperty; |
| 31 | import org.osgi.service.cm.Configuration; |
| 32 | import org.osgi.service.cm.ConfigurationAdmin; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.Dictionary; |
| 38 | import java.util.Enumeration; |
| 39 | import java.util.Hashtable; |
| 40 | import java.util.Map; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkArgument; |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Implementation of the centralized component configuration service. |
| 49 | */ |
| 50 | @Component(immediate = true) |
| 51 | @Service |
| 52 | public 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 Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 117 | if (clear && cps != null) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 118 | cps.keySet().forEach(name -> store.unsetProperty(componentName, name)); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 119 | 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'Connor | 85c2926 | 2015-03-10 20:53:43 -0700 | [diff] [blame] | 130 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 131 | return map != null ? ImmutableSet.copyOf(map.values()) : null; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 132 | } |
| 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 Vachuska | db320ab | 2015-04-22 09:03:33 -0700 | [diff] [blame] | 196 | log.warn("Unable to reset non-existent property {} for component {}", |
| 197 | name, componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 198 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Loads existing property values that may have been set. |
| 202 | private void loadExistingValues(String componentName) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 203 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 204 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 205 | 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 Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 228 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 229 | 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 | } |