blob: 7c2081e5578d5dc3af34cbcd6a8f738c75f9b318 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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) {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700132 configs.keySet().forEach(k -> {
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800133 if (Objects.equals(k.configKey, configFactory.configKey()) &&
134 isAssignableFrom(configFactory, k)) {
Thomas Vachuska95148362016-07-13 14:23:27 -0700135 // Prune whether valid or not
136 Versioned<JsonNode> versioned = configs.remove(k);
137 // Allow for the value to be processed by another node already
138 if (versioned != null) {
139 validateConfig(k, configFactory, versioned.value());
140 }
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800141 }
142 });
Thomas Vachuska096bcc82016-03-07 21:30:29 -0800143 }
144
145 @SuppressWarnings("unchecked")
146 private boolean isAssignableFrom(ConfigFactory configFactory, ConfigKey k) {
147 return configFactory.subjectFactory().subjectClass().isAssignableFrom(k.subject.getClass());
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800148 }
149
150 @SuppressWarnings("unchecked")
151 private void validateConfig(ConfigKey key, ConfigFactory configFactory, JsonNode json) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800152 Object subject;
153 if (key.subject instanceof String) {
154 subject = configFactory.subjectFactory().createSubject((String) key.subject);
155 } else {
156 subject = key.subject;
157 }
158 Config config = createConfig(subject, configFactory.configClass(), json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800159 try {
160 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800161 configs.putAndGet(key(subject, configFactory.configClass()), json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800162 } catch (Exception e) {
163 log.warn("Failed to validate pending {} configuration for {}: {}",
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800164 key.configKey, key.subject, json);
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800165 }
166 }
167
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700168 @Override
169 public void removeConfigFactory(ConfigFactory configFactory) {
170 factoriesByConfig.remove(configFactory.configClass().getName());
Jonathan Hart73518ac2016-05-20 08:00:22 -0700171 processExistingConfigs(configFactory);
Thomas Vachuskae6360222015-07-21 10:10:36 -0700172 notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
173 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700174 }
175
Jonathan Hart73518ac2016-05-20 08:00:22 -0700176 // Sweep through any configurations for the config factory, set back to pending state.
177 private void processExistingConfigs(ConfigFactory configFactory) {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700178 configs.keySet().forEach(k -> {
Jonathan Hart73518ac2016-05-20 08:00:22 -0700179 if (Objects.equals(configFactory.configClass().getName(), k.configClass)) {
Ray Milkey38809282016-07-07 14:36:23 -0700180 Versioned<JsonNode> remove = configs.remove(k);
181 if (remove != null) {
182 JsonNode json = remove.value();
183 configs.put(key(k.subject, configFactory.configKey()), json);
184 log.debug("Set config pending: {}, {}", k.subject, k.configClass);
185 }
Jonathan Hart73518ac2016-05-20 08:00:22 -0700186 }
187 });
188 }
189
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700190 @Override
191 @SuppressWarnings("unchecked")
192 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
HIGUCHI Yutaca2208d2016-02-18 15:03:08 -0800193 return factoriesByConfig.get(configClass.getName());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700194 }
195
196 @Override
197 @SuppressWarnings("unchecked")
198 public <S> Set<S> getSubjects(Class<S> subjectClass) {
199 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
200 configs.keySet().forEach(k -> {
201 if (subjectClass.isInstance(k.subject)) {
202 builder.add((S) k.subject);
203 }
204 });
205 return builder.build();
206 }
207
208 @Override
209 @SuppressWarnings("unchecked")
210 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
211 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
212 String cName = configClass.getName();
213 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800214 if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700215 builder.add((S) k.subject);
216 }
217 });
218 return builder.build();
219 }
220
221 @Override
222 @SuppressWarnings("unchecked")
223 public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) {
224 ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder();
225 configs.keySet().forEach(k -> {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800226 if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) {
Jonathan Hart80fe4422016-05-24 18:47:37 -0700227 ConfigFactory<S, ? extends Config<S>> configFactory = factoriesByConfig.get(k.configClass);
228 if (configFactory == null) {
Thomas Vachuska698ba662016-07-28 11:42:39 -0700229 log.warn("Found config but no config factory: subject={}, configClass={}",
230 subject, k.configClass);
231 } else {
232 builder.add(configFactory.configClass());
Jonathan Hart80fe4422016-05-24 18:47:37 -0700233 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700234 }
235 });
236 return builder.build();
237 }
238
239 @Override
240 public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
Madan Jampanic6371882016-06-03 21:30:17 -0700241 Versioned<JsonNode> json = configs.get(key(subject, configClass));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700242 return json != null ? createConfig(subject, configClass, json.value()) : null;
243 }
244
245
246 @Override
247 public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700248 ConfigFactory<S, C> factory = getConfigFactory(configClass);
249 Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass),
250 k -> factory.isList() ?
251 mapper.createArrayNode() :
252 mapper.createObjectNode());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700253 return createConfig(subject, configClass, json.value());
254 }
255
256 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700257 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800258 // Create the configuration and validate it.
259 C config = createConfig(subject, configClass, json);
Jonathan Hart54b83e82016-03-26 20:37:20 -0700260
261 try {
262 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
263 } catch (RuntimeException e) {
264 ConfigFactory<S, C> configFactory = getConfigFactory(configClass);
265 String subjectKey = configFactory.subjectFactory().subjectClassKey();
266 String subjectString = configFactory.subjectFactory().subjectKey(config.subject());
267 String configKey = config.key();
268
269 throw new InvalidConfigException(subjectKey, subjectString, configKey, e);
270 }
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800271
272 // Insert the validated configuration and get it back.
273 Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
274
275 // Re-create the config if for some reason what we attempted to put
276 // was supplanted by someone else already.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800277 return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value());
278 }
279
280 @Override
281 public <S> void queueConfig(S subject, String configKey, JsonNode json) {
282 configs.put(key(subject, configKey), json);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700283 }
284
285 @Override
286 public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) {
287 configs.remove(key(subject, configClass));
288 }
289
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800290 @Override
291 public <S> void clearQueuedConfig(S subject, String configKey) {
292 configs.remove(key(subject, configKey));
293 }
294
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530295 @Override
296 public <S> void clearConfig(S subject) {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700297 configs.keySet().forEach(k -> {
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530298 if (Objects.equals(subject, k.subject) && delegate != null) {
299 configs.remove(k);
300 }
301 });
302 }
303
304 @Override
305 public <S> void clearConfig() {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700306 configs.keySet().forEach(k -> {
Deepa Vaddireddy0c49b602016-06-02 12:19:07 +0530307 if (delegate != null) {
308 configs.remove(k);
309 }
310 });
311 }
312
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700313 /**
314 * Produces a config from the specified subject, config class and raw JSON.
315 *
316 * @param subject config subject
317 * @param configClass config class
318 * @param json raw JSON data
319 * @return config object or null of no factory found or if the specified
320 * JSON is null
321 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700322 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700323 JsonNode json) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700324 return createConfig(subject, configClass, json, false);
325 }
326
327 /**
328 * Produces a config from the specified subject, config class and raw JSON.
329 *
330 * The config can optionally be detached, which means it does not contain a
331 * reference to an apply delegate. This means a detached config can not be
332 * applied. This should be used only for passing the config object in the
333 * NetworkConfigEvent.
334 *
335 * @param subject config subject
336 * @param configClass config class
337 * @param json raw JSON data
338 * @param detached whether the config should be detached, that is, should
339 * be created without setting an apply delegate.
340 * @return config object or null of no factory found or if the specified
341 * JSON is null
342 */
343 @SuppressWarnings("unchecked")
344 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
345 JsonNode json, boolean detached) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700346 if (json != null) {
347 ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
348 if (factory != null) {
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700349 validateJsonType(json, factory);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700350 C config = factory.createConfig();
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700351 config.init(subject, factory.configKey(), json, mapper,
352 detached ? null : applyDelegate);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700353 return config;
354 }
355 }
356 return null;
357 }
358
Charles Chan023a8982016-02-04 11:00:41 -0800359 /**
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700360 * Validates that the type of the JSON node is appropriate for the type of
361 * configuration. A list type configuration must be created with an
362 * ArrayNode, and an object type configuration must be created with an
363 * ObjectNode.
Charles Chan023a8982016-02-04 11:00:41 -0800364 *
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700365 * @param json JSON node to check
366 * @param factory config factory of configuration
367 * @param <S> subject
368 * @param <C> configuration
369 * @return true if the JSON node type is appropriate for the configuration
Charles Chan023a8982016-02-04 11:00:41 -0800370 */
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700371 private <S, C extends Config<S>> boolean validateJsonType(JsonNode json,
372 ConfigFactory<S, C> factory) {
373 if (factory.isList() && !(json instanceof ArrayNode)) {
374 throw new IllegalArgumentException(INVALID_JSON_LIST);
Charles Chan023a8982016-02-04 11:00:41 -0800375 }
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700376 if (!factory.isList() && !(json instanceof ObjectNode)) {
377 throw new IllegalArgumentException(INVALID_JSON_OBJECT);
378 }
379
380 return true;
Charles Chan023a8982016-02-04 11:00:41 -0800381 }
382
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700383
384 // Auxiliary delegate to receive notifications about changes applied to
385 // the network configuration - by the apps.
386 private class InternalApplyDelegate implements ConfigApplyDelegate {
387 @Override
388 public void onApply(Config config) {
389 configs.put(key(config.subject(), config.getClass()), config.node());
390 }
391 }
392
393 // Produces a key for uniquely tracking a subject config.
394 private static ConfigKey key(Object subject, Class<?> configClass) {
395 return new ConfigKey(subject, configClass);
396 }
397
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800398 // Produces a key for uniquely tracking a subject config.
399 private static ConfigKey key(Object subject, String configKey) {
400 return new ConfigKey(subject, configKey);
401 }
402
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700403 // Auxiliary key to track subject configurations.
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800404 // Keys with non-null configKey are pending configurations.
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700405 private static final class ConfigKey {
406 final Object subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800407 final String configKey;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700408 final String configClass;
409
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800410 // Create a key for pending configuration class
411 private ConfigKey(Object subject, String configKey) {
412 this.subject = subject;
413 this.configKey = configKey;
414 this.configClass = null;
415 }
416
417 // Create a key for registered class configuration
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700418 private ConfigKey(Object subject, Class<?> configClass) {
419 this.subject = subject;
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800420 this.configKey = null;
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700421 this.configClass = configClass.getName();
422 }
423
424 @Override
425 public int hashCode() {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800426 return Objects.hash(subject, configKey, configClass);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700427 }
428
429 @Override
430 public boolean equals(Object obj) {
431 if (this == obj) {
432 return true;
433 }
434 if (obj instanceof ConfigKey) {
435 final ConfigKey other = (ConfigKey) obj;
436 return Objects.equals(this.subject, other.subject)
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800437 && Objects.equals(this.configKey, other.configKey)
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700438 && Objects.equals(this.configClass, other.configClass);
439 }
440 return false;
441 }
442 }
443
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700444 private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700445 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700446 public void event(MapEvent<ConfigKey, JsonNode> event) {
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800447 // Do not delegate pending configs.
448 if (event.key().configClass == null) {
449 return;
450 }
451
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700452 ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
453 if (factory != null) {
Charles Chan023a8982016-02-04 11:00:41 -0800454 Object subject = event.key().subject;
455 Class configClass = factory.configClass();
456 Versioned<JsonNode> newValue = event.newValue();
457 Versioned<JsonNode> oldValue = event.oldValue();
458
459 Config config = (newValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700460 createConfig(subject, configClass, newValue.value(), true) :
461 null;
Charles Chan023a8982016-02-04 11:00:41 -0800462 Config prevConfig = (oldValue != null) ?
Jonathan Hartb11c4d02016-03-23 09:05:44 -0700463 createConfig(subject, configClass, oldValue.value(), true) :
464 null;
Charles Chan023a8982016-02-04 11:00:41 -0800465
466 NetworkConfigEvent.Type type;
467 switch (event.type()) {
468 case INSERT:
469 type = CONFIG_ADDED;
470 break;
471 case UPDATE:
472 type = CONFIG_UPDATED;
473 break;
474 case REMOVE:
475 default:
476 type = CONFIG_REMOVED;
477 break;
478 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700479 notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
Charles Chan023a8982016-02-04 11:00:41 -0800480 config, prevConfig, factory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700481 }
482 }
483 }
484}