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.broker;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
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;
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;
35 public class ShardedDOMWriteTransactionAdapter implements DOMDataTreeWriteTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMWriteTransactionAdapter.class);
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);
45 private final @NonNull DOMDataTreeService treeService;
46 private final @NonNull Object txIdentifier;
48 private boolean finished = false;
49 private boolean initialized = false;
51 ShardedDOMWriteTransactionAdapter(final Object identifier, final DOMDataTreeService transactionDelegator) {
52 this.treeService = requireNonNull(transactionDelegator);
53 this.txIdentifier = requireNonNull(identifier);
57 public boolean cancel() {
58 LOG.debug("{}: Cancelling transaction");
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()));
74 public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
76 LOG.debug("{}: Submitting transaction", txIdentifier);
78 // If underlying producers, transactions and cursors are
79 // not even initialized just seal this transaction and
80 // return immediate future
82 return CommitInfo.emptyFluentFuture();
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()));
90 // Now we can close producers and mark transaction as finished
94 return aggregatedSubmit.transform(unused -> CommitInfo.empty(), MoreExecutors.directExecutor());
98 public Object getIdentifier() {
103 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
104 final NormalizedNode<?, ?> data) {
106 LOG.debug("{}: Invoking put operation at {}:{}", txIdentifier, store, path);
107 LOG.trace("{}: payload is {}", txIdentifier, data);
109 initializeDataTreeProducerLayer(path.getParent());
112 final DOMDataTreeWriteCursor cursor = cursorMap.get(store);
113 cursor.write(path.getLastPathArgument(), data);
117 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
118 final NormalizedNode<?, ?> data) {
120 LOG.debug("{}: Invoking merge operation at {}:{}", txIdentifier, store, path);
121 LOG.trace("{}: payload is {}", txIdentifier, data);
123 initializeDataTreeProducerLayer(path.getParent());
126 final DOMDataTreeWriteCursor cursor = cursorMap.get(store);
127 cursor.merge(path.getLastPathArgument(), data);
131 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
133 LOG.debug("{}: Invoking delete operation at {}:{}", txIdentifier, store, path);
135 initializeDataTreeProducerLayer(path.getParent());
138 final DOMDataTreeWriteCursor cursor = cursorMap.get(store);
139 cursor.delete(path.getLastPathArgument());
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");
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))));
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));
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)));
174 private void checkRunning() {
175 checkState(!finished, "{}: Transaction already finished");
178 private void closeProducers() {
179 producerMap.values().forEach(domDataTreeProducer -> {
181 domDataTreeProducer.close();
182 } catch (final DOMDataTreeProducerException e) {
183 throw new IllegalStateException("Trying to close DOMDataTreeProducer with open transaction", e);