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;
29 private DataAndMetadataSnapshot(final StoreMetadataNode metadataTree, final Optional<SchemaContext> schemaCtx) {
30 this.metadataTree = metadataTree;
31 this.schemaContext = schemaCtx;
34 public static Builder builder() {
38 public static DataAndMetadataSnapshot createEmpty() {
39 return createEmpty(new NodeIdentifier(SchemaContext.NAME));
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)
50 return new DataAndMetadataSnapshot(metadata,Optional.<SchemaContext>absent());
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()
58 .setNodeVersion(UnsignedLong.ZERO)
59 .setSubtreeVersion(UnsignedLong.ZERO)
61 return new DataAndMetadataSnapshot(metadata, Optional.of(ctx));
64 public Optional<SchemaContext> getSchemaContext() {
68 public NormalizedNode<?, ?> getDataTree() {
69 return metadataTree.getData();
72 public StoreMetadataNode getMetadataTree() {
76 public Optional<StoreMetadataNode> read(final InstanceIdentifier path) {
77 return TreeNodeUtils.findNode(metadataTree, path);
80 public static class Builder {
81 private NormalizedNode<?, ?> dataTree;
82 private StoreMetadataNode metadataTree;
83 private SchemaContext schemaContext;
85 public NormalizedNode<?, ?> getDataTree() {
89 public Builder setMetadataTree(final StoreMetadataNode metadataTree) {
90 this.metadataTree = metadataTree;
94 public Builder setSchemaContext(final SchemaContext schemaContext) {
95 this.schemaContext = schemaContext;
99 public DataAndMetadataSnapshot build() {
100 return new DataAndMetadataSnapshot(metadataTree, Optional.fromNullable(schemaContext));