blob: 7a812c974bebf9321e0df9c786d1a0a2a5cc5f3a [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)) {
176 JsonNode json = configs.remove(k).value();
177 configs.put(key(k.subject, configFactory.configKey()), json);
178 log.debug("Set config pending: {}, {}", k.subject, k.configClass);
179 }
180 });
181 }
182
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700183 @Override
184 @SuppressWarnings("unchecked")
185 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800186 return factoriesByConfig.get(configClass.getName());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700187 }
188
189 @Override
190 @SuppressWarnings("unchecked")
191 public <S> Set<S> getSubjects(Class<S> subjectClass) {
192 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
193 configs.keySet().forEach(k -> {
194 if (subjectClass.isInstance(k.subject)) {
195 builder.add((S) k.subject);
196 }
197 });
198 return builder.build();
199 }
200
201 @Override
202 @SuppressWarnings("unchecked")
203 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
204 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
205 String cName = configClass.getName();
206 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800207 if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700208 builder.add((S) k.subject);
209 }
210 });
211 return builder.build();
212 }
213
214 @Override
215 @SuppressWarnings("unchecked")
216 public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) {
217 ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder();
218 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800219 if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) {
Jonathan Hart80fe4422016-05-24 18:47:37 -0700220 ConfigFactory<S, ? extends Config<S>> configFactory = factoriesByConfig.get(k.configClass);
221 if (configFactory == null) {
222 log.error("Found config but no config factory: subject={}, configClass={}",
223 subject, k.configClass);
224 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700225 builder.add(factoriesByConfig.get(k.configClass).configClass());
226 }
227 });
228 return builder.build();
229 }
230
231 @Override
232 public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
Madan Jampanic6371882016-06-03 21:30:17 -0700233 Versioned<JsonNode> json = configs.get(key(subject, configClass));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700234 return json != null ? createConfig(subject, configClass, json.value()) : null;
235 }
236
237
238 @Override
239 public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700240 ConfigFactory<S, C> factory = getConfigFactory(configClass);
241 Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass),
242 k -> factory.isList() ?
243 mapper.createArrayNode() :
244 mapper.createObjectNode());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700245 return createConfig(subject, configClass, json.value());
246 }
247
248 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700249 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800250 // Create the configuration and validate it.
251 C config = createConfig(subject, configClass, json);
Jonathan Hart54b83e82016-03-26 20:37:20 -0700252
253 try {
254 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
255 } catch (RuntimeException e) {
256 ConfigFactory<S, C> configFactory = getConfigFactory(configClass);
257 String subjectKey = configFactory.subjectFactory().subjectClassKey();
258 String subjectString = configFactory.subjectFactory().subjectKey(config.subject());
259 String configKey = config.key();
260
261 throw new InvalidConfigException(subjectKey, subjectString, configKey, e);
262 }
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800263
264 // Insert the validated configuration and get it back.
265 Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
266
267 // Re-create the config if for some reason what we attempted to put
268 // was supplanted by someone else already.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800269 return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value());
270 }
271
272 @Override
273 public <S> void queueConfig(S subject, String configKey, JsonNode json) {
274 configs.put(key(subject, configKey), json);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700275 }
276
277 @Override
278 public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) {
279 configs.remove(key(subject, configClass));
280 }
281
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800282 @Override
283 public <S> void clearQueuedConfig(S subject, String configKey) {
284 configs.remove(key(subject, configKey));
285 }
286
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530287 @Override
288 public <S> void clearConfig(S subject) {
289 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
290 if (Objects.equals(subject, k.subject) && delegate != null) {
291 configs.remove(k);
292 }
293 });
294 }
295
296 @Override
297 public <S> void clearConfig() {
298 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
299 if (delegate != null) {
300 configs.remove(k);
301 }
302 });
303 }
304
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700305 /**
306 * Produces a config from the specified subject, config class and raw JSON.
307 *
308 * @param subject config subject
309 * @param configClass config class
310 * @param json raw JSON data
311 * @return config object or null of no factory found or if the specified
312 * JSON is null
313 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700314 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700315 JsonNode json) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700316 return createConfig(subject, configClass, json, false);
317 }
318
319 /**
320 * Produces a config from the specified subject, config class and raw JSON.
321 *
322 * The config can optionally be detached, which means it does not contain a
323 * reference to an apply delegate. This means a detached config can not be
324 * applied. This should be used only for passing the config object in the
325 * NetworkConfigEvent.
326 *
327 * @param subject config subject
328 * @param configClass config class
329 * @param json raw JSON data
330 * @param detached whether the config should be detached, that is, should
331 * be created without setting an apply delegate.
332 * @return config object or null of no factory found or if the specified
333 * JSON is null
334 */
335 @SuppressWarnings("unchecked")
336 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
337 JsonNode json, boolean detached) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700338 if (json != null) {
339 ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
340 if (factory != null) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700341 validateJsonType(json, factory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700342 C config = factory.createConfig();
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700343 config.init(subject, factory.configKey(), json, mapper,
344 detached ? null : applyDelegate);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700345 return config;
346 }
347 }
348 return null;
349 }
350
Charles Chan023a8982016-02-04 11:00:41 -0800351 /**
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700352 * Validates that the type of the JSON node is appropriate for the type of
353 * configuration. A list type configuration must be created with an
354 * ArrayNode, and an object type configuration must be created with an
355 * ObjectNode.
Charles Chan023a8982016-02-04 11:00:41 -0800356 *
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700357 * @param json JSON node to check
358 * @param factory config factory of configuration
359 * @param <S> subject
360 * @param <C> configuration
361 * @return true if the JSON node type is appropriate for the configuration
Charles Chan023a8982016-02-04 11:00:41 -0800362 */
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700363 private <S, C extends Config<S>> boolean validateJsonType(JsonNode json,
364 ConfigFactory<S, C> factory) {
365 if (factory.isList() && !(json instanceof ArrayNode)) {
366 throw new IllegalArgumentException(INVALID_JSON_LIST);
Charles Chan023a8982016-02-04 11:00:41 -0800367 }
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700368 if (!factory.isList() && !(json instanceof ObjectNode)) {
369 throw new IllegalArgumentException(INVALID_JSON_OBJECT);
370 }
371
372 return true;
Charles Chan023a8982016-02-04 11:00:41 -0800373 }
374
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700375
376 // Auxiliary delegate to receive notifications about changes applied to
377 // the network configuration - by the apps.
378 private class InternalApplyDelegate implements ConfigApplyDelegate {
379 @Override
380 public void onApply(Config config) {
381 configs.put(key(config.subject(), config.getClass()), config.node());
382 }
383 }
384
385 // Produces a key for uniquely tracking a subject config.
386 private static ConfigKey key(Object subject, Class<?> configClass) {
387 return new ConfigKey(subject, configClass);
388 }
389
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800390 // Produces a key for uniquely tracking a subject config.
391 private static ConfigKey key(Object subject, String configKey) {
392 return new ConfigKey(subject, configKey);
393 }
394
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700395 // Auxiliary key to track subject configurations.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800396 // Keys with non-null configKey are pending configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700397 private static final class ConfigKey {
398 final Object subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800399 final String configKey;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700400 final String configClass;
401
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800402 // Create a key for pending configuration class
403 private ConfigKey(Object subject, String configKey) {
404 this.subject = subject;
405 this.configKey = configKey;
406 this.configClass = null;
407 }
408
409 // Create a key for registered class configuration
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700410 private ConfigKey(Object subject, Class<?> configClass) {
411 this.subject = subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800412 this.configKey = null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700413 this.configClass = configClass.getName();
414 }
415
416 @Override
417 public int hashCode() {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800418 return Objects.hash(subject, configKey, configClass);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700419 }
420
421 @Override
422 public boolean equals(Object obj) {
423 if (this == obj) {
424 return true;
425 }
426 if (obj instanceof ConfigKey) {
427 final ConfigKey other = (ConfigKey) obj;
428 return Objects.equals(this.subject, other.subject)
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800429 && Objects.equals(this.configKey, other.configKey)
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700430 && Objects.equals(this.configClass, other.configClass);
431 }
432 return false;
433 }
434 }
435
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700436 private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700437 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700438 public void event(MapEvent<ConfigKey, JsonNode> event) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800439 // Do not delegate pending configs.
440 if (event.key().configClass == null) {
441 return;
442 }
443
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700444 ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
445 if (factory != null) {
Charles Chan023a8982016-02-04 11:00:41 -0800446 Object subject = event.key().subject;
447 Class configClass = factory.configClass();
448 Versioned<JsonNode> newValue = event.newValue();
449 Versioned<JsonNode> oldValue = event.oldValue();
450
451 Config config = (newValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700452 createConfig(subject, configClass, newValue.value(), true) :
453 null;
Charles Chan023a8982016-02-04 11:00:41 -0800454 Config prevConfig = (oldValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700455 createConfig(subject, configClass, oldValue.value(), true) :
456 null;
Charles Chan023a8982016-02-04 11:00:41 -0800457
458 NetworkConfigEvent.Type type;
459 switch (event.type()) {
460 case INSERT:
461 type = CONFIG_ADDED;
462 break;
463 case UPDATE:
464 type = CONFIG_UPDATED;
465 break;
466 case REMOVE:
467 default:
468 type = CONFIG_REMOVED;
469 break;
470 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700471 notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
Charles Chan023a8982016-02-04 11:00:41 -0800472 config, prevConfig, factory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700473 }
474 }
475 }
476}