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