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> |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 123 | addNode(ResourceId complete, DataNode node) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 124 | CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 125 | List<NodeKey> nodeKeyList = complete.nodeKeys(); |
| 126 | NodeKey f = nodeKeyList.get(0); |
| 127 | if (f.schemaId().name().compareTo("/") == 0) { |
| 128 | nodeKeyList.remove(0); |
| 129 | } |
Sithara Punnassery | bb64490 | 2017-03-16 22:08:29 -0700 | [diff] [blame] | 130 | String spath = ResourceIdParser.parseResId(complete); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 131 | if (spath == null) { |
| 132 | throw new FailedException("Invalid RsourceId, cannot create Node"); |
| 133 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 134 | 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 Punnassery | bb64490 | 2017-03-16 22:08:29 -0700 | [diff] [blame] | 138 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 139 | spath = ResourceIdParser.appendNodeKey(spath, node.key()); |
| 140 | parseNode(spath, node); |
| 141 | return eventFuture; |
| 142 | } |
| 143 | |
| 144 | private void parseNode(String path, DataNode node) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 145 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 146 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 153 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 154 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 162 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 163 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 190 | traverseInner(tempPath, (InnerNode) v); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 191 | } 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 209 | public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) { |
| 210 | CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null); |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 211 | List<NodeKey> nodeKeyList = path.nodeKeys(); |
| 212 | NodeKey f = nodeKeyList.get(0); |
| 213 | if (f.schemaId().name().compareTo("/") == 0) { |
| 214 | nodeKeyList.remove(0); |
| 215 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 216 | 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 221 | if (type == null) { |
| 222 | throw new FailedException("Requested node or some of the parents" + |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 223 | "are not present in the requested path"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 224 | } |
| 225 | DataNode retVal = null; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 226 | 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 239 | retVal = superBldr.build(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 240 | } 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 249 | //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf); |
| 250 | //LeafNode lfnd = readLeaf(tempPath); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 251 | superBldr.addKeyLeaf(keyLeaf.leafSchema().name(), |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 252 | keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue())); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 253 | } |
| 254 | readInner(superBldr, spath); |
| 255 | retVal = superBldr.build(); |
| 256 | } else { |
| 257 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 258 | } |
| 259 | if (retVal != null) { |
| 260 | eventFuture = CompletableFuture.completedFuture(retVal); |
| 261 | } else { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 262 | log.info("STORE: Failed to READ node"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 263 | } |
| 264 | return eventFuture; |
| 265 | } |
| 266 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 267 | 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 Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 276 | String[] names = k.split(ResourceIdParser.NM_CHK); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 277 | String name = names[0]; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 278 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 279 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 280 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 287 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 288 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 302 | String[] keys = k.split(ResourceIdParser.KEY_CHK); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 303 | for (int i = 1; i < keys.length; i++) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 304 | //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]); |
| 305 | //LeafNode lfnd = readLeaf(curKey); |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 306 | String[] keydata = keys[i].split(ResourceIdParser.NM_CHK); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 307 | tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 308 | } |
| 309 | readInner(tempBldr, tempPath); |
| 310 | } else { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 311 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 312 | } |
| 313 | }); |
| 314 | superBldr.exitNode(); |
| 315 | } |
| 316 | |
| 317 | private LeafNode readLeaf(String path) { |
| 318 | return objectStore.get(path).value(); |
| 319 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 320 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 321 | @Override |
| 322 | public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) { |
| 323 | throw new FailedException("Not yet implemented"); |
| 324 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 325 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 326 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 327 | public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 328 | throw new FailedException("Not yet implemented"); |
| 329 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 330 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 331 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 332 | public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 333 | throw new FailedException("Not yet implemented"); |
| 334 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 335 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 336 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 337 | public CompletableFuture<Boolean> deleteNode(ResourceId path) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 338 | throw new FailedException("Not yet implemented"); |
| 339 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 340 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 341 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 342 | public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 343 | List<NodeKey> nodeKeyList = path.nodeKeys(); |
| 344 | NodeKey f = nodeKeyList.get(0); |
| 345 | if (f.schemaId().name().compareTo("/") == 0) { |
| 346 | nodeKeyList.remove(0); |
| 347 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 348 | String spath = ResourceIdParser.parseResId(path); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 349 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 354 | throw new FailedException("Node delete failed"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 355 | } |
| 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 364 | public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> { |
| 365 | @Override |
| 366 | public void event(DocumentTreeEvent<DataNode.Type> event) { |
| 367 | DynamicConfigEvent.Type type; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 368 | ResourceId path; |
| 369 | switch (event.type()) { |
| 370 | case CREATED: |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 371 | type = NODE_ADDED; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 372 | //log.info("NODE added in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 373 | break; |
| 374 | case UPDATED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 375 | //log.info("NODE updated in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 376 | type = NODE_UPDATED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 377 | break; |
| 378 | case DELETED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 379 | //log.info("NODE deleted in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 380 | type = NODE_DELETED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 381 | break; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 382 | default: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 383 | //log.info("UNKNOWN operation in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 384 | type = UNKNOWN_OPRN; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 385 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 386 | path = ResourceIdParser.getResId(event.path().pathElements()); |
| 387 | notifyDelegate(new DynamicConfigEvent(type, path)); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 391 | public class InternalMapListener implements MapEventListener<String, LeafNode> { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 392 | @Override |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 393 | public void event(MapEvent<String, LeafNode> event) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 394 | switch (event.type()) { |
| 395 | case INSERT: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 396 | //log.info("NODE created in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 397 | break; |
| 398 | case UPDATE: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 399 | //log.info("NODE updated in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 400 | break; |
| 401 | case REMOVE: |
| 402 | default: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 403 | //log.info("NODE removed in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 404 | break; |
| 405 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 408 | |
| 409 | private <T> T complete(CompletableFuture<T> future) { |
| 410 | try { |
| 411 | return future.get(); |
| 412 | } catch (InterruptedException e) { |
| 413 | Thread.currentThread().interrupt(); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 414 | if (e == null) { |
| 415 | throw new FailedException("Unknown Exception"); |
| 416 | } else { |
| 417 | throw new FailedException(e.getCause().getMessage()); |
| 418 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 419 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 420 | if (e == null) { |
| 421 | throw new FailedException("Unknown Exception"); |
| 422 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 423 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 434 | if (future.get() != null) { |
| 435 | return future.get().value(); |
| 436 | } else { |
| 437 | return null; |
| 438 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 439 | } catch (InterruptedException e) { |
| 440 | Thread.currentThread().interrupt(); |
| 441 | throw new FailedException(e.getCause().getMessage()); |
| 442 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 443 | if (e == null) { |
| 444 | throw new FailedException("Unknown Exception"); |
| 445 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 446 | 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 454 | } |