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.LinkedHashMap;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Iterables;
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 Builder builder() {
53 public UnsignedLong getNodeVersion() {
54 return this.nodeVersion;
58 public PathArgument getIdentifier() {
59 return data.getIdentifier();
62 public UnsignedLong getSubtreeVersion() {
63 return subtreeVersion;
66 public NormalizedNode<?, ?> getData() {
70 public Iterable<StoreMetadataNode> getChildren() {
71 return Iterables.unmodifiableIterable(children.values());
75 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
76 return Optional.fromNullable(children.get(key));
80 public String toString() {
81 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
84 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
85 if (currentMetadata.isPresent()) {
86 return Optional.of(currentMetadata.get().getNodeVersion());
88 return Optional.absent();
91 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
92 final PathArgument child) {
93 if (parent.isPresent()) {
94 return parent.get().getChild(child);
96 return Optional.absent();
99 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
100 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
101 Builder builder = builder() //
102 .setNodeVersion(nodeVersion) //
103 .setSubtreeVersion(subtreeVersion) //
105 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
107 @SuppressWarnings("unchecked")
108 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
109 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
110 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
113 return builder.build();
116 public static class Builder {
118 private UnsignedLong nodeVersion;
119 private UnsignedLong subtreeVersion;
120 private NormalizedNode<?, ?> data;
121 private Map<PathArgument, StoreMetadataNode> children = new LinkedHashMap<>();
122 private boolean dirty = false;
127 public UnsignedLong getVersion() {
132 public Builder setNodeVersion(final UnsignedLong version) {
133 this.nodeVersion = version;
137 public Builder setSubtreeVersion(final UnsignedLong version) {
138 this.subtreeVersion = version;
142 public Builder setData(final NormalizedNode<?, ?> data) {
147 public Builder add(final StoreMetadataNode node) {
149 children = new LinkedHashMap<>(children);
152 children.put(node.getIdentifier(), node);
156 public StoreMetadataNode build() {
157 checkState(data != null, "Data node should not be null.");
158 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
159 "Subtree version must be equals or greater than node version.");
161 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
165 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
166 return createRecursively(node, version, version);