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.LinkedHashMap;
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.collect.Iterables;
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() {
85 public Iterable<StoreMetadataNode> getChildren() {
86 return Iterables.unmodifiableIterable(children.values());
90 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
91 return Optional.fromNullable(children.get(key));
95 public String toString() {
96 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
99 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
100 if (currentMetadata.isPresent()) {
101 return Optional.of(currentMetadata.get().getNodeVersion());
103 return Optional.absent();
106 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
107 final PathArgument child) {
108 if (parent.isPresent()) {
109 return parent.get().getChild(child);
111 return Optional.absent();
114 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
115 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
116 Builder builder = builder() //
117 .setNodeVersion(nodeVersion) //
118 .setSubtreeVersion(subtreeVersion) //
120 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
122 @SuppressWarnings("unchecked")
123 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
124 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
125 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
128 return builder.build();
131 public static class Builder {
133 private UnsignedLong nodeVersion;
134 private UnsignedLong subtreeVersion;
135 private NormalizedNode<?, ?> data;
136 private Map<PathArgument, StoreMetadataNode> children;
137 private boolean dirty = false;
140 children = new LinkedHashMap<>();
143 public Builder(StoreMetadataNode node) {
144 children = new LinkedHashMap<>(node.children);
147 public UnsignedLong getVersion() {
152 public Builder setNodeVersion(final UnsignedLong version) {
153 this.nodeVersion = version;
157 public Builder setSubtreeVersion(final UnsignedLong version) {
158 this.subtreeVersion = version;
162 public Builder setData(final NormalizedNode<?, ?> data) {
167 public Builder add(final StoreMetadataNode node) {
169 children = new LinkedHashMap<>(children);
172 children.put(node.getIdentifier(), node);
176 public Builder remove(final PathArgument id) {
178 children = new LinkedHashMap<>(children);
185 public StoreMetadataNode build() {
186 checkState(data != null, "Data node should not be null.");
187 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
188 "Subtree version must be equals or greater than node version.");
190 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
194 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
195 return createRecursively(node, version, version);