Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [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 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
| 20 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 21 | import java.util.Iterator; |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 24 | import java.util.concurrent.atomic.AtomicLong; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 25 | |
| 26 | import org.onosproject.store.service.DocumentPath; |
| 27 | import org.onosproject.store.service.DocumentTree; |
| 28 | import org.onosproject.store.service.DocumentTreeListener; |
| 29 | import org.onosproject.store.service.DocumentTreeNode; |
| 30 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 31 | import org.onosproject.store.service.NoSuchDocumentPathException; |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.Ordering; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.Versioned; |
| 34 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 35 | import com.google.common.base.Supplier; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 36 | import com.google.common.collect.Maps; |
| 37 | |
| 38 | /** |
| 39 | * Simple implementation of a {@link DocumentTree}. |
| 40 | * |
| 41 | * @param <V> tree node value type |
| 42 | */ |
| 43 | public class DefaultDocumentTree<V> implements DocumentTree<V> { |
| 44 | |
| 45 | private static final DocumentPath ROOT_PATH = DocumentPath.from("root"); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | final DefaultDocumentTreeNode<V> root; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 47 | private final Supplier<Long> versionSupplier; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 48 | |
| 49 | public DefaultDocumentTree() { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 50 | AtomicLong versionCounter = new AtomicLong(0); |
| 51 | versionSupplier = versionCounter::incrementAndGet; |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 52 | root = new DefaultDocumentTreeNode<>(ROOT_PATH, null, versionSupplier.get(), Ordering.NATURAL, null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 55 | public DefaultDocumentTree(Supplier<Long> versionSupplier, Ordering ordering) { |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 56 | root = new DefaultDocumentTreeNode<>(ROOT_PATH, null, versionSupplier.get(), ordering, null); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 57 | this.versionSupplier = versionSupplier; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 60 | DefaultDocumentTree(Supplier<Long> versionSupplier, DefaultDocumentTreeNode<V> root) { |
| 61 | this.root = root; |
| 62 | this.versionSupplier = versionSupplier; |
| 63 | } |
| 64 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 65 | @Override |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 66 | public String name() { |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | @Override |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 71 | public DocumentPath root() { |
| 72 | return ROOT_PATH; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Map<String, Versioned<V>> getChildren(DocumentPath path) { |
| 77 | DocumentTreeNode<V> node = getNode(path); |
| 78 | if (node != null) { |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 79 | Map<String, Versioned<V>> childrenValues = Maps.newLinkedHashMap(); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 80 | node.children().forEachRemaining(n -> childrenValues.put(simpleName(n.path()), n.value())); |
| 81 | return childrenValues; |
| 82 | } |
| 83 | throw new NoSuchDocumentPathException(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Versioned<V> get(DocumentPath path) { |
| 88 | DocumentTreeNode<V> currentNode = getNode(path); |
| 89 | return currentNode != null ? currentNode.value() : null; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Versioned<V> set(DocumentPath path, V value) { |
| 94 | checkRootModification(path); |
| 95 | DefaultDocumentTreeNode<V> node = getNode(path); |
| 96 | if (node != null) { |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 97 | return node.update(value, versionSupplier.get()); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 98 | } else { |
| 99 | create(path, value); |
| 100 | return null; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean create(DocumentPath path, V value) { |
| 106 | checkRootModification(path); |
| 107 | DocumentTreeNode<V> node = getNode(path); |
| 108 | if (node != null) { |
| 109 | return false; |
| 110 | } |
| 111 | DocumentPath parentPath = path.parent(); |
| 112 | DefaultDocumentTreeNode<V> parentNode = getNode(parentPath); |
| 113 | if (parentNode == null) { |
| 114 | throw new IllegalDocumentModificationException(); |
| 115 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 116 | parentNode.addChild(simpleName(path), value, versionSupplier.get()); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
| 120 | @Override |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 121 | public boolean createRecursive(DocumentPath path, V value) { |
| 122 | checkRootModification(path); |
| 123 | DocumentTreeNode<V> node = getNode(path); |
| 124 | if (node != null) { |
| 125 | return false; |
| 126 | } |
| 127 | DocumentPath parentPath = path.parent(); |
| 128 | if (getNode(parentPath) == null) { |
| 129 | createRecursive(parentPath, null); |
| 130 | } |
| 131 | DefaultDocumentTreeNode<V> parentNode = getNode(parentPath); |
| 132 | if (parentNode == null) { |
| 133 | throw new IllegalDocumentModificationException(); |
| 134 | } |
| 135 | parentNode.addChild(simpleName(path), value, versionSupplier.get()); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | @Override |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 140 | public boolean replace(DocumentPath path, V newValue, long version) { |
| 141 | checkRootModification(path); |
| 142 | DocumentTreeNode<V> node = getNode(path); |
| 143 | if (node != null && node.value() != null && node.value().version() == version) { |
Jordan Halterman | 9052b20 | 2017-08-01 09:41:32 -0700 | [diff] [blame] | 144 | set(path, newValue); |
| 145 | return true; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 146 | } |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean replace(DocumentPath path, V newValue, V currentValue) { |
| 152 | checkRootModification(path); |
| 153 | if (Objects.equals(newValue, currentValue)) { |
| 154 | return false; |
| 155 | } |
| 156 | DocumentTreeNode<V> node = getNode(path); |
| 157 | if (node != null && Objects.equals(Versioned.valueOrNull(node.value()), currentValue)) { |
| 158 | set(path, newValue); |
| 159 | return true; |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Versioned<V> removeNode(DocumentPath path) { |
| 166 | checkRootModification(path); |
| 167 | DefaultDocumentTreeNode<V> nodeToRemove = getNode(path); |
| 168 | if (nodeToRemove == null) { |
| 169 | throw new NoSuchDocumentPathException(); |
| 170 | } |
| 171 | if (nodeToRemove.hasChildren()) { |
| 172 | throw new IllegalDocumentModificationException(); |
| 173 | } |
| 174 | DefaultDocumentTreeNode<V> parent = (DefaultDocumentTreeNode<V>) nodeToRemove.parent(); |
| 175 | parent.removeChild(simpleName(path)); |
| 176 | return nodeToRemove.value(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void addListener(DocumentPath path, DocumentTreeListener<V> listener) { |
| 181 | // TODO Auto-generated method stub |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void removeListener(DocumentTreeListener<V> listener) { |
| 186 | // TODO Auto-generated method stub |
| 187 | } |
| 188 | |
| 189 | private DefaultDocumentTreeNode<V> getNode(DocumentPath path) { |
| 190 | Iterator<String> pathElements = path.pathElements().iterator(); |
| 191 | DefaultDocumentTreeNode<V> currentNode = root; |
Yuta HIGUCHI | 6b2ec28 | 2017-11-03 21:08:38 -0700 | [diff] [blame] | 192 | checkArgument("root".equals(pathElements.next()), "Path should start with root: %s", path); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 193 | while (pathElements.hasNext() && currentNode != null) { |
| 194 | currentNode = (DefaultDocumentTreeNode<V>) currentNode.child(pathElements.next()); |
| 195 | } |
| 196 | return currentNode; |
| 197 | } |
| 198 | |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 199 | private String simpleName(DocumentPath path) { |
| 200 | return path.pathElements().get(path.pathElements().size() - 1); |
| 201 | } |
| 202 | |
| 203 | private void checkRootModification(DocumentPath path) { |
| 204 | if (ROOT_PATH.equals(path)) { |
| 205 | throw new IllegalDocumentModificationException(); |
| 206 | } |
| 207 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 208 | } |