Merge "BUG-980: stop emiting copyright"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / StoreMetadataNode.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Iterables;
24 import com.google.common.primitives.UnsignedLong;
25
26 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
27
28     private final UnsignedLong nodeVersion;
29     private final UnsignedLong subtreeVersion;
30     private final NormalizedNode<?, ?> data;
31
32     private final Map<PathArgument, StoreMetadataNode> children;
33
34     /**
35      *
36      * @param data
37      * @param nodeVersion
38      * @param subtreeVersion
39      * @param children Map of children, must not be modified externally
40      */
41     protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
42             final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
43         this.nodeVersion = nodeVersion;
44         this.subtreeVersion = subtreeVersion;
45         this.data = data;
46         this.children = Preconditions.checkNotNull(children);
47     }
48
49     public static Builder builder() {
50         return new Builder();
51     }
52
53     public UnsignedLong getNodeVersion() {
54         return this.nodeVersion;
55     }
56
57     @Override
58     public PathArgument getIdentifier() {
59         return data.getIdentifier();
60     }
61
62     public UnsignedLong getSubtreeVersion() {
63         return subtreeVersion;
64     }
65
66     public NormalizedNode<?, ?> getData() {
67         return this.data;
68     }
69
70     public Iterable<StoreMetadataNode> getChildren() {
71         return Iterables.unmodifiableIterable(children.values());
72     }
73
74     @Override
75     public Optional<StoreMetadataNode> getChild(final PathArgument key) {
76         return Optional.fromNullable(children.get(key));
77     }
78
79     @Override
80     public String toString() {
81         return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
82     }
83
84     public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
85         if (currentMetadata.isPresent()) {
86             return Optional.of(currentMetadata.get().getNodeVersion());
87         }
88         return Optional.absent();
89     }
90
91     public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
92             final PathArgument child) {
93         if (parent.isPresent()) {
94             return parent.get().getChild(child);
95         }
96         return Optional.absent();
97     }
98
99     public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
100             final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
101         Builder builder = builder() //
102                 .setNodeVersion(nodeVersion) //
103                 .setSubtreeVersion(subtreeVersion) //
104                 .setData(node);
105         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
106
107             @SuppressWarnings("unchecked")
108             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
109             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
110                 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
111             }
112         }
113         return builder.build();
114     }
115
116     public static class Builder {
117
118         private UnsignedLong nodeVersion;
119         private UnsignedLong subtreeVersion;
120         private NormalizedNode<?, ?> data;
121         private Map<PathArgument, StoreMetadataNode> children = new LinkedHashMap<>();
122         private boolean dirty = false;
123
124         private Builder() {}
125
126
127         public UnsignedLong getVersion() {
128             return nodeVersion;
129
130         }
131
132         public Builder setNodeVersion(final UnsignedLong version) {
133             this.nodeVersion = version;
134             return this;
135         }
136
137         public Builder setSubtreeVersion(final UnsignedLong version) {
138             this.subtreeVersion = version;
139             return this;
140         }
141
142         public Builder setData(final NormalizedNode<?, ?> data) {
143             this.data = data;
144             return this;
145         }
146
147         public Builder add(final StoreMetadataNode node) {
148             if (dirty) {
149                 children = new LinkedHashMap<>(children);
150                 dirty = false;
151             }
152             children.put(node.getIdentifier(), node);
153             return this;
154         }
155
156         public StoreMetadataNode build() {
157             checkState(data != null, "Data node should not be null.");
158             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
159                     "Subtree version must be equals or greater than node version.");
160             dirty = true;
161             return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
162         }
163     }
164
165     public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
166         return createRecursively(node, version, version);
167     }
168
169 }