Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[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.LinkedHashMap;
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.collect.Iterables;
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     public Iterable<StoreMetadataNode> getChildren() {
86         return Iterables.unmodifiableIterable(children.values());
87     }
88
89     @Override
90     public Optional<StoreMetadataNode> getChild(final PathArgument key) {
91         return Optional.fromNullable(children.get(key));
92     }
93
94     @Override
95     public String toString() {
96         return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
97     }
98
99     public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
100         if (currentMetadata.isPresent()) {
101             return Optional.of(currentMetadata.get().getNodeVersion());
102         }
103         return Optional.absent();
104     }
105
106     public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent,
107             final PathArgument child) {
108         if (parent.isPresent()) {
109             return parent.get().getChild(child);
110         }
111         return Optional.absent();
112     }
113
114     public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
115             final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
116         Builder builder = builder() //
117                 .setNodeVersion(nodeVersion) //
118                 .setSubtreeVersion(subtreeVersion) //
119                 .setData(node);
120         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
121
122             @SuppressWarnings("unchecked")
123             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
124             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
125                 builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
126             }
127         }
128         return builder.build();
129     }
130
131     public static class Builder {
132
133         private UnsignedLong nodeVersion;
134         private UnsignedLong subtreeVersion;
135         private NormalizedNode<?, ?> data;
136         private Map<PathArgument, StoreMetadataNode> children;
137         private boolean dirty = false;
138
139         private Builder() {
140             children = new LinkedHashMap<>();
141         }
142
143         public Builder(StoreMetadataNode node) {
144             children = new LinkedHashMap<>(node.children);
145         }
146
147         public UnsignedLong getVersion() {
148             return nodeVersion;
149
150         }
151
152         public Builder setNodeVersion(final UnsignedLong version) {
153             this.nodeVersion = version;
154             return this;
155         }
156
157         public Builder setSubtreeVersion(final UnsignedLong version) {
158             this.subtreeVersion = version;
159             return this;
160         }
161
162         public Builder setData(final NormalizedNode<?, ?> data) {
163             this.data = data;
164             return this;
165         }
166
167         public Builder add(final StoreMetadataNode node) {
168             if (dirty) {
169                 children = new LinkedHashMap<>(children);
170                 dirty = false;
171             }
172             children.put(node.getIdentifier(), node);
173             return this;
174         }
175
176         public Builder remove(final PathArgument id) {
177             if (dirty) {
178                 children = new LinkedHashMap<>(children);
179                 dirty = false;
180             }
181             children.remove(id);
182             return this;
183         }
184
185         public StoreMetadataNode build() {
186             checkState(data != null, "Data node should not be null.");
187             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
188                     "Subtree version must be equals or greater than node version.");
189             dirty = true;
190             return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
191         }
192     }
193
194     public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
195         return createRecursively(node, version, version);
196     }
197 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.