059abf20726039fa338c81f2ac0fcda9aad1b4a8
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InmemoryDOMDataTreeShardWriteTransaction.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 com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.Map.Entry;
19 import org.opendaylight.mdsal.common.api.ReadFailedException;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 class InmemoryDOMDataTreeShardWriteTransaction implements DOMDataTreeShardWriteTransaction {
32
33     private static final Logger LOG = LoggerFactory.getLogger(InmemoryDOMDataTreeShardWriteTransaction.class);
34
35     private enum SimpleCursorOperation {
36         MERGE {
37             @Override
38             void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
39                     final NormalizedNode<?, ?> data) {
40                 cursor.merge(child, data);
41             }
42         },
43         DELETE {
44             @Override
45             void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
46                     final NormalizedNode<?, ?> data) {
47                 cursor.delete(child);
48             }
49         },
50         WRITE {
51             @Override
52             void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
53                     final NormalizedNode<?, ?> data) {
54                 cursor.write(child, data);
55             }
56         };
57
58         abstract void applyOnLeaf(DOMDataTreeWriteCursor cursor, PathArgument child, NormalizedNode<?, ?> data);
59
60         void apply(final DOMDataTreeWriteCursor cursor, final YangInstanceIdentifier path,
61                 final NormalizedNode<?, ?> data) {
62             int enterCount = 0;
63             final Iterator<PathArgument> it = path.getPathArguments().iterator();
64             while (it.hasNext()) {
65                 final PathArgument currentArg = it.next();
66                 if (it.hasNext()) {
67                     // We need to enter one level deeper, we are not at leaf (modified) node
68                     cursor.enter(currentArg);
69                     enterCount++;
70                 } else {
71                     applyOnLeaf(cursor, currentArg, data);
72                 }
73             }
74             cursor.exit(enterCount);
75         }
76     }
77
78     private final ArrayList<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>();
79     private final InMemoryDOMDataTreeShardChangePublisher changePublisher;
80     private final ShardDataModification modification;
81     private final ListeningExecutorService executor;
82     private final DataTree rootShardDataTree;
83
84     private DataTreeModification rootModification = null;
85     private DOMDataTreeWriteCursor cursor;
86     private boolean finished = false;
87
88     InmemoryDOMDataTreeShardWriteTransaction(final ShardDataModification root,
89                                              final DataTree rootShardDataTree,
90                                              final InMemoryDOMDataTreeShardChangePublisher changePublisher, final ListeningExecutorService executor) {
91         this.modification = Preconditions.checkNotNull(root);
92         this.rootShardDataTree = Preconditions.checkNotNull(rootShardDataTree);
93         this.changePublisher = Preconditions.checkNotNull(changePublisher);
94         this.executor = executor;
95     }
96
97     private DOMDataTreeWriteCursor getCursor() {
98         if (cursor == null) {
99             cursor = new ShardDataModificationCursor(modification, this);
100         }
101         return cursor;
102     }
103
104     void delete(final YangInstanceIdentifier path) {
105         final YangInstanceIdentifier relativePath = toRelative(path);
106         Preconditions.checkArgument(!YangInstanceIdentifier.EMPTY.equals(relativePath),
107                 "Deletion of shard root is not allowed");
108         SimpleCursorOperation.DELETE.apply(getCursor(), relativePath , null);
109     }
110
111     void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
112         SimpleCursorOperation.MERGE.apply(getCursor(), toRelative(path), data);
113     }
114
115     void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
116         SimpleCursorOperation.DELETE.apply(getCursor(), toRelative(path), data);
117     }
118
119     private YangInstanceIdentifier toRelative(final YangInstanceIdentifier path) {
120         final Optional<YangInstanceIdentifier> relative =
121                 path.relativeTo(modification.getPrefix().getRootIdentifier());
122         Preconditions.checkArgument(relative.isPresent());
123         return relative.get();
124     }
125
126     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
127         throw new UnsupportedOperationException("Not implemented yet");
128     }
129
130     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
131         throw new UnsupportedOperationException("Not implemented yet");
132     }
133
134     @Override
135     public void close() {
136         Preconditions.checkState(!finished, "Attempting to close an already finished transaction.");
137         modification.closeTransactions();
138         if (cursor != null) {
139             cursor.close();
140         }
141         finished = true;
142     }
143
144     void cursorClosed() {
145         Preconditions.checkNotNull(cursor);
146         cursor = null;
147     }
148
149     public boolean isFinished() {
150         return finished;
151     }
152
153     @Override
154     public void ready() {
155         Preconditions.checkState(!finished, "Attempting to ready an already finished transaction.");
156         Preconditions.checkState(cursor == null, "Attempting to ready a transaction that has an open cursor.");
157         Preconditions.checkNotNull(modification, "Attempting to ready an empty transaction.");
158
159         LOG.debug("Readying open transaction on shard {}", modification.getPrefix());
160         rootModification = modification.seal();
161
162         cohorts.add(new InMemoryDOMDataTreeShardThreePhaseCommitCohort(rootShardDataTree, rootModification, changePublisher));
163         for (final Entry<DOMDataTreeIdentifier, ForeignShardModificationContext> entry : modification.getChildShards().entrySet()) {
164             cohorts.add(new ForeignShardThreePhaseCommitCohort(entry.getKey(), entry.getValue()));
165         }
166         finished = true;
167     }
168
169     @Override
170     public ListenableFuture<Void> submit() {
171         LOG.debug("Submitting open transaction on shard {}", modification.getPrefix());
172
173         Preconditions.checkNotNull(cohorts);
174         Preconditions.checkState(!cohorts.isEmpty(), "Transaction was not readied yet.");
175
176         final ListenableFuture<Void> submit = executor.submit(new ShardSubmitCoordinationTask(modification.getPrefix(), cohorts));
177
178         return submit;
179     }
180
181     @Override
182     public ListenableFuture<Boolean> validate() {
183         LOG.debug("CanCommit on open transaction on shard {}", modification.getPrefix());
184
185         final ListenableFuture<Boolean> submit = executor.submit(new ShardCanCommitCoordinationTask(modification.getPrefix(), cohorts));
186         return submit;
187     }
188
189     @Override
190     public ListenableFuture<Void> prepare() {
191         LOG.debug("PreCommit on open transaction on shard {}", modification.getPrefix());
192
193         final ListenableFuture<Void> submit = executor.submit(new ShardPreCommitCoordinationTask(modification.getPrefix(), cohorts));
194         return submit;
195     }
196
197     @Override
198     public ListenableFuture<Void> commit() {
199         LOG.debug("Commit open transaction on shard {}", modification.getPrefix());
200
201         final ListenableFuture<Void> submit = executor.submit(new ShardCommitCoordinationTask(modification.getPrefix(), cohorts));
202         return submit;
203     }
204
205     public void followUp() {
206
207     }
208
209     @Override
210     public DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
211         Preconditions.checkState(!finished, "Transaction is finished/closed already.");
212         Preconditions.checkState(cursor == null, "Previous cursor wasn't closed");
213         final DOMDataTreeWriteCursor ret = getCursor();
214         final YangInstanceIdentifier relativePath = toRelative(prefix.getRootIdentifier());
215         ret.enter(relativePath.getPathArguments());
216         return ret;
217     }
218 }