2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.Iterator;
15 import org.opendaylight.mdsal.common.api.ReadFailedException;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 class InmemoryDOMDataTreeShardWriteTransaction implements DOMDataTreeShardWriteTransaction {
23 private enum SimpleCursorOperation {
26 void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
27 final NormalizedNode<?, ?> data) {
28 cursor.merge(child, data);
33 void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
34 final NormalizedNode<?, ?> data) {
40 void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
41 final NormalizedNode<?, ?> data) {
42 cursor.write(child, data);
46 abstract void applyOnLeaf(DOMDataTreeWriteCursor cursor, PathArgument child, NormalizedNode<?, ?> data);
48 void apply(final DOMDataTreeWriteCursor cursor, final YangInstanceIdentifier path,
49 final NormalizedNode<?, ?> data) {
51 Iterator<PathArgument> it = path.getPathArguments().iterator();
52 while (it.hasNext()) {
53 PathArgument currentArg = it.next();
55 // We need to enter one level deeper, we are not at leaf (modified) node
56 cursor.enter(currentArg);
59 applyOnLeaf(cursor, currentArg, data);
62 cursor.exit(enterCount);
66 private final ShardDataModification modification;
67 private DOMDataTreeWriteCursor cursor;
69 InmemoryDOMDataTreeShardWriteTransaction(final ShardDataModification root) {
70 this.modification = Preconditions.checkNotNull(root);
73 private DOMDataTreeWriteCursor getCursor() {
75 cursor = new ShardDataModificationCursor(modification);
80 void delete(final YangInstanceIdentifier path) {
81 YangInstanceIdentifier relativePath = toRelative(path);
82 Preconditions.checkArgument(!YangInstanceIdentifier.EMPTY.equals(relativePath),
83 "Deletion of shard root is not allowed");
84 SimpleCursorOperation.DELETE.apply(getCursor(), relativePath , null);
87 void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
88 SimpleCursorOperation.MERGE.apply(getCursor(), toRelative(path), data);
91 void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
92 SimpleCursorOperation.DELETE.apply(getCursor(), toRelative(path), data);
95 private YangInstanceIdentifier toRelative(final YangInstanceIdentifier path) {
96 Optional<YangInstanceIdentifier> relative =
97 path.relativeTo(modification.getPrefix().getRootIdentifier());
98 Preconditions.checkArgument(relative.isPresent());
99 return relative.get();
102 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
103 // FIXME: Implement this
107 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
108 // TODO Auto-generated method stub
113 public Object getIdentifier() {
114 // TODO Auto-generated method stub
118 public void close() {
119 // TODO Auto-generated method stub
123 public void ready() {
131 public void followUp() {
136 public DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
137 DOMDataTreeWriteCursor ret = getCursor();
138 YangInstanceIdentifier relativePath = toRelative(prefix.getRootIdentifier());
139 ret.enter(relativePath.getPathArguments());