Create NetconfDataTreeService with base and additional operations for netconf
[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.DOMDataTreeReadTransaction;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
20 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
21 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
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  * Strategy that allow to communicate with netconf devices in terms of md-sal transactions.
28  *
29  * @see DOMTransactionChain
30  * @see DOMDataTreeReadWriteTransaction
31  */
32 public class MdsalRestconfStrategy implements RestconfStrategy {
33     private final InstanceIdentifierContext<?> instanceIdentifier;
34     private final DOMTransactionChain transactionChain;
35     private DOMDataTreeReadWriteTransaction rwTx;
36     private final TransactionChainHandler transactionChainHandler;
37
38     public MdsalRestconfStrategy(final InstanceIdentifierContext<?> instanceIdentifier,
39                                  final TransactionChainHandler transactionChainHandler) {
40         this.instanceIdentifier = requireNonNull(instanceIdentifier);
41         this.transactionChainHandler = requireNonNull(transactionChainHandler);
42         transactionChain = transactionChainHandler.get();
43     }
44
45     @Override
46     public void prepareReadWriteExecution() {
47         rwTx = transactionChain.newReadWriteTransaction();
48     }
49
50     @Override
51     public void cancel() {
52         if (rwTx != null) {
53             rwTx.cancel();
54         }
55         transactionChain.close();
56     }
57
58     @Override
59     public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
60                                                                  final YangInstanceIdentifier path) {
61         try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
62             return tx.read(store, path);
63         }
64     }
65
66     @Override
67     public FluentFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path) {
68         return rwTx.exists(store, path);
69     }
70
71     @Override
72     public void delete(LogicalDatastoreType store, final YangInstanceIdentifier path) {
73         rwTx.delete(store, path);
74     }
75
76     @Override
77     public void merge(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
78         rwTx.merge(store, path, data);
79     }
80
81     @Override
82     public void create(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
83         rwTx.put(store, path, data);
84     }
85
86     @Override
87     public void replace(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
88         create(store, path, data);
89     }
90
91     @Override
92     public FluentFuture<? extends @NonNull CommitInfo> commit() {
93         return rwTx.commit();
94     }
95
96     @Override
97     public DOMTransactionChain getTransactionChain() {
98         return transactionChain;
99     }
100
101     @Override
102     public InstanceIdentifierContext<?> getInstanceIdentifier() {
103         return instanceIdentifier;
104     }
105
106     @Override
107     public TransactionChainHandler getTransactionChainHandler() {
108         return transactionChainHandler;
109     }
110
111     @Override
112     public RestconfStrategy buildStrategy(final InstanceIdentifierContext<?> instanceIdentifierContext) {
113         return new MdsalRestconfStrategy(instanceIdentifierContext, this.transactionChainHandler);
114     }
115 }