blob: 97731a692e2e8c13430c395a9779a28c769d8a33 [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 *
Sithara Punnassery61a80252017-08-07 11:16:08 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08009 *
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;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070032import org.onosproject.d.config.ResourceIds;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080033import org.onosproject.store.AbstractStore;
34import org.onosproject.store.serializers.KryoNamespaces;
35import org.onosproject.store.service.AsyncDocumentTree;
36import org.onosproject.store.service.ConsistentMap;
37import org.onosproject.store.service.DocumentPath;
38import org.onosproject.store.service.DocumentTreeEvent;
39import org.onosproject.store.service.DocumentTreeListener;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080040import org.onosproject.store.service.IllegalDocumentModificationException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080041import org.onosproject.store.service.MapEvent;
42import org.onosproject.store.service.MapEventListener;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080043import org.onosproject.store.service.NoSuchDocumentPathException;
Sithara Punnasserydb3591b2017-08-10 19:44:53 -070044import org.onosproject.store.service.Ordering;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080045import org.onosproject.store.service.Serializer;
46import org.onosproject.store.service.StorageService;
47import org.onosproject.store.service.Versioned;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080048import org.onosproject.yang.model.DataNode;
49import org.onosproject.yang.model.InnerNode;
50import org.onosproject.yang.model.KeyLeaf;
51import org.onosproject.yang.model.LeafListKey;
52import org.onosproject.yang.model.LeafNode;
53import org.onosproject.yang.model.ListKey;
54import org.onosproject.yang.model.NodeKey;
55import org.onosproject.yang.model.ResourceId;
56import org.onosproject.yang.model.SchemaId;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080057import org.slf4j.Logger;
58import org.slf4j.LoggerFactory;
Henry Yu5c54e772017-04-19 14:13:56 -040059
60import java.math.BigInteger;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070061import java.util.LinkedHashMap;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080062import java.util.Map;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070063import java.util.Optional;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080064import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080065import java.util.concurrent.ExecutionException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080066
Sithara Punnassery44e2a702017-03-06 15:38:10 -080067import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080068import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
Henry Yu5c54e772017-04-19 14:13:56 -040069import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080070import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN;
71
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080072/**
73 * Implementation of the dynamic config store.
74 */
Sithara Punnassery06208792017-02-10 16:25:29 -080075@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080076@Component(immediate = true)
77@Service
78public class DistributedDynamicConfigStore
79 extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate>
80 implements DynamicConfigStore {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070081
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080082 private final Logger log = LoggerFactory.getLogger(getClass());
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070083
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected StorageService storageService;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070086
87 // FIXME transactionally mutate the 2 or consolidate into 1 AsyncDocTree
88 // effectively tree structure only
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080089 private AsyncDocumentTree<DataNode.Type> keystore;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070090 // TODO Can we pass DocumentPath directly to ConsistentMap?
91 // Map<DocumentPath as String, leaf value>
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080092 private ConsistentMap<String, LeafNode> objectStore;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070093
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080094 private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080095 private final MapEventListener<String, LeafNode> olistener = new InternalMapListener();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080096
97 @Activate
98 public void activateStore() {
99 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
100 .register(KryoNamespaces.BASIC)
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700101 .register(Class.class)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800102 .register(DataNode.Type.class)
103 .register(LeafNode.class)
104 .register(InnerNode.class)
105 .register(ResourceId.class)
106 .register(NodeKey.class)
107 .register(SchemaId.class)
Sithara Punnassery43833e12017-03-14 16:29:19 -0700108 .register(LeafListKey.class)
109 .register(ListKey.class)
110 .register(KeyLeaf.class)
Henry Yu5c54e772017-04-19 14:13:56 -0400111 .register(BigInteger.class)
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700112 .register(LinkedHashMap.class);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800113 keystore = storageService.<DataNode.Type>documentTreeBuilder()
114 .withSerializer(Serializer.using(kryoBuilder.build()))
115 .withName("config-key-store")
116 .withRelaxedReadConsistency()
Sithara Punnasserydb3591b2017-08-10 19:44:53 -0700117 .withOrdering(Ordering.INSERTION)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800118 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800119 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800120 .withSerializer(Serializer.using(kryoBuilder.build()))
121 .withName("config-object-store")
122 .withRelaxedReadConsistency()
123 .build();
124 keystore.addListener(klistener);
125 objectStore.addListener(olistener);
126 log.info("DyanmicConfig Store Active");
127 }
128
129 @Deactivate
130 public void deactivateStore() {
131 keystore.removeListener(klistener);
132 objectStore.removeListener(olistener);
133 log.info("DyanmicConfig Store Stopped");
134 }
135
136 @Override
137 public CompletableFuture<Boolean>
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700138 addNode(ResourceId parent, DataNode node) {
139 String spath = ResourceIdParser.parseResId(parent);
140 log.trace(" addNode({}, {})", parent, node);
141 log.trace(" spath={}", spath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800142 if (spath == null) {
143 throw new FailedException("Invalid RsourceId, cannot create Node");
144 }
Yuta HIGUCHI153d3582017-09-01 16:59:52 -0700145 if (!spath.equals(ResourceIdParser.ROOT)) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700146 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700147 throw new FailedException("Node or parent does not exist for " + spath);
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700148 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700149 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700150 ResourceId abs = ResourceIds.resourceId(parent, node);
151 //spath = ResourceIdParser.appendNodeKey(spath, node.key());
152 parseNode(ResourceIdParser.parseResId(abs), node);
Yuta HIGUCHI153d3582017-09-01 16:59:52 -0700153 return CompletableFuture.completedFuture(true);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800154 }
155
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700156 // FIXME this is more like addNode
157 /**
158 * @param path pointing to {@code node}
159 * @param node node
160 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800161 private void parseNode(String path, DataNode node) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700162 log.trace("parseNode({}, {})", path, node);
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700163 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800164 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400165 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800166 }
167 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
168 addLeaf(path, (LeafNode) node);
169 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
170 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700171 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800172 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400173 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800174 }
175 addLeaf(path, (LeafNode) node);
176 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
177 traverseInner(path, (InnerNode) node);
178 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
179 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700180 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800181 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400182 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800183 }
184 traverseInner(path, (InnerNode) node);
185 } else {
186 throw new FailedException("Invalid node type");
187 }
188 }
189
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700190 // FIXME this is more like addInnteNode
191 /**
192 * @param path pointing to {@code node}
193 * @param node node
194 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800195 private void traverseInner(String path, InnerNode node) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700196 log.trace("traverseInner({}, {})", path, node);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800197 addKey(path, node.type());
198 Map<NodeKey, DataNode> entries = node.childNodes();
199 if (entries.size() == 0) {
Henry Yu5c54e772017-04-19 14:13:56 -0400200 return;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800201 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700202 // FIXME ignoring results
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800203 entries.forEach((k, v) -> {
204 String tempPath;
205 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
206 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
207 addLeaf(tempPath, (LeafNode) v);
208 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
209 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
210 addLeaf(tempPath, (LeafNode) v);
211 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
212 traverseInner(tempPath, (InnerNode) v);
213 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
214 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700215 traverseInner(tempPath, (InnerNode) v);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800216 } else {
217 throw new FailedException("Invalid node type");
218 }
219 });
220 }
221
222 private Boolean addLeaf(String path, LeafNode node) {
223 objectStore.put(path, node);
224 return addKey(path, node.type());
225 }
226
227 private Boolean addKey(String path, DataNode.Type type) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700228 log.trace("addKey({}, {})", path, type);
229 DocumentPath dpath = DocumentPath.from(path);
230 log.trace("dpath={}", dpath);
231 // FIXME Not atomic, should probably use create or replace
232 if (completeVersioned(keystore.get(dpath)) != null) {
233 completeVersioned(keystore.set(dpath, type));
234 log.trace("true");
Sithara Punnasseryc70b7e52017-08-10 14:28:08 -0700235 return true;
236 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700237 log.trace(" keystore.create({}, {})", dpath, type);
238 Boolean result = complete(keystore.create(dpath, type));
239 log.trace("{}", result);
240 return result;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800241 }
242
243 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800244 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
245 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800246 String spath = ResourceIdParser.parseResId(path);
247 DocumentPath dpath = DocumentPath.from(spath);
248 DataNode.Type type = null;
249 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
250 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800251 if (type == null) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700252 throw new FailedException("Requested node or some of the parents " +
253 "are not present in the requested path: " +
254 spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800255 }
256 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800257 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
258 retVal = readLeaf(spath);
259 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
260 retVal = readLeaf(spath);
261 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
262 NodeKey key = ResourceIdParser.getInstanceKey(path);
263 if (key == null) {
264 throw new FailedException("Key type did not match node type");
265 }
266 DataNode.Builder superBldr = InnerNode
267 .builder(key.schemaId().name(), key.schemaId().namespace())
268 .type(type);
269 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800270 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800271 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
272 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
273 if (key == null) {
274 throw new FailedException("Key type did not match node type");
275 }
276 DataNode.Builder superBldr = InnerNode
277 .builder(key.schemaId().name(), key.schemaId().namespace())
278 .type(type);
279 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700280 //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
281 //LeafNode lfnd = readLeaf(tempPath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800282 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
Henry Yu5c54e772017-04-19 14:13:56 -0400283 keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue()));
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800284 }
285 readInner(superBldr, spath);
286 retVal = superBldr.build();
287 } else {
288 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800289 }
290 if (retVal != null) {
291 eventFuture = CompletableFuture.completedFuture(retVal);
292 } else {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700293 log.info("STORE: Failed to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800294 }
295 return eventFuture;
296 }
297
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800298 private void readInner(DataNode.Builder superBldr, String spath) {
299 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
300 DocumentPath.from(spath));
301 Map<String, Versioned<DataNode.Type>> entries = null;
302 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700303 if ((entries != null) && (!entries.isEmpty())) {
304 entries.forEach((k, v) -> {
305 String[] names = k.split(ResourceIdParser.NM_CHK);
306 String name = names[0];
307 String nmSpc = ResourceIdParser.getNamespace(names[1]);
308 String keyVal = ResourceIdParser.getKeyVal(names[1]);
309 DataNode.Type type = v.value();
310 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
311 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
312 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
313 .type(type)
314 .exitNode();
315 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
316 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
317 LeafNode lfnode = readLeaf(mlpath);
318 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
319 .type(type)
320 .addLeafListValue(lfnode.value())
321 .exitNode();
322 //TODO this alone should be sufficient and take the nm, nmspc too
323 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
324 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
325 .type(type);
326 readInner(tempBldr, tempPath);
327 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
328 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
329 .type(type);
330 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
331 String[] keys = k.split(ResourceIdParser.KEY_CHK);
332 for (int i = 1; i < keys.length; i++) {
333 //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
334 //LeafNode lfnd = readLeaf(curKey);
335 String[] keydata = keys[i].split(ResourceIdParser.NM_CHK);
336 tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]);
337 }
338 readInner(tempBldr, tempPath);
339 } else {
340 throw new FailedException("Invalid node type");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800341 }
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700342 });
343 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800344 superBldr.exitNode();
345 }
346
347 private LeafNode readLeaf(String path) {
348 return objectStore.get(path).value();
349 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700350
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700351 private void parseForUpdate(String path, DataNode node) {
352 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
353 addLeaf(path, (LeafNode) node);
354 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
355 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
356 addLeaf(path, (LeafNode) node);
357 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
358 traverseInner(path, (InnerNode) node);
359 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
360 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
361 traverseInner(path, (InnerNode) node);
362 } else {
363 throw new FailedException("Invalid node type");
364 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800365 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700366
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800367 @Override
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700368 public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) {
369 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700370 String spath = ResourceIdParser.parseResId(complete);
371 if (spath == null) {
372 throw new FailedException("Invalid RsourceId, cannot update Node");
373 }
374 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
375 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
376 throw new FailedException("Node or parent doesnot exist, cannot update");
377 }
378 }
379 spath = ResourceIdParser.appendNodeKey(spath, node.key());
380 parseForUpdate(spath, node);
381 return eventFuture;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800382 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700383
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800384 @Override
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700385 public CompletableFuture<Boolean> nodeExist(ResourceId complete) {
386 Boolean stat = true;
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700387 String spath = ResourceIdParser.parseResId(complete);
388 if (spath == null) {
389 stat = false;
390 } else if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
391 stat = false;
392 }
393 return CompletableFuture.completedFuture(stat);
394 }
395
396 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800397 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800398 throw new FailedException("Not yet implemented");
399 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700400
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800401 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800402 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800403 throw new FailedException("Not yet implemented");
404 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800405
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700406 private void deleteInner(String spath) {
407 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
408 DocumentPath.from(spath));
409 Map<String, Versioned<DataNode.Type>> entries = null;
410 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700411 if ((entries != null) && (!entries.isEmpty())) {
412 entries.forEach((k, v) -> {
413 String[] names = k.split(ResourceIdParser.NM_CHK);
414 String name = names[0];
415 String nmSpc = ResourceIdParser.getNamespace(names[1]);
416 String keyVal = ResourceIdParser.getKeyVal(names[1]);
417 DataNode.Type type = v.value();
418 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
419 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
420 removeLeaf(tempPath);
421 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
422 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
423 removeLeaf(mlpath);
424 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
425 deleteInner(tempPath);
426 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
427 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
428 deleteInner(tempPath);
429 } else {
430 throw new FailedException("Invalid node type");
431 }
432 });
433 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700434 keystore.removeNode(DocumentPath.from(spath));
435 }
436
437 private void removeLeaf(String path) {
438 keystore.removeNode(DocumentPath.from(path));
439 objectStore.remove(path);
440 }
441
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800442 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800443 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
444 String spath = ResourceIdParser.parseResId(path);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700445 if (spath == null) {
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700446 throw new FailedException("Invalid RsourceId, cannot delete Node");
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700447 }
448 if (spath.compareTo(ResourceIdParser.ROOT) == 0) {
449 throw new FailedException("Cannot delete Root");
450 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800451 DocumentPath dpath = DocumentPath.from(spath);
452 DataNode.Type type = null;
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700453 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
454 type = completeVersioned(ret);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800455 if (type == null) {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700456 throw new FailedException("Cannot delete, Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400457 "are not present in the requested path");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800458 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700459 DataNode retVal = null;
460 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
461 removeLeaf(spath);
462 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
463 removeLeaf(spath);
464 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
465 deleteInner(spath);
466 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
467 deleteInner(spath);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800468 } else {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700469 throw new FailedException("Invalid node type");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800470 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700471 return CompletableFuture.completedFuture(true);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800472 }
473
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800474 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
475 @Override
476 public void event(DocumentTreeEvent<DataNode.Type> event) {
477 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800478 ResourceId path;
479 switch (event.type()) {
480 case CREATED:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800481 type = NODE_ADDED;
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700482 //log.info("NODE added in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800483 break;
484 case UPDATED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700485 //log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800486 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800487 break;
488 case DELETED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700489 //log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800490 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800491 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800492 default:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700493 //log.info("UNKNOWN operation in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800494 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800495 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800496 path = ResourceIdParser.getResId(event.path().pathElements());
497 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800498 }
499 }
500
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800501 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800502 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800503 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800504 switch (event.type()) {
505 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800506 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800507 break;
508 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800509 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800510 break;
511 case REMOVE:
512 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800513 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800514 break;
515 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800516 }
517 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800518
519 private <T> T complete(CompletableFuture<T> future) {
520 try {
521 return future.get();
522 } catch (InterruptedException e) {
523 Thread.currentThread().interrupt();
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700524 throw new FailedException(e.getCause().getMessage());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800525 } catch (ExecutionException e) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700526 if (e.getCause() instanceof IllegalDocumentModificationException) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700527 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node",
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700528 e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800529 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700530 throw new FailedException("ResourceId does not exist", e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800531 } else {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700532 throw new FailedException("Datastore operation failed", e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800533 }
534 }
535 }
536
537 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700538 return Optional.ofNullable(complete(future))
539 .map(Versioned::value)
540 .orElse(null);
541 }
542}