2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl;
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;
18 import com.google.common.base.Optional;
19 import com.google.common.primitives.UnsignedLong;
21 class DataAndMetadataSnapshot {
23 private final StoreMetadataNode metadataTree;
24 private final Optional<SchemaContext> schemaContext;
26 private DataAndMetadataSnapshot(final StoreMetadataNode metadataTree, final Optional<SchemaContext> schemaCtx) {
27 this.metadataTree = metadataTree;
28 this.schemaContext = schemaCtx;
31 public static Builder builder() {
35 public static DataAndMetadataSnapshot createEmpty() {
36 return createEmpty(new NodeIdentifier(SchemaContext.NAME));
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)
47 return new DataAndMetadataSnapshot(metadata,Optional.<SchemaContext>absent());
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()
55 .setNodeVersion(UnsignedLong.ZERO)
56 .setSubtreeVersion(UnsignedLong.ZERO)
58 return new DataAndMetadataSnapshot(metadata, Optional.of(ctx));
61 public Optional<SchemaContext> getSchemaContext() {
65 public NormalizedNode<?, ?> getDataTree() {
66 return metadataTree.getData();
69 public StoreMetadataNode getMetadataTree() {
73 public Optional<StoreMetadataNode> read(final InstanceIdentifier path) {
74 return TreeNodeUtils.findNode(metadataTree, path);
77 public static class Builder {
78 private NormalizedNode<?, ?> dataTree;
79 private StoreMetadataNode metadataTree;
80 private SchemaContext schemaContext;
82 public NormalizedNode<?, ?> getDataTree() {
86 public Builder setMetadataTree(final StoreMetadataNode metadataTree) {
87 this.metadataTree = metadataTree;
91 public Builder setSchemaContext(final SchemaContext schemaContext) {
92 this.schemaContext = schemaContext;
96 public DataAndMetadataSnapshot build() {
97 return new DataAndMetadataSnapshot(metadataTree, Optional.fromNullable(schemaContext));