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