blob: 1af7b34fb1de0bd58d7d14ffd61ffbe7f656e2c0 [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) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700170 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800171 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400172 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800173 }
174 addLeaf(path, (LeafNode) node);
175 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
176 traverseInner(path, (InnerNode) node);
177 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700178 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800179 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400180 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800181 }
182 traverseInner(path, (InnerNode) node);
183 } else {
184 throw new FailedException("Invalid node type");
185 }
186 }
187
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700188 // FIXME this is more like addInnteNode
189 /**
190 * @param path pointing to {@code node}
191 * @param node node
192 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800193 private void traverseInner(String path, InnerNode node) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700194 log.trace("traverseInner({}, {})", path, node);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800195 addKey(path, node.type());
196 Map<NodeKey, DataNode> entries = node.childNodes();
197 if (entries.size() == 0) {
Henry Yu5c54e772017-04-19 14:13:56 -0400198 return;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800199 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700200 // FIXME ignoring results
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800201 entries.forEach((k, v) -> {
202 String tempPath;
203 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
204 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
205 addLeaf(tempPath, (LeafNode) v);
206 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
207 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
208 addLeaf(tempPath, (LeafNode) v);
209 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
210 traverseInner(tempPath, (InnerNode) v);
211 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
212 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700213 traverseInner(tempPath, (InnerNode) v);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800214 } else {
215 throw new FailedException("Invalid node type");
216 }
217 });
218 }
219
220 private Boolean addLeaf(String path, LeafNode node) {
221 objectStore.put(path, node);
222 return addKey(path, node.type());
223 }
224
225 private Boolean addKey(String path, DataNode.Type type) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700226 log.trace("addKey({}, {})", path, type);
227 DocumentPath dpath = DocumentPath.from(path);
228 log.trace("dpath={}", dpath);
229 // FIXME Not atomic, should probably use create or replace
230 if (completeVersioned(keystore.get(dpath)) != null) {
231 completeVersioned(keystore.set(dpath, type));
232 log.trace("true");
Sithara Punnasseryc70b7e52017-08-10 14:28:08 -0700233 return true;
234 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700235 log.trace(" keystore.create({}, {})", dpath, type);
236 Boolean result = complete(keystore.create(dpath, type));
237 log.trace("{}", result);
238 return result;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800239 }
240
241 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800242 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
243 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800244 String spath = ResourceIdParser.parseResId(path);
245 DocumentPath dpath = DocumentPath.from(spath);
246 DataNode.Type type = null;
247 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
248 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800249 if (type == null) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700250 throw new FailedException("Requested node or some of the parents " +
251 "are not present in the requested path: " +
252 spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800253 }
254 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800255 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
256 retVal = readLeaf(spath);
257 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
258 retVal = readLeaf(spath);
259 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
260 NodeKey key = ResourceIdParser.getInstanceKey(path);
261 if (key == null) {
262 throw new FailedException("Key type did not match node type");
263 }
264 DataNode.Builder superBldr = InnerNode
265 .builder(key.schemaId().name(), key.schemaId().namespace())
266 .type(type);
267 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800268 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800269 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
270 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
271 if (key == null) {
272 throw new FailedException("Key type did not match node type");
273 }
274 DataNode.Builder superBldr = InnerNode
275 .builder(key.schemaId().name(), key.schemaId().namespace())
276 .type(type);
277 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700278 //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
279 //LeafNode lfnd = readLeaf(tempPath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800280 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
Henry Yu5c54e772017-04-19 14:13:56 -0400281 keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue()));
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800282 }
283 readInner(superBldr, spath);
284 retVal = superBldr.build();
285 } else {
286 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800287 }
288 if (retVal != null) {
289 eventFuture = CompletableFuture.completedFuture(retVal);
290 } else {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700291 log.info("STORE: Failed to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800292 }
293 return eventFuture;
294 }
295
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800296 private void readInner(DataNode.Builder superBldr, String spath) {
297 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
298 DocumentPath.from(spath));
299 Map<String, Versioned<DataNode.Type>> entries = null;
300 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700301 if ((entries != null) && (!entries.isEmpty())) {
302 entries.forEach((k, v) -> {
303 String[] names = k.split(ResourceIdParser.NM_CHK);
304 String name = names[0];
305 String nmSpc = ResourceIdParser.getNamespace(names[1]);
306 String keyVal = ResourceIdParser.getKeyVal(names[1]);
307 DataNode.Type type = v.value();
308 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
309 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
310 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
311 .type(type)
312 .exitNode();
313 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
314 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
315 LeafNode lfnode = readLeaf(mlpath);
316 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
317 .type(type)
318 .addLeafListValue(lfnode.value())
319 .exitNode();
320 //TODO this alone should be sufficient and take the nm, nmspc too
321 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
322 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
323 .type(type);
324 readInner(tempBldr, tempPath);
325 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
326 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
327 .type(type);
328 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
329 String[] keys = k.split(ResourceIdParser.KEY_CHK);
330 for (int i = 1; i < keys.length; i++) {
331 //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
332 //LeafNode lfnd = readLeaf(curKey);
333 String[] keydata = keys[i].split(ResourceIdParser.NM_CHK);
334 tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]);
335 }
336 readInner(tempBldr, tempPath);
337 } else {
338 throw new FailedException("Invalid node type");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800339 }
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700340 });
341 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800342 superBldr.exitNode();
343 }
344
345 private LeafNode readLeaf(String path) {
346 return objectStore.get(path).value();
347 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700348
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700349 private void parseForUpdate(String path, DataNode node) {
350 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
351 addLeaf(path, (LeafNode) node);
352 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
353 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
354 addLeaf(path, (LeafNode) node);
355 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
356 traverseInner(path, (InnerNode) node);
357 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
358 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
359 traverseInner(path, (InnerNode) node);
360 } else {
361 throw new FailedException("Invalid node type");
362 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800363 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700364
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800365 @Override
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700366 public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) {
367 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnassery0da1a9c2017-05-17 16:16:22 -0700368 String spath = ResourceIdParser.parseResId(complete);
369 if (spath == null) {
370 throw new FailedException("Invalid RsourceId, cannot update Node");
371 }
372 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
373 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
374 throw new FailedException("Node or parent doesnot exist, cannot update");
375 }
376 }
377 spath = ResourceIdParser.appendNodeKey(spath, node.key());
378 parseForUpdate(spath, node);
379 return eventFuture;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800380 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700381
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800382 @Override
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700383 public CompletableFuture<Boolean> nodeExist(ResourceId complete) {
384 Boolean stat = true;
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700385 String spath = ResourceIdParser.parseResId(complete);
386 if (spath == null) {
387 stat = false;
388 } else if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
389 stat = false;
390 }
391 return CompletableFuture.completedFuture(stat);
392 }
393
394 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800395 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800396 throw new FailedException("Not yet implemented");
397 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700398
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800399 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800400 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800401 throw new FailedException("Not yet implemented");
402 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800403
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700404 private void deleteInner(String spath) {
405 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
406 DocumentPath.from(spath));
407 Map<String, Versioned<DataNode.Type>> entries = null;
408 entries = complete(ret);
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700409 if ((entries != null) && (!entries.isEmpty())) {
410 entries.forEach((k, v) -> {
411 String[] names = k.split(ResourceIdParser.NM_CHK);
412 String name = names[0];
413 String nmSpc = ResourceIdParser.getNamespace(names[1]);
414 String keyVal = ResourceIdParser.getKeyVal(names[1]);
415 DataNode.Type type = v.value();
416 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
417 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
418 removeLeaf(tempPath);
419 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
420 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
421 removeLeaf(mlpath);
422 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
423 deleteInner(tempPath);
424 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
425 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
426 deleteInner(tempPath);
427 } else {
428 throw new FailedException("Invalid node type");
429 }
430 });
431 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700432 keystore.removeNode(DocumentPath.from(spath));
433 }
434
435 private void removeLeaf(String path) {
436 keystore.removeNode(DocumentPath.from(path));
437 objectStore.remove(path);
438 }
439
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800440 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800441 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
442 String spath = ResourceIdParser.parseResId(path);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700443 if (spath == null) {
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700444 throw new FailedException("Invalid RsourceId, cannot delete Node");
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700445 }
446 if (spath.compareTo(ResourceIdParser.ROOT) == 0) {
447 throw new FailedException("Cannot delete Root");
448 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800449 DocumentPath dpath = DocumentPath.from(spath);
450 DataNode.Type type = null;
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700451 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
452 type = completeVersioned(ret);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800453 if (type == null) {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700454 throw new FailedException("Cannot delete, Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400455 "are not present in the requested path");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800456 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700457 DataNode retVal = null;
458 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
459 removeLeaf(spath);
460 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
461 removeLeaf(spath);
462 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
463 deleteInner(spath);
464 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
465 deleteInner(spath);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800466 } else {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700467 throw new FailedException("Invalid node type");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800468 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700469 return CompletableFuture.completedFuture(true);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800470 }
471
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800472 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
473 @Override
474 public void event(DocumentTreeEvent<DataNode.Type> event) {
475 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800476 ResourceId path;
477 switch (event.type()) {
478 case CREATED:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800479 type = NODE_ADDED;
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700480 //log.info("NODE added in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800481 break;
482 case UPDATED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700483 //log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800484 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800485 break;
486 case DELETED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700487 //log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800488 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800489 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800490 default:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700491 //log.info("UNKNOWN operation in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800492 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800493 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800494 path = ResourceIdParser.getResId(event.path().pathElements());
495 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800496 }
497 }
498
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800499 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800500 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800501 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800502 switch (event.type()) {
503 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800504 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800505 break;
506 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800507 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800508 break;
509 case REMOVE:
510 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800511 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800512 break;
513 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800514 }
515 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800516
517 private <T> T complete(CompletableFuture<T> future) {
518 try {
519 return future.get();
520 } catch (InterruptedException e) {
521 Thread.currentThread().interrupt();
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700522 throw new FailedException(e.getCause().getMessage());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800523 } catch (ExecutionException e) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700524 if (e.getCause() instanceof IllegalDocumentModificationException) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700525 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 -0700526 e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800527 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700528 throw new FailedException("ResourceId does not exist", e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800529 } else {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700530 throw new FailedException("Datastore operation failed", e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800531 }
532 }
533 }
534
535 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700536 return Optional.ofNullable(complete(future))
537 .map(Versioned::value)
538 .orElse(null);
539 }
540}