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