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