Reorganize transactionChainHandler usage.
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / DeleteDataTransactionUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.utils;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import javax.ws.rs.core.Response;
12 import javax.ws.rs.core.Response.Status;
13 import org.opendaylight.mdsal.common.api.CommitInfo;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
16 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
17 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19
20 /**
21  * Util class for delete specific data in config DS.
22  *
23  */
24 public final class DeleteDataTransactionUtil {
25
26     private DeleteDataTransactionUtil() {
27         throw new UnsupportedOperationException("Util class.");
28     }
29
30     /**
31      * Delete data from DS via transaction.
32      *
33      * @param transactionNode
34      *             Wrapper for data of transaction
35      * @return {@link Response}
36      */
37     public static Response deleteData(final TransactionVarsWrapper transactionNode) {
38         final DOMTransactionChain transactionChain = transactionNode.getTransactionChainHandler().get();
39         final FluentFuture<? extends CommitInfo> future = submitData(transactionChain,
40                 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
41         final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
42         //This method will close transactionChain
43         FutureCallbackTx.addCallback(future, RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE, response,
44                 transactionChain);
45         return response.build();
46     }
47
48     /**
49      * Delete data via transaction. Return error if data to delete does not exist.
50      *
51      * @param transactionChain
52      *             transaction chain
53      * @param path
54      *             path of data to delete
55      * @return {@link FluentFuture}
56      */
57     private static FluentFuture<? extends CommitInfo> submitData(
58             final DOMTransactionChain transactionChain, final YangInstanceIdentifier path) {
59         final DOMDataTreeReadWriteTransaction readWriteTx = transactionChain.newReadWriteTransaction();
60         TransactionUtil.checkItemExists(transactionChain, readWriteTx, LogicalDatastoreType.CONFIGURATION, path,
61                 RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE);
62         readWriteTx.delete(LogicalDatastoreType.CONFIGURATION, path);
63         return readWriteTx.commit();
64     }
65 }