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