blob: 87148a85e94c4922ed8c57b4696c9c8f3e8c24d0 [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
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700287 /**
288 * Produces a config from the specified subject, config class and raw JSON.
289 *
290 * @param subject config subject
291 * @param configClass config class
292 * @param json raw JSON data
293 * @return config object or null of no factory found or if the specified
294 * JSON is null
295 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700296 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700297 JsonNode json) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700298 return createConfig(subject, configClass, json, false);
299 }
300
301 /**
302 * Produces a config from the specified subject, config class and raw JSON.
303 *
304 * The config can optionally be detached, which means it does not contain a
305 * reference to an apply delegate. This means a detached config can not be
306 * applied. This should be used only for passing the config object in the
307 * NetworkConfigEvent.
308 *
309 * @param subject config subject
310 * @param configClass config class
311 * @param json raw JSON data
312 * @param detached whether the config should be detached, that is, should
313 * be created without setting an apply delegate.
314 * @return config object or null of no factory found or if the specified
315 * JSON is null
316 */
317 @SuppressWarnings("unchecked")
318 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
319 JsonNode json, boolean detached) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700320 if (json != null) {
321 ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
322 if (factory != null) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700323 validateJsonType(json, factory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700324 C config = factory.createConfig();
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700325 config.init(subject, factory.configKey(), json, mapper,
326 detached ? null : applyDelegate);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700327 return config;
328 }
329 }
330 return null;
331 }
332
Charles Chan023a8982016-02-04 11:00:41 -0800333 /**
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700334 * Validates that the type of the JSON node is appropriate for the type of
335 * configuration. A list type configuration must be created with an
336 * ArrayNode, and an object type configuration must be created with an
337 * ObjectNode.
Charles Chan023a8982016-02-04 11:00:41 -0800338 *
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700339 * @param json JSON node to check
340 * @param factory config factory of configuration
341 * @param <S> subject
342 * @param <C> configuration
343 * @return true if the JSON node type is appropriate for the configuration
Charles Chan023a8982016-02-04 11:00:41 -0800344 */
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700345 private <S, C extends Config<S>> boolean validateJsonType(JsonNode json,
346 ConfigFactory<S, C> factory) {
347 if (factory.isList() && !(json instanceof ArrayNode)) {
348 throw new IllegalArgumentException(INVALID_JSON_LIST);
Charles Chan023a8982016-02-04 11:00:41 -0800349 }
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700350 if (!factory.isList() && !(json instanceof ObjectNode)) {
351 throw new IllegalArgumentException(INVALID_JSON_OBJECT);
352 }
353
354 return true;
Charles Chan023a8982016-02-04 11:00:41 -0800355 }
356
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700357
358 // Auxiliary delegate to receive notifications about changes applied to
359 // the network configuration - by the apps.
360 private class InternalApplyDelegate implements ConfigApplyDelegate {
361 @Override
362 public void onApply(Config config) {
363 configs.put(key(config.subject(), config.getClass()), config.node());
364 }
365 }
366
367 // Produces a key for uniquely tracking a subject config.
368 private static ConfigKey key(Object subject, Class<?> configClass) {
369 return new ConfigKey(subject, configClass);
370 }
371
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800372 // Produces a key for uniquely tracking a subject config.
373 private static ConfigKey key(Object subject, String configKey) {
374 return new ConfigKey(subject, configKey);
375 }
376
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700377 // Auxiliary key to track subject configurations.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800378 // Keys with non-null configKey are pending configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700379 private static final class ConfigKey {
380 final Object subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800381 final String configKey;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700382 final String configClass;
383
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800384 // Create a key for pending configuration class
385 private ConfigKey(Object subject, String configKey) {
386 this.subject = subject;
387 this.configKey = configKey;
388 this.configClass = null;
389 }
390
391 // Create a key for registered class configuration
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700392 private ConfigKey(Object subject, Class<?> configClass) {
393 this.subject = subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800394 this.configKey = null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700395 this.configClass = configClass.getName();
396 }
397
398 @Override
399 public int hashCode() {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800400 return Objects.hash(subject, configKey, configClass);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700401 }
402
403 @Override
404 public boolean equals(Object obj) {
405 if (this == obj) {
406 return true;
407 }
408 if (obj instanceof ConfigKey) {
409 final ConfigKey other = (ConfigKey) obj;
410 return Objects.equals(this.subject, other.subject)
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800411 && Objects.equals(this.configKey, other.configKey)
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700412 && Objects.equals(this.configClass, other.configClass);
413 }
414 return false;
415 }
416 }
417
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700418 private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700419 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700420 public void event(MapEvent<ConfigKey, JsonNode> event) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800421 // Do not delegate pending configs.
422 if (event.key().configClass == null) {
423 return;
424 }
425
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700426 ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
427 if (factory != null) {
Charles Chan023a8982016-02-04 11:00:41 -0800428 Object subject = event.key().subject;
429 Class configClass = factory.configClass();
430 Versioned<JsonNode> newValue = event.newValue();
431 Versioned<JsonNode> oldValue = event.oldValue();
432
433 Config config = (newValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700434 createConfig(subject, configClass, newValue.value(), true) :
435 null;
Charles Chan023a8982016-02-04 11:00:41 -0800436 Config prevConfig = (oldValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700437 createConfig(subject, configClass, oldValue.value(), true) :
438 null;
Charles Chan023a8982016-02-04 11:00:41 -0800439
440 NetworkConfigEvent.Type type;
441 switch (event.type()) {
442 case INSERT:
443 type = CONFIG_ADDED;
444 break;
445 case UPDATE:
446 type = CONFIG_UPDATED;
447 break;
448 case REMOVE:
449 default:
450 type = CONFIG_REMOVED;
451 break;
452 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700453 notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
Charles Chan023a8982016-02-04 11:00:41 -0800454 config, prevConfig, factory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700455 }
456 }
457 }
458}