Centralize RestconfStrategy allocation
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / MdsalRestconfStrategy.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.rests.transactions;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
21 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
22 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 /**
27  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
28  *
29  * @see DOMTransactionChain
30  * @see DOMDataTreeReadWriteTransaction
31  */
32 public final class MdsalRestconfStrategy extends RestconfStrategy {
33     private final DOMTransactionChain transactionChain;
34     private final TransactionChainHandler transactionChainHandler;
35
36     private DOMDataTreeReadWriteTransaction rwTx;
37
38     public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
39         this(new TransactionChainHandler(dataBroker));
40     }
41
42     public MdsalRestconfStrategy(final TransactionChainHandler transactionChainHandler) {
43         this.transactionChainHandler = requireNonNull(transactionChainHandler);
44         transactionChain = transactionChainHandler.get();
45     }
46
47     @Override
48     public void prepareReadWriteExecution() {
49         rwTx = transactionChain.newReadWriteTransaction();
50     }
51
52     @Override
53     public void cancel() {
54         if (rwTx != null) {
55             rwTx.cancel();
56         }
57         transactionChain.close();
58     }
59
60     @Override
61     public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
62             final YangInstanceIdentifier path) {
63         try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
64             return tx.read(store, path);
65         }
66     }
67
68     @Override
69     public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
70         return rwTx.exists(store, path);
71     }
72
73     @Override
74     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
75         rwTx.delete(store, path);
76     }
77
78     @Override
79     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
80             final NormalizedNode<?, ?> data) {
81         rwTx.merge(store, path, data);
82     }
83
84     @Override
85     public void create(final LogicalDatastoreType store, final YangInstanceIdentifier path,
86             final NormalizedNode<?, ?> data) {
87         rwTx.put(store, path, data);
88     }
89
90     @Override
91     public void replace(final LogicalDatastoreType store, final YangInstanceIdentifier path,
92             final NormalizedNode<?, ?> data) {
93         create(store, path, data);
94     }
95
96     @Override
97     public FluentFuture<? extends @NonNull CommitInfo> commit() {
98         return rwTx.commit();
99     }
100
101     @Override
102     public DOMTransactionChain getTransactionChain() {
103         return transactionChain;
104     }
105
106     @Override
107     public TransactionChainHandler getTransactionChainHandler() {
108         return transactionChainHandler;
109     }
110 }