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