blob: 64beb93d65c68cf40e877d4773b29b270b11b1a0 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska96d55b12015-05-11 08:52:03 -07003 *
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 Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.store.config.impl;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070019import com.fasterxml.jackson.databind.ObjectMapper;
Jonathan Hart111b42b2015-07-14 13:28:05 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070021import com.fasterxml.jackson.databind.node.BooleanNode;
22import com.fasterxml.jackson.databind.node.DoubleNode;
Ayaka Koshibe1a002512015-09-03 13:09:23 -070023import com.fasterxml.jackson.databind.node.IntNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070024import com.fasterxml.jackson.databind.node.JsonNodeFactory;
25import com.fasterxml.jackson.databind.node.LongNode;
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080026import com.fasterxml.jackson.databind.node.NullNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070027import com.fasterxml.jackson.databind.node.ObjectNode;
28import com.fasterxml.jackson.databind.node.ShortNode;
29import com.fasterxml.jackson.databind.node.TextNode;
30import com.google.common.collect.ImmutableSet;
31import com.google.common.collect.Maps;
32import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Component;
34import org.apache.felix.scr.annotations.Deactivate;
35import org.apache.felix.scr.annotations.Reference;
36import org.apache.felix.scr.annotations.ReferenceCardinality;
37import org.apache.felix.scr.annotations.Service;
38import org.onlab.util.KryoNamespace;
Ray Milkeya4122362015-08-18 15:19:08 -070039import org.onosproject.net.config.Config;
40import org.onosproject.net.config.ConfigApplyDelegate;
41import org.onosproject.net.config.ConfigFactory;
Jonathan Hart54b83e82016-03-26 20:37:20 -070042import org.onosproject.net.config.InvalidConfigException;
Ray Milkeya4122362015-08-18 15:19:08 -070043import org.onosproject.net.config.NetworkConfigEvent;
44import org.onosproject.net.config.NetworkConfigStore;
45import org.onosproject.net.config.NetworkConfigStoreDelegate;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070046import org.onosproject.store.AbstractStore;
47import org.onosproject.store.serializers.KryoNamespaces;
48import org.onosproject.store.service.ConsistentMap;
49import org.onosproject.store.service.MapEvent;
50import org.onosproject.store.service.MapEventListener;
51import org.onosproject.store.service.Serializer;
52import org.onosproject.store.service.StorageService;
53import org.onosproject.store.service.Versioned;
54import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.LinkedHashMap;
58import java.util.Map;
59import java.util.Objects;
60import java.util.Set;
61
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080062import static com.google.common.base.Preconditions.checkArgument;
Jonathan Hartb11c4d02016-03-23 09:05:44 -070063import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED;
64import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
65import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED;
66import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
67import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070068
69/**
70 * Implementation of a distributed network configuration store.
71 */
72@Component(immediate = true)
73@Service
74public class DistributedNetworkConfigStore
75 extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate>
76 implements NetworkConfigStore {
77
78 private final Logger log = LoggerFactory.getLogger(getClass());
79
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080080 private static final String INVALID_CONFIG_JSON =
81 "JSON node does not contain valid configuration";
Jonathan Hartb11c4d02016-03-23 09:05:44 -070082 private static final String INVALID_JSON_LIST =
83 "JSON node is not a list for list type config";
84 private static final String INVALID_JSON_OBJECT =
85 "JSON node is not an object for object type config";
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080086
Thomas Vachuska96d55b12015-05-11 08:52:03 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected StorageService storageService;
89
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070090 private ConsistentMap<ConfigKey, JsonNode> configs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091
92 private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap();
93 private final ObjectMapper mapper = new ObjectMapper();
94 private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate();
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070095 private final MapEventListener<ConfigKey, JsonNode> listener = new InternalMapListener();
Thomas Vachuska96d55b12015-05-11 08:52:03 -070096
97 @Activate
98 public void activate() {
99 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
100 .register(KryoNamespaces.API)
Jonathan Hart111b42b2015-07-14 13:28:05 -0700101 .register(ConfigKey.class, ObjectNode.class, ArrayNode.class,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700102 JsonNodeFactory.class, LinkedHashMap.class,
103 TextNode.class, BooleanNode.class,
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800104 LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class,
105 NullNode.class);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700106
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700107 configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700108 .withSerializer(Serializer.using(kryoBuilder.build()))
109 .withName("onos-network-configs")
Madan Jampani3d6a2f62015-08-12 07:19:07 -0700110 .withRelaxedReadConsistency()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700111 .build();
112 configs.addListener(listener);
113 log.info("Started");
114 }
115
116 @Deactivate
117 public void deactivate() {
118 configs.removeListener(listener);
119 log.info("Stopped");
120 }
121
122 @Override
123 public void addConfigFactory(ConfigFactory configFactory) {
124 factoriesByConfig.put(configFactory.configClass().getName(), configFactory);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800125 processPendingConfigs(configFactory);
Thomas Vachuskae6360222015-07-21 10:10:36 -0700126 notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(),
127 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700128 }
129
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800130 // Sweep through any pending configurations, validate them and then prune them.
131 private void processPendingConfigs(ConfigFactory configFactory) {
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800132 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
133 if (Objects.equals(k.configKey, configFactory.configKey()) &&
134 isAssignableFrom(configFactory, k)) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800135 validateConfig(k, configFactory, configs.get(k).value());
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800136 configs.remove(k); // Prune whether valid or not
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800137 }
138 });
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800139 }
140
141 @SuppressWarnings("unchecked")
142 private boolean isAssignableFrom(ConfigFactory configFactory, ConfigKey k) {
143 return configFactory.subjectFactory().subjectClass().isAssignableFrom(k.subject.getClass());
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800144 }
145
146 @SuppressWarnings("unchecked")
147 private void validateConfig(ConfigKey key, ConfigFactory configFactory, JsonNode json) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800148 Object subject;
149 if (key.subject instanceof String) {
150 subject = configFactory.subjectFactory().createSubject((String) key.subject);
151 } else {
152 subject = key.subject;
153 }
154 Config config = createConfig(subject, configFactory.configClass(), json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800155 try {
156 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800157 configs.putAndGet(key(subject, configFactory.configClass()), json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800158 } catch (Exception e) {
159 log.warn("Failed to validate pending {} configuration for {}: {}",
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800160 key.configKey, key.subject, json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800161 }
162 }
163
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700164 @Override
165 public void removeConfigFactory(ConfigFactory configFactory) {
166 factoriesByConfig.remove(configFactory.configClass().getName());
Jonathan Hart73518ac2016-05-20 08:00:22 -0700167 processExistingConfigs(configFactory);
Thomas Vachuskae6360222015-07-21 10:10:36 -0700168 notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
169 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700170 }
171
Jonathan Hart73518ac2016-05-20 08:00:22 -0700172 // Sweep through any configurations for the config factory, set back to pending state.
173 private void processExistingConfigs(ConfigFactory configFactory) {
174 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
175 if (Objects.equals(configFactory.configClass().getName(), k.configClass)) {
Ray Milkey38809282016-07-07 14:36:23 -0700176 Versioned<JsonNode> remove = configs.remove(k);
177 if (remove != null) {
178 JsonNode json = remove.value();
179 configs.put(key(k.subject, configFactory.configKey()), json);
180 log.debug("Set config pending: {}, {}", k.subject, k.configClass);
181 }
Jonathan Hart73518ac2016-05-20 08:00:22 -0700182 }
183 });
184 }
185
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700186 @Override
187 @SuppressWarnings("unchecked")
188 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800189 return factoriesByConfig.get(configClass.getName());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700190 }
191
192 @Override
193 @SuppressWarnings("unchecked")
194 public <S> Set<S> getSubjects(Class<S> subjectClass) {
195 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
196 configs.keySet().forEach(k -> {
197 if (subjectClass.isInstance(k.subject)) {
198 builder.add((S) k.subject);
199 }
200 });
201 return builder.build();
202 }
203
204 @Override
205 @SuppressWarnings("unchecked")
206 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
207 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
208 String cName = configClass.getName();
209 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800210 if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700211 builder.add((S) k.subject);
212 }
213 });
214 return builder.build();
215 }
216
217 @Override
218 @SuppressWarnings("unchecked")
219 public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) {
220 ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder();
221 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800222 if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) {
Jonathan Hart80fe4422016-05-24 18:47:37 -0700223 ConfigFactory<S, ? extends Config<S>> configFactory = factoriesByConfig.get(k.configClass);
224 if (configFactory == null) {
225 log.error("Found config but no config factory: subject={}, configClass={}",
226 subject, k.configClass);
227 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700228 builder.add(factoriesByConfig.get(k.configClass).configClass());
229 }
230 });
231 return builder.build();
232 }
233
234 @Override
235 public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
Madan Jampanic6371882016-06-03 21:30:17 -0700236 Versioned<JsonNode> json = configs.get(key(subject, configClass));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700237 return json != null ? createConfig(subject, configClass, json.value()) : null;
238 }
239
240
241 @Override
242 public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700243 ConfigFactory<S, C> factory = getConfigFactory(configClass);
244 Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass),
245 k -> factory.isList() ?
246 mapper.createArrayNode() :
247 mapper.createObjectNode());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700248 return createConfig(subject, configClass, json.value());
249 }
250
251 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700252 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800253 // Create the configuration and validate it.
254 C config = createConfig(subject, configClass, json);
Jonathan Hart54b83e82016-03-26 20:37:20 -0700255
256 try {
257 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
258 } catch (RuntimeException e) {
259 ConfigFactory<S, C> configFactory = getConfigFactory(configClass);
260 String subjectKey = configFactory.subjectFactory().subjectClassKey();
261 String subjectString = configFactory.subjectFactory().subjectKey(config.subject());
262 String configKey = config.key();
263
264 throw new InvalidConfigException(subjectKey, subjectString, configKey, e);
265 }
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800266
267 // Insert the validated configuration and get it back.
268 Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
269
270 // Re-create the config if for some reason what we attempted to put
271 // was supplanted by someone else already.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800272 return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value());
273 }
274
275 @Override
276 public <S> void queueConfig(S subject, String configKey, JsonNode json) {
277 configs.put(key(subject, configKey), json);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700278 }
279
280 @Override
281 public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) {
282 configs.remove(key(subject, configClass));
283 }
284
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800285 @Override
286 public <S> void clearQueuedConfig(S subject, String configKey) {
287 configs.remove(key(subject, configKey));
288 }
289
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530290 @Override
291 public <S> void clearConfig(S subject) {
292 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
293 if (Objects.equals(subject, k.subject) && delegate != null) {
294 configs.remove(k);
295 }
296 });
297 }
298
299 @Override
300 public <S> void clearConfig() {
301 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
302 if (delegate != null) {
303 configs.remove(k);
304 }
305 });
306 }
307
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700308 /**
309 * Produces a config from the specified subject, config class and raw JSON.
310 *
311 * @param subject config subject
312 * @param configClass config class
313 * @param json raw JSON data
314 * @return config object or null of no factory found or if the specified
315 * JSON is null
316 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700317 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700318 JsonNode json) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700319 return createConfig(subject, configClass, json, false);
320 }
321
322 /**
323 * Produces a config from the specified subject, config class and raw JSON.
324 *
325 * The config can optionally be detached, which means it does not contain a
326 * reference to an apply delegate. This means a detached config can not be
327 * applied. This should be used only for passing the config object in the
328 * NetworkConfigEvent.
329 *
330 * @param subject config subject
331 * @param configClass config class
332 * @param json raw JSON data
333 * @param detached whether the config should be detached, that is, should
334 * be created without setting an apply delegate.
335 * @return config object or null of no factory found or if the specified
336 * JSON is null
337 */
338 @SuppressWarnings("unchecked")
339 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
340 JsonNode json, boolean detached) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700341 if (json != null) {
342 ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
343 if (factory != null) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700344 validateJsonType(json, factory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700345 C config = factory.createConfig();
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700346 config.init(subject, factory.configKey(), json, mapper,
347 detached ? null : applyDelegate);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700348 return config;
349 }
350 }
351 return null;
352 }
353
Charles Chan023a8982016-02-04 11:00:41 -0800354 /**
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700355 * Validates that the type of the JSON node is appropriate for the type of
356 * configuration. A list type configuration must be created with an
357 * ArrayNode, and an object type configuration must be created with an
358 * ObjectNode.
Charles Chan023a8982016-02-04 11:00:41 -0800359 *
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700360 * @param json JSON node to check
361 * @param factory config factory of configuration
362 * @param <S> subject
363 * @param <C> configuration
364 * @return true if the JSON node type is appropriate for the configuration
Charles Chan023a8982016-02-04 11:00:41 -0800365 */
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700366 private <S, C extends Config<S>> boolean validateJsonType(JsonNode json,
367 ConfigFactory<S, C> factory) {
368 if (factory.isList() && !(json instanceof ArrayNode)) {
369 throw new IllegalArgumentException(INVALID_JSON_LIST);
Charles Chan023a8982016-02-04 11:00:41 -0800370 }
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700371 if (!factory.isList() && !(json instanceof ObjectNode)) {
372 throw new IllegalArgumentException(INVALID_JSON_OBJECT);
373 }
374
375 return true;
Charles Chan023a8982016-02-04 11:00:41 -0800376 }
377
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700378
379 // Auxiliary delegate to receive notifications about changes applied to
380 // the network configuration - by the apps.
381 private class InternalApplyDelegate implements ConfigApplyDelegate {
382 @Override
383 public void onApply(Config config) {
384 configs.put(key(config.subject(), config.getClass()), config.node());
385 }
386 }
387
388 // Produces a key for uniquely tracking a subject config.
389 private static ConfigKey key(Object subject, Class<?> configClass) {
390 return new ConfigKey(subject, configClass);
391 }
392
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800393 // Produces a key for uniquely tracking a subject config.
394 private static ConfigKey key(Object subject, String configKey) {
395 return new ConfigKey(subject, configKey);
396 }
397
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700398 // Auxiliary key to track subject configurations.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800399 // Keys with non-null configKey are pending configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700400 private static final class ConfigKey {
401 final Object subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800402 final String configKey;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700403 final String configClass;
404
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800405 // Create a key for pending configuration class
406 private ConfigKey(Object subject, String configKey) {
407 this.subject = subject;
408 this.configKey = configKey;
409 this.configClass = null;
410 }
411
412 // Create a key for registered class configuration
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700413 private ConfigKey(Object subject, Class<?> configClass) {
414 this.subject = subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800415 this.configKey = null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700416 this.configClass = configClass.getName();
417 }
418
419 @Override
420 public int hashCode() {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800421 return Objects.hash(subject, configKey, configClass);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700422 }
423
424 @Override
425 public boolean equals(Object obj) {
426 if (this == obj) {
427 return true;
428 }
429 if (obj instanceof ConfigKey) {
430 final ConfigKey other = (ConfigKey) obj;
431 return Objects.equals(this.subject, other.subject)
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800432 && Objects.equals(this.configKey, other.configKey)
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700433 && Objects.equals(this.configClass, other.configClass);
434 }
435 return false;
436 }
437 }
438
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700439 private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700440 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700441 public void event(MapEvent<ConfigKey, JsonNode> event) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800442 // Do not delegate pending configs.
443 if (event.key().configClass == null) {
444 return;
445 }
446
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700447 ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
448 if (factory != null) {
Charles Chan023a8982016-02-04 11:00:41 -0800449 Object subject = event.key().subject;
450 Class configClass = factory.configClass();
451 Versioned<JsonNode> newValue = event.newValue();
452 Versioned<JsonNode> oldValue = event.oldValue();
453
454 Config config = (newValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700455 createConfig(subject, configClass, newValue.value(), true) :
456 null;
Charles Chan023a8982016-02-04 11:00:41 -0800457 Config prevConfig = (oldValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700458 createConfig(subject, configClass, oldValue.value(), true) :
459 null;
Charles Chan023a8982016-02-04 11:00:41 -0800460
461 NetworkConfigEvent.Type type;
462 switch (event.type()) {
463 case INSERT:
464 type = CONFIG_ADDED;
465 break;
466 case UPDATE:
467 type = CONFIG_UPDATED;
468 break;
469 case REMOVE:
470 default:
471 type = CONFIG_REMOVED;
472 break;
473 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700474 notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
Charles Chan023a8982016-02-04 11:00:41 -0800475 config, prevConfig, factory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700476 }
477 }
478 }
479}