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; |
| 29 | import org.onosproject.config.FailedException; |
| 30 | import org.onosproject.config.Filter; |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 31 | import org.onosproject.config.ResourceIdParser; |
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; |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 57 | |
| 58 | import java.math.BigInteger; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 59 | import java.util.Map; |
| 60 | import java.util.concurrent.CompletableFuture; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 61 | import java.util.concurrent.ExecutionException; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 62 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 63 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 64 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED; |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 65 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_UPDATED; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 66 | import static org.onosproject.config.DynamicConfigEvent.Type.UNKNOWN_OPRN; |
| 67 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Implementation of the dynamic config store. |
| 70 | */ |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 71 | @Beta |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class DistributedDynamicConfigStore |
| 75 | extends AbstractStore<DynamicConfigEvent, DynamicConfigStoreDelegate> |
| 76 | implements DynamicConfigStore { |
| 77 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected StorageService storageService; |
| 80 | private AsyncDocumentTree<DataNode.Type> keystore; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 81 | private ConsistentMap<String, LeafNode> objectStore; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 82 | private final DocumentTreeListener<DataNode.Type> klistener = new InternalDocTreeListener(); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 83 | private final MapEventListener<String, LeafNode> olistener = new InternalMapListener(); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 84 | |
| 85 | @Activate |
| 86 | public void activateStore() { |
| 87 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 88 | .register(KryoNamespaces.BASIC) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 89 | .register(java.lang.Class.class) |
| 90 | .register(DataNode.Type.class) |
| 91 | .register(LeafNode.class) |
| 92 | .register(InnerNode.class) |
| 93 | .register(ResourceId.class) |
| 94 | .register(NodeKey.class) |
| 95 | .register(SchemaId.class) |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 96 | .register(LeafListKey.class) |
| 97 | .register(ListKey.class) |
| 98 | .register(KeyLeaf.class) |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 99 | .register(BigInteger.class) |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 100 | .register(java.util.LinkedHashMap.class); |
| 101 | keystore = storageService.<DataNode.Type>documentTreeBuilder() |
| 102 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 103 | .withName("config-key-store") |
| 104 | .withRelaxedReadConsistency() |
| 105 | .buildDocumentTree(); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 106 | objectStore = storageService.<String, LeafNode>consistentMapBuilder() |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 107 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 108 | .withName("config-object-store") |
| 109 | .withRelaxedReadConsistency() |
| 110 | .build(); |
| 111 | keystore.addListener(klistener); |
| 112 | objectStore.addListener(olistener); |
| 113 | log.info("DyanmicConfig Store Active"); |
| 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | public void deactivateStore() { |
| 118 | keystore.removeListener(klistener); |
| 119 | objectStore.removeListener(olistener); |
| 120 | log.info("DyanmicConfig Store Stopped"); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public CompletableFuture<Boolean> |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 125 | addNode(ResourceId complete, DataNode node) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 126 | CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true); |
Sithara Punnassery | bb64490 | 2017-03-16 22:08:29 -0700 | [diff] [blame] | 127 | String spath = ResourceIdParser.parseResId(complete); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 128 | if (spath == null) { |
| 129 | throw new FailedException("Invalid RsourceId, cannot create Node"); |
| 130 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 131 | if (spath.compareTo(ResourceIdParser.ROOT) != 0) { |
| 132 | if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) { |
| 133 | throw new FailedException("Node or parent doesnot exist"); |
| 134 | } |
Sithara Punnassery | bb64490 | 2017-03-16 22:08:29 -0700 | [diff] [blame] | 135 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 136 | spath = ResourceIdParser.appendNodeKey(spath, node.key()); |
| 137 | parseNode(spath, node); |
| 138 | return eventFuture; |
| 139 | } |
| 140 | |
| 141 | private void parseNode(String path, DataNode node) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 142 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 143 | throw new FailedException("Requested node already present in the" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 144 | " store, please use an update method"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 145 | } |
| 146 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 147 | addLeaf(path, (LeafNode) node); |
| 148 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 149 | path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key()); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 150 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 151 | throw new FailedException("Requested node already present in the" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 152 | " store, please use an update method"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 153 | } |
| 154 | addLeaf(path, (LeafNode) node); |
| 155 | } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 156 | traverseInner(path, (InnerNode) node); |
| 157 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 158 | path = ResourceIdParser.appendKeyList(path, (ListKey) node.key()); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 159 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 160 | throw new FailedException("Requested node already present in the" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 161 | " store, please use an update method"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 162 | } |
| 163 | traverseInner(path, (InnerNode) node); |
| 164 | } else { |
| 165 | throw new FailedException("Invalid node type"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private void traverseInner(String path, InnerNode node) { |
| 170 | addKey(path, node.type()); |
| 171 | Map<NodeKey, DataNode> entries = node.childNodes(); |
| 172 | if (entries.size() == 0) { |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 173 | return; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 174 | } |
| 175 | entries.forEach((k, v) -> { |
| 176 | String tempPath; |
| 177 | tempPath = ResourceIdParser.appendNodeKey(path, v.key()); |
| 178 | if (v.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 179 | addLeaf(tempPath, (LeafNode) v); |
| 180 | } else if (v.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 181 | tempPath = ResourceIdParser.appendLeafList(tempPath, (LeafListKey) v.key()); |
| 182 | addLeaf(tempPath, (LeafNode) v); |
| 183 | } else if (v.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 184 | traverseInner(tempPath, (InnerNode) v); |
| 185 | } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 186 | tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key()); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 187 | traverseInner(tempPath, (InnerNode) v); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 188 | } else { |
| 189 | throw new FailedException("Invalid node type"); |
| 190 | } |
| 191 | }); |
| 192 | } |
| 193 | |
| 194 | private Boolean addLeaf(String path, LeafNode node) { |
| 195 | objectStore.put(path, node); |
| 196 | return addKey(path, node.type()); |
| 197 | } |
| 198 | |
| 199 | private Boolean addKey(String path, DataNode.Type type) { |
| 200 | Boolean stat = false; |
| 201 | CompletableFuture<Boolean> ret = keystore.create(DocumentPath.from(path), type); |
| 202 | return complete(ret); |
| 203 | } |
| 204 | |
| 205 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 206 | public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) { |
| 207 | CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 208 | String spath = ResourceIdParser.parseResId(path); |
| 209 | DocumentPath dpath = DocumentPath.from(spath); |
| 210 | DataNode.Type type = null; |
| 211 | CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath); |
| 212 | type = completeVersioned(ret); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 213 | if (type == null) { |
| 214 | throw new FailedException("Requested node or some of the parents" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 215 | "are not present in the requested path"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 216 | } |
| 217 | DataNode retVal = null; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 218 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 219 | retVal = readLeaf(spath); |
| 220 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 221 | retVal = readLeaf(spath); |
| 222 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 223 | NodeKey key = ResourceIdParser.getInstanceKey(path); |
| 224 | if (key == null) { |
| 225 | throw new FailedException("Key type did not match node type"); |
| 226 | } |
| 227 | DataNode.Builder superBldr = InnerNode |
| 228 | .builder(key.schemaId().name(), key.schemaId().namespace()) |
| 229 | .type(type); |
| 230 | readInner(superBldr, spath); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 231 | retVal = superBldr.build(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 232 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 233 | NodeKey key = ResourceIdParser.getMultiInstanceKey(path); |
| 234 | if (key == null) { |
| 235 | throw new FailedException("Key type did not match node type"); |
| 236 | } |
| 237 | DataNode.Builder superBldr = InnerNode |
| 238 | .builder(key.schemaId().name(), key.schemaId().namespace()) |
| 239 | .type(type); |
| 240 | for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 241 | //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf); |
| 242 | //LeafNode lfnd = readLeaf(tempPath); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 243 | superBldr.addKeyLeaf(keyLeaf.leafSchema().name(), |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 244 | keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue())); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 245 | } |
| 246 | readInner(superBldr, spath); |
| 247 | retVal = superBldr.build(); |
| 248 | } else { |
| 249 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 250 | } |
| 251 | if (retVal != null) { |
| 252 | eventFuture = CompletableFuture.completedFuture(retVal); |
| 253 | } else { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 254 | log.info("STORE: Failed to READ node"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 255 | } |
| 256 | return eventFuture; |
| 257 | } |
| 258 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 259 | private void readInner(DataNode.Builder superBldr, String spath) { |
| 260 | CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren( |
| 261 | DocumentPath.from(spath)); |
| 262 | Map<String, Versioned<DataNode.Type>> entries = null; |
| 263 | entries = complete(ret); |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 264 | if ((entries != null) && (!entries.isEmpty())) { |
| 265 | entries.forEach((k, v) -> { |
| 266 | String[] names = k.split(ResourceIdParser.NM_CHK); |
| 267 | String name = names[0]; |
| 268 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 269 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
| 270 | DataNode.Type type = v.value(); |
| 271 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 272 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 273 | superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value()) |
| 274 | .type(type) |
| 275 | .exitNode(); |
| 276 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 277 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
| 278 | LeafNode lfnode = readLeaf(mlpath); |
| 279 | superBldr.createChildBuilder(name, nmSpc, lfnode.value()) |
| 280 | .type(type) |
| 281 | .addLeafListValue(lfnode.value()) |
| 282 | .exitNode(); |
| 283 | //TODO this alone should be sufficient and take the nm, nmspc too |
| 284 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 285 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 286 | .type(type); |
| 287 | readInner(tempBldr, tempPath); |
| 288 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 289 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 290 | .type(type); |
| 291 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
| 292 | String[] keys = k.split(ResourceIdParser.KEY_CHK); |
| 293 | for (int i = 1; i < keys.length; i++) { |
| 294 | //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]); |
| 295 | //LeafNode lfnd = readLeaf(curKey); |
| 296 | String[] keydata = keys[i].split(ResourceIdParser.NM_CHK); |
| 297 | tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]); |
| 298 | } |
| 299 | readInner(tempBldr, tempPath); |
| 300 | } else { |
| 301 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 302 | } |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 303 | }); |
| 304 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 305 | superBldr.exitNode(); |
| 306 | } |
| 307 | |
| 308 | private LeafNode readLeaf(String path) { |
| 309 | return objectStore.get(path).value(); |
| 310 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 311 | |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 312 | private void parseForUpdate(String path, DataNode node) { |
| 313 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 314 | addLeaf(path, (LeafNode) node); |
| 315 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 316 | path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key()); |
| 317 | addLeaf(path, (LeafNode) node); |
| 318 | } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 319 | traverseInner(path, (InnerNode) node); |
| 320 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 321 | path = ResourceIdParser.appendKeyList(path, (ListKey) node.key()); |
| 322 | traverseInner(path, (InnerNode) node); |
| 323 | } else { |
| 324 | throw new FailedException("Invalid node type"); |
| 325 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 326 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 327 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 328 | @Override |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 329 | public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) { |
| 330 | CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true); |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 331 | String spath = ResourceIdParser.parseResId(complete); |
| 332 | if (spath == null) { |
| 333 | throw new FailedException("Invalid RsourceId, cannot update Node"); |
| 334 | } |
| 335 | if (spath.compareTo(ResourceIdParser.ROOT) != 0) { |
| 336 | if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) { |
| 337 | throw new FailedException("Node or parent doesnot exist, cannot update"); |
| 338 | } |
| 339 | } |
| 340 | spath = ResourceIdParser.appendNodeKey(spath, node.key()); |
| 341 | parseForUpdate(spath, node); |
| 342 | return eventFuture; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 343 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 344 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 345 | @Override |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 346 | public CompletableFuture<Boolean> nodeExist(ResourceId complete) { |
| 347 | Boolean stat = true; |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 348 | String spath = ResourceIdParser.parseResId(complete); |
| 349 | if (spath == null) { |
| 350 | stat = false; |
| 351 | } else if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) { |
| 352 | stat = false; |
| 353 | } |
| 354 | return CompletableFuture.completedFuture(stat); |
| 355 | } |
| 356 | |
| 357 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 358 | public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 359 | throw new FailedException("Not yet implemented"); |
| 360 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 361 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 362 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 363 | public CompletableFuture<Boolean> deleteNode(ResourceId path) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 364 | throw new FailedException("Not yet implemented"); |
| 365 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 366 | |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 367 | private void deleteInner(String spath) { |
| 368 | CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren( |
| 369 | DocumentPath.from(spath)); |
| 370 | Map<String, Versioned<DataNode.Type>> entries = null; |
| 371 | entries = complete(ret); |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 372 | if ((entries != null) && (!entries.isEmpty())) { |
| 373 | entries.forEach((k, v) -> { |
| 374 | String[] names = k.split(ResourceIdParser.NM_CHK); |
| 375 | String name = names[0]; |
| 376 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 377 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
| 378 | DataNode.Type type = v.value(); |
| 379 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 380 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 381 | removeLeaf(tempPath); |
| 382 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 383 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
| 384 | removeLeaf(mlpath); |
| 385 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 386 | deleteInner(tempPath); |
| 387 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 388 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
| 389 | deleteInner(tempPath); |
| 390 | } else { |
| 391 | throw new FailedException("Invalid node type"); |
| 392 | } |
| 393 | }); |
| 394 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 395 | keystore.removeNode(DocumentPath.from(spath)); |
| 396 | } |
| 397 | |
| 398 | private void removeLeaf(String path) { |
| 399 | keystore.removeNode(DocumentPath.from(path)); |
| 400 | objectStore.remove(path); |
| 401 | } |
| 402 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 403 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 404 | public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) { |
| 405 | String spath = ResourceIdParser.parseResId(path); |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 406 | if (spath == null) { |
Sithara Punnassery | bc9edb1 | 2017-07-20 14:32:33 -0700 | [diff] [blame^] | 407 | throw new FailedException("Invalid RsourceId, cannot delete Node"); |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 408 | } |
| 409 | if (spath.compareTo(ResourceIdParser.ROOT) == 0) { |
| 410 | throw new FailedException("Cannot delete Root"); |
| 411 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 412 | DocumentPath dpath = DocumentPath.from(spath); |
| 413 | DataNode.Type type = null; |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 414 | CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath); |
| 415 | type = completeVersioned(ret); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 416 | if (type == null) { |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 417 | throw new FailedException("Cannot delete, Requested node or some of the parents" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 418 | "are not present in the requested path"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 419 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 420 | DataNode retVal = null; |
| 421 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 422 | removeLeaf(spath); |
| 423 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 424 | removeLeaf(spath); |
| 425 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 426 | deleteInner(spath); |
| 427 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 428 | deleteInner(spath); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 429 | } else { |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 430 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 431 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 432 | return CompletableFuture.completedFuture(true); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 435 | public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> { |
| 436 | @Override |
| 437 | public void event(DocumentTreeEvent<DataNode.Type> event) { |
| 438 | DynamicConfigEvent.Type type; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 439 | ResourceId path; |
| 440 | switch (event.type()) { |
| 441 | case CREATED: |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 442 | type = NODE_ADDED; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 443 | //log.info("NODE added in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 444 | break; |
| 445 | case UPDATED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 446 | //log.info("NODE updated in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 447 | type = NODE_UPDATED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 448 | break; |
| 449 | case DELETED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 450 | //log.info("NODE deleted in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 451 | type = NODE_DELETED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 452 | break; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 453 | default: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 454 | //log.info("UNKNOWN operation in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 455 | type = UNKNOWN_OPRN; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 456 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 457 | path = ResourceIdParser.getResId(event.path().pathElements()); |
| 458 | notifyDelegate(new DynamicConfigEvent(type, path)); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 462 | public class InternalMapListener implements MapEventListener<String, LeafNode> { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 463 | @Override |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 464 | public void event(MapEvent<String, LeafNode> event) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 465 | switch (event.type()) { |
| 466 | case INSERT: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 467 | //log.info("NODE created in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 468 | break; |
| 469 | case UPDATE: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 470 | //log.info("NODE updated in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 471 | break; |
| 472 | case REMOVE: |
| 473 | default: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 474 | //log.info("NODE removed in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 475 | break; |
| 476 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 479 | |
| 480 | private <T> T complete(CompletableFuture<T> future) { |
| 481 | try { |
| 482 | return future.get(); |
| 483 | } catch (InterruptedException e) { |
| 484 | Thread.currentThread().interrupt(); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 485 | if (e == null) { |
| 486 | throw new FailedException("Unknown Exception"); |
| 487 | } else { |
| 488 | throw new FailedException(e.getCause().getMessage()); |
| 489 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 490 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 491 | if (e == null) { |
| 492 | throw new FailedException("Unknown Exception"); |
| 493 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 494 | throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node"); |
| 495 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 496 | throw new FailedException("Resource id does not exist"); |
| 497 | } else { |
| 498 | throw new FailedException("Datastore operation failed"); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) { |
| 504 | try { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 505 | if (future.get() != null) { |
| 506 | return future.get().value(); |
| 507 | } else { |
| 508 | return null; |
| 509 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 510 | } catch (InterruptedException e) { |
| 511 | Thread.currentThread().interrupt(); |
| 512 | throw new FailedException(e.getCause().getMessage()); |
| 513 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 514 | if (e == null) { |
| 515 | throw new FailedException("Unknown Exception"); |
| 516 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 517 | throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node"); |
| 518 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 519 | throw new FailedException("Resource id does not exist"); |
| 520 | } else { |
| 521 | throw new FailedException("Datastore operation failed"); |
| 522 | } |
| 523 | } |
| 524 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 525 | } |