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 org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
18 abstract class WriteableNodeOperation implements WriteCursorStrategy {
19 private final WriteableModificationNode node;
20 private final DOMDataTreeWriteCursor cursor;
22 WriteableNodeOperation(final WriteableModificationNode node, final DOMDataTreeWriteCursor cursor) {
23 this.node = Preconditions.checkNotNull(node);
24 this.cursor = Preconditions.checkNotNull(cursor);
27 protected final DOMDataTreeWriteCursor getCursor() {
31 private void delete(final PathArgument arg, final WriteableModificationNode potentialChild) {
33 if (potentialChild != null) {
34 potentialChild.markDeleted();
39 public final void delete(final PathArgument arg) {
40 delete(arg, node.getChild(arg));
44 public final void merge(final PathArgument arg, final NormalizedNode<?, ?> data) {
45 WriteableModificationNode potentialChild = node.getChild(arg);
46 if (potentialChild == null) {
47 cursor.merge(arg, data);
49 potentialChild.createOperation(cursor).mergeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
54 public final void write(final PathArgument arg, final NormalizedNode<?, ?> data) {
55 WriteableModificationNode potentialChild = node.getChild(arg);
56 if (potentialChild == null) {
57 cursor.write(arg, data);
59 potentialChild.createOperation(cursor).writeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
64 public final WriteCursorStrategy enter(final PathArgument arg) {
66 WriteableModificationNode child = node.getChild(arg);
68 return child.createOperation(cursor);
70 return new DelegatingWriteCursorStrategy() {
72 protected DOMDataTreeWriteCursor delegate() {
79 public final void mergeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
80 for (NormalizedNode<?, ?> child : data.getValue()) {
81 PathArgument childId = child.getIdentifier();
82 WriteableModificationNode shardChild = node.getChild(childId);
83 if (shardChild != null) {
84 // FIXME: Decompose somehow
85 throw new UnsupportedOperationException("Not implemented yet");
87 cursor.merge(childId, child);
93 @SuppressWarnings("rawtypes")
94 public final void writeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
95 // write the entire thing into the cursor
96 write(data.getIdentifier(), data);
97 // write the children with subshard check and subshard write if we are going into subshard
98 cursor.enter(data.getIdentifier());
99 for (NormalizedNode<?, ?> writtenChild : data.getValue()) {
100 write(writtenChild.getIdentifier(), writtenChild);
102 // Delete step - remove subshard data that was written into current shard
103 for (PathArgument childId : node.getChildrenWithSubshards().keySet()) {
104 @SuppressWarnings("unchecked")
105 Optional<NormalizedNode<?, ?>> writtenValue = ((NormalizedNodeContainer) data).getChild(childId);
106 if (writtenValue.isPresent()) {
107 // delete from current
108 cursor.delete(childId);