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 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
29 private final UnsignedLong nodeVersion;
30 private final UnsignedLong subtreeVersion;
31 private final NormalizedNode<?, ?> data;
33 private final Map<PathArgument, StoreMetadataNode> children;
39 * @param subtreeVersion
40 * @param children Map of children, must not be modified externally
42 protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
43 final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
44 this.nodeVersion = nodeVersion;
45 this.subtreeVersion = subtreeVersion;
47 this.children = Preconditions.checkNotNull(children);
50 public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
51 return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
52 Collections.<PathArgument, StoreMetadataNode>emptyMap());
55 public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
56 final UnsignedLong subtreeVersion) {
57 this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
60 public static Builder builder() {
64 public static Builder builder(StoreMetadataNode node) {
65 return new Builder(node);
68 public UnsignedLong getNodeVersion() {
69 return this.nodeVersion;
73 public PathArgument getIdentifier() {
74 return data.getIdentifier();
77 public UnsignedLong getSubtreeVersion() {
78 return subtreeVersion;
81 public NormalizedNode<?, ?> getData() {
86 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
87 return Optional.fromNullable(children.get(key));
91 public String toString() {
92 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
95 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
96 if (currentMetadata.isPresent()) {
97 return Optional.of(currentMetadata.get().getNodeVersion());
99 return Optional.absent();
102 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
103 final PathArgument child) {
104 if (parent.isPresent()) {
105 return parent.get().getChild(child);
107 return Optional.absent();
110 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
111 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
112 Builder builder = builder() //
113 .setNodeVersion(nodeVersion) //
114 .setSubtreeVersion(subtreeVersion) //
116 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
118 @SuppressWarnings("unchecked")
119 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
120 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
121 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
124 return builder.build();
127 public static class Builder {
129 private UnsignedLong nodeVersion;
130 private UnsignedLong subtreeVersion;
131 private NormalizedNode<?, ?> data;
132 private Map<PathArgument, StoreMetadataNode> children;
133 private boolean dirty = false;
136 children = new HashMap<>();
139 public Builder(StoreMetadataNode node) {
140 children = new HashMap<>(node.children);
143 public UnsignedLong getVersion() {
148 public Builder setNodeVersion(final UnsignedLong version) {
149 this.nodeVersion = version;
153 public Builder setSubtreeVersion(final UnsignedLong version) {
154 this.subtreeVersion = version;
158 public Builder setData(final NormalizedNode<?, ?> data) {
163 public Builder add(final StoreMetadataNode node) {
165 children = new HashMap<>(children);
168 children.put(node.getIdentifier(), node);
172 public Builder remove(final PathArgument id) {
174 children = new HashMap<>(children);
181 public StoreMetadataNode build() {
182 checkState(data != null, "Data node should not be null.");
183 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
184 "Subtree version must be equals or greater than node version.");
186 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
190 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
191 return createRecursively(node, version, version);