0c880cd99fcc7bc78a4be72ec86fb6b3a91df27c
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / 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.restful.utils;
9
10 import com.google.common.util.concurrent.CheckedFuture;
11 import javax.ws.rs.core.Response;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
16 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18
19 /**
20  * Util class for delete specific data in config DS.
21  *
22  */
23 public final class DeleteDataTransactionUtil {
24
25     private DeleteDataTransactionUtil() {
26         throw new UnsupportedOperationException("Util class.");
27     }
28
29     /**
30      * Delete data from DS via transaction.
31      *
32      * @param transactionNode
33      *             Wrapper for data of transaction
34      * @return {@link Response}
35      */
36     public static Response deleteData(final TransactionVarsWrapper transactionNode) {
37         final CheckedFuture<Void, TransactionCommitFailedException> future = submitData(
38                 transactionNode.getTransactionChain(), transactionNode.getTransactionChain().newReadWriteTransaction(),
39                 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
40         final ResponseFactory response = new ResponseFactory();
41         FutureCallbackTx.addCallback(future, RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE, response);
42         return response.build();
43     }
44
45     /**
46      * Delete data via transaction. Return error if data to delete does not exist.
47      *
48      * @param transactionChain
49      *             transaction chain
50      * @param readWriteTx
51      *             read and write transaction
52      * @param path
53      *             path of data to delete
54      * @return {@link CheckedFuture}
55      */
56     private static CheckedFuture<Void, TransactionCommitFailedException> submitData(
57             final DOMTransactionChain transactionChain, final DOMDataReadWriteTransaction readWriteTx,
58             final YangInstanceIdentifier path) {
59         TransactionUtil.checkItemExists(transactionChain, readWriteTx, LogicalDatastoreType.CONFIGURATION, path,
60                 RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE);
61         readWriteTx.delete(LogicalDatastoreType.CONFIGURATION, path);
62         return readWriteTx.submit();
63     }
64 }