blob: 6fba8af7a701da1fabfd7f75d960071c8c7d3781 [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08003 *
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 Punnassery9306e6b2017-02-06 15:38:19 -080059import java.util.Map;
60import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080061import java.util.concurrent.ExecutionException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080062
Sithara Punnassery44e2a702017-03-06 15:38:10 -080063import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080064import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
Henry Yu5c54e772017-04-19 14:13:56 -040065import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080066import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN;
67
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080068/**
69 * Implementation of the dynamic config store.
70 */
Sithara Punnassery06208792017-02-10 16:25:29 -080071@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080072@Component(immediate = true)
73@Service
74public class DistributedDynamicConfigStore
75 extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate>
76 implements DynamicConfigStore {
77 private final Logger log = LoggerFactory.getLogger(getClass());
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected StorageService storageService;
80 private AsyncDocumentTree<DataNode.Type> keystore;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080081 private ConsistentMap<String, LeafNode> objectStore;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080082 private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080083 private final MapEventListener<String, LeafNode> olistener = new InternalMapListener();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080084
85 @Activate
86 public void activateStore() {
87 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
88 .register(KryoNamespaces.BASIC)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080089 .register(java.lang.Class.class)
90 .register(DataNode.Type.class)
91 .register(LeafNode.class)
92 .register(InnerNode.class)
93 .register(ResourceId.class)
94 .register(NodeKey.class)
95 .register(SchemaId.class)
Sithara Punnassery43833e12017-03-14 16:29:19 -070096 .register(LeafListKey.class)
97 .register(ListKey.class)
98 .register(KeyLeaf.class)
Henry Yu5c54e772017-04-19 14:13:56 -040099 .register(BigInteger.class)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800100 .register(java.util.LinkedHashMap.class);
101 keystore = storageService.<DataNode.Type>documentTreeBuilder()
102 .withSerializer(Serializer.using(kryoBuilder.build()))
103 .withName("config-key-store")
104 .withRelaxedReadConsistency()
105 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800106 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800107 .withSerializer(Serializer.using(kryoBuilder.build()))
108 .withName("config-object-store")
109 .withRelaxedReadConsistency()
110 .build();
111 keystore.addListener(klistener);
112 objectStore.addListener(olistener);
113 log.info("DyanmicConfig Store Active");
114 }
115
116 @Deactivate
117 public void deactivateStore() {
118 keystore.removeListener(klistener);
119 objectStore.removeListener(olistener);
120 log.info("DyanmicConfig Store Stopped");
121 }
122
123 @Override
124 public CompletableFuture<Boolean>
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700125 addNode(ResourceId complete, DataNode node) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800126 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnasserybb644902017-03-16 22:08:29 -0700127 String spath = ResourceIdParser.parseResId(complete);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800128 if (spath == null) {
129 throw new FailedException("Invalid RsourceId, cannot create Node");
130 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700131 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
132 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
133 throw new FailedException("Node or parent doesnot exist");
134 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700135 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800136 spath = ResourceIdParser.appendNodeKey(spath, node.key());
137 parseNode(spath, node);
138 return eventFuture;
139 }
140
141 private void parseNode(String path, DataNode node) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700142 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800143 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400144 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800145 }
146 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
147 addLeaf(path, (LeafNode) node);
148 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
149 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700150 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800151 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400152 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800153 }
154 addLeaf(path, (LeafNode) node);
155 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
156 traverseInner(path, (InnerNode) node);
157 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
158 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700159 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800160 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400161 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800162 }
163 traverseInner(path, (InnerNode) node);
164 } else {
165 throw new FailedException("Invalid node type");
166 }
167 }
168
169 private void traverseInner(String path, InnerNode node) {
170 addKey(path, node.type());
171 Map<NodeKey, DataNode> entries = node.childNodes();
172 if (entries.size() == 0) {
Henry Yu5c54e772017-04-19 14:13:56 -0400173 return;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800174 }
175 entries.forEach((k, v) -> {
176 String tempPath;
177 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
178 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
179 addLeaf(tempPath, (LeafNode) v);
180 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
181 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
182 addLeaf(tempPath, (LeafNode) v);
183 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
184 traverseInner(tempPath, (InnerNode) v);
185 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
186 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700187 traverseInner(tempPath, (InnerNode) v);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800188 } else {
189 throw new FailedException("Invalid node type");
190 }
191 });
192 }
193
194 private Boolean addLeaf(String path, LeafNode node) {
195 objectStore.put(path, node);
196 return addKey(path, node.type());
197 }
198
199 private Boolean addKey(String path, DataNode.Type type) {
200 Boolean stat = false;
201 CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type);
202 return complete(ret);
203 }
204
205 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800206 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
207 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800208 String spath = ResourceIdParser.parseResId(path);
209 DocumentPath dpath = DocumentPath.from(spath);
210 DataNode.Type type = null;
211 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
212 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800213 if (type == null) {
214 throw new FailedException("Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400215 "are not present in the requested path");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800216 }
217 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800218 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
219 retVal = readLeaf(spath);
220 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
221 retVal = readLeaf(spath);
222 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
223 NodeKey key = ResourceIdParser.getInstanceKey(path);
224 if (key == null) {
225 throw new FailedException("Key type did not match node type");
226 }
227 DataNode.Builder superBldr = InnerNode
228 .builder(key.schemaId().name(), key.schemaId().namespace())
229 .type(type);
230 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800231 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800232 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
233 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
234 if (key == null) {
235 throw new FailedException("Key type did not match node type");
236 }
237 DataNode.Builder superBldr = InnerNode
238 .builder(key.schemaId().name(), key.schemaId().namespace())
239 .type(type);
240 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700241 //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
242 //LeafNode lfnd = readLeaf(tempPath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800243 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
Henry Yu5c54e772017-04-19 14:13:56 -0400244 keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue()));
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800245 }
246 readInner(superBldr, spath);
247 retVal = superBldr.build();
248 } else {
249 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800250 }
251 if (retVal != null) {
252 eventFuture = CompletableFuture.completedFuture(retVal);
253 } else {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700254 log.info("STORE: Failed to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800255 }
256 return eventFuture;
257 }
258
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800259 private void readInner(DataNode.Builder superBldr, String spath) {
260 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
261 DocumentPath.from(spath));
262 Map<String, Versioned<DataNode.Type>> entries = null;
263 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700264 if ((entries != null) && (!entries.isEmpty())) {
265 entries.forEach((k, v) -> {
266 String[] names = k.split(ResourceIdParser.NM_CHK);
267 String name = names[0];
268 String nmSpc = ResourceIdParser.getNamespace(names[1]);
269 String keyVal = ResourceIdParser.getKeyVal(names[1]);
270 DataNode.Type type = v.value();
271 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
272 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
273 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
274 .type(type)
275 .exitNode();
276 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
277 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
278 LeafNode lfnode = readLeaf(mlpath);
279 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
280 .type(type)
281 .addLeafListValue(lfnode.value())
282 .exitNode();
283 //TODO this alone should be sufficient and take the nm, nmspc too
284 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
285 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
286 .type(type);
287 readInner(tempBldr, tempPath);
288 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
289 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
290 .type(type);
291 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
292 String[] keys = k.split(ResourceIdParser.KEY_CHK);
293 for (int i = 1; i < keys.length; i++) {
294 //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
295 //LeafNode lfnd = readLeaf(curKey);
296 String[] keydata = keys[i].split(ResourceIdParser.NM_CHK);
297 tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]);
298 }
299 readInner(tempBldr, tempPath);
300 } else {
301 throw new FailedException("Invalid node type");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800302 }
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700303 });
304 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800305 superBldr.exitNode();
306 }
307
308 private LeafNode readLeaf(String path) {
309 return objectStore.get(path).value();
310 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700311
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700312 private void parseForUpdate(String path, DataNode node) {
313 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
314 addLeaf(path, (LeafNode) node);
315 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
316 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
317 addLeaf(path, (LeafNode) node);
318 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
319 traverseInner(path, (InnerNode) node);
320 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
321 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
322 traverseInner(path, (InnerNode) node);
323 } else {
324 throw new FailedException("Invalid node type");
325 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800326 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700327
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800328 @Override
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700329 public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) {
330 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700331 String spath = ResourceIdParser.parseResId(complete);
332 if (spath == null) {
333 throw new FailedException("Invalid RsourceId, cannot update Node");
334 }
335 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
336 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
337 throw new FailedException("Node or parent doesnot exist, cannot update");
338 }
339 }
340 spath = ResourceIdParser.appendNodeKey(spath, node.key());
341 parseForUpdate(spath, node);
342 return eventFuture;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800343 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700344
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800345 @Override
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700346 public CompletableFuture<Boolean> nodeExist(ResourceId complete) {
347 Boolean stat = true;
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700348 String spath = ResourceIdParser.parseResId(complete);
349 if (spath == null) {
350 stat = false;
351 } else if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
352 stat = false;
353 }
354 return CompletableFuture.completedFuture(stat);
355 }
356
357 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800358 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800359 throw new FailedException("Not yet implemented");
360 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700361
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800362 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800363 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800364 throw new FailedException("Not yet implemented");
365 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800366
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700367 private void deleteInner(String spath) {
368 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
369 DocumentPath.from(spath));
370 Map<String, Versioned<DataNode.Type>> entries = null;
371 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700372 if ((entries != null) && (!entries.isEmpty())) {
373 entries.forEach((k, v) -> {
374 String[] names = k.split(ResourceIdParser.NM_CHK);
375 String name = names[0];
376 String nmSpc = ResourceIdParser.getNamespace(names[1]);
377 String keyVal = ResourceIdParser.getKeyVal(names[1]);
378 DataNode.Type type = v.value();
379 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
380 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
381 removeLeaf(tempPath);
382 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
383 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
384 removeLeaf(mlpath);
385 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
386 deleteInner(tempPath);
387 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
388 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
389 deleteInner(tempPath);
390 } else {
391 throw new FailedException("Invalid node type");
392 }
393 });
394 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700395 keystore.removeNode(DocumentPath.from(spath));
396 }
397
398 private void removeLeaf(String path) {
399 keystore.removeNode(DocumentPath.from(path));
400 objectStore.remove(path);
401 }
402
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800403 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800404 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
405 String spath = ResourceIdParser.parseResId(path);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700406 if (spath == null) {
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700407 throw new FailedException("Invalid RsourceId, cannot delete Node");
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700408 }
409 if (spath.compareTo(ResourceIdParser.ROOT) == 0) {
410 throw new FailedException("Cannot delete Root");
411 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800412 DocumentPath dpath = DocumentPath.from(spath);
413 DataNode.Type type = null;
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700414 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
415 type = completeVersioned(ret);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800416 if (type == null) {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700417 throw new FailedException("Cannot delete, Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400418 "are not present in the requested path");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800419 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700420 DataNode retVal = null;
421 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
422 removeLeaf(spath);
423 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
424 removeLeaf(spath);
425 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
426 deleteInner(spath);
427 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
428 deleteInner(spath);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800429 } else {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700430 throw new FailedException("Invalid node type");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800431 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700432 return CompletableFuture.completedFuture(true);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800433 }
434
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800435 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
436 @Override
437 public void event(DocumentTreeEvent<DataNode.Type> event) {
438 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800439 ResourceId path;
440 switch (event.type()) {
441 case CREATED:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800442 type = NODE_ADDED;
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700443 //log.info("NODE added in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800444 break;
445 case UPDATED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700446 //log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800447 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800448 break;
449 case DELETED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700450 //log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800451 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800452 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800453 default:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700454 //log.info("UNKNOWN operation in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800455 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800456 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800457 path = ResourceIdParser.getResId(event.path().pathElements());
458 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800459 }
460 }
461
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800462 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800463 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800464 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800465 switch (event.type()) {
466 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800467 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800468 break;
469 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800470 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800471 break;
472 case REMOVE:
473 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800474 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800475 break;
476 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800477 }
478 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800479
480 private <T> T complete(CompletableFuture<T> future) {
481 try {
482 return future.get();
483 } catch (InterruptedException e) {
484 Thread.currentThread().interrupt();
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700485 if (e == null) {
486 throw new FailedException("Unknown Exception");
487 } else {
488 throw new FailedException(e.getCause().getMessage());
489 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800490 } catch (ExecutionException e) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700491 if (e == null) {
492 throw new FailedException("Unknown Exception");
493 } else if (e.getCause() instanceof IllegalDocumentModificationException) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800494 throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node");
495 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
496 throw new FailedException("Resource id does not exist");
497 } else {
498 throw new FailedException("Datastore operation failed");
499 }
500 }
501 }
502
503 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
504 try {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700505 if (future.get() != null) {
506 return future.get().value();
507 } else {
508 return null;
509 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800510 } catch (InterruptedException e) {
511 Thread.currentThread().interrupt();
512 throw new FailedException(e.getCause().getMessage());
513 } catch (ExecutionException e) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700514 if (e == null) {
515 throw new FailedException("Unknown Exception");
516 } else if (e.getCause() instanceof IllegalDocumentModificationException) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800517 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node");
518 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
519 throw new FailedException("Resource id does not exist");
520 } else {
521 throw new FailedException("Datastore operation failed");
522 }
523 }
524 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800525}