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 | * |
| 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) { |
Sithara Punnassery | c70b7e5 | 2017-08-10 14:28:08 -0700 | [diff] [blame^] | 200 | if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) { |
| 201 | completeVersioned(keystore.set(DocumentPath.from(path), type)); |
| 202 | return true; |
| 203 | } |
| 204 | return complete(keystore.create(DocumentPath.from(path), type)); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 208 | public CompletableFuture<DataNode> readNode(ResourceId path, Filter filter) { |
| 209 | CompletableFuture<DataNode> eventFuture = CompletableFuture.completedFuture(null); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 210 | String spath = ResourceIdParser.parseResId(path); |
| 211 | DocumentPath dpath = DocumentPath.from(spath); |
| 212 | DataNode.Type type = null; |
| 213 | CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath); |
| 214 | type = completeVersioned(ret); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 215 | if (type == null) { |
| 216 | throw new FailedException("Requested node or some of the parents" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 217 | "are not present in the requested path"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 218 | } |
| 219 | DataNode retVal = null; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 220 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 221 | retVal = readLeaf(spath); |
| 222 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 223 | retVal = readLeaf(spath); |
| 224 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 225 | NodeKey key = ResourceIdParser.getInstanceKey(path); |
| 226 | if (key == null) { |
| 227 | throw new FailedException("Key type did not match node type"); |
| 228 | } |
| 229 | DataNode.Builder superBldr = InnerNode |
| 230 | .builder(key.schemaId().name(), key.schemaId().namespace()) |
| 231 | .type(type); |
| 232 | readInner(superBldr, spath); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 233 | retVal = superBldr.build(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 234 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 235 | NodeKey key = ResourceIdParser.getMultiInstanceKey(path); |
| 236 | if (key == null) { |
| 237 | throw new FailedException("Key type did not match node type"); |
| 238 | } |
| 239 | DataNode.Builder superBldr = InnerNode |
| 240 | .builder(key.schemaId().name(), key.schemaId().namespace()) |
| 241 | .type(type); |
| 242 | for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 243 | //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf); |
| 244 | //LeafNode lfnd = readLeaf(tempPath); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 245 | superBldr.addKeyLeaf(keyLeaf.leafSchema().name(), |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 246 | keyLeaf.leafSchema().namespace(), String.valueOf(keyLeaf.leafValue())); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 247 | } |
| 248 | readInner(superBldr, spath); |
| 249 | retVal = superBldr.build(); |
| 250 | } else { |
| 251 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 252 | } |
| 253 | if (retVal != null) { |
| 254 | eventFuture = CompletableFuture.completedFuture(retVal); |
| 255 | } else { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 256 | log.info("STORE: Failed to READ node"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 257 | } |
| 258 | return eventFuture; |
| 259 | } |
| 260 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 261 | private void readInner(DataNode.Builder superBldr, String spath) { |
| 262 | CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren( |
| 263 | DocumentPath.from(spath)); |
| 264 | Map<String, Versioned<DataNode.Type>> entries = null; |
| 265 | entries = complete(ret); |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 266 | if ((entries != null) && (!entries.isEmpty())) { |
| 267 | entries.forEach((k, v) -> { |
| 268 | String[] names = k.split(ResourceIdParser.NM_CHK); |
| 269 | String name = names[0]; |
| 270 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 271 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
| 272 | DataNode.Type type = v.value(); |
| 273 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 274 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 275 | superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value()) |
| 276 | .type(type) |
| 277 | .exitNode(); |
| 278 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 279 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
| 280 | LeafNode lfnode = readLeaf(mlpath); |
| 281 | superBldr.createChildBuilder(name, nmSpc, lfnode.value()) |
| 282 | .type(type) |
| 283 | .addLeafListValue(lfnode.value()) |
| 284 | .exitNode(); |
| 285 | //TODO this alone should be sufficient and take the nm, nmspc too |
| 286 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 287 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 288 | .type(type); |
| 289 | readInner(tempBldr, tempPath); |
| 290 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 291 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 292 | .type(type); |
| 293 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
| 294 | String[] keys = k.split(ResourceIdParser.KEY_CHK); |
| 295 | for (int i = 1; i < keys.length; i++) { |
| 296 | //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]); |
| 297 | //LeafNode lfnd = readLeaf(curKey); |
| 298 | String[] keydata = keys[i].split(ResourceIdParser.NM_CHK); |
| 299 | tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]); |
| 300 | } |
| 301 | readInner(tempBldr, tempPath); |
| 302 | } else { |
| 303 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 304 | } |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 305 | }); |
| 306 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 307 | superBldr.exitNode(); |
| 308 | } |
| 309 | |
| 310 | private LeafNode readLeaf(String path) { |
| 311 | return objectStore.get(path).value(); |
| 312 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 313 | |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 314 | private void parseForUpdate(String path, DataNode node) { |
| 315 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 316 | addLeaf(path, (LeafNode) node); |
| 317 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 318 | path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key()); |
| 319 | addLeaf(path, (LeafNode) node); |
| 320 | } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 321 | traverseInner(path, (InnerNode) node); |
| 322 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 323 | path = ResourceIdParser.appendKeyList(path, (ListKey) node.key()); |
| 324 | traverseInner(path, (InnerNode) node); |
| 325 | } else { |
| 326 | throw new FailedException("Invalid node type"); |
| 327 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 328 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 329 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 330 | @Override |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 331 | public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) { |
| 332 | CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true); |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame] | 333 | String spath = ResourceIdParser.parseResId(complete); |
| 334 | if (spath == null) { |
| 335 | throw new FailedException("Invalid RsourceId, cannot update Node"); |
| 336 | } |
| 337 | if (spath.compareTo(ResourceIdParser.ROOT) != 0) { |
| 338 | if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) { |
| 339 | throw new FailedException("Node or parent doesnot exist, cannot update"); |
| 340 | } |
| 341 | } |
| 342 | spath = ResourceIdParser.appendNodeKey(spath, node.key()); |
| 343 | parseForUpdate(spath, node); |
| 344 | return eventFuture; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 345 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 346 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 347 | @Override |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 348 | public CompletableFuture<Boolean> nodeExist(ResourceId complete) { |
| 349 | Boolean stat = true; |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 350 | String spath = ResourceIdParser.parseResId(complete); |
| 351 | if (spath == null) { |
| 352 | stat = false; |
| 353 | } else if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) { |
| 354 | stat = false; |
| 355 | } |
| 356 | return CompletableFuture.completedFuture(stat); |
| 357 | } |
| 358 | |
| 359 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 360 | public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 361 | throw new FailedException("Not yet implemented"); |
| 362 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 363 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 364 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 365 | public CompletableFuture<Boolean> deleteNode(ResourceId path) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 366 | throw new FailedException("Not yet implemented"); |
| 367 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 368 | |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 369 | private void deleteInner(String spath) { |
| 370 | CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren( |
| 371 | DocumentPath.from(spath)); |
| 372 | Map<String, Versioned<DataNode.Type>> entries = null; |
| 373 | entries = complete(ret); |
Sithara Punnassery | 18ffcc7 | 2017-05-18 14:24:30 -0700 | [diff] [blame] | 374 | if ((entries != null) && (!entries.isEmpty())) { |
| 375 | entries.forEach((k, v) -> { |
| 376 | String[] names = k.split(ResourceIdParser.NM_CHK); |
| 377 | String name = names[0]; |
| 378 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 379 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
| 380 | DataNode.Type type = v.value(); |
| 381 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 382 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 383 | removeLeaf(tempPath); |
| 384 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 385 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
| 386 | removeLeaf(mlpath); |
| 387 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 388 | deleteInner(tempPath); |
| 389 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 390 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
| 391 | deleteInner(tempPath); |
| 392 | } else { |
| 393 | throw new FailedException("Invalid node type"); |
| 394 | } |
| 395 | }); |
| 396 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 397 | keystore.removeNode(DocumentPath.from(spath)); |
| 398 | } |
| 399 | |
| 400 | private void removeLeaf(String path) { |
| 401 | keystore.removeNode(DocumentPath.from(path)); |
| 402 | objectStore.remove(path); |
| 403 | } |
| 404 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 405 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 406 | public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) { |
| 407 | String spath = ResourceIdParser.parseResId(path); |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 408 | if (spath == null) { |
Sithara Punnassery | bc9edb1 | 2017-07-20 14:32:33 -0700 | [diff] [blame] | 409 | throw new FailedException("Invalid RsourceId, cannot delete Node"); |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 410 | } |
| 411 | if (spath.compareTo(ResourceIdParser.ROOT) == 0) { |
| 412 | throw new FailedException("Cannot delete Root"); |
| 413 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 414 | DocumentPath dpath = DocumentPath.from(spath); |
| 415 | DataNode.Type type = null; |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 416 | CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath); |
| 417 | type = completeVersioned(ret); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 418 | if (type == null) { |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 419 | throw new FailedException("Cannot delete, Requested node or some of the parents" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 420 | "are not present in the requested path"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 421 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 422 | DataNode retVal = null; |
| 423 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 424 | removeLeaf(spath); |
| 425 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 426 | removeLeaf(spath); |
| 427 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 428 | deleteInner(spath); |
| 429 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 430 | deleteInner(spath); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 431 | } else { |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 432 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 433 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 434 | return CompletableFuture.completedFuture(true); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 437 | public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> { |
| 438 | @Override |
| 439 | public void event(DocumentTreeEvent<DataNode.Type> event) { |
| 440 | DynamicConfigEvent.Type type; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 441 | ResourceId path; |
| 442 | switch (event.type()) { |
| 443 | case CREATED: |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 444 | type = NODE_ADDED; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 445 | //log.info("NODE added in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 446 | break; |
| 447 | case UPDATED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 448 | //log.info("NODE updated in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 449 | type = NODE_UPDATED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 450 | break; |
| 451 | case DELETED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 452 | //log.info("NODE deleted in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 453 | type = NODE_DELETED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 454 | break; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 455 | default: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 456 | //log.info("UNKNOWN operation in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 457 | type = UNKNOWN_OPRN; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 458 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 459 | path = ResourceIdParser.getResId(event.path().pathElements()); |
| 460 | notifyDelegate(new DynamicConfigEvent(type, path)); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 464 | public class InternalMapListener implements MapEventListener<String, LeafNode> { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 465 | @Override |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 466 | public void event(MapEvent<String, LeafNode> event) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 467 | switch (event.type()) { |
| 468 | case INSERT: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 469 | //log.info("NODE created in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 470 | break; |
| 471 | case UPDATE: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 472 | //log.info("NODE updated in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 473 | break; |
| 474 | case REMOVE: |
| 475 | default: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 476 | //log.info("NODE removed in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 477 | break; |
| 478 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 481 | |
| 482 | private <T> T complete(CompletableFuture<T> future) { |
| 483 | try { |
| 484 | return future.get(); |
| 485 | } catch (InterruptedException e) { |
| 486 | Thread.currentThread().interrupt(); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 487 | if (e == null) { |
| 488 | throw new FailedException("Unknown Exception"); |
| 489 | } else { |
| 490 | throw new FailedException(e.getCause().getMessage()); |
| 491 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 492 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 493 | if (e == null) { |
| 494 | throw new FailedException("Unknown Exception"); |
| 495 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 496 | throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node"); |
| 497 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 498 | throw new FailedException("Resource id does not exist"); |
| 499 | } else { |
| 500 | throw new FailedException("Datastore operation failed"); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) { |
| 506 | try { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 507 | if (future.get() != null) { |
| 508 | return future.get().value(); |
| 509 | } else { |
| 510 | return null; |
| 511 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 512 | } catch (InterruptedException e) { |
| 513 | Thread.currentThread().interrupt(); |
| 514 | throw new FailedException(e.getCause().getMessage()); |
| 515 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 516 | if (e == null) { |
| 517 | throw new FailedException("Unknown Exception"); |
| 518 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 519 | throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node"); |
| 520 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 521 | throw new FailedException("Resource id does not exist"); |
| 522 | } else { |
| 523 | throw new FailedException("Datastore operation failed"); |
| 524 | } |
| 525 | } |
| 526 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 527 | } |