blob: e4fcb0e79741e2fe173860abd8722cb7c62edd49 [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.config.impl;
17
Sithara Punnassery06208792017-02-10 16:25:29 -080018import com.google.common.annotations.Beta;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.config.DynamicConfigEvent;
27import org.onosproject.config.DynamicConfigStore;
28import org.onosproject.config.DynamicConfigStoreDelegate;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080029import org.onosproject.config.ResourceIdParser;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080030import org.onosproject.config.FailedException;
31import org.onosproject.config.Filter;
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;
Sithara Punnassery43833e12017-03-14 16:29:19 -070057import java.util.List;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080058import java.util.Map;
59import java.util.concurrent.CompletableFuture;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080060import java.util.concurrent.ExecutionException;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080061
Sithara Punnassery44e2a702017-03-06 15:38:10 -080062import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
63import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED;
64import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
65import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN;
66
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080067/**
68 * Implementation of the dynamic config store.
69 */
Sithara Punnassery06208792017-02-10 16:25:29 -080070@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080071@Component(immediate = true)
72@Service
73public class DistributedDynamicConfigStore
74 extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate>
75 implements DynamicConfigStore {
76 private final Logger log = LoggerFactory.getLogger(getClass());
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected StorageService storageService;
79 private AsyncDocumentTree<DataNode.Type> keystore;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080080 private ConsistentMap<String, LeafNode> objectStore;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080081 private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080082 private final MapEventListener<String, LeafNode> olistener = new InternalMapListener();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080083
84 @Activate
85 public void activateStore() {
86 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
87 .register(KryoNamespaces.BASIC)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080088 .register(java.lang.Class.class)
89 .register(DataNode.Type.class)
90 .register(LeafNode.class)
91 .register(InnerNode.class)
92 .register(ResourceId.class)
93 .register(NodeKey.class)
94 .register(SchemaId.class)
Sithara Punnassery43833e12017-03-14 16:29:19 -070095 .register(LeafListKey.class)
96 .register(ListKey.class)
97 .register(KeyLeaf.class)
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080098 .register(java.util.LinkedHashMap.class);
99 keystore = storageService.<DataNode.Type>documentTreeBuilder()
100 .withSerializer(Serializer.using(kryoBuilder.build()))
101 .withName("config-key-store")
102 .withRelaxedReadConsistency()
103 .buildDocumentTree();
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800104 objectStore = storageService.<String, LeafNode>consistentMapBuilder()
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800105 .withSerializer(Serializer.using(kryoBuilder.build()))
106 .withName("config-object-store")
107 .withRelaxedReadConsistency()
108 .build();
109 keystore.addListener(klistener);
110 objectStore.addListener(olistener);
111 log.info("DyanmicConfig Store Active");
112 }
113
114 @Deactivate
115 public void deactivateStore() {
116 keystore.removeListener(klistener);
117 objectStore.removeListener(olistener);
118 log.info("DyanmicConfig Store Stopped");
119 }
120
121 @Override
122 public CompletableFuture<Boolean>
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700123 addNode(ResourceId complete, DataNode node) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800124 CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
Sithara Punnassery43833e12017-03-14 16:29:19 -0700125 List<NodeKey> nodeKeyList = complete.nodeKeys();
126 NodeKey f = nodeKeyList.get(0);
127 if (f.schemaId().name().compareTo("/") == 0) {
128 nodeKeyList.remove(0);
129 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700130 String spath = ResourceIdParser.parseResId(complete);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800131 if (spath == null) {
132 throw new FailedException("Invalid RsourceId, cannot create Node");
133 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700134 if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
135 if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
136 throw new FailedException("Node or parent doesnot exist");
137 }
Sithara Punnasserybb644902017-03-16 22:08:29 -0700138 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800139 spath = ResourceIdParser.appendNodeKey(spath, node.key());
140 parseNode(spath, node);
141 return eventFuture;
142 }
143
144 private void parseNode(String path, DataNode node) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700145 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800146 throw new FailedException("Requested node already present in the" +
147 " store, please use an update method");
148 }
149 if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
150 addLeaf(path, (LeafNode) node);
151 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
152 path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700153 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800154 throw new FailedException("Requested node already present in the" +
155 " store, please use an update method");
156 }
157 addLeaf(path, (LeafNode) node);
158 } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
159 traverseInner(path, (InnerNode) node);
160 } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
161 path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700162 if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800163 throw new FailedException("Requested node already present in the" +
164 " store, please use an update method");
165 }
166 traverseInner(path, (InnerNode) node);
167 } else {
168 throw new FailedException("Invalid node type");
169 }
170 }
171
172 private void traverseInner(String path, InnerNode node) {
173 addKey(path, node.type());
174 Map<NodeKey, DataNode> entries = node.childNodes();
175 if (entries.size() == 0) {
176 throw new FailedException("Inner node cannot have empty children map");
177 }
178 entries.forEach((k, v) -> {
179 String tempPath;
180 tempPath = ResourceIdParser.appendNodeKey(path, v.key());
181 if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
182 addLeaf(tempPath, (LeafNode) v);
183 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
184 tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key());
185 addLeaf(tempPath, (LeafNode) v);
186 } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
187 traverseInner(tempPath, (InnerNode) v);
188 } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
189 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700190 traverseInner(tempPath, (InnerNode) v);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800191 } else {
192 throw new FailedException("Invalid node type");
193 }
194 });
195 }
196
197 private Boolean addLeaf(String path, LeafNode node) {
198 objectStore.put(path, node);
199 return addKey(path, node.type());
200 }
201
202 private Boolean addKey(String path, DataNode.Type type) {
203 Boolean stat = false;
204 CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type);
205 return complete(ret);
206 }
207
208 @Override
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800209 public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) {
210 CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null);
Sithara Punnassery43833e12017-03-14 16:29:19 -0700211 List<NodeKey> nodeKeyList = path.nodeKeys();
212 NodeKey f = nodeKeyList.get(0);
213 if (f.schemaId().name().compareTo("/") == 0) {
214 nodeKeyList.remove(0);
215 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800216 String spath = ResourceIdParser.parseResId(path);
217 DocumentPath dpath = DocumentPath.from(spath);
218 DataNode.Type type = null;
219 CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath);
220 type = completeVersioned(ret);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800221 if (type == null) {
222 throw new FailedException("Requested node or some of the parents" +
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800223 "are not present in the requested path");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800224 }
225 DataNode retVal = null;
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800226 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
227 retVal = readLeaf(spath);
228 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
229 retVal = readLeaf(spath);
230 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
231 NodeKey key = ResourceIdParser.getInstanceKey(path);
232 if (key == null) {
233 throw new FailedException("Key type did not match node type");
234 }
235 DataNode.Builder superBldr = InnerNode
236 .builder(key.schemaId().name(), key.schemaId().namespace())
237 .type(type);
238 readInner(superBldr, spath);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800239 retVal = superBldr.build();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800240 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
241 NodeKey key = ResourceIdParser.getMultiInstanceKey(path);
242 if (key == null) {
243 throw new FailedException("Key type did not match node type");
244 }
245 DataNode.Builder superBldr = InnerNode
246 .builder(key.schemaId().name(), key.schemaId().namespace())
247 .type(type);
248 for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700249 //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
250 //LeafNode lfnd = readLeaf(tempPath);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800251 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
Sithara Punnasserybda82502017-03-22 19:08:19 -0700252 keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue()));
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800253 }
254 readInner(superBldr, spath);
255 retVal = superBldr.build();
256 } else {
257 throw new FailedException("Invalid node type");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800258 }
259 if (retVal != null) {
260 eventFuture = CompletableFuture.completedFuture(retVal);
261 } else {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700262 log.info("STORE: Failed to READ node");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800263 }
264 return eventFuture;
265 }
266
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800267 private void readInner(DataNode.Builder superBldr, String spath) {
268 CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(
269 DocumentPath.from(spath));
270 Map<String, Versioned<DataNode.Type>> entries = null;
271 entries = complete(ret);
272 if ((entries == null) || (entries.size() == 0)) {
273 throw new FailedException("Inner node cannot have empty children map");
274 }
275 entries.forEach((k, v) -> {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700276 String[] names = k.split(ResourceIdParser.NM_CHK);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800277 String name = names[0];
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700278 String nmSpc = ResourceIdParser.getNamespace(names[1]);
279 String keyVal = ResourceIdParser.getKeyVal(names[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800280 DataNode.Type type = v.value();
281 String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
282 if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
283 superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value())
284 .type(type)
285 .exitNode();
286 } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700287 String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800288 LeafNode lfnode = readLeaf(mlpath);
289 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
290 .type(type)
291 .addLeafListValue(lfnode.value())
292 .exitNode();
293 //TODO this alone should be sufficient and take the nm, nmspc too
294 } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
295 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
296 .type(type);
297 readInner(tempBldr, tempPath);
298 } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
299 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
300 .type(type);
301 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700302 String[] keys = k.split(ResourceIdParser.KEY_CHK);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800303 for (int i = 1; i < keys.length; i++) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700304 //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
305 //LeafNode lfnd = readLeaf(curKey);
Sithara Punnasserybda82502017-03-22 19:08:19 -0700306 String[] keydata = keys[i].split(ResourceIdParser.NM_CHK);
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700307 tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800308 }
309 readInner(tempBldr, tempPath);
310 } else {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700311 throw new FailedException("Invalid node type");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800312 }
313 });
314 superBldr.exitNode();
315 }
316
317 private LeafNode readLeaf(String path) {
318 return objectStore.get(path).value();
319 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700320
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800321 @Override
322 public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) {
323 throw new FailedException("Not yet implemented");
324 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700325
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800326 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800327 public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800328 throw new FailedException("Not yet implemented");
329 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700330
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800331 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800332 public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800333 throw new FailedException("Not yet implemented");
334 }
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700335
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800336 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800337 public CompletableFuture<Boolean> deleteNode(ResourceId path) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800338 throw new FailedException("Not yet implemented");
339 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800340
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800341 @Override
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800342 public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) {
Sithara Punnassery43833e12017-03-14 16:29:19 -0700343 List<NodeKey> nodeKeyList = path.nodeKeys();
344 NodeKey f = nodeKeyList.get(0);
345 if (f.schemaId().name().compareTo("/") == 0) {
346 nodeKeyList.remove(0);
347 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800348 String spath = ResourceIdParser.parseResId(path);
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800349 DocumentPath dpath = DocumentPath.from(spath);
350 DataNode.Type type = null;
351 CompletableFuture<Versioned<DataNode.Type>> vtype = keystore.removeNode(dpath);
352 type = completeVersioned(vtype);
353 if (type == null) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700354 throw new FailedException("Node delete failed");
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800355 }
356 Versioned<LeafNode> res = objectStore.remove(spath);
357 if (res == null) {
358 return CompletableFuture.completedFuture(false);
359 } else {
360 return CompletableFuture.completedFuture(true);
361 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800362 }
363
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800364 public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> {
365 @Override
366 public void event(DocumentTreeEvent<DataNode.Type> event) {
367 DynamicConfigEvent.Type type;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800368 ResourceId path;
369 switch (event.type()) {
370 case CREATED:
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800371 type = NODE_ADDED;
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700372 //log.info("NODE added in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800373 break;
374 case UPDATED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700375 //log.info("NODE updated in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800376 type = NODE_UPDATED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800377 break;
378 case DELETED:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700379 //log.info("NODE deleted in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800380 type = NODE_DELETED;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800381 break;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800382 default:
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700383 //log.info("UNKNOWN operation in store");
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800384 type = UNKNOWN_OPRN;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800385 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800386 path = ResourceIdParser.getResId(event.path().pathElements());
387 notifyDelegate(new DynamicConfigEvent(type, path));
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800388 }
389 }
390
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800391 public class InternalMapListener implements MapEventListener<String, LeafNode> {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800392 @Override
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800393 public void event(MapEvent<String, LeafNode> event) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800394 switch (event.type()) {
395 case INSERT:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800396 //log.info("NODE created in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800397 break;
398 case UPDATE:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800399 //log.info("NODE updated in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800400 break;
401 case REMOVE:
402 default:
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800403 //log.info("NODE removed in store");
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800404 break;
405 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800406 }
407 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800408
409 private <T> T complete(CompletableFuture<T> future) {
410 try {
411 return future.get();
412 } catch (InterruptedException e) {
413 Thread.currentThread().interrupt();
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700414 if (e == null) {
415 throw new FailedException("Unknown Exception");
416 } else {
417 throw new FailedException(e.getCause().getMessage());
418 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800419 } catch (ExecutionException e) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700420 if (e == null) {
421 throw new FailedException("Unknown Exception");
422 } else if (e.getCause() instanceof IllegalDocumentModificationException) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800423 throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node");
424 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
425 throw new FailedException("Resource id does not exist");
426 } else {
427 throw new FailedException("Datastore operation failed");
428 }
429 }
430 }
431
432 private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
433 try {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700434 if (future.get() != null) {
435 return future.get().value();
436 } else {
437 return null;
438 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800439 } catch (InterruptedException e) {
440 Thread.currentThread().interrupt();
441 throw new FailedException(e.getCause().getMessage());
442 } catch (ExecutionException e) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700443 if (e == null) {
444 throw new FailedException("Unknown Exception");
445 } else if (e.getCause() instanceof IllegalDocumentModificationException) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800446 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node");
447 } else if (e.getCause() instanceof NoSuchDocumentPathException) {
448 throw new FailedException("Resource id does not exist");
449 } else {
450 throw new FailedException("Datastore operation failed");
451 }
452 }
453 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800454}