Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.config.impl; |
| 17 | |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.config.DynamicConfigEvent; |
| 27 | import org.onosproject.config.DynamicConfigStore; |
| 28 | import org.onosproject.config.DynamicConfigStoreDelegate; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 29 | import org.onosproject.config.ResourceIdParser; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 30 | import org.onosproject.config.FailedException; |
| 31 | import org.onosproject.config.Filter; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 32 | import org.onosproject.store.AbstractStore; |
| 33 | import org.onosproject.store.serializers.KryoNamespaces; |
| 34 | import org.onosproject.store.service.AsyncDocumentTree; |
| 35 | import org.onosproject.store.service.ConsistentMap; |
| 36 | import org.onosproject.store.service.DocumentPath; |
| 37 | import org.onosproject.store.service.DocumentTreeEvent; |
| 38 | import org.onosproject.store.service.DocumentTreeListener; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 39 | import org.onosproject.store.service.IllegalDocumentModificationException; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 40 | import org.onosproject.store.service.MapEvent; |
| 41 | import org.onosproject.store.service.MapEventListener; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 42 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 43 | import org.onosproject.store.service.Serializer; |
| 44 | import org.onosproject.store.service.StorageService; |
| 45 | import org.onosproject.store.service.Versioned; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 46 | import org.onosproject.yang.model.DataNode; |
| 47 | import org.onosproject.yang.model.InnerNode; |
| 48 | import org.onosproject.yang.model.KeyLeaf; |
| 49 | import org.onosproject.yang.model.LeafListKey; |
| 50 | import org.onosproject.yang.model.LeafNode; |
| 51 | import org.onosproject.yang.model.ListKey; |
| 52 | import org.onosproject.yang.model.NodeKey; |
| 53 | import org.onosproject.yang.model.ResourceId; |
| 54 | import org.onosproject.yang.model.SchemaId; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | import org.slf4j.LoggerFactory; |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame^] | 57 | import java.util.List; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 58 | import java.util.Map; |
| 59 | import java.util.concurrent.CompletableFuture; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 60 | import java.util.concurrent.ExecutionException; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 61 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 62 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED; |
| 63 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED; |
| 64 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED; |
| 65 | import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN; |
| 66 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Implementation of the dynamic config store. |
| 69 | */ |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 70 | @Beta |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 71 | @Component(immediate = true) |
| 72 | @Service |
| 73 | public 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 80 | private ConsistentMap<String, LeafNode> objectStore; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 81 | private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener(); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 82 | private final MapEventListener<String, LeafNode> olistener = new InternalMapListener(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 83 | |
| 84 | @Activate |
| 85 | public void activateStore() { |
| 86 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 87 | .register(KryoNamespaces.BASIC) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 88 | .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 Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame^] | 95 | .register(LeafListKey.class) |
| 96 | .register(ListKey.class) |
| 97 | .register(KeyLeaf.class) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 98 | .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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 104 | objectStore = storageService.<String, LeafNode>consistentMapBuilder() |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 105 | .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> |
| 123 | addNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 124 | throw new FailedException("Not yet implemented"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 128 | public CompletableFuture<Boolean> |
| 129 | addRecursive(ResourceId complete, DataNode node) { |
| 130 | CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame^] | 131 | //Workaround |
| 132 | List<NodeKey> nodeKeyList = complete.nodeKeys(); |
| 133 | NodeKey f = nodeKeyList.get(0); |
| 134 | if (f.schemaId().name().compareTo("/") == 0) { |
| 135 | nodeKeyList.remove(0); |
| 136 | } |
| 137 | //Workaround end |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 138 | ResourceId path = ResourceIdParser.getParent(complete); |
| 139 | String spath = ResourceIdParser.parseResId(path); |
| 140 | if (spath == null) { |
| 141 | throw new FailedException("Invalid RsourceId, cannot create Node"); |
| 142 | } |
| 143 | /*if (keystore.get(DocumentPath.from(spath)).join() == null) { |
| 144 | ////TODO is recursively creating missing parents required? |
| 145 | throw new FailedException("Some of the parents in the path " + |
| 146 | "are not present, creation not supported currently"); |
| 147 | }*/ |
| 148 | spath = ResourceIdParser.appendNodeKey(spath, node.key()); |
| 149 | parseNode(spath, node); |
| 150 | return eventFuture; |
| 151 | } |
| 152 | |
| 153 | private void parseNode(String path, DataNode node) { |
| 154 | if (keystore.get(DocumentPath.from(path)).join() != null) { |
| 155 | throw new FailedException("Requested node already present in the" + |
| 156 | " store, please use an update method"); |
| 157 | } |
| 158 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 159 | addLeaf(path, (LeafNode) node); |
| 160 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 161 | path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key()); |
| 162 | if (keystore.get(DocumentPath.from(path)).join() != null) { |
| 163 | throw new FailedException("Requested node already present in the" + |
| 164 | " store, please use an update method"); |
| 165 | } |
| 166 | addLeaf(path, (LeafNode) node); |
| 167 | } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 168 | traverseInner(path, (InnerNode) node); |
| 169 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 170 | path = ResourceIdParser.appendKeyList(path, (ListKey) node.key()); |
| 171 | if (keystore.get(DocumentPath.from(path)).join() != null) { |
| 172 | throw new FailedException("Requested node already present in the" + |
| 173 | " store, please use an update method"); |
| 174 | } |
| 175 | traverseInner(path, (InnerNode) node); |
| 176 | } else { |
| 177 | throw new FailedException("Invalid node type"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void traverseInner(String path, InnerNode node) { |
| 182 | addKey(path, node.type()); |
| 183 | Map<NodeKey, DataNode> entries = node.childNodes(); |
| 184 | if (entries.size() == 0) { |
| 185 | throw new FailedException("Inner node cannot have empty children map"); |
| 186 | } |
| 187 | entries.forEach((k, v) -> { |
| 188 | String tempPath; |
| 189 | tempPath = ResourceIdParser.appendNodeKey(path, v.key()); |
| 190 | if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 191 | addLeaf(tempPath, (LeafNode) v); |
| 192 | } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 193 | tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key()); |
| 194 | addLeaf(tempPath, (LeafNode) v); |
| 195 | } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 196 | traverseInner(tempPath, (InnerNode) v); |
| 197 | } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 198 | tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key()); |
| 199 | traverseInner(path, (InnerNode) v); |
| 200 | } else { |
| 201 | throw new FailedException("Invalid node type"); |
| 202 | } |
| 203 | }); |
| 204 | } |
| 205 | |
| 206 | private Boolean addLeaf(String path, LeafNode node) { |
| 207 | objectStore.put(path, node); |
| 208 | return addKey(path, node.type()); |
| 209 | } |
| 210 | |
| 211 | private Boolean addKey(String path, DataNode.Type type) { |
| 212 | Boolean stat = false; |
| 213 | CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type); |
| 214 | return complete(ret); |
| 215 | } |
| 216 | |
| 217 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 218 | public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) { |
| 219 | CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame^] | 220 | //Workaround |
| 221 | List<NodeKey> nodeKeyList = path.nodeKeys(); |
| 222 | NodeKey f = nodeKeyList.get(0); |
| 223 | if (f.schemaId().name().compareTo("/") == 0) { |
| 224 | nodeKeyList.remove(0); |
| 225 | } |
| 226 | //Workaround end |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 227 | String spath = ResourceIdParser.parseResId(path); |
| 228 | DocumentPath dpath = DocumentPath.from(spath); |
| 229 | DataNode.Type type = null; |
| 230 | CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath); |
| 231 | type = completeVersioned(ret); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 232 | if (type == null) { |
| 233 | throw new FailedException("Requested node or some of the parents" + |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 234 | "are not present in the requested path"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 235 | } |
| 236 | DataNode retVal = null; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 237 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 238 | retVal = readLeaf(spath); |
| 239 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 240 | retVal = readLeaf(spath); |
| 241 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 242 | NodeKey key = ResourceIdParser.getInstanceKey(path); |
| 243 | if (key == null) { |
| 244 | throw new FailedException("Key type did not match node type"); |
| 245 | } |
| 246 | DataNode.Builder superBldr = InnerNode |
| 247 | .builder(key.schemaId().name(), key.schemaId().namespace()) |
| 248 | .type(type); |
| 249 | readInner(superBldr, spath); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 250 | retVal = superBldr.build(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 251 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 252 | NodeKey key = ResourceIdParser.getMultiInstanceKey(path); |
| 253 | if (key == null) { |
| 254 | throw new FailedException("Key type did not match node type"); |
| 255 | } |
| 256 | DataNode.Builder superBldr = InnerNode |
| 257 | .builder(key.schemaId().name(), key.schemaId().namespace()) |
| 258 | .type(type); |
| 259 | for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) { |
| 260 | String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf); |
| 261 | LeafNode lfnd = readLeaf(tempPath); |
| 262 | superBldr.addKeyLeaf(keyLeaf.leafSchema().name(), |
| 263 | keyLeaf.leafSchema().namespace(), lfnd.value()); |
| 264 | } |
| 265 | readInner(superBldr, spath); |
| 266 | retVal = superBldr.build(); |
| 267 | } else { |
| 268 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 269 | } |
| 270 | if (retVal != null) { |
| 271 | eventFuture = CompletableFuture.completedFuture(retVal); |
| 272 | } else { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 273 | log.info("STORE: FAILED to READ node"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 274 | } |
| 275 | return eventFuture; |
| 276 | } |
| 277 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 278 | private void readInner(DataNode.Builder superBldr, String spath) { |
| 279 | CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren( |
| 280 | DocumentPath.from(spath)); |
| 281 | Map<String, Versioned<DataNode.Type>> entries = null; |
| 282 | entries = complete(ret); |
| 283 | if ((entries == null) || (entries.size() == 0)) { |
| 284 | throw new FailedException("Inner node cannot have empty children map"); |
| 285 | } |
| 286 | entries.forEach((k, v) -> { |
| 287 | String[] names = k.split(ResourceIdParser.NM_SEP); |
| 288 | String name = names[0]; |
| 289 | String nmSpc = names[1]; |
| 290 | DataNode.Type type = v.value(); |
| 291 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 292 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 293 | superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value()) |
| 294 | .type(type) |
| 295 | .exitNode(); |
| 296 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 297 | String mlpath = ResourceIdParser.appendLeafList(tempPath, names[2]); |
| 298 | LeafNode lfnode = readLeaf(mlpath); |
| 299 | superBldr.createChildBuilder(name, nmSpc, lfnode.value()) |
| 300 | .type(type) |
| 301 | .addLeafListValue(lfnode.value()) |
| 302 | .exitNode(); |
| 303 | //TODO this alone should be sufficient and take the nm, nmspc too |
| 304 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 305 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 306 | .type(type); |
| 307 | readInner(tempBldr, tempPath); |
| 308 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 309 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 310 | .type(type); |
| 311 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
| 312 | String[] keys = k.split(ResourceIdParser.KEY_SEP); |
| 313 | for (int i = 1; i < keys.length; i++) { |
| 314 | String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]); |
| 315 | LeafNode lfnd = readLeaf(curKey); |
| 316 | String[] keydata = keys[i].split(ResourceIdParser.NM_SEP); |
| 317 | superBldr.addKeyLeaf(keydata[0], keydata[1], lfnd.value()); |
| 318 | } |
| 319 | readInner(tempBldr, tempPath); |
| 320 | } else { |
| 321 | throw new FailedException("Node type should either be LEAF or INNERNODE"); |
| 322 | } |
| 323 | }); |
| 324 | superBldr.exitNode(); |
| 325 | } |
| 326 | |
| 327 | private LeafNode readLeaf(String path) { |
| 328 | return objectStore.get(path).value(); |
| 329 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 330 | @Override |
| 331 | public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) { |
| 332 | throw new FailedException("Not yet implemented"); |
| 333 | } |
| 334 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 335 | public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 336 | throw new FailedException("Not yet implemented"); |
| 337 | } |
| 338 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 339 | public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 340 | throw new FailedException("Not yet implemented"); |
| 341 | } |
| 342 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 343 | public CompletableFuture<Boolean> deleteNode(ResourceId path) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 344 | throw new FailedException("Not yet implemented"); |
| 345 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 346 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 347 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 348 | public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame^] | 349 | //Workaround |
| 350 | List<NodeKey> nodeKeyList = path.nodeKeys(); |
| 351 | NodeKey f = nodeKeyList.get(0); |
| 352 | if (f.schemaId().name().compareTo("/") == 0) { |
| 353 | nodeKeyList.remove(0); |
| 354 | } |
| 355 | //Workaround end |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 356 | String spath = ResourceIdParser.parseResId(path); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 357 | DocumentPath dpath = DocumentPath.from(spath); |
| 358 | DataNode.Type type = null; |
| 359 | CompletableFuture<Versioned<DataNode.Type>> vtype = keystore.removeNode(dpath); |
| 360 | type = completeVersioned(vtype); |
| 361 | if (type == null) { |
| 362 | throw new FailedException("node delete failed"); |
| 363 | } |
| 364 | Versioned<LeafNode> res = objectStore.remove(spath); |
| 365 | if (res == null) { |
| 366 | return CompletableFuture.completedFuture(false); |
| 367 | } else { |
| 368 | return CompletableFuture.completedFuture(true); |
| 369 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 372 | public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> { |
| 373 | @Override |
| 374 | public void event(DocumentTreeEvent<DataNode.Type> event) { |
| 375 | DynamicConfigEvent.Type type; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 376 | ResourceId path; |
| 377 | switch (event.type()) { |
| 378 | case CREATED: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 379 | log.info("NODE created in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 380 | type = NODE_ADDED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 381 | break; |
| 382 | case UPDATED: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 383 | log.info("NODE updated in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 384 | type = NODE_UPDATED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 385 | break; |
| 386 | case DELETED: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 387 | log.info("NODE deleted in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 388 | type = NODE_DELETED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 389 | break; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 390 | default: |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 391 | log.info("UNKNOWN operation in store"); |
| 392 | type = UNKNOWN_OPRN; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 393 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 394 | path = ResourceIdParser.getResId(event.path().pathElements()); |
| 395 | notifyDelegate(new DynamicConfigEvent(type, path)); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 399 | public class InternalMapListener implements MapEventListener<String, LeafNode> { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 400 | @Override |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 401 | public void event(MapEvent<String, LeafNode> event) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 402 | switch (event.type()) { |
| 403 | case INSERT: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 404 | //log.info("NODE created in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 405 | break; |
| 406 | case UPDATE: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 407 | //log.info("NODE updated in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 408 | break; |
| 409 | case REMOVE: |
| 410 | default: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 411 | //log.info("NODE removed in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 412 | break; |
| 413 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 416 | |
| 417 | private <T> T complete(CompletableFuture<T> future) { |
| 418 | try { |
| 419 | return future.get(); |
| 420 | } catch (InterruptedException e) { |
| 421 | Thread.currentThread().interrupt(); |
| 422 | throw new FailedException(e.getCause().getMessage()); |
| 423 | } catch (ExecutionException e) { |
| 424 | if (e.getCause() instanceof IllegalDocumentModificationException) { |
| 425 | throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node"); |
| 426 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 427 | throw new FailedException("Resource id does not exist"); |
| 428 | } else { |
| 429 | throw new FailedException("Datastore operation failed"); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) { |
| 435 | try { |
| 436 | return future.get().value(); |
| 437 | } catch (InterruptedException e) { |
| 438 | Thread.currentThread().interrupt(); |
| 439 | throw new FailedException(e.getCause().getMessage()); |
| 440 | } catch (ExecutionException e) { |
| 441 | if (e.getCause() instanceof IllegalDocumentModificationException) { |
| 442 | throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node"); |
| 443 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 444 | throw new FailedException("Resource id does not exist"); |
| 445 | } else { |
| 446 | throw new FailedException("Datastore operation failed"); |
| 447 | } |
| 448 | } |
| 449 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 450 | } |