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

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