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