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