b3222e0c91d3e48147c5f124e24d6a2e2352bab7
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMWriteTransactionAdapter.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.broker;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.util.concurrent.FluentFuture;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.Collections;
17 import java.util.EnumMap;
18 import java.util.List;
19 import java.util.Map;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class ShardedDOMWriteTransactionAdapter implements DOMDataTreeWriteTransaction {
36     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMWriteTransactionAdapter.class);
37
38     private final Map<LogicalDatastoreType, DOMDataTreeCursorAwareTransaction> transactionMap = new EnumMap<>(
39             LogicalDatastoreType.class);
40     private final Map<LogicalDatastoreType, DOMDataTreeWriteCursor> cursorMap = new EnumMap<>(
41             LogicalDatastoreType.class);
42     private final Map<LogicalDatastoreType, DOMDataTreeProducer> producerMap = new EnumMap<>(
43             LogicalDatastoreType.class);
44
45     private final @NonNull DOMDataTreeService treeService;
46     private final @NonNull Object txIdentifier;
47
48     private boolean finished = false;
49     private boolean initialized = false;
50
51     ShardedDOMWriteTransactionAdapter(final Object identifier, final DOMDataTreeService transactionDelegator) {
52         this.treeService = requireNonNull(transactionDelegator);
53         this.txIdentifier = requireNonNull(identifier);
54     }
55
56     @Override
57     public boolean cancel() {
58         LOG.debug("{}: Cancelling transaction");
59         if (finished) {
60             return false;
61         }
62
63         // We close cursor, cancel transactions and close producers and
64         // mark transaction as finished
65         cursorMap.values().forEach(DOMDataTreeWriteCursor::close);
66         transactionMap.values().forEach(domDataTreeCursorAwareTransaction ->
67                 checkState(domDataTreeCursorAwareTransaction.cancel()));
68         closeProducers();
69         finished = true;
70         return true;
71     }
72
73     @Override
74     public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
75         checkRunning();
76         LOG.debug("{}: Submitting transaction", txIdentifier);
77         if (!initialized) {
78             // If underlying producers, transactions and cursors are
79             // not even initialized just seal this transaction and
80             // return immediate future
81             finished = true;
82             return CommitInfo.emptyFluentFuture();
83         }
84         // First we need to close cursors
85         cursorMap.values().forEach(DOMDataTreeWriteCursor::close);
86         final FluentFuture<List<CommitInfo>> aggregatedSubmit = FluentFuture.from(Futures.allAsList(
87                 transactionMap.get(LogicalDatastoreType.CONFIGURATION).commit(),
88                 transactionMap.get(LogicalDatastoreType.OPERATIONAL).commit()));
89
90         // Now we can close producers and mark transaction as finished
91         closeProducers();
92         finished = true;
93
94         return aggregatedSubmit.transform(unused -> CommitInfo.empty(), MoreExecutors.directExecutor());
95     }
96
97     @Override
98     public Object getIdentifier() {
99         return txIdentifier;
100     }
101
102     @Override
103     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
104             final NormalizedNode<?, ?> data) {
105         checkRunning();
106         LOG.debug("{}: Invoking put operation at {}:{}", txIdentifier, store, path);
107         LOG.trace("{}: payload is {}", txIdentifier, data);
108         if (!initialized) {
109             initializeDataTreeProducerLayer(path.getParent());
110         }
111
112         final DOMDataTreeWriteCursor cursor = cursorMap.get(store);
113         cursor.write(path.getLastPathArgument(), data);
114     }
115
116     @Override
117     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
118                       final NormalizedNode<?, ?> data) {
119         checkRunning();
120         LOG.debug("{}: Invoking merge operation at {}:{}", txIdentifier, store, path);
121         LOG.trace("{}: payload is {}", txIdentifier, data);
122         if (!initialized) {
123             initializeDataTreeProducerLayer(path.getParent());
124         }
125
126         final DOMDataTreeWriteCursor cursor = cursorMap.get(store);
127         cursor.merge(path.getLastPathArgument(), data);
128     }
129
130     @Override
131     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
132         checkRunning();
133         LOG.debug("{}: Invoking delete operation at {}:{}", txIdentifier, store, path);
134         if (!initialized) {
135             initializeDataTreeProducerLayer(path.getParent());
136         }
137
138         final DOMDataTreeWriteCursor cursor = cursorMap.get(store);
139         cursor.delete(path.getLastPathArgument());
140     }
141
142     // TODO initialize producer, transaction and cursor for only
143     // for necessary data store at one time
144     private void initializeDataTreeProducerLayer(final YangInstanceIdentifier path) {
145         checkState(producerMap.isEmpty(), "Producers already initialized");
146         checkState(transactionMap.isEmpty(), "Transactions already initialized");
147         checkState(cursorMap.isEmpty(), "Cursors already initialized");
148
149         LOG.debug("{}: Creating data tree producers on path {}", txIdentifier, path);
150         producerMap.put(LogicalDatastoreType.CONFIGURATION,
151                 treeService.createProducer(
152                         Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, path))));
153         producerMap.put(LogicalDatastoreType.OPERATIONAL,
154                 treeService.createProducer(
155                         Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, path))));
156
157         LOG.debug("{}: Creating DOMDataTreeCursorAwareTransactions delegates on {}", txIdentifier, path);
158         transactionMap.put(LogicalDatastoreType.CONFIGURATION,
159                 producerMap.get(LogicalDatastoreType.CONFIGURATION).createTransaction(true));
160         transactionMap.put(LogicalDatastoreType.OPERATIONAL,
161                 producerMap.get(LogicalDatastoreType.OPERATIONAL).createTransaction(true));
162
163         LOG.debug("{}: Creating DOMDataTreeWriteCursors delegates");
164         cursorMap.put(LogicalDatastoreType.CONFIGURATION,
165                 transactionMap.get(LogicalDatastoreType.CONFIGURATION)
166                         .createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, path)));
167         cursorMap.put(LogicalDatastoreType.OPERATIONAL,
168                 transactionMap.get(LogicalDatastoreType.OPERATIONAL)
169                         .createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, path)));
170
171         initialized = true;
172     }
173
174     private void checkRunning() {
175         checkState(!finished, "{}: Transaction already finished");
176     }
177
178     private void closeProducers() {
179         producerMap.values().forEach(domDataTreeProducer -> {
180             try {
181                 domDataTreeProducer.close();
182             } catch (final DOMDataTreeProducerException e) {
183                 throw new IllegalStateException("Trying to close DOMDataTreeProducer with open transaction", e);
184             }
185         });
186     }
187 }