blob: 18caf5f54ecd7d3d39c3c66d9b60ae4232ded5e8 [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
Gaurav Agrawalc3445a12017-11-16 20:37:58 +053060import java.math.BigDecimal;
Henry Yu5c54e772017-04-19 14:13:56 -040061import java.math.BigInteger;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070062import java.util.LinkedHashMap;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080063import java.util.Map;
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070064import java.util.Optional;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080065import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080066import java.util.concurrent.ExecutionException;
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)
Gaurav Agrawalc3445a12017-11-16 20:37:58 +0530112 .register(BigDecimal.class)
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700113 .register(LinkedHashMap.class);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800114 keystore = storageService.<DataNode.Type>documentTreeBuilder()
115 .withSerializer(Serializer.using(kryoBuilder.build()))
116 .withName("config-key-store")
117 .withRelaxedReadConsistency()
Sithara Punnasserydb3591b2017-08-10 19:44:53 -0700118 .withOrdering(Ordering.INSERTION)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800119 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800120 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800121 .withSerializer(Serializer.using(kryoBuilder.build()))
122 .withName("config-object-store")
123 .withRelaxedReadConsistency()
124 .build();
125 keystore.addListener(klistener);
126 objectStore.addListener(olistener);
Yuta HIGUCHI289f45c2017-11-01 17:49:45 -0700127 log.info("Started");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800128 }
129
130 @Deactivate
131 public void deactivateStore() {
132 keystore.removeListener(klistener);
133 objectStore.removeListener(olistener);
Yuta HIGUCHI289f45c2017-11-01 17:49:45 -0700134 log.info("Stopped");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800135 }
136
137 @Override
138 public CompletableFuture<Boolean>
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700139 addNode(ResourceId parent, DataNode node) {
140 String spath = ResourceIdParser.parseResId(parent);
141 log.trace(" addNode({}, {})", parent, node);
142 log.trace(" spath={}", spath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800143 if (spath == null) {
Yuta HIGUCHI289f45c2017-11-01 17:49:45 -0700144 throw new FailedException("Invalid ResourceId, cannot create Node");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800145 }
Yuta HIGUCHI153d3582017-09-01 16:59:52 -0700146 if (!spath.equals(ResourceIdParser.ROOT)) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700147 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700148 throw new FailedException("Node or parent does not exist for " + spath);
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700149 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700150 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700151 ResourceId abs = ResourceIds.resourceId(parent, node);
152 //spath = ResourceIdParser.appendNodeKey(spath, node.key());
153 parseNode(ResourceIdParser.parseResId(abs), node);
Yuta HIGUCHI153d3582017-09-01 16:59:52 -0700154 return CompletableFuture.completedFuture(true);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800155 }
156
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700157 // FIXME this is more like addNode
158 /**
159 * @param path pointing to {@code node}
160 * @param node node
161 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800162 private void parseNode(String path, DataNode node) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700163 log.trace("parseNode({}, {})", path, node);
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700164 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800165 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400166 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800167 }
168 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
169 addLeaf(path, (LeafNode) node);
170 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
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) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700179 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800180 throw new FailedException("Requested node already present in the" +
Henry Yu5c54e772017-04-19 14:13:56 -0400181 " store, please use an update method");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800182 }
183 traverseInner(path, (InnerNode) node);
184 } else {
185 throw new FailedException("Invalid node type");
186 }
187 }
188
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700189 // FIXME this is more like addInnteNode
190 /**
191 * @param path pointing to {@code node}
192 * @param node node
193 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800194 private void traverseInner(String path, InnerNode node) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700195 log.trace("traverseInner({}, {})", path, node);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800196 addKey(path, node.type());
197 Map<NodeKey, DataNode> entries = node.childNodes();
198 if (entries.size() == 0) {
Henry Yu5c54e772017-04-19 14:13:56 -0400199 return;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800200 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700201 // FIXME ignoring results
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800202 entries.forEach((k, v) -> {
203 String tempPath;
204 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
205 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
206 addLeaf(tempPath, (LeafNode) v);
207 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
208 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
209 addLeaf(tempPath, (LeafNode) v);
210 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
211 traverseInner(tempPath, (InnerNode) v);
212 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
213 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700214 traverseInner(tempPath, (InnerNode) v);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800215 } else {
216 throw new FailedException("Invalid node type");
217 }
218 });
219 }
220
221 private Boolean addLeaf(String path, LeafNode node) {
222 objectStore.put(path, node);
223 return addKey(path, node.type());
224 }
225
226 private Boolean addKey(String path, DataNode.Type type) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700227 log.trace("addKey({}, {})", path, type);
228 DocumentPath dpath = DocumentPath.from(path);
229 log.trace("dpath={}", dpath);
230 // FIXME Not atomic, should probably use create or replace
231 if (completeVersioned(keystore.get(dpath)) != null) {
Yuta HIGUCHI9285d972017-10-16 16:15:00 -0700232 log.trace(" keystore.set({}, {})", dpath, type);
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700233 completeVersioned(keystore.set(dpath, type));
Sithara Punnasseryc70b7e52017-08-10 14:28:08 -0700234 return true;
235 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700236 log.trace(" keystore.create({}, {})", dpath, type);
237 Boolean result = complete(keystore.create(dpath, type));
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700238 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);
Yuta HIGUCHI9285d972017-10-16 16:15:00 -0700301 log.trace(" keystore.getChildren({})", spath);
302 log.trace(" entries keys:{}", entries.keySet());
Ray Milkeyfe0e0852018-01-18 11:14:05 -0800303 if (!entries.isEmpty()) {
Sithara Punnassery18ffcc72017-05-18 14:24:30 -0700304 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 }
Yuta HIGUCHI9285d972017-10-16 16:15:00 -0700434 log.trace(" keystore.removeNode({})", spath);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700435 keystore.removeNode(DocumentPath.from(spath));
436 }
437
438 private void removeLeaf(String path) {
Yuta HIGUCHI9285d972017-10-16 16:15:00 -0700439 log.trace(" keystore.removeNode({})", path);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700440 keystore.removeNode(DocumentPath.from(path));
441 objectStore.remove(path);
442 }
443
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800444 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800445 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
446 String spath = ResourceIdParser.parseResId(path);
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700447 if (spath == null) {
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700448 throw new FailedException("Invalid RsourceId, cannot delete Node");
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700449 }
450 if (spath.compareTo(ResourceIdParser.ROOT) == 0) {
451 throw new FailedException("Cannot delete Root");
452 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800453 DocumentPath dpath = DocumentPath.from(spath);
454 DataNode.Type type = null;
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700455 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
456 type = completeVersioned(ret);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800457 if (type == null) {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700458 throw new FailedException("Cannot delete, Requested node or some of the parents" +
Henry Yu5c54e772017-04-19 14:13:56 -0400459 "are not present in the requested path");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800460 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700461 DataNode retVal = null;
462 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
463 removeLeaf(spath);
464 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
465 removeLeaf(spath);
466 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
467 deleteInner(spath);
468 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
469 deleteInner(spath);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800470 } else {
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700471 throw new FailedException("Invalid node type");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800472 }
Sithara Punnasserye4ab4f22017-03-27 19:11:00 -0700473 return CompletableFuture.completedFuture(true);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800474 }
475
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800476 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
477 @Override
478 public void event(DocumentTreeEvent<DataNode.Type> event) {
479 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800480 ResourceId path;
481 switch (event.type()) {
482 case CREATED:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800483 type = NODE_ADDED;
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700484 //log.info("NODE added in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800485 break;
486 case UPDATED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700487 //log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800488 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800489 break;
490 case DELETED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700491 //log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800492 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800493 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800494 default:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700495 //log.info("UNKNOWN operation in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800496 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800497 }
Yuta HIGUCHIdbc08c02018-02-12 16:40:05 -0800498 // FIXME don't use ResourceIdParser
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800499 path = ResourceIdParser.getResId(event.path().pathElements());
500 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800501 }
502 }
503
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800504 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800505 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800506 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800507 switch (event.type()) {
508 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800509 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800510 break;
511 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800512 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800513 break;
514 case REMOVE:
515 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800516 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800517 break;
518 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800519 }
520 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800521
522 private <T> T complete(CompletableFuture<T> future) {
523 try {
524 return future.get();
525 } catch (InterruptedException e) {
526 Thread.currentThread().interrupt();
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700527 throw new FailedException(e.getCause().getMessage());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800528 } catch (ExecutionException e) {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700529 if (e.getCause() instanceof IllegalDocumentModificationException) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700530 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 -0700531 e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800532 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700533 throw new FailedException("ResourceId does not exist", e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800534 } else {
Yuta HIGUCHIea1fe522017-09-01 14:24:02 -0700535 throw new FailedException("Datastore operation failed", e.getCause());
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800536 }
537 }
538 }
539
540 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700541 return Optional.ofNullable(complete(future))
542 .map(Versioned::value)
543 .orElse(null);
544 }
545}