BUG-509: simple cleanup of DataTreeModification
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / 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.data;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Map;
15
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;
22
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25 import com.google.common.primitives.UnsignedLong;
26
27 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
28
29     private final UnsignedLong nodeVersion;
30     private final UnsignedLong subtreeVersion;
31     private final NormalizedNode<?, ?> data;
32
33     private final Map<PathArgument, StoreMetadataNode> children;
34
35     /**
36      *
37      * @param data
38      * @param nodeVersion
39      * @param subtreeVersion
40      * @param children Map of children, must not be modified externally
41      */
42     protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
43             final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
44         this.nodeVersion = nodeVersion;
45         this.subtreeVersion = subtreeVersion;
46         this.data = data;
47         this.children = Preconditions.checkNotNull(children);
48     }
49
50     public static StoreMetadataNode createEmpty(final NormalizedNode<?, ?> data) {
51         return new StoreMetadataNode(data, UnsignedLong.ZERO, UnsignedLong.ZERO,
52                 Collections.<PathArgument, StoreMetadataNode>emptyMap());
53     }
54
55     public StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
56             final UnsignedLong subtreeVersion) {
57         this(data, nodeVersion, subtreeVersion, Collections.<PathArgument, StoreMetadataNode>emptyMap());
58     }
59
60     public static Builder builder() {
61         return new Builder();
62     }
63
64     public static Builder builder(StoreMetadataNode node) {
65         return new Builder(node);
66     }
67
68     public UnsignedLong getNodeVersion() {
69         return this.nodeVersion;
70     }
71
72     @Override
73     public PathArgument getIdentifier() {
74         return data.getIdentifier();
75     }
76
77     public UnsignedLong getSubtreeVersion() {
78         return subtreeVersion;
79     }
80
81     public NormalizedNode<?, ?> getData() {
82         return this.data;
83     }
84
85     @Override
86     public Optional<StoreMetadataNode> getChild(final PathArgument key) {
87         return Optional.fromNullable(children.get(key));
88     }
89
90     @Override
91     public String toString() {
92         return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
93     }
94
95     public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
96         if (currentMetadata.isPresent()) {
97             return Optional.of(currentMetadata.get().getNodeVersion());
98         }
99         return Optional.absent();
100     }
101
102     public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
103             final PathArgument child) {
104         if (parent.isPresent()) {
105             return parent.get().getChild(child);
106         }
107         return Optional.absent();
108     }
109
110     public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
111             final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
112         Builder builder = builder() //
113                 .setNodeVersion(nodeVersion) //
114                 .setSubtreeVersion(subtreeVersion) //
115                 .setData(node);
116         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
117
118             @SuppressWarnings("unchecked")
119             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
120             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
121                 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
122             }
123         }
124         return builder.build();
125     }
126
127     public static class Builder {
128
129         private UnsignedLong nodeVersion;
130         private UnsignedLong subtreeVersion;
131         private NormalizedNode<?, ?> data;
132         private Map<PathArgument, StoreMetadataNode> children;
133         private boolean dirty = false;
134
135         private Builder() {
136             children = new HashMap<>();
137         }
138
139         public Builder(StoreMetadataNode node) {
140             children = new HashMap<>(node.children);
141         }
142
143         public UnsignedLong getVersion() {
144             return nodeVersion;
145
146         }
147
148         public Builder setNodeVersion(final UnsignedLong version) {
149             this.nodeVersion = version;
150             return this;
151         }
152
153         public Builder setSubtreeVersion(final UnsignedLong version) {
154             this.subtreeVersion = version;
155             return this;
156         }
157
158         public Builder setData(final NormalizedNode<?, ?> data) {
159             this.data = data;
160             return this;
161         }
162
163         public Builder add(final StoreMetadataNode node) {
164             if (dirty) {
165                 children = new HashMap<>(children);
166                 dirty = false;
167             }
168             children.put(node.getIdentifier(), node);
169             return this;
170         }
171
172         public Builder remove(final PathArgument id) {
173             if (dirty) {
174                 children = new HashMap<>(children);
175                 dirty = false;
176             }
177             children.remove(id);
178             return this;
179         }
180
181         public StoreMetadataNode build() {
182             checkState(data != null, "Data node should not be null.");
183             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
184                     "Subtree version must be equals or greater than node version.");
185             dirty = true;
186             return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
187         }
188     }
189
190     public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
191         return createRecursively(node, version, version);
192     }
193 }