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