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