Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.store.config.impl; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.BooleanNode; |
| 22 | import com.fasterxml.jackson.databind.node.DoubleNode; |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 23 | import com.fasterxml.jackson.databind.node.IntNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 25 | import com.fasterxml.jackson.databind.node.LongNode; |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.node.NullNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | import com.fasterxml.jackson.databind.node.ShortNode; |
| 29 | import com.fasterxml.jackson.databind.node.TextNode; |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | import com.google.common.collect.Maps; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 32 | import com.google.common.collect.Sets; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Activate; |
| 34 | import org.apache.felix.scr.annotations.Component; |
| 35 | import org.apache.felix.scr.annotations.Deactivate; |
| 36 | import org.apache.felix.scr.annotations.Reference; |
| 37 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 38 | import org.apache.felix.scr.annotations.Service; |
| 39 | import org.onlab.util.KryoNamespace; |
Thomas Vachuska | 5f2cbe6 | 2015-07-30 15:10:34 -0700 | [diff] [blame] | 40 | import org.onlab.util.Tools; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 41 | import org.onosproject.net.config.Config; |
| 42 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 43 | import org.onosproject.net.config.ConfigFactory; |
| 44 | import org.onosproject.net.config.NetworkConfigEvent; |
| 45 | import org.onosproject.net.config.NetworkConfigStore; |
| 46 | import org.onosproject.net.config.NetworkConfigStoreDelegate; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 47 | import org.onosproject.store.AbstractStore; |
| 48 | import org.onosproject.store.serializers.KryoNamespaces; |
| 49 | import org.onosproject.store.service.ConsistentMap; |
Thomas Vachuska | 5f2cbe6 | 2015-07-30 15:10:34 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.ConsistentMapException; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.MapEvent; |
| 52 | import org.onosproject.store.service.MapEventListener; |
| 53 | import org.onosproject.store.service.Serializer; |
| 54 | import org.onosproject.store.service.StorageService; |
| 55 | import org.onosproject.store.service.Versioned; |
| 56 | import org.slf4j.Logger; |
| 57 | import org.slf4j.LoggerFactory; |
| 58 | |
| 59 | import java.util.LinkedHashMap; |
| 60 | import java.util.Map; |
| 61 | import java.util.Objects; |
| 62 | import java.util.Set; |
| 63 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 64 | import static com.google.common.base.Preconditions.checkArgument; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 65 | import static org.onosproject.net.config.NetworkConfigEvent.Type.*; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Implementation of a distributed network configuration store. |
| 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | @Service |
| 72 | public class DistributedNetworkConfigStore |
| 73 | extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate> |
| 74 | implements NetworkConfigStore { |
| 75 | |
| 76 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 77 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 78 | private static final int MAX_BACKOFF = 10; |
| 79 | private static final String INVALID_CONFIG_JSON = |
| 80 | "JSON node does not contain valid configuration"; |
| 81 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected StorageService storageService; |
| 84 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 85 | private ConsistentMap<ConfigKey, JsonNode> configs; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 86 | |
| 87 | private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap(); |
| 88 | private final ObjectMapper mapper = new ObjectMapper(); |
| 89 | private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate(); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 90 | private final MapEventListener<ConfigKey, JsonNode> listener = new InternalMapListener(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | |
| 92 | @Activate |
| 93 | public void activate() { |
| 94 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 95 | .register(KryoNamespaces.API) |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 96 | .register(ConfigKey.class, ObjectNode.class, ArrayNode.class, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 97 | JsonNodeFactory.class, LinkedHashMap.class, |
| 98 | TextNode.class, BooleanNode.class, |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 99 | LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class, |
| 100 | NullNode.class); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 101 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 102 | configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 103 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 104 | .withName("onos-network-configs") |
Madan Jampani | 3d6a2f6 | 2015-08-12 07:19:07 -0700 | [diff] [blame] | 105 | .withRelaxedReadConsistency() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 106 | .build(); |
| 107 | configs.addListener(listener); |
| 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | public void deactivate() { |
| 113 | configs.removeListener(listener); |
| 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void addConfigFactory(ConfigFactory configFactory) { |
| 119 | factoriesByConfig.put(configFactory.configClass().getName(), configFactory); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 120 | processPendingConfigs(configFactory); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 121 | notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(), |
| 122 | configFactory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 125 | // Sweep through any pending configurations, validate them and then prune them. |
| 126 | private void processPendingConfigs(ConfigFactory configFactory) { |
| 127 | Set<ConfigKey> toBePruned = Sets.newHashSet(); |
| 128 | configs.keySet().forEach(k -> { |
| 129 | if (Objects.equals(k.configKey, configFactory.configKey())) { |
| 130 | validateConfig(k, configFactory, configs.get(k).value()); |
| 131 | toBePruned.add(k); // Prune whether valid or not |
| 132 | } |
| 133 | }); |
| 134 | toBePruned.forEach(configs::remove); |
| 135 | } |
| 136 | |
| 137 | @SuppressWarnings("unchecked") |
| 138 | private void validateConfig(ConfigKey key, ConfigFactory configFactory, JsonNode json) { |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame^] | 139 | Object subject; |
| 140 | if (key.subject instanceof String) { |
| 141 | subject = configFactory.subjectFactory().createSubject((String) key.subject); |
| 142 | } else { |
| 143 | subject = key.subject; |
| 144 | } |
| 145 | Config config = createConfig(subject, configFactory.configClass(), json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 146 | try { |
| 147 | checkArgument(config.isValid(), INVALID_CONFIG_JSON); |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame^] | 148 | configs.putAndGet(key(subject, configFactory.configClass()), json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 149 | } catch (Exception e) { |
| 150 | log.warn("Failed to validate pending {} configuration for {}: {}", |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame^] | 151 | key.configKey, key.subject, json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void removeConfigFactory(ConfigFactory configFactory) { |
| 157 | factoriesByConfig.remove(configFactory.configClass().getName()); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 158 | notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(), |
| 159 | configFactory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | @SuppressWarnings("unchecked") |
| 164 | public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) { |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame^] | 165 | return factoriesByConfig.get(configClass.getName()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | @SuppressWarnings("unchecked") |
| 170 | public <S> Set<S> getSubjects(Class<S> subjectClass) { |
| 171 | ImmutableSet.Builder<S> builder = ImmutableSet.builder(); |
| 172 | configs.keySet().forEach(k -> { |
| 173 | if (subjectClass.isInstance(k.subject)) { |
| 174 | builder.add((S) k.subject); |
| 175 | } |
| 176 | }); |
| 177 | return builder.build(); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | @SuppressWarnings("unchecked") |
| 182 | public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) { |
| 183 | ImmutableSet.Builder<S> builder = ImmutableSet.builder(); |
| 184 | String cName = configClass.getName(); |
| 185 | configs.keySet().forEach(k -> { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 186 | if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 187 | builder.add((S) k.subject); |
| 188 | } |
| 189 | }); |
| 190 | return builder.build(); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | @SuppressWarnings("unchecked") |
| 195 | public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) { |
| 196 | ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder(); |
| 197 | configs.keySet().forEach(k -> { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 198 | if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 199 | builder.add(factoriesByConfig.get(k.configClass).configClass()); |
| 200 | } |
| 201 | }); |
| 202 | return builder.build(); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) { |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 207 | // TODO: need to identify and address the root cause for timeouts. |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 208 | Versioned<JsonNode> json = Tools.retryable(configs::get, ConsistentMapException.class, 1, MAX_BACKOFF) |
| 209 | .apply(key(subject, configClass)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 210 | return json != null ? createConfig(subject, configClass, json.value()) : null; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | @Override |
| 215 | public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 216 | ConfigFactory<S, C> factory = getConfigFactory(configClass); |
| 217 | Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass), |
| 218 | k -> factory.isList() ? |
| 219 | mapper.createArrayNode() : |
| 220 | mapper.createObjectNode()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 221 | return createConfig(subject, configClass, json.value()); |
| 222 | } |
| 223 | |
| 224 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 225 | public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) { |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 226 | // Create the configuration and validate it. |
| 227 | C config = createConfig(subject, configClass, json); |
| 228 | checkArgument(config.isValid(), INVALID_CONFIG_JSON); |
| 229 | |
| 230 | // Insert the validated configuration and get it back. |
| 231 | Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json); |
| 232 | |
| 233 | // Re-create the config if for some reason what we attempted to put |
| 234 | // was supplanted by someone else already. |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 235 | return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value()); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public <S> void queueConfig(S subject, String configKey, JsonNode json) { |
| 240 | configs.put(key(subject, configKey), json); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) { |
| 245 | configs.remove(key(subject, configClass)); |
| 246 | } |
| 247 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 248 | @Override |
| 249 | public <S> void clearQueuedConfig(S subject, String configKey) { |
| 250 | configs.remove(key(subject, configKey)); |
| 251 | } |
| 252 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Produces a config from the specified subject, config class and raw JSON. |
| 255 | * |
| 256 | * @param subject config subject |
| 257 | * @param configClass config class |
| 258 | * @param json raw JSON data |
| 259 | * @return config object or null of no factory found or if the specified |
| 260 | * JSON is null |
| 261 | */ |
| 262 | @SuppressWarnings("unchecked") |
| 263 | private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass, |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 264 | JsonNode json) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 265 | if (json != null) { |
| 266 | ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName()); |
| 267 | if (factory != null) { |
| 268 | C config = factory.createConfig(); |
| 269 | config.init(subject, factory.configKey(), json, mapper, applyDelegate); |
| 270 | return config; |
| 271 | } |
| 272 | } |
| 273 | return null; |
| 274 | } |
| 275 | |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 276 | /** |
| 277 | * Produces a detached config from the specified subject, config class and |
| 278 | * raw JSON. |
| 279 | * |
| 280 | * A detached config can no longer be applied. This should be used only for |
| 281 | * passing the config object in the NetworkConfigEvent. |
| 282 | * |
| 283 | * @param subject config subject |
| 284 | * @param configClass config class |
| 285 | * @param json raw JSON data |
| 286 | * @return config object or null of no factory found or if the specified |
| 287 | * JSON is null |
| 288 | */ |
| 289 | @SuppressWarnings("unchecked") |
| 290 | private Config createDetachedConfig(Object subject, |
| 291 | Class configClass, JsonNode json) { |
| 292 | if (json != null) { |
| 293 | ConfigFactory factory = factoriesByConfig.get(configClass.getName()); |
| 294 | if (factory != null) { |
| 295 | Config config = factory.createConfig(); |
| 296 | config.init(subject, factory.configKey(), json, mapper, null); |
| 297 | return config; |
| 298 | } |
| 299 | } |
| 300 | return null; |
| 301 | } |
| 302 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 303 | |
| 304 | // Auxiliary delegate to receive notifications about changes applied to |
| 305 | // the network configuration - by the apps. |
| 306 | private class InternalApplyDelegate implements ConfigApplyDelegate { |
| 307 | @Override |
| 308 | public void onApply(Config config) { |
| 309 | configs.put(key(config.subject(), config.getClass()), config.node()); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Produces a key for uniquely tracking a subject config. |
| 314 | private static ConfigKey key(Object subject, Class<?> configClass) { |
| 315 | return new ConfigKey(subject, configClass); |
| 316 | } |
| 317 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 318 | // Produces a key for uniquely tracking a subject config. |
| 319 | private static ConfigKey key(Object subject, String configKey) { |
| 320 | return new ConfigKey(subject, configKey); |
| 321 | } |
| 322 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 323 | // Auxiliary key to track subject configurations. |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 324 | // Keys with non-null configKey are pending configurations. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 325 | private static final class ConfigKey { |
| 326 | final Object subject; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 327 | final String configKey; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 328 | final String configClass; |
| 329 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 330 | // Create a key for pending configuration class |
| 331 | private ConfigKey(Object subject, String configKey) { |
| 332 | this.subject = subject; |
| 333 | this.configKey = configKey; |
| 334 | this.configClass = null; |
| 335 | } |
| 336 | |
| 337 | // Create a key for registered class configuration |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 338 | private ConfigKey(Object subject, Class<?> configClass) { |
| 339 | this.subject = subject; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 340 | this.configKey = null; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 341 | this.configClass = configClass.getName(); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public int hashCode() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 346 | return Objects.hash(subject, configKey, configClass); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public boolean equals(Object obj) { |
| 351 | if (this == obj) { |
| 352 | return true; |
| 353 | } |
| 354 | if (obj instanceof ConfigKey) { |
| 355 | final ConfigKey other = (ConfigKey) obj; |
| 356 | return Objects.equals(this.subject, other.subject) |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 357 | && Objects.equals(this.configKey, other.configKey) |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 358 | && Objects.equals(this.configClass, other.configClass); |
| 359 | } |
| 360 | return false; |
| 361 | } |
| 362 | } |
| 363 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 364 | private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 365 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 366 | public void event(MapEvent<ConfigKey, JsonNode> event) { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 367 | // Do not delegate pending configs. |
| 368 | if (event.key().configClass == null) { |
| 369 | return; |
| 370 | } |
| 371 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 372 | ConfigFactory factory = factoriesByConfig.get(event.key().configClass); |
| 373 | if (factory != null) { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 374 | Object subject = event.key().subject; |
| 375 | Class configClass = factory.configClass(); |
| 376 | Versioned<JsonNode> newValue = event.newValue(); |
| 377 | Versioned<JsonNode> oldValue = event.oldValue(); |
| 378 | |
| 379 | Config config = (newValue != null) ? |
| 380 | createDetachedConfig(subject, configClass, newValue.value()) : |
| 381 | null; |
| 382 | Config prevConfig = (oldValue != null) ? |
| 383 | createDetachedConfig(subject, configClass, oldValue.value()) : |
| 384 | null; |
| 385 | |
| 386 | NetworkConfigEvent.Type type; |
| 387 | switch (event.type()) { |
| 388 | case INSERT: |
| 389 | type = CONFIG_ADDED; |
| 390 | break; |
| 391 | case UPDATE: |
| 392 | type = CONFIG_UPDATED; |
| 393 | break; |
| 394 | case REMOVE: |
| 395 | default: |
| 396 | type = CONFIG_REMOVED; |
| 397 | break; |
| 398 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 399 | notifyDelegate(new NetworkConfigEvent(type, event.key().subject, |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 400 | config, prevConfig, factory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |