Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 39 | import org.onosproject.net.config.Config; |
| 40 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 41 | import org.onosproject.net.config.ConfigFactory; |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 42 | import org.onosproject.net.config.InvalidConfigException; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 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; |
| 49 | import org.onosproject.store.service.MapEvent; |
| 50 | import org.onosproject.store.service.MapEventListener; |
| 51 | import org.onosproject.store.service.Serializer; |
| 52 | import org.onosproject.store.service.StorageService; |
| 53 | import org.onosproject.store.service.Versioned; |
| 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
| 57 | import java.util.LinkedHashMap; |
| 58 | import java.util.Map; |
| 59 | import java.util.Objects; |
| 60 | import java.util.Set; |
| 61 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 62 | import static com.google.common.base.Preconditions.checkArgument; |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 63 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 64 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED; |
| 65 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
| 66 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED; |
| 67 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Implementation of a distributed network configuration store. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class DistributedNetworkConfigStore |
| 75 | extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate> |
| 76 | implements NetworkConfigStore { |
| 77 | |
| 78 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 79 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 80 | private static final String INVALID_CONFIG_JSON = |
| 81 | "JSON node does not contain valid configuration"; |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 82 | 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 Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 86 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 88 | protected StorageService storageService; |
| 89 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 90 | private ConsistentMap<ConfigKey, JsonNode> configs; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | |
| 92 | private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap(); |
| 93 | private final ObjectMapper mapper = new ObjectMapper(); |
| 94 | private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate(); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 95 | private final MapEventListener<ConfigKey, JsonNode> listener = new InternalMapListener(); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 96 | |
| 97 | @Activate |
| 98 | public void activate() { |
| 99 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 100 | .register(KryoNamespaces.API) |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 101 | .register(ConfigKey.class, ObjectNode.class, ArrayNode.class, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 102 | JsonNodeFactory.class, LinkedHashMap.class, |
| 103 | TextNode.class, BooleanNode.class, |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 104 | LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class, |
| 105 | NullNode.class); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 106 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 107 | configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 108 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 109 | .withName("onos-network-configs") |
Madan Jampani | 3d6a2f6 | 2015-08-12 07:19:07 -0700 | [diff] [blame] | 110 | .withRelaxedReadConsistency() |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 111 | .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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 125 | processPendingConfigs(configFactory); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 126 | notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(), |
| 127 | configFactory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 130 | // Sweep through any pending configurations, validate them and then prune them. |
| 131 | private void processPendingConfigs(ConfigFactory configFactory) { |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 132 | ImmutableSet.copyOf(configs.keySet()).forEach(k -> { |
| 133 | if (Objects.equals(k.configKey, configFactory.configKey()) && |
| 134 | isAssignableFrom(configFactory, k)) { |
Thomas Vachuska | 9514836 | 2016-07-13 14:23:27 -0700 | [diff] [blame] | 135 | // 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 141 | } |
| 142 | }); |
Thomas Vachuska | 096bcc8 | 2016-03-07 21:30:29 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @SuppressWarnings("unchecked") |
| 146 | private boolean isAssignableFrom(ConfigFactory configFactory, ConfigKey k) { |
| 147 | return configFactory.subjectFactory().subjectClass().isAssignableFrom(k.subject.getClass()); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @SuppressWarnings("unchecked") |
| 151 | private void validateConfig(ConfigKey key, ConfigFactory configFactory, JsonNode json) { |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 152 | 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 159 | try { |
| 160 | checkArgument(config.isValid(), INVALID_CONFIG_JSON); |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 161 | configs.putAndGet(key(subject, configFactory.configClass()), json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 162 | } catch (Exception e) { |
| 163 | log.warn("Failed to validate pending {} configuration for {}: {}", |
HIGUCHI Yuta | ca2208d | 2016-02-18 15:03:08 -0800 | [diff] [blame] | 164 | key.configKey, key.subject, json); |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public void removeConfigFactory(ConfigFactory configFactory) { |
| 170 | factoriesByConfig.remove(configFactory.configClass().getName()); |
Jonathan Hart | 73518ac | 2016-05-20 08:00:22 -0700 | [diff] [blame] | 171 | processExistingConfigs(configFactory); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 172 | notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(), |
| 173 | configFactory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jonathan Hart | 73518ac | 2016-05-20 08:00:22 -0700 | [diff] [blame] | 176 | // Sweep through any configurations for the config factory, set back to pending state. |
| 177 | private void processExistingConfigs(ConfigFactory configFactory) { |
| 178 | ImmutableSet.copyOf(configs.keySet()).forEach(k -> { |
| 179 | if (Objects.equals(configFactory.configClass().getName(), k.configClass)) { |
Ray Milkey | 3880928 | 2016-07-07 14:36:23 -0700 | [diff] [blame] | 180 | 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 Hart | 73518ac | 2016-05-20 08:00:22 -0700 | [diff] [blame] | 186 | } |
| 187 | }); |
| 188 | } |
| 189 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 190 | @Override |
| 191 | @SuppressWarnings("unchecked") |
| 192 | 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] | 193 | return factoriesByConfig.get(configClass.getName()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 194 | } |
| 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 214 | if (subjectClass.isInstance(k.subject) && Objects.equals(cName, k.configClass)) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 215 | 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 226 | if (Objects.equals(subject, k.subject) && k.configClass != null && delegate != null) { |
Jonathan Hart | 80fe442 | 2016-05-24 18:47:37 -0700 | [diff] [blame] | 227 | ConfigFactory<S, ? extends Config<S>> configFactory = factoriesByConfig.get(k.configClass); |
| 228 | if (configFactory == null) { |
Thomas Vachuska | 698ba66 | 2016-07-28 11:42:39 -0700 | [diff] [blame] | 229 | log.warn("Found config but no config factory: subject={}, configClass={}", |
| 230 | subject, k.configClass); |
| 231 | } else { |
| 232 | builder.add(configFactory.configClass()); |
Jonathan Hart | 80fe442 | 2016-05-24 18:47:37 -0700 | [diff] [blame] | 233 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 234 | } |
| 235 | }); |
| 236 | return builder.build(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) { |
Madan Jampani | c637188 | 2016-06-03 21:30:17 -0700 | [diff] [blame] | 241 | Versioned<JsonNode> json = configs.get(key(subject, configClass)); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 242 | 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 Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 248 | 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 253 | return createConfig(subject, configClass, json.value()); |
| 254 | } |
| 255 | |
| 256 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 257 | 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] | 258 | // Create the configuration and validate it. |
| 259 | C config = createConfig(subject, configClass, json); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 260 | |
| 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 Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 271 | |
| 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 277 | 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 283 | } |
| 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 290 | @Override |
| 291 | public <S> void clearQueuedConfig(S subject, String configKey) { |
| 292 | configs.remove(key(subject, configKey)); |
| 293 | } |
| 294 | |
Deepa Vaddireddy | 0c49b60 | 2016-06-02 12:19:07 +0530 | [diff] [blame] | 295 | @Override |
| 296 | public <S> void clearConfig(S subject) { |
| 297 | ImmutableSet.copyOf(configs.keySet()).forEach(k -> { |
| 298 | if (Objects.equals(subject, k.subject) && delegate != null) { |
| 299 | configs.remove(k); |
| 300 | } |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public <S> void clearConfig() { |
| 306 | ImmutableSet.copyOf(configs.keySet()).forEach(k -> { |
| 307 | if (delegate != null) { |
| 308 | configs.remove(k); |
| 309 | } |
| 310 | }); |
| 311 | } |
| 312 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 313 | /** |
| 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 322 | 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] | 323 | JsonNode json) { |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 324 | 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 346 | if (json != null) { |
| 347 | ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName()); |
| 348 | if (factory != null) { |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 349 | validateJsonType(json, factory); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 350 | C config = factory.createConfig(); |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 351 | config.init(subject, factory.configKey(), json, mapper, |
| 352 | detached ? null : applyDelegate); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 353 | return config; |
| 354 | } |
| 355 | } |
| 356 | return null; |
| 357 | } |
| 358 | |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 359 | /** |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 360 | * 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 Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 364 | * |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 365 | * @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 Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 370 | */ |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 371 | 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 Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 375 | } |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 376 | if (!factory.isList() && !(json instanceof ObjectNode)) { |
| 377 | throw new IllegalArgumentException(INVALID_JSON_OBJECT); |
| 378 | } |
| 379 | |
| 380 | return true; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 383 | |
| 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 398 | // 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 403 | // Auxiliary key to track subject configurations. |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 404 | // Keys with non-null configKey are pending configurations. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 405 | private static final class ConfigKey { |
| 406 | final Object subject; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 407 | final String configKey; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 408 | final String configClass; |
| 409 | |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 410 | // 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 418 | private ConfigKey(Object subject, Class<?> configClass) { |
| 419 | this.subject = subject; |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 420 | this.configKey = null; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 421 | this.configClass = configClass.getName(); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public int hashCode() { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 426 | return Objects.hash(subject, configKey, configClass); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 427 | } |
| 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 Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 437 | && Objects.equals(this.configKey, other.configKey) |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 438 | && Objects.equals(this.configClass, other.configClass); |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 444 | private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 445 | @Override |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 446 | public void event(MapEvent<ConfigKey, JsonNode> event) { |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 447 | // Do not delegate pending configs. |
| 448 | if (event.key().configClass == null) { |
| 449 | return; |
| 450 | } |
| 451 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 452 | ConfigFactory factory = factoriesByConfig.get(event.key().configClass); |
| 453 | if (factory != null) { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 454 | 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 Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 460 | createConfig(subject, configClass, newValue.value(), true) : |
| 461 | null; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 462 | Config prevConfig = (oldValue != null) ? |
Jonathan Hart | b11c4d0 | 2016-03-23 09:05:44 -0700 | [diff] [blame] | 463 | createConfig(subject, configClass, oldValue.value(), true) : |
| 464 | null; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 465 | |
| 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 Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 479 | notifyDelegate(new NetworkConfigEvent(type, event.key().subject, |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 480 | config, prevConfig, factory.configClass())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |