blob: 1c6325835cec68dba8d6909f65dbefc9ffc1a66a [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
2 * Copyright 2016-present 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 */
16package org.onosproject.config.impl;
17
Sithara Punnassery06208792017-02-10 16:25:29 -080018import com.google.common.annotations.Beta;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.config.DynamicConfigEvent;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080027import org.onosproject.config.DynamicConfigListener;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080028import org.onosproject.config.DynamicConfigStore;
29import org.onosproject.config.DynamicConfigStoreDelegate;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080030import org.onosproject.config.ResourceIdParser;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080031import org.onosproject.config.FailedException;
32import org.onosproject.config.Filter;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080033//import org.onosproject.config.cfgreceiver.CfgReceiver;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080034import org.onosproject.store.AbstractStore;
35import org.onosproject.store.serializers.KryoNamespaces;
36import org.onosproject.store.service.AsyncDocumentTree;
37import org.onosproject.store.service.ConsistentMap;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080038import org.onosproject.store.service.ConsistentMapException;
39import org.onosproject.store.service.ConsistentMultimap;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080040import org.onosproject.store.service.DocumentPath;
41import org.onosproject.store.service.DocumentTreeEvent;
42import org.onosproject.store.service.DocumentTreeListener;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080043import org.onosproject.store.service.IllegalDocumentModificationException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080044import org.onosproject.store.service.MapEvent;
45import org.onosproject.store.service.MapEventListener;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080046import org.onosproject.store.service.NoSuchDocumentPathException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080047import org.onosproject.store.service.Serializer;
48import org.onosproject.store.service.StorageService;
49import org.onosproject.store.service.Versioned;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080050import org.onosproject.yang.model.DataNode;
51import org.onosproject.yang.model.InnerNode;
52import org.onosproject.yang.model.KeyLeaf;
53import org.onosproject.yang.model.LeafListKey;
54import org.onosproject.yang.model.LeafNode;
55import org.onosproject.yang.model.ListKey;
56import org.onosproject.yang.model.NodeKey;
57import org.onosproject.yang.model.ResourceId;
58import org.onosproject.yang.model.SchemaId;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080059import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080061import java.util.Collection;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080062import java.util.Map;
63import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080064import java.util.concurrent.ExecutionException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080065
Sithara Punnassery44e2a702017-03-06 15:38:10 -080066import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
67import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED;
68import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
69import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN;
70
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080071/**
72 * Implementation of the dynamic config store.
73 */
Sithara Punnassery06208792017-02-10 16:25:29 -080074@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080075@Component(immediate = true)
76@Service
77public class DistributedDynamicConfigStore
78 extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate>
79 implements DynamicConfigStore {
80 private final Logger log = LoggerFactory.getLogger(getClass());
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected StorageService storageService;
83 private AsyncDocumentTree<DataNode.Type> keystore;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080084 private ConsistentMap<String, LeafNode> objectStore;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080085 private ConsistentMultimap<String, DynamicConfigListener> lstnrStore;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080086 private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080087 private final MapEventListener<String, LeafNode> olistener = new InternalMapListener();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080088
89 @Activate
90 public void activateStore() {
91 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
92 .register(KryoNamespaces.BASIC)
Sithara Punnassery44e2a702017-03-06 15:38:10 -080093 //.register(String.class)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080094 .register(java.lang.Class.class)
95 .register(DataNode.Type.class)
96 .register(LeafNode.class)
97 .register(InnerNode.class)
98 .register(ResourceId.class)
99 .register(NodeKey.class)
100 .register(SchemaId.class)
101 .register(java.util.LinkedHashMap.class);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800102 //.register(CfgReceiver.InternalDynamicConfigListener.class);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800103 keystore = storageService.<DataNode.Type>documentTreeBuilder()
104 .withSerializer(Serializer.using(kryoBuilder.build()))
105 .withName("config-key-store")
106 .withRelaxedReadConsistency()
107 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800108 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800109 .withSerializer(Serializer.using(kryoBuilder.build()))
110 .withName("config-object-store")
111 .withRelaxedReadConsistency()
112 .build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800113 lstnrStore = storageService.<String, DynamicConfigListener>consistentMultimapBuilder()
114 .withSerializer(Serializer.using(kryoBuilder.build()))
115 .withName("config-listener-registry")
116 .withRelaxedReadConsistency()
117 .build();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800118 keystore.addListener(klistener);
119 objectStore.addListener(olistener);
120 log.info("DyanmicConfig Store Active");
121 }
122
123 @Deactivate
124 public void deactivateStore() {
125 keystore.removeListener(klistener);
126 objectStore.removeListener(olistener);
127 log.info("DyanmicConfig Store Stopped");
128 }
129
130 @Override
131 public CompletableFuture<Boolean>
132 addNode(ResourceId path, DataNode node) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800133 throw new FailedException("Not yet implemented");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800134 }
135
136 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800137 public CompletableFuture<Boolean>
138 addRecursive(ResourceId complete, DataNode node) {
139 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
140 ResourceId path = ResourceIdParser.getParent(complete);
141 String spath = ResourceIdParser.parseResId(path);
142 if (spath == null) {
143 throw new FailedException("Invalid RsourceId, cannot create Node");
144 }
145 /*if (keystore.get(DocumentPath.from(spath)).join() == null) {
146 ////TODO is recursively creating missing parents required?
147 throw new FailedException("Some of the parents in the path " +
148 "are not present, creation not supported currently");
149 }*/
150 spath = ResourceIdParser.appendNodeKey(spath, node.key());
151 parseNode(spath, node);
152 return eventFuture;
153 }
154
155 private void parseNode(String path, DataNode node) {
156 if (keystore.get(DocumentPath.from(path)).join() != null) {
157 throw new FailedException("Requested node already present in the" +
158 " store, please use an update method");
159 }
160 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
161 addLeaf(path, (LeafNode) node);
162 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
163 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
164 if (keystore.get(DocumentPath.from(path)).join() != null) {
165 throw new FailedException("Requested node already present in the" +
166 " store, please use an update method");
167 }
168 addLeaf(path, (LeafNode) node);
169 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
170 traverseInner(path, (InnerNode) node);
171 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
172 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
173 if (keystore.get(DocumentPath.from(path)).join() != null) {
174 throw new FailedException("Requested node already present in the" +
175 " store, please use an update method");
176 }
177 traverseInner(path, (InnerNode) node);
178 } else {
179 throw new FailedException("Invalid node type");
180 }
181 }
182
183 private void traverseInner(String path, InnerNode node) {
184 addKey(path, node.type());
185 Map<NodeKey, DataNode> entries = node.childNodes();
186 if (entries.size() == 0) {
187 throw new FailedException("Inner node cannot have empty children map");
188 }
189 entries.forEach((k, v) -> {
190 String tempPath;
191 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
192 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
193 addLeaf(tempPath, (LeafNode) v);
194 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
195 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
196 addLeaf(tempPath, (LeafNode) v);
197 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
198 traverseInner(tempPath, (InnerNode) v);
199 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
200 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
201 traverseInner(path, (InnerNode) v);
202 } else {
203 throw new FailedException("Invalid node type");
204 }
205 });
206 }
207
208 private Boolean addLeaf(String path, LeafNode node) {
209 objectStore.put(path, node);
210 return addKey(path, node.type());
211 }
212
213 private Boolean addKey(String path, DataNode.Type type) {
214 Boolean stat = false;
215 CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type);
216 return complete(ret);
217 }
218
219 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800220 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
221 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800222 String spath = ResourceIdParser.parseResId(path);
223 DocumentPath dpath = DocumentPath.from(spath);
224 DataNode.Type type = null;
225 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
226 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800227 if (type == null) {
228 throw new FailedException("Requested node or some of the parents" +
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800229 "are not present in the requested path");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800230 }
231 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800232 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
233 retVal = readLeaf(spath);
234 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
235 retVal = readLeaf(spath);
236 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
237 NodeKey key = ResourceIdParser.getInstanceKey(path);
238 if (key == null) {
239 throw new FailedException("Key type did not match node type");
240 }
241 DataNode.Builder superBldr = InnerNode
242 .builder(key.schemaId().name(), key.schemaId().namespace())
243 .type(type);
244 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800245 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800246 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
247 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
248 if (key == null) {
249 throw new FailedException("Key type did not match node type");
250 }
251 DataNode.Builder superBldr = InnerNode
252 .builder(key.schemaId().name(), key.schemaId().namespace())
253 .type(type);
254 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
255 String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
256 LeafNode lfnd = readLeaf(tempPath);
257 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
258 keyLeaf.leafSchema().namespace(), lfnd.value());
259 }
260 readInner(superBldr, spath);
261 retVal = superBldr.build();
262 } else {
263 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800264 }
265 if (retVal != null) {
266 eventFuture = CompletableFuture.completedFuture(retVal);
267 } else {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800268 log.info("STORE: FAILED to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800269 }
270 return eventFuture;
271 }
272
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800273 private void readInner(DataNode.Builder superBldr, String spath) {
274 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
275 DocumentPath.from(spath));
276 Map<String, Versioned<DataNode.Type>> entries = null;
277 entries = complete(ret);
278 if ((entries == null) || (entries.size() == 0)) {
279 throw new FailedException("Inner node cannot have empty children map");
280 }
281 entries.forEach((k, v) -> {
282 String[] names = k.split(ResourceIdParser.NM_SEP);
283 String name = names[0];
284 String nmSpc = names[1];
285 DataNode.Type type = v.value();
286 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
287 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
288 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
289 .type(type)
290 .exitNode();
291 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
292 String mlpath = ResourceIdParser.appendLeafList(tempPath, names[2]);
293 LeafNode lfnode = readLeaf(mlpath);
294 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
295 .type(type)
296 .addLeafListValue(lfnode.value())
297 .exitNode();
298 //TODO this alone should be sufficient and take the nm, nmspc too
299 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
300 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
301 .type(type);
302 readInner(tempBldr, tempPath);
303 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
304 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
305 .type(type);
306 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
307 String[] keys = k.split(ResourceIdParser.KEY_SEP);
308 for (int i = 1; i < keys.length; i++) {
309 String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
310 LeafNode lfnd = readLeaf(curKey);
311 String[] keydata = keys[i].split(ResourceIdParser.NM_SEP);
312 superBldr.addKeyLeaf(keydata[0], keydata[1], lfnd.value());
313 }
314 readInner(tempBldr, tempPath);
315 } else {
316 throw new FailedException("Node type should either be LEAF or INNERNODE");
317 }
318 });
319 superBldr.exitNode();
320 }
321
322 private LeafNode readLeaf(String path) {
323 return objectStore.get(path).value();
324 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800325 @Override
326 public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) {
327 throw new FailedException("Not yet implemented");
328 }
329 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800330 public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800331 throw new FailedException("Not yet implemented");
332 }
333 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800334 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800335 throw new FailedException("Not yet implemented");
336 }
337 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800338 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800339 throw new FailedException("Not yet implemented");
340 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800341
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800342 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800343 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
344 String spath = ResourceIdParser.parseResId(path);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800345 DocumentPath dpath = DocumentPath.from(spath);
346 DataNode.Type type = null;
347 CompletableFuture<Versioned<DataNode.Type>> vtype = keystore.removeNode(dpath);
348 type = completeVersioned(vtype);
349 if (type == null) {
350 throw new FailedException("node delete failed");
351 }
352 Versioned<LeafNode> res = objectStore.remove(spath);
353 if (res == null) {
354 return CompletableFuture.completedFuture(false);
355 } else {
356 return CompletableFuture.completedFuture(true);
357 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800358 }
359
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800360 @Override
361 public void addConfigListener(ResourceId path, DynamicConfigListener listener) {
362 String lpath = ResourceIdParser.parseResId(path);
363 try {
364 lstnrStore.put(lpath, listener);
365 } catch (ConsistentMapException e) {
366 throw new FailedException(e.getCause().getMessage());
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800367 }
368 }
369
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800370 @Override
371 public void removeConfigListener(ResourceId path, DynamicConfigListener listener) {
372 String lpath = ResourceIdParser.parseResId(path);
373 try {
374 lstnrStore.remove(lpath, listener);
375 } catch (ConsistentMapException e) {
376 throw new FailedException(e.getCause().getMessage());
377 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800378 }
379
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800380 @Override
381 public Collection<? extends DynamicConfigListener> getConfigListener(ResourceId path) {
382 String lpath = ResourceIdParser.parseResId(path);
383 try {
384 Versioned<Collection<? extends DynamicConfigListener>> ls = lstnrStore.get(lpath);
385 if (ls != null) {
386 return ls.value();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800387 } else {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800388 log.info("STORE: no Listeners!!");
389 return null;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800390 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800391 } catch (ConsistentMapException e) {
392 //throw new FailedException(e.getCause().getMessage());
393 throw new FailedException("getConfigListener failed");
394 } catch (NullPointerException e) {
395 throw new FailedException(e.getCause().getMessage());
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800396 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800397 }
398
399 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
400 @Override
401 public void event(DocumentTreeEvent<DataNode.Type> event) {
402 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800403 ResourceId path;
404 switch (event.type()) {
405 case CREATED:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800406 log.info("NODE created in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800407 type = NODE_ADDED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800408 break;
409 case UPDATED:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800410 log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800411 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800412 break;
413 case DELETED:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800414 log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800415 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800416 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800417 default:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800418 log.info("UNKNOWN operation in store");
419 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800420 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800421 path = ResourceIdParser.getResId(event.path().pathElements());
422 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800423 }
424 }
425
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800426 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800427 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800428 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800429 switch (event.type()) {
430 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800431 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800432 break;
433 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800434 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800435 break;
436 case REMOVE:
437 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800438 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800439 break;
440 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800441 }
442 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800443
444 private <T> T complete(CompletableFuture<T> future) {
445 try {
446 return future.get();
447 } catch (InterruptedException e) {
448 Thread.currentThread().interrupt();
449 throw new FailedException(e.getCause().getMessage());
450 } catch (ExecutionException e) {
451 if (e.getCause() instanceof IllegalDocumentModificationException) {
452 throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node");
453 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
454 throw new FailedException("Resource id does not exist");
455 } else {
456 throw new FailedException("Datastore operation failed");
457 }
458 }
459 }
460
461 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
462 try {
463 return future.get().value();
464 } catch (InterruptedException e) {
465 Thread.currentThread().interrupt();
466 throw new FailedException(e.getCause().getMessage());
467 } catch (ExecutionException e) {
468 if (e.getCause() instanceof IllegalDocumentModificationException) {
469 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node");
470 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
471 throw new FailedException("Resource id does not exist");
472 } else {
473 throw new FailedException("Datastore operation failed");
474 }
475 }
476 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800477}