Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 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; |
| 34 | import org.osgi.service.cm.Configuration; |
| 35 | import org.osgi.service.cm.ConfigurationAdmin; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | import java.util.Dictionary; |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 41 | import java.util.HashSet; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 42 | import java.util.Hashtable; |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 43 | import java.util.List; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 44 | import java.util.Map; |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 45 | import java.util.Objects; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 46 | import java.util.Set; |
| 47 | |
| 48 | import static com.google.common.base.Preconditions.checkArgument; |
| 49 | import static com.google.common.base.Preconditions.checkNotNull; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 50 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 52 | import static org.onosproject.security.AppPermission.Type.*; |
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"; |
Brian O'Connor | 57e83e8 | 2015-09-22 15:45:37 -0700 | [diff] [blame] | 64 | private static final String COMPONENT_MISSING = "Component %s is not registered"; |
| 65 | private static final String PROPERTY_MISSING = "Property %s does not exist for %s"; |
| 66 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 67 | |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 68 | //Symbolic constants for use with the accumulator |
| 69 | private static final int MAX_ITEMS = 100; |
| 70 | private static final int MAX_BATCH_MILLIS = 1000; |
| 71 | private static final int MAX_IDLE_MILLIS = 250; |
| 72 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 73 | private static final String RESOURCE_EXT = ".cfgdef"; |
| 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate(); |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 78 | private final InternalAccumulator accumulator = new InternalAccumulator(); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected ComponentConfigStore store; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected ConfigurationAdmin cfgAdmin; |
| 85 | |
| 86 | // Locally maintained catalog of definitions. |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 87 | private final Map<String, Map<String, ConfigProperty>> properties = |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 88 | Maps.newConcurrentMap(); |
| 89 | |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 90 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 91 | @Activate |
| 92 | public void activate() { |
| 93 | store.setDelegate(delegate); |
| 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
| 99 | store.unsetDelegate(delegate); |
| 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Set<String> getComponentNames() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 105 | checkPermission(CONFIG_READ); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 106 | return ImmutableSet.copyOf(properties.keySet()); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void registerProperties(Class<?> componentClass) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 111 | checkPermission(CONFIG_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 112 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 113 | String componentName = componentClass.getName(); |
| 114 | String resourceName = componentClass.getSimpleName() + RESOURCE_EXT; |
| 115 | try (InputStream ris = componentClass.getResourceAsStream(resourceName)) { |
| 116 | checkArgument(ris != null, "Property definitions not found at resource %s", |
| 117 | resourceName); |
| 118 | |
| 119 | // Read the definitions |
| 120 | Set<ConfigProperty> defs = ConfigPropertyDefinitions.read(ris); |
| 121 | |
| 122 | // Produce a new map of the properties and register it. |
| 123 | Map<String, ConfigProperty> map = Maps.newConcurrentMap(); |
| 124 | defs.forEach(p -> map.put(p.name(), p)); |
| 125 | |
| 126 | properties.put(componentName, map); |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 127 | loadExistingValues(componentName, map); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 128 | } catch (IOException e) { |
| 129 | log.error("Unable to read property definitions from resource " + resourceName, e); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void unregisterProperties(Class<?> componentClass, boolean clear) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 135 | checkPermission(CONFIG_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 136 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 137 | String componentName = componentClass.getName(); |
| 138 | checkNotNull(componentName, COMPONENT_NULL); |
| 139 | Map<String, ConfigProperty> cps = properties.remove(componentName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 140 | if (clear && cps != null) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 141 | cps.keySet().forEach(name -> store.unsetProperty(componentName, name)); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 142 | clearExistingValues(componentName); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Clears any existing values that may have been set. |
| 147 | private void clearExistingValues(String componentName) { |
| 148 | triggerUpdate(componentName); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Set<ConfigProperty> getProperties(String componentName) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 153 | checkPermission(CONFIG_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 154 | |
Brian O'Connor | 85c2926 | 2015-03-10 20:53:43 -0700 | [diff] [blame] | 155 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 156 | return map != null ? ImmutableSet.copyOf(map.values()) : null; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void setProperty(String componentName, String name, String value) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 161 | checkPermission(CONFIG_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 162 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 163 | checkNotNull(componentName, COMPONENT_NULL); |
| 164 | checkNotNull(name, PROPERTY_NULL); |
Brian O'Connor | 57e83e8 | 2015-09-22 15:45:37 -0700 | [diff] [blame] | 165 | |
| 166 | checkArgument(properties.containsKey(componentName), |
| 167 | COMPONENT_MISSING, componentName); |
| 168 | checkArgument(properties.get(componentName).containsKey(name), |
| 169 | PROPERTY_MISSING, name, componentName); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 170 | checkValidity(componentName, name, value); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 171 | store.setProperty(componentName, name, value); |
| 172 | } |
| 173 | |
| 174 | @Override |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 175 | public void preSetProperty(String componentName, String name, String value) { |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 176 | checkPermission(CONFIG_WRITE); |
| 177 | |
| 178 | checkNotNull(componentName, COMPONENT_NULL); |
| 179 | checkNotNull(name, PROPERTY_NULL); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 180 | checkValidity(componentName, name, value); |
andrea | dd106a6 | 2015-10-02 13:39:48 -0700 | [diff] [blame] | 181 | store.setProperty(componentName, name, value); |
| 182 | } |
| 183 | |
| 184 | @Override |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 185 | public void unsetProperty(String componentName, String name) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 186 | checkPermission(CONFIG_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 187 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 188 | checkNotNull(componentName, COMPONENT_NULL); |
| 189 | checkNotNull(name, PROPERTY_NULL); |
Brian O'Connor | 57e83e8 | 2015-09-22 15:45:37 -0700 | [diff] [blame] | 190 | |
| 191 | checkArgument(properties.containsKey(componentName), |
| 192 | COMPONENT_MISSING, componentName); |
| 193 | checkArgument(properties.get(componentName).containsKey(name), |
| 194 | PROPERTY_MISSING, name, componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 195 | store.unsetProperty(componentName, name); |
| 196 | } |
| 197 | |
| 198 | private class InternalStoreDelegate implements ComponentConfigStoreDelegate { |
| 199 | |
| 200 | @Override |
| 201 | public void notify(ComponentConfigEvent event) { |
| 202 | String componentName = event.subject(); |
| 203 | String name = event.name(); |
| 204 | String value = event.value(); |
| 205 | |
| 206 | switch (event.type()) { |
| 207 | case PROPERTY_SET: |
| 208 | set(componentName, name, value); |
| 209 | break; |
| 210 | case PROPERTY_UNSET: |
| 211 | reset(componentName, name); |
| 212 | break; |
| 213 | default: |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 219 | // Buffers multiple subsequent configuration updates into one notification. |
| 220 | private class InternalAccumulator extends AbstractAccumulator<String> |
| 221 | implements Accumulator<String> { |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 222 | |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 223 | protected InternalAccumulator() { |
| 224 | super(SharedExecutors.getTimer(), MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Thomas Vachuska | 92af89f | 2015-06-22 15:16:23 -0700 | [diff] [blame] | 228 | public void processItems(List<String> items) { |
| 229 | // Conversion to set removes duplicates |
| 230 | Set<String> componentSet = new HashSet<>(items); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 231 | componentSet.forEach(ComponentConfigManager.this::triggerUpdate); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 235 | // Locates the property in the component map and replaces it with an |
| 236 | // updated copy. |
| 237 | private void set(String componentName, String name, String value) { |
| 238 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 239 | if (map != null) { |
| 240 | ConfigProperty prop = map.get(name); |
| 241 | if (prop != null) { |
| 242 | map.put(name, ConfigProperty.setProperty(prop, value)); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 243 | accumulator.add(componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | } |
Thomas Vachuska | 4b32fcb | 2018-02-20 12:35:19 -0800 | [diff] [blame] | 247 | |
| 248 | // If definition doesn't exist in local catalog, cache the property. |
| 249 | preSet(componentName, name, value); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Locates the property in the component map and replaces it with an |
| 253 | // reset copy. |
| 254 | private void reset(String componentName, String name) { |
| 255 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 256 | if (map != null) { |
| 257 | ConfigProperty prop = map.get(name); |
| 258 | if (prop != null) { |
| 259 | map.put(name, ConfigProperty.resetProperty(prop)); |
Aaron Kruglikov | 6685521 | 2015-06-22 12:29:02 -0700 | [diff] [blame] | 260 | accumulator.add(componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 261 | return; |
| 262 | } |
Thomas Vachuska | db320ab | 2015-04-22 09:03:33 -0700 | [diff] [blame] | 263 | log.warn("Unable to reset non-existent property {} for component {}", |
| 264 | name, componentName); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 265 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Thomas Vachuska | 4b32fcb | 2018-02-20 12:35:19 -0800 | [diff] [blame] | 268 | // Stores non-existent property so that loadExistingValues() can load in future. |
| 269 | private void preSet(String componentName, String name, String value) { |
| 270 | try { |
| 271 | Configuration config = cfgAdmin.getConfiguration(componentName, null); |
| 272 | Dictionary<String, Object> props = config.getProperties(); |
| 273 | if (props == null) { |
| 274 | props = new Hashtable<>(); |
| 275 | } |
| 276 | props.put(name, value); |
| 277 | config.update(props); |
| 278 | } catch (IOException e) { |
| 279 | log.error("Failed to preset configuration for {}", componentName); |
| 280 | } |
| 281 | } |
| 282 | |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 283 | // Checks whether the value of the specified configuration property is a valid one or not. |
| 284 | private void checkValidity(String componentName, String name, String newValue) { |
| 285 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 286 | if (map == null) { |
| 287 | return; |
| 288 | } |
| 289 | ConfigProperty prop = map.get(name); |
| 290 | ConfigProperty.Type type = prop.type(); |
| 291 | try { |
| 292 | switch (type) { |
| 293 | case INTEGER: |
| 294 | Integer.parseInt(newValue); |
| 295 | break; |
| 296 | case LONG: |
| 297 | Long.parseLong(newValue); |
| 298 | break; |
| 299 | case FLOAT: |
| 300 | Float.parseFloat(newValue); |
| 301 | break; |
| 302 | case DOUBLE: |
| 303 | Double.parseDouble(newValue); |
| 304 | break; |
| 305 | case BOOLEAN: |
| 306 | if (!((newValue != null) && (newValue.equalsIgnoreCase("true") || |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 307 | newValue.equalsIgnoreCase("false")))) { |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 308 | throw new IllegalArgumentException("Invalid " + type + " value"); |
| 309 | } |
| 310 | break; |
| 311 | case STRING: |
| 312 | case BYTE: |
| 313 | //do nothing |
| 314 | break; |
| 315 | default: |
| 316 | log.warn("Not a valid config property type"); |
| 317 | break; |
| 318 | |
| 319 | } |
| 320 | } catch (NumberFormatException e) { |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 321 | throw new NumberFormatException("Invalid " + type + " value"); |
sivachidambaram subramanian | 8c9a701 | 2017-04-26 14:59:02 +0530 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 325 | // Loads existing property values that may have been learned from other |
| 326 | // nodes in the cluster before the local property registration. |
| 327 | private void loadExistingValues(String componentName, |
| 328 | Map<String, ConfigProperty> map) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 329 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 330 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 331 | Dictionary<String, Object> localProperties = cfg.getProperties(); |
| 332 | |
| 333 | // Iterate over all registered config properties... |
| 334 | for (ConfigProperty p : ImmutableSet.copyOf(map.values())) { |
| 335 | String name = p.name(); |
| 336 | String globalValue = store.getProperty(componentName, name); |
| 337 | String localValue = localProperties != null ? (String) localProperties.get(name) : null; |
Thomas Vachuska | 611f466 | 2018-02-27 11:30:09 -0800 | [diff] [blame] | 338 | |
| 339 | log.debug("Processing {} property {}; global {}; local {}", |
| 340 | componentName, name, globalValue, localValue); |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 341 | try { |
Thomas Vachuska | 611f466 | 2018-02-27 11:30:09 -0800 | [diff] [blame] | 342 | // 1) If the global value is the same as default, or |
| 343 | // 2) if it is not set explicitly, but the local value is |
| 344 | // not the same as the default, reset local value to default. |
| 345 | if (Objects.equals(globalValue, p.defaultValue()) || |
| 346 | (globalValue == null && |
| 347 | !Objects.equals(localValue, p.defaultValue()))) { |
| 348 | log.debug("Resetting {} property {}; value same as default", |
| 349 | componentName, name); |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 350 | reset(componentName, name); |
| 351 | |
| 352 | } else if (!Objects.equals(globalValue, localValue)) { |
Thomas Vachuska | 611f466 | 2018-02-27 11:30:09 -0800 | [diff] [blame] | 353 | // If the local value is different from global value |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 354 | // validate the global value and apply it locally. |
Thomas Vachuska | 611f466 | 2018-02-27 11:30:09 -0800 | [diff] [blame] | 355 | log.debug("Setting {} property {} to {}", |
| 356 | componentName, name, globalValue); |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 357 | checkValidity(componentName, name, globalValue); |
| 358 | set(componentName, name, globalValue); |
Thomas Vachuska | 611f466 | 2018-02-27 11:30:09 -0800 | [diff] [blame] | 359 | |
| 360 | } else { |
| 361 | // Otherwise, simply update the registered property |
| 362 | // with the global value. |
| 363 | log.debug("Syncing {} property {} to {}", |
| 364 | componentName, name, globalValue); |
| 365 | map.put(name, ConfigProperty.setProperty(p, globalValue)); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 366 | } |
Thomas Vachuska | 9f6bd52 | 2018-02-12 15:53:01 -0800 | [diff] [blame] | 367 | } catch (IllegalArgumentException e) { |
| 368 | log.warn("Value {} is not a valid {}; using default", |
| 369 | globalValue, p.type()); |
| 370 | reset(componentName, name); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } catch (IOException e) { |
| 374 | log.error("Unable to get configuration for " + componentName, e); |
| 375 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 378 | private void triggerUpdate(String componentName) { |
| 379 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 380 | Configuration cfg = cfgAdmin.getConfiguration(componentName, null); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 381 | Map<String, ConfigProperty> map = properties.get(componentName); |
| 382 | Dictionary<String, Object> props = new Hashtable<>(); |
Thomas Vachuska | 611f466 | 2018-02-27 11:30:09 -0800 | [diff] [blame] | 383 | map.values().stream().filter(p -> p.value() != null) |
| 384 | .forEach(p -> props.put(p.name(), p.value())); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 385 | cfg.update(props); |
| 386 | } catch (IOException e) { |
| 387 | log.warn("Unable to update configuration for " + componentName, e); |
| 388 | } |
| 389 | } |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 390 | } |