Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 3 | * |
| 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 Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.store.config.impl; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.BooleanNode; |
| 22 | import com.fasterxml.jackson.databind.node.DoubleNode; |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 23 | import com.fasterxml.jackson.databind.node.IntNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 24 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 25 | import com.fasterxml.jackson.databind.node.LongNode; |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.node.NullNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | import com.fasterxml.jackson.databind.node.ShortNode; |
| 29 | import com.fasterxml.jackson.databind.node.TextNode; |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | import com.google.common.collect.Maps; |
| 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Reference; |
| 36 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 37 | import org.apache.felix.scr.annotations.Service; |
| 38 | import org.onlab.util.KryoNamespace; |
Thomas Vachuska | 5f2cbe6 | 2015-07-30 15:10:34 -0700 | [diff] [blame] | 39 | import org.onlab.util.Tools; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 40 | import org.onosproject.net.config.Config; |
| 41 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 42 | import org.onosproject.net.config.ConfigFactory; |
| 43 | import org.onosproject.net.config.NetworkConfigEvent; |
| 44 | import org.onosproject.net.config.NetworkConfigStore; |
| 45 | import org.onosproject.net.config.NetworkConfigStoreDelegate; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 46 | import org.onosproject.store.AbstractStore; |
| 47 | import org.onosproject.store.serializers.KryoNamespaces; |
| 48 | import org.onosproject.store.service.ConsistentMap; |
Thomas Vachuska | 5f2cbe6 | 2015-07-30 15:10:34 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.ConsistentMapException; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.MapEvent; |
| 51 | import org.onosproject.store.service.MapEventListener; |
| 52 | import org.onosproject.store.service.Serializer; |
| 53 | import org.onosproject.store.service.StorageService; |
| 54 | import org.onosproject.store.service.Versioned; |
| 55 | import org.slf4j.Logger; |
| 56 | import org.slf4j.LoggerFactory; |
| 57 | |
| 58 | import java.util.LinkedHashMap; |
| 59 | import java.util.Map; |
| 60 | import java.util.Objects; |
| 61 | import java.util.Set; |
| 62 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 64 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 65 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED; |
| 66 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
| 67 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED; |
| 68 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Implementation of a distributed network configuration store. |
| 72 | */ |
| 73 | @Component(immediate = true) |
| 74 | @Service |
| 75 | public class DistributedNetworkConfigStore |
| 76 | extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate> |
| 77 | implements NetworkConfigStore { |
| 78 | |
| 79 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 80 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 81 | private static final int MAX_BACKOFF = 10; |
| 82 | private static final String INVALID_CONFIG_JSON = |
| 83 | "JSON node does not contain valid configuration"; |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 84 | 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 Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 88 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected StorageService storageService; |
| 91 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 92 | private ConsistentMap<ConfigKey, JsonNode> configs; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 93 | |
| 94 | private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap(); |
| 95 | private final ObjectMapper mapper = new ObjectMapper(); |
| 96 | private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate(); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 97 | private final MapEventListener<ConfigKey, JsonNode> listener = new InternalMapListener(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 98 | |
| 99 | @Activate |
| 100 | public void activate() { |
| 101 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 102 | .register(KryoNamespaces.API) |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 103 | .register(ConfigKey.class, ObjectNode.class, ArrayNode.class, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 104 | JsonNodeFactory.class, LinkedHashMap.class, |
| 105 | TextNode.class, BooleanNode.class, |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 106 | LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class, |
| 107 | NullNode.class); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 108 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 109 | configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 110 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 111 | .withName("onos-network-configs") |
Madan Jampani | 3d6a2f6 | 2015-08-12 07:19:07 -0700 | [diff] [blame] | 112 | .withRelaxedReadConsistency() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 113 | .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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 127 | processPendingConfigs(configFactory); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 128 | notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(), |
| 129 | configFactory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 132 | // Sweep through any pending configurations, validate them and then prune them. |
| 133 | private void processPendingConfigs(ConfigFactory configFactory) { |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 134 | ImmutableSet.copyOf(configs.keySet()).forEach(k -> { |
| 135 | if (Objects.equals(k.configKey, configFactory.configKey()) && |
| 136 | isAssignableFrom(configFactory, k)) { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 137 | validateConfig(k, configFactory, configs.get(k).value()); |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 138 | configs.remove(k); // Prune whether valid or not |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 139 | } |
| 140 | }); |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @SuppressWarnings("unchecked") |
| 144 | private boolean isAssignableFrom(ConfigFactory configFactory, ConfigKey k) { |
| 145 | return configFactory.subjectFactory().subjectClass().isAssignableFrom(k.subject.getClass()); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @SuppressWarnings("unchecked") |
| 149 | private void validateConfig(ConfigKey key, ConfigFactory configFactory, JsonNode json) { |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 150 | 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 157 | try { |
| 158 | checkArgument(config.isValid(), INVALID_CONFIG_JSON); |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 159 | configs.putAndGet(key(subject, configFactory.configClass()), json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 160 | } catch (Exception e) { |
| 161 | log.warn("Failed to validate pending {} configuration for {}: {}", |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 162 | key.configKey, key.subject, json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 166 | @Override |
| 167 | public void removeConfigFactory(ConfigFactory configFactory) { |
| 168 | factoriesByConfig.remove(configFactory.configClass().getName()); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 169 | notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(), |
| 170 | configFactory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
| 174 | @SuppressWarnings("unchecked") |
| 175 | public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) { |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 176 | return factoriesByConfig.get(configClass.getName()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | @SuppressWarnings("unchecked") |
| 181 | public <S> Set<S> getSubjects(Class<S> subjectClass) { |
| 182 | ImmutableSet.Builder<S> builder = ImmutableSet.builder(); |
| 183 | configs.keySet().forEach(k -> { |
| 184 | if (subjectClass.isInstance(k.subject)) { |
| 185 | builder.add((S) k.subject); |
| 186 | } |
| 187 | }); |
| 188 | return builder.build(); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | @SuppressWarnings("unchecked") |
| 193 | public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) { |
| 194 | ImmutableSet.Builder<S> builder = ImmutableSet.builder(); |
| 195 | String cName = configClass.getName(); |
| 196 | configs.keySet().forEach(k -> { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 197 | if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 198 | builder.add((S) k.subject); |
| 199 | } |
| 200 | }); |
| 201 | return builder.build(); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | @SuppressWarnings("unchecked") |
| 206 | public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) { |
| 207 | ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder(); |
| 208 | configs.keySet().forEach(k -> { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 209 | if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 210 | builder.add(factoriesByConfig.get(k.configClass).configClass()); |
| 211 | } |
| 212 | }); |
| 213 | return builder.build(); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) { |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 218 | // TODO: need to identify and address the root cause for timeouts. |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 219 | Versioned<JsonNode> json = Tools.retryable(configs::get, ConsistentMapException.class, 1, MAX_BACKOFF) |
| 220 | .apply(key(subject, configClass)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 221 | return json != null ? createConfig(subject, configClass, json.value()) : null; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | @Override |
| 226 | public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 227 | ConfigFactory<S, C> factory = getConfigFactory(configClass); |
| 228 | Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass), |
| 229 | k -> factory.isList() ? |
| 230 | mapper.createArrayNode() : |
| 231 | mapper.createObjectNode()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 232 | return createConfig(subject, configClass, json.value()); |
| 233 | } |
| 234 | |
| 235 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 236 | public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) { |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 237 | // Create the configuration and validate it. |
| 238 | C config = createConfig(subject, configClass, json); |
| 239 | checkArgument(config.isValid(), INVALID_CONFIG_JSON); |
| 240 | |
| 241 | // Insert the validated configuration and get it back. |
| 242 | Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json); |
| 243 | |
| 244 | // Re-create the config if for some reason what we attempted to put |
| 245 | // was supplanted by someone else already. |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 246 | return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value()); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public <S> void queueConfig(S subject, String configKey, JsonNode json) { |
| 251 | configs.put(key(subject, configKey), json); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) { |
| 256 | configs.remove(key(subject, configClass)); |
| 257 | } |
| 258 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 259 | @Override |
| 260 | public <S> void clearQueuedConfig(S subject, String configKey) { |
| 261 | configs.remove(key(subject, configKey)); |
| 262 | } |
| 263 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Produces a config from the specified subject, config class and raw JSON. |
| 266 | * |
| 267 | * @param subject config subject |
| 268 | * @param configClass config class |
| 269 | * @param json raw JSON data |
| 270 | * @return config object or null of no factory found or if the specified |
| 271 | * JSON is null |
| 272 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 273 | private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass, |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 274 | JsonNode json) { |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 275 | return createConfig(subject, configClass, json, false); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Produces a config from the specified subject, config class and raw JSON. |
| 280 | * |
| 281 | * The config can optionally be detached, which means it does not contain a |
| 282 | * reference to an apply delegate. This means a detached config can not be |
| 283 | * applied. This should be used only for passing the config object in the |
| 284 | * NetworkConfigEvent. |
| 285 | * |
| 286 | * @param subject config subject |
| 287 | * @param configClass config class |
| 288 | * @param json raw JSON data |
| 289 | * @param detached whether the config should be detached, that is, should |
| 290 | * be created without setting an apply delegate. |
| 291 | * @return config object or null of no factory found or if the specified |
| 292 | * JSON is null |
| 293 | */ |
| 294 | @SuppressWarnings("unchecked") |
| 295 | private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass, |
| 296 | JsonNode json, boolean detached) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 297 | if (json != null) { |
| 298 | ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName()); |
| 299 | if (factory != null) { |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 300 | validateJsonType(json, factory); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 301 | C config = factory.createConfig(); |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 302 | config.init(subject, factory.configKey(), json, mapper, |
| 303 | detached ? null : applyDelegate); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 304 | return config; |
| 305 | } |
| 306 | } |
| 307 | return null; |
| 308 | } |
| 309 | |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 310 | /** |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 311 | * Validates that the type of the JSON node is appropriate for the type of |
| 312 | * configuration. A list type configuration must be created with an |
| 313 | * ArrayNode, and an object type configuration must be created with an |
| 314 | * ObjectNode. |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 315 | * |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 316 | * @param json JSON node to check |
| 317 | * @param factory config factory of configuration |
| 318 | * @param <S> subject |
| 319 | * @param <C> configuration |
| 320 | * @return true if the JSON node type is appropriate for the configuration |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 321 | */ |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 322 | private <S, C extends Config<S>> boolean validateJsonType(JsonNode json, |
| 323 | ConfigFactory<S, C> factory) { |
| 324 | if (factory.isList() && !(json instanceof ArrayNode)) { |
| 325 | throw new IllegalArgumentException(INVALID_JSON_LIST); |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 326 | } |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 327 | if (!factory.isList() && !(json instanceof ObjectNode)) { |
| 328 | throw new IllegalArgumentException(INVALID_JSON_OBJECT); |
| 329 | } |
| 330 | |
| 331 | return true; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 334 | |
| 335 | // Auxiliary delegate to receive notifications about changes applied to |
| 336 | // the network configuration - by the apps. |
| 337 | private class InternalApplyDelegate implements ConfigApplyDelegate { |
| 338 | @Override |
| 339 | public void onApply(Config config) { |
| 340 | configs.put(key(config.subject(), config.getClass()), config.node()); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Produces a key for uniquely tracking a subject config. |
| 345 | private static ConfigKey key(Object subject, Class<?> configClass) { |
| 346 | return new ConfigKey(subject, configClass); |
| 347 | } |
| 348 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 349 | // Produces a key for uniquely tracking a subject config. |
| 350 | private static ConfigKey key(Object subject, String configKey) { |
| 351 | return new ConfigKey(subject, configKey); |
| 352 | } |
| 353 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 354 | // Auxiliary key to track subject configurations. |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 355 | // Keys with non-null configKey are pending configurations. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 356 | private static final class ConfigKey { |
| 357 | final Object subject; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 358 | final String configKey; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 359 | final String configClass; |
| 360 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 361 | // Create a key for pending configuration class |
| 362 | private ConfigKey(Object subject, String configKey) { |
| 363 | this.subject = subject; |
| 364 | this.configKey = configKey; |
| 365 | this.configClass = null; |
| 366 | } |
| 367 | |
| 368 | // Create a key for registered class configuration |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 369 | private ConfigKey(Object subject, Class<?> configClass) { |
| 370 | this.subject = subject; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 371 | this.configKey = null; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 372 | this.configClass = configClass.getName(); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public int hashCode() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 377 | return Objects.hash(subject, configKey, configClass); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public boolean equals(Object obj) { |
| 382 | if (this == obj) { |
| 383 | return true; |
| 384 | } |
| 385 | if (obj instanceof ConfigKey) { |
| 386 | final ConfigKey other = (ConfigKey) obj; |
| 387 | return Objects.equals(this.subject, other.subject) |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 388 | && Objects.equals(this.configKey, other.configKey) |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 389 | && Objects.equals(this.configClass, other.configClass); |
| 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | } |
| 394 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 395 | private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 396 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 397 | public void event(MapEvent<ConfigKey, JsonNode> event) { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 398 | // Do not delegate pending configs. |
| 399 | if (event.key().configClass == null) { |
| 400 | return; |
| 401 | } |
| 402 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 403 | ConfigFactory factory = factoriesByConfig.get(event.key().configClass); |
| 404 | if (factory != null) { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 405 | Object subject = event.key().subject; |
| 406 | Class configClass = factory.configClass(); |
| 407 | Versioned<JsonNode> newValue = event.newValue(); |
| 408 | Versioned<JsonNode> oldValue = event.oldValue(); |
| 409 | |
| 410 | Config config = (newValue != null) ? |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 411 | createConfig(subject, configClass, newValue.value(), true) : |
| 412 | null; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 413 | Config prevConfig = (oldValue != null) ? |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 414 | createConfig(subject, configClass, oldValue.value(), true) : |
| 415 | null; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 416 | |
| 417 | NetworkConfigEvent.Type type; |
| 418 | switch (event.type()) { |
| 419 | case INSERT: |
| 420 | type = CONFIG_ADDED; |
| 421 | break; |
| 422 | case UPDATE: |
| 423 | type = CONFIG_UPDATED; |
| 424 | break; |
| 425 | case REMOVE: |
| 426 | default: |
| 427 | type = CONFIG_REMOVED; |
| 428 | break; |
| 429 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 430 | notifyDelegate(new NetworkConfigEvent(type, event.key().subject, |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 431 | config, prevConfig, factory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |