blob: d1359e3659b4a53b2c7d17740114fe4bcfd099db [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;
29import org.onosproject.config.FailedException;
30import org.onosproject.config.Filter;
Henry Yu5c54e772017-04-19 14:13:56 -040031import org.onosproject.config.ResourceIdParser;
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;
Henry Yu5c54e772017-04-19 14:13:56 -040057
58import java.math.BigInteger;
Sithara Punnassery43833e12017-03-14 16:29:19 -070059import java.util.List;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080060import java.util.Map;
61import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080062import java.util.concurrent.ExecutionException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080063
Sithara Punnassery44e2a702017-03-06 15:38:10 -080064import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080065import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
Henry Yu5c54e772017-04-19 14:13:56 -040066import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080067import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN;
68
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080069/**
70 * Implementation of the dynamic config store.
71 */
Sithara Punnassery06208792017-02-10 16:25:29 -080072@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080073@Component(immediate = true)
74@Service
75public class DistributedDynamicConfigStore
76 extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate>
77 implements DynamicConfigStore {
78 private final Logger log = LoggerFactory.getLogger(getClass());
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected StorageService storageService;
81 private AsyncDocumentTree<DataNode.Type> keystore;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080082 private ConsistentMap<String, LeafNode> objectStore;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080083 private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080084 private final MapEventListener<String, LeafNode> olistener = new InternalMapListener();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080085
86 @Activate
87 public void activateStore() {
88 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
89 .register(KryoNamespaces.BASIC)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080090 .register(java.lang.Class.class)
91 .register(DataNode.Type.class)
92 .register(LeafNode.class)
93 .register(InnerNode.class)
94 .register(ResourceId.class)
95 .register(NodeKey.class)
96 .register(SchemaId.class)
Sithara Punnassery43833e12017-03-14 16:29:19 -070097 .register(LeafListKey.class)
98 .register(ListKey.class)
99 .register(KeyLeaf.class)
Henry Yu5c54e772017-04-19 14:13:56 -0400100 .register(BigInteger.class)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800101 .register(java.util.LinkedHashMap.class);
102 keystore = storageService.<DataNode.Type>documentTreeBuilder()
103 .withSerializer(Serializer.using(kryoBuilder.build()))
104 .withName("config-key-store")
105 .withRelaxedReadConsistency()
106 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800107 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800108 .withSerializer(Serializer.using(kryoBuilder.build()))
109 .withName("config-object-store")
110 .withRelaxedReadConsistency()
111 .build();
112 keystore.addListener(klistener);
113 objectStore.addListener(olistener);
114 log.info("DyanmicConfig Store Active");
115 }
116
117 @Deactivate
118 public void deactivateStore() {
119 keystore.removeListener(klistener);
120 objectStore.removeListener(olistener);
121 log.info("DyanmicConfig Store Stopped");
122 }
123
124 @Override
125 public CompletableFuture<Boolean>
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700126 addNode(ResourceId complete, DataNode node) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800127 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnassery43833e12017-03-14 16:29:19 -0700128 List<NodeKey> nodeKeyList = complete.nodeKeys();
129 NodeKey f = nodeKeyList.get(0);
130 if (f.schemaId().name().compareTo("/") == 0) {
131 nodeKeyList.remove(0);
132 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700133 String spath = ResourceIdParser.parseResId(complete);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800134 if (spath == null) {
135 throw new FailedException("Invalid RsourceId, cannot create Node");
136 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700137 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
138 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
139 throw new FailedException("Node or parent doesnot exist");
140 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700141 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800142 spath = ResourceIdParser.appendNodeKey(spath, node.key());
143 parseNode(spath, node);
144 return eventFuture;
145 }
146
147 private void parseNode(String path, DataNode node) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700148 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800149 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400150 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800151 }
152 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
153 addLeaf(path, (LeafNode) node);
154 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
155 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700156 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800157 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400158 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800159 }
160 addLeaf(path, (LeafNode) node);
161 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
162 traverseInner(path, (InnerNode) node);
163 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
164 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700165 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800166 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400167 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800168 }
169 traverseInner(path, (InnerNode) node);
170 } else {
171 throw new FailedException("Invalid node type");
172 }
173 }
174
175 private void traverseInner(String path, InnerNode node) {
176 addKey(path, node.type());
177 Map<NodeKey, DataNode> entries = node.childNodes();
178 if (entries.size() == 0) {
Henry Yu5c54e772017-04-19 14:13:56 -0400179 return;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800180 }
181 entries.forEach((k, v) -> {
182 String tempPath;
183 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
184 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
185 addLeaf(tempPath, (LeafNode) v);
186 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
187 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
188 addLeaf(tempPath, (LeafNode) v);
189 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
190 traverseInner(tempPath, (InnerNode) v);
191 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
192 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700193 traverseInner(tempPath, (InnerNode) v);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800194 } else {
195 throw new FailedException("Invalid node type");
196 }
197 });
198 }
199
200 private Boolean addLeaf(String path, LeafNode node) {
201 objectStore.put(path, node);
202 return addKey(path, node.type());
203 }
204
205 private Boolean addKey(String path, DataNode.Type type) {
206 Boolean stat = false;
207 CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type);
208 return complete(ret);
209 }
210
211 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800212 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
213 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery43833e12017-03-14 16:29:19 -0700214 List<NodeKey> nodeKeyList = path.nodeKeys();
215 NodeKey f = nodeKeyList.get(0);
216 if (f.schemaId().name().compareTo("/") == 0) {
217 nodeKeyList.remove(0);
218 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800219 String spath = ResourceIdParser.parseResId(path);
220 DocumentPath dpath = DocumentPath.from(spath);
221 DataNode.Type type = null;
222 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
223 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800224 if (type == null) {
225 throw new FailedException("Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400226 "are not present in the requested path");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800227 }
228 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800229 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
230 retVal = readLeaf(spath);
231 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
232 retVal = readLeaf(spath);
233 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
234 NodeKey key = ResourceIdParser.getInstanceKey(path);
235 if (key == null) {
236 throw new FailedException("Key type did not match node type");
237 }
238 DataNode.Builder superBldr = InnerNode
239 .builder(key.schemaId().name(), key.schemaId().namespace())
240 .type(type);
241 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800242 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800243 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
244 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
245 if (key == null) {
246 throw new FailedException("Key type did not match node type");
247 }
248 DataNode.Builder superBldr = InnerNode
249 .builder(key.schemaId().name(), key.schemaId().namespace())
250 .type(type);
251 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700252 //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
253 //LeafNode lfnd = readLeaf(tempPath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800254 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
Henry Yu5c54e772017-04-19 14:13:56 -0400255 keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue()));
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800256 }
257 readInner(superBldr, spath);
258 retVal = superBldr.build();
259 } else {
260 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800261 }
262 if (retVal != null) {
263 eventFuture = CompletableFuture.completedFuture(retVal);
264 } else {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700265 log.info("STORE: Failed to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800266 }
267 return eventFuture;
268 }
269
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800270 private void readInner(DataNode.Builder superBldr, String spath) {
271 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
272 DocumentPath.from(spath));
273 Map<String, Versioned<DataNode.Type>> entries = null;
274 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700275 if ((entries != null) && (!entries.isEmpty())) {
276 entries.forEach((k, v) -> {
277 String[] names = k.split(ResourceIdParser.NM_CHK);
278 String name = names[0];
279 String nmSpc = ResourceIdParser.getNamespace(names[1]);
280 String keyVal = ResourceIdParser.getKeyVal(names[1]);
281 DataNode.Type type = v.value();
282 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
283 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
284 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
285 .type(type)
286 .exitNode();
287 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
288 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
289 LeafNode lfnode = readLeaf(mlpath);
290 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
291 .type(type)
292 .addLeafListValue(lfnode.value())
293 .exitNode();
294 //TODO this alone should be sufficient and take the nm, nmspc too
295 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
296 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
297 .type(type);
298 readInner(tempBldr, tempPath);
299 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
300 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
301 .type(type);
302 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
303 String[] keys = k.split(ResourceIdParser.KEY_CHK);
304 for (int i = 1; i < keys.length; i++) {
305 //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
306 //LeafNode lfnd = readLeaf(curKey);
307 String[] keydata = keys[i].split(ResourceIdParser.NM_CHK);
308 tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]);
309 }
310 readInner(tempBldr, tempPath);
311 } else {
312 throw new FailedException("Invalid node type");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800313 }
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700314 });
315 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800316 superBldr.exitNode();
317 }
318
319 private LeafNode readLeaf(String path) {
320 return objectStore.get(path).value();
321 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700322
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700323 private void parseForUpdate(String path, DataNode node) {
324 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
325 addLeaf(path, (LeafNode) node);
326 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
327 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
328 addLeaf(path, (LeafNode) node);
329 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
330 traverseInner(path, (InnerNode) node);
331 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
332 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
333 traverseInner(path, (InnerNode) node);
334 } else {
335 throw new FailedException("Invalid node type");
336 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800337 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700338
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800339 @Override
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700340 public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) {
341 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
342 List<NodeKey> nodeKeyList = complete.nodeKeys();
343 NodeKey f = nodeKeyList.get(0);
344 if (f.schemaId().name().compareTo("/") == 0) {
345 nodeKeyList.remove(0);
346 }
347 String spath = ResourceIdParser.parseResId(complete);
348 if (spath == null) {
349 throw new FailedException("Invalid RsourceId, cannot update Node");
350 }
351 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
352 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
353 throw new FailedException("Node or parent doesnot exist, cannot update");
354 }
355 }
356 spath = ResourceIdParser.appendNodeKey(spath, node.key());
357 parseForUpdate(spath, node);
358 return eventFuture;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800359 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700360
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800361 @Override
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700362 public CompletableFuture<Boolean> nodeExist(ResourceId complete) {
363 Boolean stat = true;
364 List<NodeKey> nodeKeyList = complete.nodeKeys();
365 NodeKey f = nodeKeyList.get(0);
366 if (f.schemaId().name().compareTo("/") == 0) {
367 nodeKeyList.remove(0);
368 }
369 String spath = ResourceIdParser.parseResId(complete);
370 if (spath == null) {
371 stat = false;
372 } else if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
373 stat = false;
374 }
375 return CompletableFuture.completedFuture(stat);
376 }
377
378 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800379 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800380 throw new FailedException("Not yet implemented");
381 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700382
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800383 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800384 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800385 throw new FailedException("Not yet implemented");
386 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800387
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700388 private void deleteInner(String spath) {
389 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
390 DocumentPath.from(spath));
391 Map<String, Versioned<DataNode.Type>> entries = null;
392 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700393 if ((entries != null) && (!entries.isEmpty())) {
394 entries.forEach((k, v) -> {
395 String[] names = k.split(ResourceIdParser.NM_CHK);
396 String name = names[0];
397 String nmSpc = ResourceIdParser.getNamespace(names[1]);
398 String keyVal = ResourceIdParser.getKeyVal(names[1]);
399 DataNode.Type type = v.value();
400 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
401 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
402 removeLeaf(tempPath);
403 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
404 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
405 removeLeaf(mlpath);
406 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
407 deleteInner(tempPath);
408 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
409 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
410 deleteInner(tempPath);
411 } else {
412 throw new FailedException("Invalid node type");
413 }
414 });
415 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700416 keystore.removeNode(DocumentPath.from(spath));
417 }
418
419 private void removeLeaf(String path) {
420 keystore.removeNode(DocumentPath.from(path));
421 objectStore.remove(path);
422 }
423
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800424 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800425 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
Sithara Punnassery43833e12017-03-14 16:29:19 -0700426 List<NodeKey> nodeKeyList = path.nodeKeys();
427 NodeKey f = nodeKeyList.get(0);
428 if (f.schemaId().name().compareTo("/") == 0) {
429 nodeKeyList.remove(0);
430 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800431 String spath = ResourceIdParser.parseResId(path);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700432 if (spath == null) {
433 throw new FailedException("Invalid RsourceId, cannot create Node");
434 }
435 if (spath.compareTo(ResourceIdParser.ROOT) == 0) {
436 throw new FailedException("Cannot delete Root");
437 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800438 DocumentPath dpath = DocumentPath.from(spath);
439 DataNode.Type type = null;
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700440 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
441 type = completeVersioned(ret);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800442 if (type == null) {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700443 throw new FailedException("Cannot delete, Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400444 "are not present in the requested path");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800445 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700446 DataNode retVal = null;
447 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
448 removeLeaf(spath);
449 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
450 removeLeaf(spath);
451 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
452 deleteInner(spath);
453 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
454 deleteInner(spath);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800455 } else {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700456 throw new FailedException("Invalid node type");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800457 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700458 return CompletableFuture.completedFuture(true);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800459 }
460
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800461 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
462 @Override
463 public void event(DocumentTreeEvent<DataNode.Type> event) {
464 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800465 ResourceId path;
466 switch (event.type()) {
467 case CREATED:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800468 type = NODE_ADDED;
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700469 //log.info("NODE added in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800470 break;
471 case UPDATED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700472 //log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800473 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800474 break;
475 case DELETED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700476 //log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800477 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800478 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800479 default:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700480 //log.info("UNKNOWN operation in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800481 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800482 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800483 path = ResourceIdParser.getResId(event.path().pathElements());
484 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800485 }
486 }
487
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800488 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800489 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800490 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800491 switch (event.type()) {
492 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800493 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800494 break;
495 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800496 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800497 break;
498 case REMOVE:
499 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800500 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800501 break;
502 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800503 }
504 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800505
506 private <T> T complete(CompletableFuture<T> future) {
507 try {
508 return future.get();
509 } catch (InterruptedException e) {
510 Thread.currentThread().interrupt();
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700511 if (e == null) {
512 throw new FailedException("Unknown Exception");
513 } else {
514 throw new FailedException(e.getCause().getMessage());
515 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800516 } catch (ExecutionException e) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700517 if (e == null) {
518 throw new FailedException("Unknown Exception");
519 } else if (e.getCause() instanceof IllegalDocumentModificationException) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800520 throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node");
521 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
522 throw new FailedException("Resource id does not exist");
523 } else {
524 throw new FailedException("Datastore operation failed");
525 }
526 }
527 }
528
529 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
530 try {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700531 if (future.get() != null) {
532 return future.get().value();
533 } else {
534 return null;
535 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800536 } catch (InterruptedException e) {
537 Thread.currentThread().interrupt();
538 throw new FailedException(e.getCause().getMessage());
539 } catch (ExecutionException e) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700540 if (e == null) {
541 throw new FailedException("Unknown Exception");
542 } else if (e.getCause() instanceof IllegalDocumentModificationException) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800543 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node");
544 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
545 throw new FailedException("Resource id does not exist");
546 } else {
547 throw new FailedException("Datastore operation failed");
548 }
549 }
550 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800551}