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; |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 26 | import org.onlab.util.AbstractAccumulator; |
| 27 | import org.onlab.util.Accumulator; |
| 28 | import org.onlab.util.SharedExecutors; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigEvent; |
| 30 | import org.onosproject.cfg.ComponentConfigService; |
| 31 | import org.onosproject.cfg.ComponentConfigStore; |
| 32 | import org.onosproject.cfg.ComponentConfigStoreDelegate; |
| 33 | import org.onosproject.cfg.ConfigProperty; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 34 | import org.onosproject.core.Permission; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 35 | import org.osgi.service.cm.Configuration; |
| 36 | import org.osgi.service.cm.ConfigurationAdmin; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.util.Dictionary; |
| 42 | import java.util.Enumeration; |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 43 | import java.util.HashSet; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 44 | import java.util.Hashtable; |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 45 | import java.util.List; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 46 | import java.util.Map; |
| 47 | import java.util.Set; |
| 48 | |
| 49 | import static com.google.common.base.Preconditions.checkArgument; |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 51 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 53 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Implementation of the centralized component configuration service. |
| 57 | */ |
| 58 | @Component(immediate = true) |
| 59 | @Service |
| 60 | public class ComponentConfigManager implements ComponentConfigService { |
| 61 | |
| 62 | private static final String COMPONENT_NULL = "Component name cannot be null"; |
| 63 | private static final String PROPERTY_NULL = "Property name cannot be null"; |
| 64 | |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 65 | //Symbolic constants for use with the accumulator |
| 66 | private static final int MAX_ITEMS = 100; |
| 67 | private static final int MAX_BATCH_MILLIS = 1000; |
| 68 | private static final int MAX_IDLE_MILLIS = 250; |
| 69 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 70 | private static final String RESOURCE_EXT = ".cfgdef"; |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate(); |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 75 | private final InternalAccumulator accumulator = new InternalAccumulator(); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected ComponentConfigStore store; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected ConfigurationAdmin cfgAdmin; |
| 82 | |
| 83 | // Locally maintained catalog of definitions. |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 84 | private final Map<String, Map<String, ConfigProperty>> properties = |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 85 | Maps.newConcurrentMap(); |
| 86 | |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 87 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 88 | @Activate |
| 89 | public void activate() { |
| 90 | store.setDelegate(delegate); |
| 91 | log.info("Started"); |
| 92 | } |
| 93 | |
| 94 | @Deactivate |
| 95 | public void deactivate() { |
| 96 | store.unsetDelegate(delegate); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Set<String> getComponentNames() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 102 | checkPermission(Permission.CONFIG_READ); |
| 103 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 104 | return ImmutableSet.copyOf(properties.keySet()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void registerProperties(Class<?> componentClass) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 109 | checkPermission(Permission.CONFIG_WRITE); |
| 110 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 111 | String componentName = componentClass.getName(); |
| 112 | String resourceName = componentClass.getSimpleName() + RESOURCE_EXT; |
| 113 | try (InputStream ris = componentClass.getResourceAsStream(resourceName)) { |
| 114 | checkArgument(ris != null, "Property definitions not found at resource %s", |
| 115 | resourceName); |
| 116 | |
| 117 | // Read the definitions |
| 118 | Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris); |
| 119 | |
| 120 | // Produce a new map of the properties and register it. |
| 121 | Map<String, ConfigProperty> map = Maps.newConcurrentMap(); |
| 122 | defs.forEach(p -> map.put(p.name(), p)); |
| 123 | |
| 124 | properties.put(componentName, map); |
| 125 | loadExistingValues(componentName); |
| 126 | } catch (IOException e) { |
| 127 | log.error("Unable to read property definitions from resource " + resourceName, e); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void unregisterProperties(Class<?> componentClass, boolean clear) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 133 | checkPermission(Permission.CONFIG_WRITE); |
| 134 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 135 | String componentName = componentClass.getName(); |
| 136 | checkNotNull(componentName, COMPONENT_NULL); |
| 137 | Map<String, ConfigProperty> cps = properties.remove(componentName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 138 | if (clear && cps != null) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 139 | cps.keySet().forEach(name -> store.unsetProperty(componentName, name)); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 140 | clearExistingValues(componentName); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Clears any existing values that may have been set. |
| 145 | private void clearExistingValues(String componentName) { |
| 146 | triggerUpdate(componentName); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Set<ConfigProperty> getProperties(String componentName) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 151 | checkPermission(Permission.CONFIG_READ); |
| 152 | |
Brian O'Connor | 85c2926 | 2015-03-10 20:53:43 -0700 | [diff] [blame] | 153 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 154 | return map != null ? ImmutableSet.copyOf(map.values()) : null; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void setProperty(String componentName, String name, String value) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 159 | checkPermission(Permission.CONFIG_WRITE); |
| 160 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 161 | checkNotNull(componentName, COMPONENT_NULL); |
| 162 | checkNotNull(name, PROPERTY_NULL); |
| 163 | store.setProperty(componentName, name, value); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void unsetProperty(String componentName, String name) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 168 | checkPermission(Permission.CONFIG_WRITE); |
| 169 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 170 | checkNotNull(componentName, COMPONENT_NULL); |
| 171 | checkNotNull(name, PROPERTY_NULL); |
| 172 | store.unsetProperty(componentName, name); |
| 173 | } |
| 174 | |
| 175 | private class InternalStoreDelegate implements ComponentConfigStoreDelegate { |
| 176 | |
| 177 | @Override |
| 178 | public void notify(ComponentConfigEvent event) { |
| 179 | String componentName = event.subject(); |
| 180 | String name = event.name(); |
| 181 | String value = event.value(); |
| 182 | |
| 183 | switch (event.type()) { |
| 184 | case PROPERTY_SET: |
| 185 | set(componentName, name, value); |
| 186 | break; |
| 187 | case PROPERTY_UNSET: |
| 188 | reset(componentName, name); |
| 189 | break; |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 196 | // Buffers multiple subsequent configuration updates into one notification. |
| 197 | private class InternalAccumulator extends AbstractAccumulator<String> |
| 198 | implements Accumulator<String> { |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 199 | |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 200 | protected InternalAccumulator() { |
| 201 | super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 205 | public void processItems(List<String> items) { |
| 206 | // Conversion to set removes duplicates |
| 207 | Set<String> componentSet = new HashSet<>(items); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 208 | componentSet.forEach(ComponentConfigManager.this::triggerUpdate); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 212 | // Locates the property in the component map and replaces it with an |
| 213 | // updated copy. |
| 214 | private void set(String componentName, String name, String value) { |
| 215 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 216 | if (map != null) { |
| 217 | ConfigProperty prop = map.get(name); |
| 218 | if (prop != null) { |
| 219 | map.put(name, ConfigProperty.setProperty(prop, value)); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 220 | accumulator.add(componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | } |
| 224 | log.warn("Unable to set non-existent property {} for component {}", |
| 225 | name, componentName); |
| 226 | } |
| 227 | |
| 228 | // Locates the property in the component map and replaces it with an |
| 229 | // reset copy. |
| 230 | private void reset(String componentName, String name) { |
| 231 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 232 | if (map != null) { |
| 233 | ConfigProperty prop = map.get(name); |
| 234 | if (prop != null) { |
| 235 | map.put(name, ConfigProperty.resetProperty(prop)); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 236 | accumulator.add(componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 237 | return; |
| 238 | } |
Thomas Vachuska | db320ab | 2015-04-22 09:03:33 -0700 | [diff] [blame] | 239 | log.warn("Unable to reset non-existent property {} for component {}", |
| 240 | name, componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 241 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Loads existing property values that may have been set. |
| 245 | private void loadExistingValues(String componentName) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 246 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 247 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 248 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 249 | Dictionary<String, Object> props = cfg.getProperties(); |
| 250 | if (props != null) { |
| 251 | Enumeration<String> it = props.keys(); |
| 252 | while (it.hasMoreElements()) { |
| 253 | String name = it.nextElement(); |
| 254 | ConfigProperty p = map.get(name); |
| 255 | if (p != null) { |
| 256 | map.put(name, ConfigProperty.setProperty(p, (String) props.get(name))); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } catch (IOException e) { |
| 261 | log.error("Unable to get configuration for " + componentName, e); |
| 262 | } |
| 263 | |
| 264 | } |
| 265 | |
| 266 | // FIXME: This should be a slightly deferred execution to allow changing |
| 267 | // values just once per component when a number of updates arrive shortly |
| 268 | // after each other. |
| 269 | private void triggerUpdate(String componentName) { |
| 270 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 271 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 272 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 273 | Dictionary<String, Object> props = new Hashtable<>(); |
| 274 | map.values().forEach(p -> props.put(p.name(), p.value())); |
| 275 | cfg.update(props); |
| 276 | } catch (IOException e) { |
| 277 | log.warn("Unable to update configuration for " + componentName, e); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | } |