blob: 855eefbb5a2ba1b7e625fe9d207f44d647932b33 [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;
Thomas Vachuska5f2cbe62015-07-30 15:10:34 -070039import org.onlab.util.Tools;
Ray Milkeya4122362015-08-18 15:19:08 -070040import org.onosproject.net.config.Config;
41import org.onosproject.net.config.ConfigApplyDelegate;
42import org.onosproject.net.config.ConfigFactory;
43import 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;
Thomas Vachuska5f2cbe62015-07-30 15:10:34 -070049import org.onosproject.store.service.ConsistentMapException;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070050import org.onosproject.store.service.MapEvent;
51import org.onosproject.store.service.MapEventListener;
52import org.onosproject.store.service.Serializer;
53import org.onosproject.store.service.StorageService;
54import org.onosproject.store.service.Versioned;
55import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import java.util.LinkedHashMap;
59import java.util.Map;
60import java.util.Objects;
61import java.util.Set;
62
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080063import static com.google.common.base.Preconditions.checkArgument;
Jonathan Hartb11c4d02016-03-23 09:05:44 -070064import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED;
65import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
66import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED;
67import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
68import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070069
70/**
71 * Implementation of a distributed network configuration store.
72 */
73@Component(immediate = true)
74@Service
75public class DistributedNetworkConfigStore
76 extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate>
77 implements NetworkConfigStore {
78
79 private final Logger log = LoggerFactory.getLogger(getClass());
80
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080081 private static final int MAX_BACKOFF = 10;
82 private static final String INVALID_CONFIG_JSON =
83 "JSON node does not contain valid configuration";
Jonathan Hartb11c4d02016-03-23 09:05:44 -070084 private static final String INVALID_JSON_LIST =
85 "JSON node is not a list for list type config";
86 private static final String INVALID_JSON_OBJECT =
87 "JSON node is not an object for object type config";
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080088
Thomas Vachuska96d55b12015-05-11 08:52:03 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected StorageService storageService;
91
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070092 private ConsistentMap<ConfigKey, JsonNode> configs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070093
94 private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap();
95 private final ObjectMapper mapper = new ObjectMapper();
96 private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate();
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070097 private final MapEventListener<ConfigKey, JsonNode> listener = new InternalMapListener();
Thomas Vachuska96d55b12015-05-11 08:52:03 -070098
99 @Activate
100 public void activate() {
101 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
102 .register(KryoNamespaces.API)
Jonathan Hart111b42b2015-07-14 13:28:05 -0700103 .register(ConfigKey.class, ObjectNode.class, ArrayNode.class,
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700104 JsonNodeFactory.class, LinkedHashMap.class,
105 TextNode.class, BooleanNode.class,
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800106 LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class,
107 NullNode.class);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700108
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700109 configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700110 .withSerializer(Serializer.using(kryoBuilder.build()))
111 .withName("onos-network-configs")
Madan Jampani3d6a2f62015-08-12 07:19:07 -0700112 .withRelaxedReadConsistency()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700113 .build();
114 configs.addListener(listener);
115 log.info("Started");
116 }
117
118 @Deactivate
119 public void deactivate() {
120 configs.removeListener(listener);
121 log.info("Stopped");
122 }
123
124 @Override
125 public void addConfigFactory(ConfigFactory configFactory) {
126 factoriesByConfig.put(configFactory.configClass().getName(), configFactory);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800127 processPendingConfigs(configFactory);
Thomas Vachuskae6360222015-07-21 10:10:36 -0700128 notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(),
129 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700130 }
131
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800132 // Sweep through any pending configurations, validate them and then prune them.
133 private void processPendingConfigs(ConfigFactory configFactory) {
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800134 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
135 if (Objects.equals(k.configKey, configFactory.configKey()) &&
136 isAssignableFrom(configFactory, k)) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800137 validateConfig(k, configFactory, configs.get(k).value());
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800138 configs.remove(k); // Prune whether valid or not
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800139 }
140 });
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800141 }
142
143 @SuppressWarnings("unchecked")
144 private boolean isAssignableFrom(ConfigFactory configFactory, ConfigKey k) {
145 return configFactory.subjectFactory().subjectClass().isAssignableFrom(k.subject.getClass());
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800146 }
147
148 @SuppressWarnings("unchecked")
149 private void validateConfig(ConfigKey key, ConfigFactory configFactory, JsonNode json) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800150 Object subject;
151 if (key.subject instanceof String) {
152 subject = configFactory.subjectFactory().createSubject((String) key.subject);
153 } else {
154 subject = key.subject;
155 }
156 Config config = createConfig(subject, configFactory.configClass(), json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800157 try {
158 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800159 configs.putAndGet(key(subject, configFactory.configClass()), json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800160 } catch (Exception e) {
161 log.warn("Failed to validate pending {} configuration for {}: {}",
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800162 key.configKey, key.subject, json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800163 }
164 }
165
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700166 @Override
167 public void removeConfigFactory(ConfigFactory configFactory) {
168 factoriesByConfig.remove(configFactory.configClass().getName());
Jonathan Hart73518ac2016-05-20 08:00:22 -0700169 processExistingConfigs(configFactory);
Thomas Vachuskae6360222015-07-21 10:10:36 -0700170 notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
171 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700172 }
173
Jonathan Hart73518ac2016-05-20 08:00:22 -0700174 // Sweep through any configurations for the config factory, set back to pending state.
175 private void processExistingConfigs(ConfigFactory configFactory) {
176 ImmutableSet.copyOf(configs.keySet()).forEach(k -> {
177 if (Objects.equals(configFactory.configClass().getName(), k.configClass)) {
178 JsonNode json = configs.remove(k).value();
179 configs.put(key(k.subject, configFactory.configKey()), json);
180 log.debug("Set config pending: {}, {}", k.subject, k.configClass);
181 }
182 });
183 }
184
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700185 @Override
186 @SuppressWarnings("unchecked")
187 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800188 return factoriesByConfig.get(configClass.getName());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700189 }
190
191 @Override
192 @SuppressWarnings("unchecked")
193 public <S> Set<S> getSubjects(Class<S> subjectClass) {
194 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
195 configs.keySet().forEach(k -> {
196 if (subjectClass.isInstance(k.subject)) {
197 builder.add((S) k.subject);
198 }
199 });
200 return builder.build();
201 }
202
203 @Override
204 @SuppressWarnings("unchecked")
205 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
206 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
207 String cName = configClass.getName();
208 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800209 if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700210 builder.add((S) k.subject);
211 }
212 });
213 return builder.build();
214 }
215
216 @Override
217 @SuppressWarnings("unchecked")
218 public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) {
219 ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder();
220 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800221 if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) {
Jonathan Hart80fe4422016-05-24 18:47:37 -0700222 ConfigFactory<S, ? extends Config<S>> configFactory = factoriesByConfig.get(k.configClass);
223 if (configFactory == null) {
224 log.error("Found config but no config factory: subject={}, configClass={}",
225 subject, k.configClass);
226 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700227 builder.add(factoriesByConfig.get(k.configClass).configClass());
228 }
229 });
230 return builder.build();
231 }
232
233 @Override
234 public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
Madan Jampania29c6772015-08-17 13:17:07 -0700235 // TODO: need to identify and address the root cause for timeouts.
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700236 Versioned<JsonNode> json = Tools.retryable(configs::get, ConsistentMapException.class, 1, MAX_BACKOFF)
237 .apply(key(subject, configClass));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700238 return json != null ? createConfig(subject, configClass, json.value()) : null;
239 }
240
241
242 @Override
243 public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700244 ConfigFactory<S, C> factory = getConfigFactory(configClass);
245 Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass),
246 k -> factory.isList() ?
247 mapper.createArrayNode() :
248 mapper.createObjectNode());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700249 return createConfig(subject, configClass, json.value());
250 }
251
252 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700253 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800254 // Create the configuration and validate it.
255 C config = createConfig(subject, configClass, json);
256 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
257
258 // Insert the validated configuration and get it back.
259 Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
260
261 // Re-create the config if for some reason what we attempted to put
262 // was supplanted by someone else already.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800263 return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value());
264 }
265
266 @Override
267 public <S> void queueConfig(S subject, String configKey, JsonNode json) {
268 configs.put(key(subject, configKey), json);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700269 }
270
271 @Override
272 public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) {
273 configs.remove(key(subject, configClass));
274 }
275
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800276 @Override
277 public <S> void clearQueuedConfig(S subject, String configKey) {
278 configs.remove(key(subject, configKey));
279 }
280
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700281 /**
282 * Produces a config from the specified subject, config class and raw JSON.
283 *
284 * @param subject config subject
285 * @param configClass config class
286 * @param json raw JSON data
287 * @return config object or null of no factory found or if the specified
288 * JSON is null
289 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700290 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700291 JsonNode json) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700292 return createConfig(subject, configClass, json, false);
293 }
294
295 /**
296 * Produces a config from the specified subject, config class and raw JSON.
297 *
298 * The config can optionally be detached, which means it does not contain a
299 * reference to an apply delegate. This means a detached config can not be
300 * applied. This should be used only for passing the config object in the
301 * NetworkConfigEvent.
302 *
303 * @param subject config subject
304 * @param configClass config class
305 * @param json raw JSON data
306 * @param detached whether the config should be detached, that is, should
307 * be created without setting an apply delegate.
308 * @return config object or null of no factory found or if the specified
309 * JSON is null
310 */
311 @SuppressWarnings("unchecked")
312 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
313 JsonNode json, boolean detached) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700314 if (json != null) {
315 ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
316 if (factory != null) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700317 validateJsonType(json, factory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700318 C config = factory.createConfig();
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700319 config.init(subject, factory.configKey(), json, mapper,
320 detached ? null : applyDelegate);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700321 return config;
322 }
323 }
324 return null;
325 }
326
Charles Chan023a8982016-02-04 11:00:41 -0800327 /**
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700328 * Validates that the type of the JSON node is appropriate for the type of
329 * configuration. A list type configuration must be created with an
330 * ArrayNode, and an object type configuration must be created with an
331 * ObjectNode.
Charles Chan023a8982016-02-04 11:00:41 -0800332 *
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700333 * @param json JSON node to check
334 * @param factory config factory of configuration
335 * @param <S> subject
336 * @param <C> configuration
337 * @return true if the JSON node type is appropriate for the configuration
Charles Chan023a8982016-02-04 11:00:41 -0800338 */
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700339 private <S, C extends Config<S>> boolean validateJsonType(JsonNode json,
340 ConfigFactory<S, C> factory) {
341 if (factory.isList() && !(json instanceof ArrayNode)) {
342 throw new IllegalArgumentException(INVALID_JSON_LIST);
Charles Chan023a8982016-02-04 11:00:41 -0800343 }
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700344 if (!factory.isList() && !(json instanceof ObjectNode)) {
345 throw new IllegalArgumentException(INVALID_JSON_OBJECT);
346 }
347
348 return true;
Charles Chan023a8982016-02-04 11:00:41 -0800349 }
350
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700351
352 // Auxiliary delegate to receive notifications about changes applied to
353 // the network configuration - by the apps.
354 private class InternalApplyDelegate implements ConfigApplyDelegate {
355 @Override
356 public void onApply(Config config) {
357 configs.put(key(config.subject(), config.getClass()), config.node());
358 }
359 }
360
361 // Produces a key for uniquely tracking a subject config.
362 private static ConfigKey key(Object subject, Class<?> configClass) {
363 return new ConfigKey(subject, configClass);
364 }
365
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800366 // Produces a key for uniquely tracking a subject config.
367 private static ConfigKey key(Object subject, String configKey) {
368 return new ConfigKey(subject, configKey);
369 }
370
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700371 // Auxiliary key to track subject configurations.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800372 // Keys with non-null configKey are pending configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700373 private static final class ConfigKey {
374 final Object subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800375 final String configKey;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700376 final String configClass;
377
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800378 // Create a key for pending configuration class
379 private ConfigKey(Object subject, String configKey) {
380 this.subject = subject;
381 this.configKey = configKey;
382 this.configClass = null;
383 }
384
385 // Create a key for registered class configuration
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700386 private ConfigKey(Object subject, Class<?> configClass) {
387 this.subject = subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800388 this.configKey = null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700389 this.configClass = configClass.getName();
390 }
391
392 @Override
393 public int hashCode() {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800394 return Objects.hash(subject, configKey, configClass);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700395 }
396
397 @Override
398 public boolean equals(Object obj) {
399 if (this == obj) {
400 return true;
401 }
402 if (obj instanceof ConfigKey) {
403 final ConfigKey other = (ConfigKey) obj;
404 return Objects.equals(this.subject, other.subject)
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800405 && Objects.equals(this.configKey, other.configKey)
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700406 && Objects.equals(this.configClass, other.configClass);
407 }
408 return false;
409 }
410 }
411
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700412 private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700413 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700414 public void event(MapEvent<ConfigKey, JsonNode> event) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800415 // Do not delegate pending configs.
416 if (event.key().configClass == null) {
417 return;
418 }
419
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700420 ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
421 if (factory != null) {
Charles Chan023a8982016-02-04 11:00:41 -0800422 Object subject = event.key().subject;
423 Class configClass = factory.configClass();
424 Versioned<JsonNode> newValue = event.newValue();
425 Versioned<JsonNode> oldValue = event.oldValue();
426
427 Config config = (newValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700428 createConfig(subject, configClass, newValue.value(), true) :
429 null;
Charles Chan023a8982016-02-04 11:00:41 -0800430 Config prevConfig = (oldValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700431 createConfig(subject, configClass, oldValue.value(), true) :
432 null;
Charles Chan023a8982016-02-04 11:00:41 -0800433
434 NetworkConfigEvent.Type type;
435 switch (event.type()) {
436 case INSERT:
437 type = CONFIG_ADDED;
438 break;
439 case UPDATE:
440 type = CONFIG_UPDATED;
441 break;
442 case REMOVE:
443 default:
444 type = CONFIG_REMOVED;
445 break;
446 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700447 notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
Charles Chan023a8982016-02-04 11:00:41 -0800448 config, prevConfig, factory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700449 }
450 }
451 }
452}