Do not use entrySet() where values() or keySet() suffices
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / WriteableNodeOperation.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
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
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;
17
18 abstract class WriteableNodeOperation implements WriteCursorStrategy {
19     private final WriteableModificationNode node;
20     private final DOMDataTreeWriteCursor cursor;
21
22     WriteableNodeOperation(final WriteableModificationNode node, final DOMDataTreeWriteCursor cursor) {
23         this.node = Preconditions.checkNotNull(node);
24         this.cursor = Preconditions.checkNotNull(cursor);
25     }
26
27     protected final DOMDataTreeWriteCursor getCursor() {
28         return cursor;
29     }
30
31     private void delete(final PathArgument arg, final WriteableModificationNode potentialChild) {
32         cursor.delete(arg);
33         if (potentialChild != null) {
34             potentialChild.markDeleted();
35         }
36     }
37
38     @Override
39     public final void delete(final PathArgument arg) {
40         delete(arg, node.getChild(arg));
41     }
42
43     @Override
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);
48         } else {
49             potentialChild.createOperation(cursor).mergeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
50         }
51     }
52
53     @Override
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);
58         } else {
59             potentialChild.createOperation(cursor).writeToCurrent((NormalizedNodeContainer<?, ?, ?>) data);
60         }
61     }
62
63     @Override
64     public final WriteCursorStrategy enter(final PathArgument arg) {
65         cursor.enter(arg);
66         WriteableModificationNode child = node.getChild(arg);
67         if (child != null) {
68             return child.createOperation(cursor);
69         }
70         return new DelegatingWriteCursorStrategy() {
71             @Override
72             protected DOMDataTreeWriteCursor delegate() {
73                 return cursor;
74             }
75         };
76     }
77
78     @Override
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");
86             } else {
87                 cursor.merge(childId, child);
88             }
89         }
90     }
91
92     @Override
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);
101         }
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);
109             }
110         }
111         cursor.exit();
112     }
113 }