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;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.primitives.UnsignedLong;
24 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
26 private final UnsignedLong nodeVersion;
27 private final UnsignedLong subtreeVersion;
28 private final NormalizedNode<?, ?> data;
30 private final Map<PathArgument, StoreMetadataNode> children;
32 protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
33 final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
34 this.nodeVersion = nodeVersion;
35 this.subtreeVersion = subtreeVersion;
37 this.children = ImmutableMap.copyOf(children);
41 public static Builder builder() {
45 public UnsignedLong getNodeVersion() {
46 return this.nodeVersion;
50 public PathArgument getIdentifier() {
51 return data.getIdentifier();
54 public UnsignedLong getSubtreeVersion() {
55 return subtreeVersion;
58 public NormalizedNode<?, ?> getData() {
62 public Iterable<StoreMetadataNode> getChildren() {
63 return children.values();
67 public Optional<StoreMetadataNode> getChild(final PathArgument key) {
68 return Optional.fromNullable(children.get(key));
72 public String toString() {
73 return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
76 public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
77 if (currentMetadata.isPresent()) {
78 return Optional.of(currentMetadata.get().getNodeVersion());
80 return Optional.absent();
83 public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
84 final PathArgument child) {
85 if (parent.isPresent()) {
86 return parent.get().getChild(child);
88 return Optional.absent();
91 public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node,
92 final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
93 Builder builder = builder() //
94 .setNodeVersion(nodeVersion) //
95 .setSubtreeVersion(subtreeVersion) //
97 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
99 @SuppressWarnings("unchecked")
100 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
101 for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
102 builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion));
105 return builder.build();
108 public static class Builder {
110 private UnsignedLong nodeVersion;
111 private UnsignedLong subtreeVersion;
112 private NormalizedNode<?, ?> data;
113 private final ImmutableMap.Builder<PathArgument, StoreMetadataNode> children = ImmutableMap.builder();
118 public UnsignedLong getVersion() {
123 public Builder setNodeVersion(final UnsignedLong version) {
124 this.nodeVersion = version;
128 public Builder setSubtreeVersion(final UnsignedLong version) {
129 this.subtreeVersion = version;
133 public Builder setData(final NormalizedNode<?, ?> data) {
138 public Builder add(final StoreMetadataNode node) {
139 children.put(node.getIdentifier(), node);
143 public StoreMetadataNode build() {
144 checkState(data != null, "Data node should not be null.");
145 checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
146 "Subtree version must be equals or greater than node version.");
147 return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children.build());
151 public static StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
152 return createRecursivelly(node, version, version);