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 | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 275 | /*if ((entries == null) || (entries.size() == 0)) { |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 276 | throw new FailedException("Inner node cannot have empty children map"); |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 277 | }*/ |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 278 | entries.forEach((k, v) -> { |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 279 | String[] names = k.split(ResourceIdParser.NM_CHK); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 280 | String name = names[0]; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 281 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 282 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 283 | DataNode.Type type = v.value(); |
| 284 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 285 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 286 | superBldr.createChildBuilder(name, nmSpc, readLeaf(tempPath).value()) |
| 287 | .type(type) |
| 288 | .exitNode(); |
| 289 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 290 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 291 | LeafNode lfnode = readLeaf(mlpath); |
| 292 | superBldr.createChildBuilder(name, nmSpc, lfnode.value()) |
| 293 | .type(type) |
| 294 | .addLeafListValue(lfnode.value()) |
| 295 | .exitNode(); |
| 296 | //TODO this alone should be sufficient and take the nm, nmspc too |
| 297 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 298 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 299 | .type(type); |
| 300 | readInner(tempBldr, tempPath); |
| 301 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 302 | DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc) |
| 303 | .type(type); |
| 304 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 305 | String[] keys = k.split(ResourceIdParser.KEY_CHK); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 306 | for (int i = 1; i < keys.length; i++) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 307 | //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]); |
| 308 | //LeafNode lfnd = readLeaf(curKey); |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 309 | String[] keydata = keys[i].split(ResourceIdParser.NM_CHK); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 310 | tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 311 | } |
| 312 | readInner(tempBldr, tempPath); |
| 313 | } else { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 314 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 315 | } |
| 316 | }); |
| 317 | superBldr.exitNode(); |
| 318 | } |
| 319 | |
| 320 | private LeafNode readLeaf(String path) { |
| 321 | return objectStore.get(path).value(); |
| 322 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 323 | |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame^] | 324 | private void parseForUpdate(String path, DataNode node) { |
| 325 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 326 | addLeaf(path, (LeafNode) node); |
| 327 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 328 | path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key()); |
| 329 | addLeaf(path, (LeafNode) node); |
| 330 | } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 331 | traverseInner(path, (InnerNode) node); |
| 332 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 333 | path = ResourceIdParser.appendKeyList(path, (ListKey) node.key()); |
| 334 | traverseInner(path, (InnerNode) node); |
| 335 | } else { |
| 336 | throw new FailedException("Invalid node type"); |
| 337 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 338 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 339 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 340 | @Override |
Sithara Punnassery | 0da1a9c | 2017-05-17 16:16:22 -0700 | [diff] [blame^] | 341 | public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) { |
| 342 | CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true); |
| 343 | List<NodeKey> nodeKeyList = complete.nodeKeys(); |
| 344 | NodeKey f = nodeKeyList.get(0); |
| 345 | if (f.schemaId().name().compareTo("/") == 0) { |
| 346 | nodeKeyList.remove(0); |
| 347 | } |
| 348 | String spath = ResourceIdParser.parseResId(complete); |
| 349 | if (spath == null) { |
| 350 | throw new FailedException("Invalid RsourceId, cannot update Node"); |
| 351 | } |
| 352 | if (spath.compareTo(ResourceIdParser.ROOT) != 0) { |
| 353 | if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) { |
| 354 | throw new FailedException("Node or parent doesnot exist, cannot update"); |
| 355 | } |
| 356 | } |
| 357 | spath = ResourceIdParser.appendNodeKey(spath, node.key()); |
| 358 | parseForUpdate(spath, node); |
| 359 | return eventFuture; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 360 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 361 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 362 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 363 | public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 364 | throw new FailedException("Not yet implemented"); |
| 365 | } |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 366 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 367 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 368 | public CompletableFuture<Boolean> deleteNode(ResourceId path) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 369 | throw new FailedException("Not yet implemented"); |
| 370 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 371 | |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 372 | private void deleteInner(String spath) { |
| 373 | CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren( |
| 374 | DocumentPath.from(spath)); |
| 375 | Map<String, Versioned<DataNode.Type>> entries = null; |
| 376 | entries = complete(ret); |
| 377 | /*if ((entries == null) || (entries.size() == 0)) { |
| 378 | throw new FailedException("Inner node cannot have empty children map"); |
| 379 | }*/ |
| 380 | entries.forEach((k, v) -> { |
| 381 | String[] names = k.split(ResourceIdParser.NM_CHK); |
| 382 | String name = names[0]; |
| 383 | String nmSpc = ResourceIdParser.getNamespace(names[1]); |
| 384 | String keyVal = ResourceIdParser.getKeyVal(names[1]); |
| 385 | DataNode.Type type = v.value(); |
| 386 | String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc); |
| 387 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 388 | removeLeaf(tempPath); |
| 389 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 390 | String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal); |
| 391 | removeLeaf(mlpath); |
| 392 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 393 | deleteInner(tempPath); |
| 394 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 395 | tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k); |
| 396 | deleteInner(tempPath); |
| 397 | } else { |
| 398 | throw new FailedException("Invalid node type"); |
| 399 | } |
| 400 | }); |
| 401 | keystore.removeNode(DocumentPath.from(spath)); |
| 402 | } |
| 403 | |
| 404 | private void removeLeaf(String path) { |
| 405 | keystore.removeNode(DocumentPath.from(path)); |
| 406 | objectStore.remove(path); |
| 407 | } |
| 408 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 409 | @Override |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 410 | public CompletableFuture<Boolean> deleteNodeRecursive(ResourceId path) { |
Sithara Punnassery | 43833e1 | 2017-03-14 16:29:19 -0700 | [diff] [blame] | 411 | List<NodeKey> nodeKeyList = path.nodeKeys(); |
| 412 | NodeKey f = nodeKeyList.get(0); |
| 413 | if (f.schemaId().name().compareTo("/") == 0) { |
| 414 | nodeKeyList.remove(0); |
| 415 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 416 | String spath = ResourceIdParser.parseResId(path); |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 417 | if (spath == null) { |
| 418 | throw new FailedException("Invalid RsourceId, cannot create Node"); |
| 419 | } |
| 420 | if (spath.compareTo(ResourceIdParser.ROOT) == 0) { |
| 421 | throw new FailedException("Cannot delete Root"); |
| 422 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 423 | DocumentPath dpath = DocumentPath.from(spath); |
| 424 | DataNode.Type type = null; |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 425 | CompletableFuture<Versioned<DataNode.Type>> ret = keystore.get(dpath); |
| 426 | type = completeVersioned(ret); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 427 | if (type == null) { |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 428 | throw new FailedException("Cannot delete, Requested node or some of the parents" + |
Henry Yu | 5c54e77 | 2017-04-19 14:13:56 -0400 | [diff] [blame] | 429 | "are not present in the requested path"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 430 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 431 | DataNode retVal = null; |
| 432 | if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 433 | removeLeaf(spath); |
| 434 | } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) { |
| 435 | removeLeaf(spath); |
| 436 | } else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) { |
| 437 | deleteInner(spath); |
| 438 | } else if (type == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 439 | deleteInner(spath); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 440 | } else { |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 441 | throw new FailedException("Invalid node type"); |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 442 | } |
Sithara Punnassery | e4ab4f2 | 2017-03-27 19:11:00 -0700 | [diff] [blame] | 443 | return CompletableFuture.completedFuture(true); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 446 | public class InternalDocTreeListener implements DocumentTreeListener<DataNode.Type> { |
| 447 | @Override |
| 448 | public void event(DocumentTreeEvent<DataNode.Type> event) { |
| 449 | DynamicConfigEvent.Type type; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 450 | ResourceId path; |
| 451 | switch (event.type()) { |
| 452 | case CREATED: |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 453 | type = NODE_ADDED; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 454 | //log.info("NODE added in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 455 | break; |
| 456 | case UPDATED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 457 | //log.info("NODE updated in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 458 | type = NODE_UPDATED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 459 | break; |
| 460 | case DELETED: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 461 | //log.info("NODE deleted in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 462 | type = NODE_DELETED; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 463 | break; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 464 | default: |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 465 | //log.info("UNKNOWN operation in store"); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 466 | type = UNKNOWN_OPRN; |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 467 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 468 | path = ResourceIdParser.getResId(event.path().pathElements()); |
| 469 | notifyDelegate(new DynamicConfigEvent(type, path)); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 473 | public class InternalMapListener implements MapEventListener<String, LeafNode> { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 474 | @Override |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 475 | public void event(MapEvent<String, LeafNode> event) { |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 476 | switch (event.type()) { |
| 477 | case INSERT: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 478 | //log.info("NODE created in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 479 | break; |
| 480 | case UPDATE: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 481 | //log.info("NODE updated in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 482 | break; |
| 483 | case REMOVE: |
| 484 | default: |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 485 | //log.info("NODE removed in store"); |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 486 | break; |
| 487 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 490 | |
| 491 | private <T> T complete(CompletableFuture<T> future) { |
| 492 | try { |
| 493 | return future.get(); |
| 494 | } catch (InterruptedException e) { |
| 495 | Thread.currentThread().interrupt(); |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 496 | if (e == null) { |
| 497 | throw new FailedException("Unknown Exception"); |
| 498 | } else { |
| 499 | throw new FailedException(e.getCause().getMessage()); |
| 500 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 501 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 502 | if (e == null) { |
| 503 | throw new FailedException("Unknown Exception"); |
| 504 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 505 | throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node"); |
| 506 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 507 | throw new FailedException("Resource id does not exist"); |
| 508 | } else { |
| 509 | throw new FailedException("Datastore operation failed"); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) { |
| 515 | try { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 516 | if (future.get() != null) { |
| 517 | return future.get().value(); |
| 518 | } else { |
| 519 | return null; |
| 520 | } |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 521 | } catch (InterruptedException e) { |
| 522 | Thread.currentThread().interrupt(); |
| 523 | throw new FailedException(e.getCause().getMessage()); |
| 524 | } catch (ExecutionException e) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 525 | if (e == null) { |
| 526 | throw new FailedException("Unknown Exception"); |
| 527 | } else if (e.getCause() instanceof IllegalDocumentModificationException) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 528 | throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node"); |
| 529 | } else if (e.getCause() instanceof NoSuchDocumentPathException) { |
| 530 | throw new FailedException("Resource id does not exist"); |
| 531 | } else { |
| 532 | throw new FailedException("Datastore operation failed"); |
| 533 | } |
| 534 | } |
| 535 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 536 | } |