2a587ce6a8692e51bafd1f798d9a8ce093cf1c69
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / shard / WritableNodeOperation.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.spi.shard;
9
10 import static java.util.Objects.requireNonNull;
11
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;
18
19 @Beta
20 @Deprecated(forRemoval = true)
21 public abstract class WritableNodeOperation implements WriteCursorStrategy {
22     private final WriteableModificationNode node;
23     private final DOMDataTreeWriteCursor cursor;
24
25     protected WritableNodeOperation(final WriteableModificationNode node, final DOMDataTreeWriteCursor cursor) {
26         this.node = requireNonNull(node);
27         this.cursor = requireNonNull(cursor);
28     }
29
30     protected final DOMDataTreeWriteCursor getCursor() {
31         return cursor;
32     }
33
34     private void delete(final PathArgument arg, final WriteableModificationNode potentialChild) {
35         cursor.delete(arg);
36         if (potentialChild != null) {
37             potentialChild.markDeleted();
38         }
39     }
40
41     @Override
42     public final void delete(final PathArgument arg) {
43         delete(arg, node.getChild(arg));
44     }
45
46     @Override
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);
51         } else {
52             potentialChild.createOperation(cursor).mergeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
53         }
54     }
55
56     @Override
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);
61         } else {
62             potentialChild.createOperation(cursor).writeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
63         }
64     }
65
66     @Override
67     public final WriteCursorStrategy enter(final PathArgument arg) {
68         cursor.enter(arg);
69         WriteableModificationNode child = node.getChild(arg);
70         if (child != null) {
71             return child.createOperation(cursor);
72         }
73         return new DelegatingWriteCursorStrategy() {
74             @Override
75             protected DOMDataTreeWriteCursor delegate() {
76                 return cursor;
77             }
78         };
79     }
80
81     @Override
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");
89             }
90
91             cursor.merge(childId, child);
92         }
93     }
94
95     @Override
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);
104         }
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());
115                     }
116                 });
117
118         cursor.exit();
119     }
120 }