2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.rests.transactions;
10 import static java.util.Objects.requireNonNull;
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;
27 * Strategy that allow to communicate with netconf devices in terms of md-sal transactions.
29 * @see DOMTransactionChain
30 * @see DOMDataTreeReadWriteTransaction
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;
38 public MdsalRestconfStrategy(final InstanceIdentifierContext<?> instanceIdentifier,
39 final TransactionChainHandler transactionChainHandler) {
40 this.instanceIdentifier = requireNonNull(instanceIdentifier);
41 this.transactionChainHandler = requireNonNull(transactionChainHandler);
42 transactionChain = transactionChainHandler.get();
46 public void prepareReadWriteExecution() {
47 rwTx = transactionChain.newReadWriteTransaction();
51 public void cancel() {
55 transactionChain.close();
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);
67 public FluentFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path) {
68 return rwTx.exists(store, path);
72 public void delete(LogicalDatastoreType store, final YangInstanceIdentifier path) {
73 rwTx.delete(store, path);
77 public void merge(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
78 rwTx.merge(store, path, data);
82 public void create(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
83 rwTx.put(store, path, data);
87 public void replace(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
88 create(store, path, data);
92 public FluentFuture<? extends @NonNull CommitInfo> commit() {
97 public DOMTransactionChain getTransactionChain() {
98 return transactionChain;
102 public InstanceIdentifierContext<?> getInstanceIdentifier() {
103 return instanceIdentifier;
107 public TransactionChainHandler getTransactionChainHandler() {
108 return transactionChainHandler;
112 public RestconfStrategy buildStrategy(final InstanceIdentifierContext<?> instanceIdentifierContext) {
113 return new MdsalRestconfStrategy(instanceIdentifierContext, this.transactionChainHandler);