BUG-509: node version is always required
[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(final UnsignedLong version) {
53         return new Builder(version);
54     }
55
56     public static Builder builder(final 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(nodeVersion) //
104                 .setSubtreeVersion(subtreeVersion) //
105                 .setData(node);
106         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
107
108             @SuppressWarnings("unchecked")
109             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
110             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
111                 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
112             }
113         }
114         return builder.build();
115     }
116
117     public static class Builder {
118
119         private final UnsignedLong nodeVersion;
120         private UnsignedLong subtreeVersion;
121         private NormalizedNode<?, ?> data;
122         private Map<PathArgument, StoreMetadataNode> children;
123         private boolean dirty = false;
124
125         private Builder(final UnsignedLong version) {
126             this.nodeVersion = Preconditions.checkNotNull(version);
127             children = new HashMap<>();
128         }
129
130         private Builder(final StoreMetadataNode node) {
131             this.nodeVersion = node.getNodeVersion();
132             children = new HashMap<>(node.children);
133         }
134
135         public Builder setSubtreeVersion(final UnsignedLong version) {
136             this.subtreeVersion = version;
137             return this;
138         }
139
140         public Builder setData(final NormalizedNode<?, ?> data) {
141             this.data = data;
142             return this;
143         }
144
145         public Builder add(final StoreMetadataNode node) {
146             if (dirty) {
147                 children = new HashMap<>(children);
148                 dirty = false;
149             }
150             children.put(node.getIdentifier(), node);
151             return this;
152         }
153
154         public Builder remove(final PathArgument id) {
155             if (dirty) {
156                 children = new HashMap<>(children);
157                 dirty = false;
158             }
159             children.remove(id);
160             return this;
161         }
162
163         public StoreMetadataNode build() {
164             checkState(data != null, "Data node should not be null.");
165             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
166                     "Subtree version must be equals or greater than node version.");
167             dirty = true;
168             return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
169         }
170     }
171
172     public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
173         return createRecursively(node, version, version);
174     }
175 }