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
8 package org.opendaylight.mdsal.dom.spi.shard;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.util.Optional;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
20 @Deprecated(forRemoval = true)
21 public abstract class WritableNodeOperation implements WriteCursorStrategy {
22 private final WriteableModificationNode node;
23 private final DOMDataTreeWriteCursor cursor;
25 protected WritableNodeOperation(final WriteableModificationNode node, final DOMDataTreeWriteCursor cursor) {
26 this.node = requireNonNull(node);
27 this.cursor = requireNonNull(cursor);
30 protected final DOMDataTreeWriteCursor getCursor() {
34 private void delete(final PathArgument arg, final WriteableModificationNode potentialChild) {
36 if (potentialChild != null) {
37 potentialChild.markDeleted();
42 public final void delete(final PathArgument arg) {
43 delete(arg, node.getChild(arg));
47 public final void merge(final PathArgument arg, final NormalizedNode<?, ?> data) {
48 WriteableModificationNode potentialChild = node.getChild(arg);
49 if (potentialChild == null) {
50 cursor.merge(arg, data);
52 potentialChild.createOperation(cursor).mergeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
57 public final void write(final PathArgument arg, final NormalizedNode<?, ?> data) {
58 WriteableModificationNode potentialChild = node.getChild(arg);
59 if (potentialChild == null) {
60 cursor.write(arg, data);
62 potentialChild.createOperation(cursor).writeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
67 public final WriteCursorStrategy enter(final PathArgument arg) {
69 WriteableModificationNode child = node.getChild(arg);
71 return child.createOperation(cursor);
73 return new DelegatingWriteCursorStrategy() {
75 protected DOMDataTreeWriteCursor delegate() {
82 public final void mergeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
83 for (NormalizedNode<?, ?> child : data.getValue()) {
84 PathArgument childId = child.getIdentifier();
85 WriteableModificationNode shardChild = node.getChild(childId);
86 if (shardChild != null) {
87 // FIXME: Decompose somehow
88 throw new UnsupportedOperationException("Not implemented yet");
91 cursor.merge(childId, child);
96 @SuppressWarnings("rawtypes")
97 public final void writeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
98 // write the entire thing into the cursor
99 write(data.getIdentifier(), data);
100 // write the children with subshard check and subshard write if we are going into subshard
101 cursor.enter(data.getIdentifier());
102 for (NormalizedNode<?, ?> writtenChild : data.getValue()) {
103 write(writtenChild.getIdentifier(), writtenChild);
105 // Delete step - remove subshard data that was written into current shard
106 // delete from current
107 node.getChildrenWithSubshards().entrySet()
108 .stream().filter(entry -> entry.getValue() instanceof WriteableSubshardBoundaryNode).forEach(entry -> {
109 @SuppressWarnings("unchecked")
110 Optional<NormalizedNode<?, ?>> writtenValue =
111 ((NormalizedNodeContainer) data).getChild(entry.getKey());
112 if (writtenValue.isPresent()) {
113 // delete from current
114 cursor.delete(entry.getKey());