blob: 27c7912279bd206d534fe0ff01176d0b940ca0b0 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 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;
Madan Jampania29c6772015-08-17 13:17:07 -070032
Thomas Vachuska96d55b12015-05-11 08:52:03 -070033import org.apache.felix.scr.annotations.Activate;
34import org.apache.felix.scr.annotations.Component;
35import org.apache.felix.scr.annotations.Deactivate;
36import org.apache.felix.scr.annotations.Reference;
37import org.apache.felix.scr.annotations.ReferenceCardinality;
38import org.apache.felix.scr.annotations.Service;
39import org.onlab.util.KryoNamespace;
Thomas Vachuska5f2cbe62015-07-30 15:10:34 -070040import org.onlab.util.Tools;
Ray Milkeya4122362015-08-18 15:19:08 -070041import org.onosproject.net.config.Config;
42import org.onosproject.net.config.ConfigApplyDelegate;
43import org.onosproject.net.config.ConfigFactory;
44import org.onosproject.net.config.NetworkConfigEvent;
45import org.onosproject.net.config.NetworkConfigStore;
46import org.onosproject.net.config.NetworkConfigStoreDelegate;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070047import org.onosproject.store.AbstractStore;
48import org.onosproject.store.serializers.KryoNamespaces;
49import org.onosproject.store.service.ConsistentMap;
Thomas Vachuska5f2cbe62015-07-30 15:10:34 -070050import org.onosproject.store.service.ConsistentMapException;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070051import org.onosproject.store.service.MapEvent;
52import org.onosproject.store.service.MapEventListener;
53import org.onosproject.store.service.Serializer;
54import org.onosproject.store.service.StorageService;
55import org.onosproject.store.service.Versioned;
56import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
59import java.util.LinkedHashMap;
60import java.util.Map;
61import java.util.Objects;
62import java.util.Set;
63
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080064import static com.google.common.base.Preconditions.checkArgument;
Ray Milkeya4122362015-08-18 15:19:08 -070065import static org.onosproject.net.config.NetworkConfigEvent.Type.*;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070066
67/**
68 * Implementation of a distributed network configuration store.
69 */
70@Component(immediate = true)
71@Service
72public class DistributedNetworkConfigStore
73 extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate>
74 implements NetworkConfigStore {
75
76 private final Logger log = LoggerFactory.getLogger(getClass());
77
Thomas Vachuskace0bbb32015-11-18 16:56:10 -080078 private static final int MAX_BACKOFF = 10;
79 private static final String INVALID_CONFIG_JSON =
80 "JSON node does not contain valid configuration";
81
Thomas Vachuska96d55b12015-05-11 08:52:03 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected StorageService storageService;
84
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070085 private ConsistentMap<ConfigKey, JsonNode> configs;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070086
87 private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap();
88 private final ObjectMapper mapper = new ObjectMapper();
89 private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate();
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070090 private final MapEventListener<ConfigKey, JsonNode> listener = new InternalMapListener();
Thomas Vachuska96d55b12015-05-11 08:52:03 -070091
92 @Activate
93 public void activate() {
94 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
95 .register(KryoNamespaces.API)
Jonathan Hart111b42b2015-07-14 13:28:05 -070096 .register(ConfigKey.class, ObjectNode.class, ArrayNode.class,
Thomas Vachuska96d55b12015-05-11 08:52:03 -070097 JsonNodeFactory.class, LinkedHashMap.class,
98 TextNode.class, BooleanNode.class,
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080099 LongNode.class, DoubleNode.class, ShortNode.class, IntNode.class,
100 NullNode.class);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700101
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700102 configs = storageService.<ConfigKey, JsonNode>consistentMapBuilder()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700103 .withSerializer(Serializer.using(kryoBuilder.build()))
104 .withName("onos-network-configs")
Madan Jampani3d6a2f62015-08-12 07:19:07 -0700105 .withRelaxedReadConsistency()
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700106 .build();
107 configs.addListener(listener);
108 log.info("Started");
109 }
110
111 @Deactivate
112 public void deactivate() {
113 configs.removeListener(listener);
114 log.info("Stopped");
115 }
116
117 @Override
118 public void addConfigFactory(ConfigFactory configFactory) {
119 factoriesByConfig.put(configFactory.configClass().getName(), configFactory);
Thomas Vachuskae6360222015-07-21 10:10:36 -0700120 notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(),
121 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700122 }
123
124 @Override
125 public void removeConfigFactory(ConfigFactory configFactory) {
126 factoriesByConfig.remove(configFactory.configClass().getName());
Thomas Vachuskae6360222015-07-21 10:10:36 -0700127 notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
128 configFactory.configClass()));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700129 }
130
131 @Override
132 @SuppressWarnings("unchecked")
133 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
134 return (ConfigFactory<S, C>) factoriesByConfig.get(configClass.getName());
135 }
136
137 @Override
138 @SuppressWarnings("unchecked")
139 public <S> Set<S> getSubjects(Class<S> subjectClass) {
140 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
141 configs.keySet().forEach(k -> {
142 if (subjectClass.isInstance(k.subject)) {
143 builder.add((S) k.subject);
144 }
145 });
146 return builder.build();
147 }
148
149 @Override
150 @SuppressWarnings("unchecked")
151 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
152 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
153 String cName = configClass.getName();
154 configs.keySet().forEach(k -> {
155 if (subjectClass.isInstance(k.subject) && cName.equals(k.configClass)) {
156 builder.add((S) k.subject);
157 }
158 });
159 return builder.build();
160 }
161
162 @Override
163 @SuppressWarnings("unchecked")
164 public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) {
165 ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder();
166 configs.keySet().forEach(k -> {
167 if (Objects.equals(subject, k.subject) && delegate != null) {
168 builder.add(factoriesByConfig.get(k.configClass).configClass());
169 }
170 });
171 return builder.build();
172 }
173
174 @Override
175 public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
Madan Jampania29c6772015-08-17 13:17:07 -0700176 // TODO: need to identify and address the root cause for timeouts.
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700177 Versioned<JsonNode> json = Tools.retryable(configs::get, ConsistentMapException.class, 1, MAX_BACKOFF)
178 .apply(key(subject, configClass));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700179 return json != null ? createConfig(subject, configClass, json.value()) : null;
180 }
181
182
183 @Override
184 public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700185 ConfigFactory<S, C> factory = getConfigFactory(configClass);
186 Versioned<JsonNode> json = configs.computeIfAbsent(key(subject, configClass),
187 k -> factory.isList() ?
188 mapper.createArrayNode() :
189 mapper.createObjectNode());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700190 return createConfig(subject, configClass, json.value());
191 }
192
193 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700194 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
Thomas Vachuskace0bbb32015-11-18 16:56:10 -0800195 // Create the configuration and validate it.
196 C config = createConfig(subject, configClass, json);
197 checkArgument(config.isValid(), INVALID_CONFIG_JSON);
198
199 // Insert the validated configuration and get it back.
200 Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
201
202 // Re-create the config if for some reason what we attempted to put
203 // was supplanted by someone else already.
204 return versioned.value() == json ? config :
205 createConfig(subject, configClass, versioned.value());
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700206 }
207
208 @Override
209 public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) {
210 configs.remove(key(subject, configClass));
211 }
212
213 /**
214 * Produces a config from the specified subject, config class and raw JSON.
215 *
216 * @param subject config subject
217 * @param configClass config class
218 * @param json raw JSON data
219 * @return config object or null of no factory found or if the specified
220 * JSON is null
221 */
222 @SuppressWarnings("unchecked")
223 private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700224 JsonNode json) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700225 if (json != null) {
226 ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
227 if (factory != null) {
228 C config = factory.createConfig();
229 config.init(subject, factory.configKey(), json, mapper, applyDelegate);
230 return config;
231 }
232 }
233 return null;
234 }
235
236
237 // Auxiliary delegate to receive notifications about changes applied to
238 // the network configuration - by the apps.
239 private class InternalApplyDelegate implements ConfigApplyDelegate {
240 @Override
241 public void onApply(Config config) {
242 configs.put(key(config.subject(), config.getClass()), config.node());
243 }
244 }
245
246 // Produces a key for uniquely tracking a subject config.
247 private static ConfigKey key(Object subject, Class<?> configClass) {
248 return new ConfigKey(subject, configClass);
249 }
250
251 // Auxiliary key to track subject configurations.
252 private static final class ConfigKey {
253 final Object subject;
254 final String configClass;
255
256 private ConfigKey(Object subject, Class<?> configClass) {
257 this.subject = subject;
258 this.configClass = configClass.getName();
259 }
260
261 @Override
262 public int hashCode() {
263 return Objects.hash(subject, configClass);
264 }
265
266 @Override
267 public boolean equals(Object obj) {
268 if (this == obj) {
269 return true;
270 }
271 if (obj instanceof ConfigKey) {
272 final ConfigKey other = (ConfigKey) obj;
273 return Objects.equals(this.subject, other.subject)
274 && Objects.equals(this.configClass, other.configClass);
275 }
276 return false;
277 }
278 }
279
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700280 private class InternalMapListener implements MapEventListener<ConfigKey, JsonNode> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700281 @Override
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700282 public void event(MapEvent<ConfigKey, JsonNode> event) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700283 NetworkConfigEvent.Type type;
284 switch (event.type()) {
285 case INSERT:
286 type = CONFIG_ADDED;
287 break;
288 case UPDATE:
289 type = CONFIG_UPDATED;
290 break;
291 case REMOVE:
292 default:
293 type = CONFIG_REMOVED;
294 break;
295 }
296 ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
297 if (factory != null) {
298 notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
299 factory.configClass()));
300 }
301 }
302 }
303}