Bug 499: Initial implementation of data tree modifications
[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
20 class DataAndMetadataSnapshot {
21
22     private final StoreMetadataNode metadataTree;
23     private final Optional<SchemaContext> schemaContext;
24
25
26
27
28     private DataAndMetadataSnapshot(final StoreMetadataNode metadataTree, final Optional<SchemaContext> schemaCtx) {
29         this.metadataTree = metadataTree;
30         this.schemaContext = schemaCtx;
31     }
32
33     public static Builder builder() {
34         return new Builder();
35     }
36
37     public static DataAndMetadataSnapshot createEmpty(final NodeIdentifier rootNode) {
38         NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(rootNode).build();
39         StoreMetadataNode metadata = StoreMetadataNode.builder()
40                 .setData(data)
41                 .build();
42         return new DataAndMetadataSnapshot(metadata,Optional.<SchemaContext>absent());
43     }
44
45     public static DataAndMetadataSnapshot createEmpty(final SchemaContext ctx) {
46         NodeIdentifier rootNodeIdentifier = new NodeIdentifier(ctx.getQName());
47         NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(rootNodeIdentifier).build();
48         StoreMetadataNode metadata = StoreMetadataNode.builder()
49                 .setData(data)
50                 .build();
51         return new DataAndMetadataSnapshot(metadata, Optional.of(ctx));
52     }
53
54     public Optional<SchemaContext> getSchemaContext() {
55         return schemaContext;
56     }
57
58     public NormalizedNode<?, ?> getDataTree() {
59         return metadataTree.getData();
60     }
61
62     public StoreMetadataNode getMetadataTree() {
63         return metadataTree;
64     }
65
66     public Optional<StoreMetadataNode> read(final InstanceIdentifier path) {
67         return TreeNodeUtils.findNode(metadataTree, path);
68     }
69
70     public static class Builder {
71         private NormalizedNode<?, ?> dataTree;
72         private StoreMetadataNode metadataTree;
73         private SchemaContext schemaContext;
74
75         public NormalizedNode<?, ?> getDataTree() {
76             return dataTree;
77         }
78
79         public Builder setMetadataTree(final StoreMetadataNode metadataTree) {
80             this.metadataTree = metadataTree;
81             return this;
82         }
83
84         public Builder setSchemaContext(final SchemaContext schemaContext) {
85             this.schemaContext = schemaContext;
86             return this;
87         }
88
89         public DataAndMetadataSnapshot build() {
90             return new DataAndMetadataSnapshot(metadataTree, Optional.fromNullable(schemaContext));
91         }
92
93     }
94 }