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); |
| 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) { |
Brian O'Connor | 85c2926 | 2015-03-10 20:53:43 -0700 | [diff] [blame] | 133 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 134 | return map != null ? ImmutableSet.copyOf(map.values()) : null; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setProperty(String componentName, String name, String value) { |
| 139 | checkNotNull(componentName, COMPONENT_NULL); |
| 140 | checkNotNull(name, PROPERTY_NULL); |
| 141 | store.setProperty(componentName, name, value); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void unsetProperty(String componentName, String name) { |
| 146 | checkNotNull(componentName, COMPONENT_NULL); |
| 147 | checkNotNull(name, PROPERTY_NULL); |
| 148 | store.unsetProperty(componentName, name); |
| 149 | } |
| 150 | |
| 151 | private class InternalStoreDelegate implements ComponentConfigStoreDelegate { |
| 152 | |
| 153 | @Override |
| 154 | public void notify(ComponentConfigEvent event) { |
| 155 | String componentName = event.subject(); |
| 156 | String name = event.name(); |
| 157 | String value = event.value(); |
| 158 | |
| 159 | switch (event.type()) { |
| 160 | case PROPERTY_SET: |
| 161 | set(componentName, name, value); |
| 162 | break; |
| 163 | case PROPERTY_UNSET: |
| 164 | reset(componentName, name); |
| 165 | break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Locates the property in the component map and replaces it with an |
| 173 | // updated copy. |
| 174 | private void set(String componentName, String name, String value) { |
| 175 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 176 | if (map != null) { |
| 177 | ConfigProperty prop = map.get(name); |
| 178 | if (prop != null) { |
| 179 | map.put(name, ConfigProperty.setProperty(prop, value)); |
| 180 | triggerUpdate(componentName); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | log.warn("Unable to set non-existent property {} for component {}", |
| 185 | name, componentName); |
| 186 | } |
| 187 | |
| 188 | // Locates the property in the component map and replaces it with an |
| 189 | // reset copy. |
| 190 | private void reset(String componentName, String name) { |
| 191 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 192 | if (map != null) { |
| 193 | ConfigProperty prop = map.get(name); |
| 194 | if (prop != null) { |
| 195 | map.put(name, ConfigProperty.resetProperty(prop)); |
| 196 | triggerUpdate(componentName); |
| 197 | return; |
| 198 | } |
| 199 | } |
| 200 | log.warn("Unable to reset non-existent property {} for component {}", |
| 201 | name, componentName); |
| 202 | } |
| 203 | |
| 204 | // Loads existing property values that may have been set. |
| 205 | private void loadExistingValues(String componentName) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 206 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 207 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 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 { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 231 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 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 | } |