BUG-509: create data tree SPI package
[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 final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
87             final UnsignedLong version) {
88         Builder builder = builder(version) //
89                 .setSubtreeVersion(version) //
90                 .setData(node);
91         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
92
93             @SuppressWarnings("unchecked")
94             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
95             for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
96                 builder.add(createRecursively(subNode, version));
97             }
98         }
99         return builder.build();
100     }
101
102     public static class Builder {
103
104         private final UnsignedLong nodeVersion;
105         private UnsignedLong subtreeVersion;
106         private NormalizedNode<?, ?> data;
107         private Map<PathArgument, StoreMetadataNode> children;
108         private boolean dirty = false;
109
110         private Builder(final UnsignedLong version) {
111             this.nodeVersion = Preconditions.checkNotNull(version);
112             children = new HashMap<>();
113         }
114
115         private Builder(final StoreMetadataNode node) {
116             this.nodeVersion = node.getNodeVersion();
117             children = new HashMap<>(node.children);
118         }
119
120         public Builder setSubtreeVersion(final UnsignedLong version) {
121             this.subtreeVersion = version;
122             return this;
123         }
124
125         public Builder setData(final NormalizedNode<?, ?> data) {
126             this.data = data;
127             return this;
128         }
129
130         public Builder add(final StoreMetadataNode node) {
131             if (dirty) {
132                 children = new HashMap<>(children);
133                 dirty = false;
134             }
135             children.put(node.getIdentifier(), node);
136             return this;
137         }
138
139         public Builder remove(final PathArgument id) {
140             if (dirty) {
141                 children = new HashMap<>(children);
142                 dirty = false;
143             }
144             children.remove(id);
145             return this;
146         }
147
148         public StoreMetadataNode build() {
149             checkState(data != null, "Data node should not be null.");
150             checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
151                     "Subtree version must be equals or greater than node version.");
152             dirty = true;
153             return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
154         }
155     }
156
157 }