d0fb5c33520a15e8dc41555e5cab00a4c6b6df82
[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.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
19 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
20 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 /**
25  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
26  *
27  * @see DOMTransactionChain
28  * @see DOMDataTreeReadWriteTransaction
29  */
30 public final class MdsalRestconfStrategy extends RestconfStrategy {
31     private final DOMTransactionChain transactionChain;
32
33     public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
34         this(new TransactionChainHandler(dataBroker));
35     }
36
37     public MdsalRestconfStrategy(final TransactionChainHandler transactionChainHandler) {
38         transactionChain = requireNonNull(transactionChainHandler).get();
39     }
40
41     @Override
42     public RestconfTransaction prepareWriteExecution() {
43         return new MdsalRestconfTransaction(transactionChain);
44     }
45
46     @Override
47     public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
48                                                                  final YangInstanceIdentifier path) {
49         try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
50             return tx.read(store, path);
51         }
52     }
53
54     @Override
55     public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
56         try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
57             return tx.exists(store, path);
58         }
59     }
60
61     @Override
62     public void close() {
63         transactionChain.close();
64     }
65 }