974f817c71776c1bb7a7a27437eba0920e993482
[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.Map;
13
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.primitives.UnsignedLong;
23
24 public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
25
26     private final UnsignedLong nodeVersion;
27     private final UnsignedLong subtreeVersion;
28     private final NormalizedNode<?, ?> data;
29
30     private final Map<PathArgument, StoreMetadataNode> children;
31
32     protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion,
33             final Map<PathArgument, StoreMetadataNode> children) {
34         this.nodeVersion = nodeVersion;
35         this.subtreeVersion = subtreeVersion;
36         this.data = data;
37         this.children = ImmutableMap.copyOf(children);
38
39     }
40
41     public static Builder builder() {
42         return new Builder();
43     }
44
45     public UnsignedLong getNodeVersion() {
46         return this.nodeVersion;
47     }
48
49     @Override
50     public PathArgument getIdentifier() {
51         return data.getIdentifier();
52     }
53
54     public UnsignedLong getSubtreeVersion() {
55         return subtreeVersion;
56     }
57
58     public NormalizedNode<?, ?> getData() {
59         return this.data;
60     }
61
62     public Iterable<StoreMetadataNode> getChildren() {
63         return children.values();
64     }
65
66     @Override
67     public Optional<StoreMetadataNode> getChild(final PathArgument key) {
68         return Optional.fromNullable(children.get(key));
69     }
70
71     @Override
72     public String toString() {
73         return "StoreMetadataNode [identifier=" + getIdentifier() + ", nodeVersion=" + nodeVersion + "]";
74     }
75
76     public static Optional<UnsignedLong> getVersion(final Optional<StoreMetadataNode> currentMetadata) {
77         if (currentMetadata.isPresent()) {
78             return Optional.of(currentMetadata.get().getNodeVersion());
79         }
80         return Optional.absent();
81     }
82
83     public static Optional<StoreMetadataNode> getChild(final Optional<StoreMetadataNode> parent, final PathArgument child) {
84         if (parent.isPresent()) {
85             return parent.get().getChild(child);
86         }
87         return Optional.absent();
88     }
89
90     public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
91         Builder builder = builder() //
92                 .setNodeVersion(version) //
93                 .setSubtreeVersion(version) //
94                 .setData(node);
95         if(node instanceof NormalizedNodeContainer<?, ?, ?>) {
96
97             @SuppressWarnings("unchecked")
98             NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
99             for(NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
100                 builder.add(createRecursivelly(subNode, version));
101             }
102         }
103         return builder.build();
104     }
105
106     public static class Builder {
107
108         private Builder() {
109
110         }
111
112         UnsignedLong nodeVersion = UnsignedLong.valueOf(0);
113         UnsignedLong subtreeVersion = UnsignedLong.valueOf(0);
114         NormalizedNode<?, ?> data;
115
116         final ImmutableMap.Builder<PathArgument, StoreMetadataNode> children = ImmutableMap.builder();
117
118         public UnsignedLong getVersion() {
119             return nodeVersion;
120
121         }
122
123         public Builder setNodeVersion(final UnsignedLong version) {
124             this.nodeVersion = version;
125             return this;
126         }
127
128         public Builder setSubtreeVersion(final UnsignedLong version) {
129             this.subtreeVersion = version;
130             return this;
131         }
132
133         public Builder setData(final NormalizedNode<?,?> data) {
134             this.data = data;
135             return this;
136         }
137
138         public Builder add(final StoreMetadataNode node) {
139             children.put(node.getIdentifier(), node);
140             return this;
141         }
142
143         public StoreMetadataNode build() {
144             checkState(data != null,"Data node should not be null.");
145             checkState(subtreeVersion.compareTo(nodeVersion) >= 0, "Subtree version must be equals or greater than node version.");
146             return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children.build());
147         }
148     }
149
150 }

©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.