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.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 class StoreMetadataNode implements Immutable, Identifiable<PathArgument> {
27 private final Map<PathArgument, StoreMetadataNode> children;
28 private final UnsignedLong nodeVersion;
29 private final UnsignedLong subtreeVersion;
30 private final NormalizedNode<?, ?> data;
36 * @param subtreeVersion
37 * @param children Map of children, must not be modified externally
39 private StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
40 final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
41 this.nodeVersion = Preconditions.checkNotNull(nodeVersion);
42 this.subtreeVersion = Preconditions.checkNotNull(subtreeVersion);
43 this.data = Preconditions.checkNotNull(data);
44 this.children = Preconditions.checkNotNull(children);
47 public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
48 return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
49 Collections.<PathArgument, StoreMetadataNode>emptyMap());
52 public static Builder builder(final UnsignedLong version) {
53 return new Builder(version);
56 public static Builder builder(final StoreMetadataNode node) {
57 return new Builder(node);
60 public UnsignedLong getNodeVersion() {
61 return this.nodeVersion;
65 public PathArgument getIdentifier() {
66 return data.getIdentifier();
69 public UnsignedLong getSubtreeVersion() {
70 return subtreeVersion;
73 public NormalizedNode<?, ?> getData() {
77 Optional<StoreMetadataNode> getChild(final PathArgument key) {
78 return Optional.fromNullable(children.get(key));
82 public String toString() {
83 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
86 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
87 if (currentMetadata.isPresent()) {
88 return Optional.of(currentMetadata.get().getNodeVersion());
90 return Optional.absent();
93 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
94 final PathArgument child) {
95 if (parent.isPresent()) {
96 return parent.get().getChild(child);
98 return Optional.absent();
101 public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
102 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
103 Builder builder = builder(nodeVersion) //
104 .setSubtreeVersion(subtreeVersion) //
106 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
108 @SuppressWarnings("unchecked")
109 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
110 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
111 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
114 return builder.build();
117 public static class Builder {
119 private final UnsignedLong nodeVersion;
120 private UnsignedLong subtreeVersion;
121 private NormalizedNode<?, ?> data;
122 private Map<PathArgument, StoreMetadataNode> children;
123 private boolean dirty = false;
125 private Builder(final UnsignedLong version) {
126 this.nodeVersion = Preconditions.checkNotNull(version);
127 children = new HashMap<>();
130 private Builder(final StoreMetadataNode node) {
131 this.nodeVersion = node.getNodeVersion();
132 children = new HashMap<>(node.children);
135 public Builder setSubtreeVersion(final UnsignedLong version) {
136 this.subtreeVersion = version;
140 public Builder setData(final NormalizedNode<?, ?> data) {
145 public Builder add(final StoreMetadataNode node) {
147 children = new HashMap<>(children);
150 children.put(node.getIdentifier(), node);
154 public Builder remove(final PathArgument id) {
156 children = new HashMap<>(children);
163 public StoreMetadataNode build() {
164 checkState(data != null, "Data node should not be null.");
165 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
166 "Subtree version must be equals or greater than node version.");
168 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
172 public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
173 return createRecursively(node, version, version);