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