blob: 094d04c892a434e133431c87040bff8b6ca1d7ae [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;
27import org.onosproject.config.DynamicConfigStore;
28import org.onosproject.config.DynamicConfigStoreDelegate;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080029import org.onosproject.config.ResourceIdParser;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080030import org.onosproject.config.FailedException;
31import org.onosproject.config.Filter;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080032import org.onosproject.store.AbstractStore;
33import org.onosproject.store.serializers.KryoNamespaces;
34import org.onosproject.store.service.AsyncDocumentTree;
35import org.onosproject.store.service.ConsistentMap;
36import org.onosproject.store.service.DocumentPath;
37import org.onosproject.store.service.DocumentTreeEvent;
38import org.onosproject.store.service.DocumentTreeListener;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080039import org.onosproject.store.service.IllegalDocumentModificationException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080040import org.onosproject.store.service.MapEvent;
41import org.onosproject.store.service.MapEventListener;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080042import org.onosproject.store.service.NoSuchDocumentPathException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080043import org.onosproject.store.service.Serializer;
44import org.onosproject.store.service.StorageService;
45import org.onosproject.store.service.Versioned;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080046import org.onosproject.yang.model.DataNode;
47import org.onosproject.yang.model.InnerNode;
48import org.onosproject.yang.model.KeyLeaf;
49import org.onosproject.yang.model.LeafListKey;
50import org.onosproject.yang.model.LeafNode;
51import org.onosproject.yang.model.ListKey;
52import org.onosproject.yang.model.NodeKey;
53import org.onosproject.yang.model.ResourceId;
54import org.onosproject.yang.model.SchemaId;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
Sithara Punnassery43833e12017-03-14 16:29:19 -070057import java.util.List;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080058import java.util.Map;
59import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080060import java.util.concurrent.ExecutionException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080061
Sithara Punnassery44e2a702017-03-06 15:38:10 -080062import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
63import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED;
64import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
65import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN;
66
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080067/**
68 * Implementation of the dynamic config store.
69 */
Sithara Punnassery06208792017-02-10 16:25:29 -080070@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080071@Component(immediate = true)
72@Service
73public class DistributedDynamicConfigStore
74 extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate>
75 implements DynamicConfigStore {
76 private final Logger log = LoggerFactory.getLogger(getClass());
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected StorageService storageService;
79 private AsyncDocumentTree<DataNode.Type> keystore;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080080 private ConsistentMap<String, LeafNode> objectStore;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080081 private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080082 private final MapEventListener<String, LeafNode> olistener = new InternalMapListener();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080083
84 @Activate
85 public void activateStore() {
86 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
87 .register(KryoNamespaces.BASIC)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080088 .register(java.lang.Class.class)
89 .register(DataNode.Type.class)
90 .register(LeafNode.class)
91 .register(InnerNode.class)
92 .register(ResourceId.class)
93 .register(NodeKey.class)
94 .register(SchemaId.class)
Sithara Punnassery43833e12017-03-14 16:29:19 -070095 .register(LeafListKey.class)
96 .register(ListKey.class)
97 .register(KeyLeaf.class)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080098 .register(java.util.LinkedHashMap.class);
99 keystore = storageService.<DataNode.Type>documentTreeBuilder()
100 .withSerializer(Serializer.using(kryoBuilder.build()))
101 .withName("config-key-store")
102 .withRelaxedReadConsistency()
103 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800104 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800105 .withSerializer(Serializer.using(kryoBuilder.build()))
106 .withName("config-object-store")
107 .withRelaxedReadConsistency()
108 .build();
109 keystore.addListener(klistener);
110 objectStore.addListener(olistener);
111 log.info("DyanmicConfig Store Active");
112 }
113
114 @Deactivate
115 public void deactivateStore() {
116 keystore.removeListener(klistener);
117 objectStore.removeListener(olistener);
118 log.info("DyanmicConfig Store Stopped");
119 }
120
121 @Override
122 public CompletableFuture<Boolean>
123 addNode(ResourceId path, DataNode node) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800124 throw new FailedException("Not yet implemented");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800125 }
126
127 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800128 public CompletableFuture<Boolean>
129 addRecursive(ResourceId complete, DataNode node) {
130 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnassery43833e12017-03-14 16:29:19 -0700131 //Workaround
132 List<NodeKey> nodeKeyList = complete.nodeKeys();
133 NodeKey f = nodeKeyList.get(0);
134 if (f.schemaId().name().compareTo("/") == 0) {
135 nodeKeyList.remove(0);
136 }
137 //Workaround end
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800138 ResourceId path = ResourceIdParser.getParent(complete);
139 String spath = ResourceIdParser.parseResId(path);
140 if (spath == null) {
141 throw new FailedException("Invalid RsourceId, cannot create Node");
142 }
143 /*if (keystore.get(DocumentPath.from(spath)).join() == null) {
144 ////TODO is recursively creating missing parents required?
145 throw new FailedException("Some of the parents in the path " +
146 "are not present, creation not supported currently");
147 }*/
148 spath = ResourceIdParser.appendNodeKey(spath, node.key());
149 parseNode(spath, node);
150 return eventFuture;
151 }
152
153 private void parseNode(String path, DataNode node) {
154 if (keystore.get(DocumentPath.from(path)).join() != null) {
155 throw new FailedException("Requested node already present in the" +
156 " store, please use an update method");
157 }
158 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
159 addLeaf(path, (LeafNode) node);
160 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
161 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
162 if (keystore.get(DocumentPath.from(path)).join() != null) {
163 throw new FailedException("Requested node already present in the" +
164 " store, please use an update method");
165 }
166 addLeaf(path, (LeafNode) node);
167 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
168 traverseInner(path, (InnerNode) node);
169 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
170 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
171 if (keystore.get(DocumentPath.from(path)).join() != null) {
172 throw new FailedException("Requested node already present in the" +
173 " store, please use an update method");
174 }
175 traverseInner(path, (InnerNode) node);
176 } else {
177 throw new FailedException("Invalid node type");
178 }
179 }
180
181 private void traverseInner(String path, InnerNode node) {
182 addKey(path, node.type());
183 Map<NodeKey, DataNode> entries = node.childNodes();
184 if (entries.size() == 0) {
185 throw new FailedException("Inner node cannot have empty children map");
186 }
187 entries.forEach((k, v) -> {
188 String tempPath;
189 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
190 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
191 addLeaf(tempPath, (LeafNode) v);
192 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
193 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
194 addLeaf(tempPath, (LeafNode) v);
195 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
196 traverseInner(tempPath, (InnerNode) v);
197 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
198 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
199 traverseInner(path, (InnerNode) v);
200 } else {
201 throw new FailedException("Invalid node type");
202 }
203 });
204 }
205
206 private Boolean addLeaf(String path, LeafNode node) {
207 objectStore.put(path, node);
208 return addKey(path, node.type());
209 }
210
211 private Boolean addKey(String path, DataNode.Type type) {
212 Boolean stat = false;
213 CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type);
214 return complete(ret);
215 }
216
217 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800218 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
219 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery43833e12017-03-14 16:29:19 -0700220 //Workaround
221 List<NodeKey> nodeKeyList = path.nodeKeys();
222 NodeKey f = nodeKeyList.get(0);
223 if (f.schemaId().name().compareTo("/") == 0) {
224 nodeKeyList.remove(0);
225 }
226 //Workaround end
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800227 String spath = ResourceIdParser.parseResId(path);
228 DocumentPath dpath = DocumentPath.from(spath);
229 DataNode.Type type = null;
230 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
231 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800232 if (type == null) {
233 throw new FailedException("Requested node or some of the parents" +
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800234 "are not present in the requested path");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800235 }
236 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800237 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
238 retVal = readLeaf(spath);
239 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
240 retVal = readLeaf(spath);
241 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
242 NodeKey key = ResourceIdParser.getInstanceKey(path);
243 if (key == null) {
244 throw new FailedException("Key type did not match node type");
245 }
246 DataNode.Builder superBldr = InnerNode
247 .builder(key.schemaId().name(), key.schemaId().namespace())
248 .type(type);
249 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800250 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800251 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
252 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
253 if (key == null) {
254 throw new FailedException("Key type did not match node type");
255 }
256 DataNode.Builder superBldr = InnerNode
257 .builder(key.schemaId().name(), key.schemaId().namespace())
258 .type(type);
259 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
260 String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
261 LeafNode lfnd = readLeaf(tempPath);
262 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
263 keyLeaf.leafSchema().namespace(), lfnd.value());
264 }
265 readInner(superBldr, spath);
266 retVal = superBldr.build();
267 } else {
268 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800269 }
270 if (retVal != null) {
271 eventFuture = CompletableFuture.completedFuture(retVal);
272 } else {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800273 log.info("STORE: FAILED to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800274 }
275 return eventFuture;
276 }
277
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800278 private void readInner(DataNode.Builder superBldr, String spath) {
279 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
280 DocumentPath.from(spath));
281 Map<String, Versioned<DataNode.Type>> entries = null;
282 entries = complete(ret);
283 if ((entries == null) || (entries.size() == 0)) {
284 throw new FailedException("Inner node cannot have empty children map");
285 }
286 entries.forEach((k, v) -> {
287 String[] names = k.split(ResourceIdParser.NM_SEP);
288 String name = names[0];
289 String nmSpc = names[1];
290 DataNode.Type type = v.value();
291 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
292 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
293 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
294 .type(type)
295 .exitNode();
296 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
297 String mlpath = ResourceIdParser.appendLeafList(tempPath, names[2]);
298 LeafNode lfnode = readLeaf(mlpath);
299 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
300 .type(type)
301 .addLeafListValue(lfnode.value())
302 .exitNode();
303 //TODO this alone should be sufficient and take the nm, nmspc too
304 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
305 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
306 .type(type);
307 readInner(tempBldr, tempPath);
308 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
309 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
310 .type(type);
311 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
312 String[] keys = k.split(ResourceIdParser.KEY_SEP);
313 for (int i = 1; i < keys.length; i++) {
314 String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
315 LeafNode lfnd = readLeaf(curKey);
316 String[] keydata = keys[i].split(ResourceIdParser.NM_SEP);
317 superBldr.addKeyLeaf(keydata[0], keydata[1], lfnd.value());
318 }
319 readInner(tempBldr, tempPath);
320 } else {
321 throw new FailedException("Node type should either be LEAF or INNERNODE");
322 }
323 });
324 superBldr.exitNode();
325 }
326
327 private LeafNode readLeaf(String path) {
328 return objectStore.get(path).value();
329 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800330 @Override
331 public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) {
332 throw new FailedException("Not yet implemented");
333 }
334 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800335 public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800336 throw new FailedException("Not yet implemented");
337 }
338 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800339 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800340 throw new FailedException("Not yet implemented");
341 }
342 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800343 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800344 throw new FailedException("Not yet implemented");
345 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800346
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800347 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800348 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
Sithara Punnassery43833e12017-03-14 16:29:19 -0700349 //Workaround
350 List<NodeKey> nodeKeyList = path.nodeKeys();
351 NodeKey f = nodeKeyList.get(0);
352 if (f.schemaId().name().compareTo("/") == 0) {
353 nodeKeyList.remove(0);
354 }
355 //Workaround end
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800356 String spath = ResourceIdParser.parseResId(path);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800357 DocumentPath dpath = DocumentPath.from(spath);
358 DataNode.Type type = null;
359 CompletableFuture<Versioned<DataNode.Type>> vtype = keystore.removeNode(dpath);
360 type = completeVersioned(vtype);
361 if (type == null) {
362 throw new FailedException("node delete failed");
363 }
364 Versioned<LeafNode> res = objectStore.remove(spath);
365 if (res == null) {
366 return CompletableFuture.completedFuture(false);
367 } else {
368 return CompletableFuture.completedFuture(true);
369 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800370 }
371
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800372 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
373 @Override
374 public void event(DocumentTreeEvent<DataNode.Type> event) {
375 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800376 ResourceId path;
377 switch (event.type()) {
378 case CREATED:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800379 log.info("NODE created in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800380 type = NODE_ADDED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800381 break;
382 case UPDATED:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800383 log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800384 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800385 break;
386 case DELETED:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800387 log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800388 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800389 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800390 default:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800391 log.info("UNKNOWN operation in store");
392 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800393 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800394 path = ResourceIdParser.getResId(event.path().pathElements());
395 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800396 }
397 }
398
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800399 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800400 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800401 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800402 switch (event.type()) {
403 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800404 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800405 break;
406 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800407 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800408 break;
409 case REMOVE:
410 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800411 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800412 break;
413 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800414 }
415 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800416
417 private <T> T complete(CompletableFuture<T> future) {
418 try {
419 return future.get();
420 } catch (InterruptedException e) {
421 Thread.currentThread().interrupt();
422 throw new FailedException(e.getCause().getMessage());
423 } catch (ExecutionException e) {
424 if (e.getCause() instanceof IllegalDocumentModificationException) {
425 throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node");
426 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
427 throw new FailedException("Resource id does not exist");
428 } else {
429 throw new FailedException("Datastore operation failed");
430 }
431 }
432 }
433
434 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
435 try {
436 return future.get().value();
437 } catch (InterruptedException e) {
438 Thread.currentThread().interrupt();
439 throw new FailedException(e.getCause().getMessage());
440 } catch (ExecutionException e) {
441 if (e.getCause() instanceof IllegalDocumentModificationException) {
442 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node");
443 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
444 throw new FailedException("Resource id does not exist");
445 } else {
446 throw new FailedException("Datastore operation failed");
447 }
448 }
449 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800450}