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 class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
28 private final Map<PathArgument, StoreMetadataNode> children;
29 private final UnsignedLong nodeVersion;
30 private final UnsignedLong subtreeVersion;
31 private final NormalizedNode<?, ?> data;
37 * @param subtreeVersion
38 * @param children Map of children, must not be modified externally
40 protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
41 final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
42 this.nodeVersion = Preconditions.checkNotNull(nodeVersion);
43 this.subtreeVersion = Preconditions.checkNotNull(subtreeVersion);
44 this.data = Preconditions.checkNotNull(data);
45 this.children = Preconditions.checkNotNull(children);
48 public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
49 return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
50 Collections.<PathArgument, StoreMetadataNode>emptyMap());
53 public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
54 final UnsignedLong subtreeVersion) {
55 this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
58 public static Builder builder() {
62 public static Builder builder(StoreMetadataNode node) {
63 return new Builder(node);
66 public UnsignedLong getNodeVersion() {
67 return this.nodeVersion;
71 public PathArgument getIdentifier() {
72 return data.getIdentifier();
75 public UnsignedLong getSubtreeVersion() {
76 return subtreeVersion;
79 public NormalizedNode<?, ?> getData() {
84 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
85 return Optional.fromNullable(children.get(key));
89 public String toString() {
90 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
93 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
94 if (currentMetadata.isPresent()) {
95 return Optional.of(currentMetadata.get().getNodeVersion());
97 return Optional.absent();
100 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
101 final PathArgument child) {
102 if (parent.isPresent()) {
103 return parent.get().getChild(child);
105 return Optional.absent();
108 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
109 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
110 Builder builder = builder() //
111 .setNodeVersion(nodeVersion) //
112 .setSubtreeVersion(subtreeVersion) //
114 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
116 @SuppressWarnings("unchecked")
117 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
118 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
119 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
122 return builder.build();
125 public static class Builder {
127 private UnsignedLong nodeVersion;
128 private UnsignedLong subtreeVersion;
129 private NormalizedNode<?, ?> data;
130 private Map<PathArgument, StoreMetadataNode> children;
131 private boolean dirty = false;
134 children = new HashMap<>();
137 public Builder(StoreMetadataNode node) {
138 children = new HashMap<>(node.children);
141 public UnsignedLong getVersion() {
146 public Builder setNodeVersion(final UnsignedLong version) {
147 this.nodeVersion = version;
151 public Builder setSubtreeVersion(final UnsignedLong version) {
152 this.subtreeVersion = version;
156 public Builder setData(final NormalizedNode<?, ?> data) {
161 public Builder add(final StoreMetadataNode node) {
163 children = new HashMap<>(children);
166 children.put(node.getIdentifier(), node);
170 public Builder remove(final PathArgument id) {
172 children = new HashMap<>(children);
179 public StoreMetadataNode build() {
180 checkState(data != null, "Data node should not be null.");
181 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
182 "Subtree version must be equals or greater than node version.");
184 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
188 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
189 return createRecursively(node, version, version);