2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.Collections;
13 import java.util.HashMap;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreTreeNode;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25 import com.google.common.primitives.UnsignedLong;
27 // FIXME: this should not be public
28 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
30 private final UnsignedLong nodeVersion;
31 private final UnsignedLong subtreeVersion;
32 private final NormalizedNode<?, ?> data;
34 private final Map<PathArgument, StoreMetadataNode> children;
40 * @param subtreeVersion
41 * @param children Map of children, must not be modified externally
43 protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
44 final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
45 this.nodeVersion = nodeVersion;
46 this.subtreeVersion = subtreeVersion;
48 this.children = Preconditions.checkNotNull(children);
51 public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
52 return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
53 Collections.<PathArgument, StoreMetadataNode>emptyMap());
56 public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
57 final UnsignedLong subtreeVersion) {
58 this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
61 public static Builder builder() {
65 public static Builder builder(StoreMetadataNode node) {
66 return new Builder(node);
69 public UnsignedLong getNodeVersion() {
70 return this.nodeVersion;
74 public PathArgument getIdentifier() {
75 return data.getIdentifier();
78 public UnsignedLong getSubtreeVersion() {
79 return subtreeVersion;
82 public NormalizedNode<?, ?> getData() {
87 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
88 return Optional.fromNullable(children.get(key));
92 public String toString() {
93 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
96 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
97 if (currentMetadata.isPresent()) {
98 return Optional.of(currentMetadata.get().getNodeVersion());
100 return Optional.absent();
103 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
104 final PathArgument child) {
105 if (parent.isPresent()) {
106 return parent.get().getChild(child);
108 return Optional.absent();
111 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
112 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
113 Builder builder = builder() //
114 .setNodeVersion(nodeVersion) //
115 .setSubtreeVersion(subtreeVersion) //
117 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
119 @SuppressWarnings("unchecked")
120 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
121 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
122 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
125 return builder.build();
128 public static class Builder {
130 private UnsignedLong nodeVersion;
131 private UnsignedLong subtreeVersion;
132 private NormalizedNode<?, ?> data;
133 private Map<PathArgument, StoreMetadataNode> children;
134 private boolean dirty = false;
137 children = new HashMap<>();
140 public Builder(StoreMetadataNode node) {
141 children = new HashMap<>(node.children);
144 public UnsignedLong getVersion() {
149 public Builder setNodeVersion(final UnsignedLong version) {
150 this.nodeVersion = version;
154 public Builder setSubtreeVersion(final UnsignedLong version) {
155 this.subtreeVersion = version;
159 public Builder setData(final NormalizedNode<?, ?> data) {
164 public Builder add(final StoreMetadataNode node) {
166 children = new HashMap<>(children);
169 children.put(node.getIdentifier(), node);
173 public Builder remove(final PathArgument id) {
175 children = new HashMap<>(children);
182 public StoreMetadataNode build() {
183 checkState(data != null, "Data node should not be null.");
184 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
185 "Subtree version must be equals or greater than node version.");
187 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
191 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
192 return createRecursively(node, version, version);