Bug 499: Initial draft of in-memory datastore and data broker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / DataAndMetadataSnapshot.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;
9
10 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
11 import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils;
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 import com.google.common.base.Optional;
19 import com.google.common.primitives.UnsignedLong;
20
21 class DataAndMetadataSnapshot {
22
23     private final StoreMetadataNode metadataTree;
24     private final Optional<SchemaContext> schemaContext;
25
26
27
28
29     private DataAndMetadataSnapshot(final StoreMetadataNode metadataTree, final Optional<SchemaContext> schemaCtx) {
30         this.metadataTree = metadataTree;
31         this.schemaContext = schemaCtx;
32     }
33
34     public static Builder builder() {
35         return new Builder();
36     }
37
38     public static DataAndMetadataSnapshot createEmpty() {
39         return createEmpty(new NodeIdentifier(SchemaContext.NAME));
40     }
41
42
43     public static DataAndMetadataSnapshot createEmpty(final NodeIdentifier rootNode) {
44         NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(rootNode).build();
45         StoreMetadataNode metadata = StoreMetadataNode.builder()
46                 .setNodeVersion(UnsignedLong.ZERO)
47                 .setSubtreeVersion(UnsignedLong.ZERO)
48                 .setData(data)
49                 .build();
50         return new DataAndMetadataSnapshot(metadata,Optional.<SchemaContext>absent());
51     }
52
53     public static DataAndMetadataSnapshot createEmpty(final SchemaContext ctx) {
54         NodeIdentifier rootNodeIdentifier = new NodeIdentifier(ctx.getQName());
55         NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(rootNodeIdentifier).build();
56         StoreMetadataNode metadata = StoreMetadataNode.builder()
57                 .setData(data)
58                 .setNodeVersion(UnsignedLong.ZERO)
59                 .setSubtreeVersion(UnsignedLong.ZERO)
60                 .build();
61         return new DataAndMetadataSnapshot(metadata, Optional.of(ctx));
62     }
63
64     public Optional<SchemaContext> getSchemaContext() {
65         return schemaContext;
66     }
67
68     public NormalizedNode<?, ?> getDataTree() {
69         return metadataTree.getData();
70     }
71
72     public StoreMetadataNode getMetadataTree() {
73         return metadataTree;
74     }
75
76     public Optional<StoreMetadataNode> read(final InstanceIdentifier path) {
77         return TreeNodeUtils.findNode(metadataTree, path);
78     }
79
80     public static class Builder {
81         private NormalizedNode<?, ?> dataTree;
82         private StoreMetadataNode metadataTree;
83         private SchemaContext schemaContext;
84
85         public NormalizedNode<?, ?> getDataTree() {
86             return dataTree;
87         }
88
89         public Builder setMetadataTree(final StoreMetadataNode metadataTree) {
90             this.metadataTree = metadataTree;
91             return this;
92         }
93
94         public Builder setSchemaContext(final SchemaContext schemaContext) {
95             this.schemaContext = schemaContext;
96             return this;
97         }
98
99         public DataAndMetadataSnapshot build() {
100             return new DataAndMetadataSnapshot(metadataTree, Optional.fromNullable(schemaContext));
101         }
102
103     }
104 }

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