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 final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
87 final UnsignedLong version) {
88 Builder builder = builder(version) //
89 .setSubtreeVersion(version) //
91 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
93 @SuppressWarnings("unchecked")
94 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
95 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
96 builder.add(createRecursively(subNode, version));
99 return builder.build();
102 public static class Builder {
104 private final UnsignedLong nodeVersion;
105 private UnsignedLong subtreeVersion;
106 private NormalizedNode<?, ?> data;
107 private Map<PathArgument, StoreMetadataNode> children;
108 private boolean dirty = false;
110 private Builder(final UnsignedLong version) {
111 this.nodeVersion = Preconditions.checkNotNull(version);
112 children = new HashMap<>();
115 private Builder(final StoreMetadataNode node) {
116 this.nodeVersion = node.getNodeVersion();
117 children = new HashMap<>(node.children);
120 public Builder setSubtreeVersion(final UnsignedLong version) {
121 this.subtreeVersion = version;
125 public Builder setData(final NormalizedNode<?, ?> data) {
130 public Builder add(final StoreMetadataNode node) {
132 children = new HashMap<>(children);
135 children.put(node.getIdentifier(), node);
139 public Builder remove(final PathArgument id) {
141 children = new HashMap<>(children);
148 public StoreMetadataNode build() {
149 checkState(data != null, "Data node should not be null.");
150 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
151 "Subtree version must be equals or greater than node version.");
153 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);