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;
10 import static com.google.common.base.Preconditions.checkState;
12 import java.util.Collections;
13 import java.util.HashMap;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Preconditions;
24 import com.google.common.primitives.UnsignedLong;
26 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
28 private final UnsignedLong nodeVersion;
29 private final UnsignedLong subtreeVersion;
30 private final NormalizedNode<?, ?> data;
32 private final Map<PathArgument, StoreMetadataNode> children;
38 * @param subtreeVersion
39 * @param children Map of children, must not be modified externally
41 protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
42 final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
43 this.nodeVersion = nodeVersion;
44 this.subtreeVersion = subtreeVersion;
46 this.children = Preconditions.checkNotNull(children);
49 public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
50 return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
51 Collections.<PathArgument, StoreMetadataNode>emptyMap());
54 public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
55 final UnsignedLong subtreeVersion) {
56 this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
59 public static Builder builder() {
63 public static Builder builder(StoreMetadataNode node) {
64 return new Builder(node);
67 public UnsignedLong getNodeVersion() {
68 return this.nodeVersion;
72 public PathArgument getIdentifier() {
73 return data.getIdentifier();
76 public UnsignedLong getSubtreeVersion() {
77 return subtreeVersion;
80 public NormalizedNode<?, ?> getData() {
85 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
86 return Optional.fromNullable(children.get(key));
90 public String toString() {
91 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
94 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
95 if (currentMetadata.isPresent()) {
96 return Optional.of(currentMetadata.get().getNodeVersion());
98 return Optional.absent();
101 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
102 final PathArgument child) {
103 if (parent.isPresent()) {
104 return parent.get().getChild(child);
106 return Optional.absent();
109 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
110 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
111 Builder builder = builder() //
112 .setNodeVersion(nodeVersion) //
113 .setSubtreeVersion(subtreeVersion) //
115 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
117 @SuppressWarnings("unchecked")
118 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
119 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
120 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
123 return builder.build();
126 public static class Builder {
128 private UnsignedLong nodeVersion;
129 private UnsignedLong subtreeVersion;
130 private NormalizedNode<?, ?> data;
131 private Map<PathArgument, StoreMetadataNode> children;
132 private boolean dirty = false;
135 children = new HashMap<>();
138 public Builder(StoreMetadataNode node) {
139 children = new HashMap<>(node.children);
142 public UnsignedLong getVersion() {
147 public Builder setNodeVersion(final UnsignedLong version) {
148 this.nodeVersion = version;
152 public Builder setSubtreeVersion(final UnsignedLong version) {
153 this.subtreeVersion = version;
157 public Builder setData(final NormalizedNode<?, ?> data) {
162 public Builder add(final StoreMetadataNode node) {
164 children = new HashMap<>(children);
167 children.put(node.getIdentifier(), node);
171 public Builder remove(final PathArgument id) {
173 children = new HashMap<>(children);
180 public StoreMetadataNode build() {
181 checkState(data != null, "Data node should not be null.");
182 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
183 "Subtree version must be equals or greater than node version.");
185 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
189 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
190 return createRecursively(node, version, version);