8db909d59ebc147a81db3e56d48681429b61ee7f
[netconf.git] / restconf / restconf-nb-bierman02 / 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  * @deprecated move to splitted module restconf-nb-rfc8040
23  */
24 @Deprecated
25 public final class DeleteDataTransactionUtil {
26
27     private DeleteDataTransactionUtil() {
28         throw new UnsupportedOperationException("Util class.");
29     }
30
31     /**
32      * Delete data from DS via transaction.
33      *
34      * @param transactionNode
35      *             Wrapper for data of transaction
36      * @return {@link Response}
37      */
38     public static Response deleteData(final TransactionVarsWrapper transactionNode) {
39         final CheckedFuture<Void, TransactionCommitFailedException> future = submitData(
40                 transactionNode.getTransactionChain(), transactionNode.getTransactionChain().newReadWriteTransaction(),
41                 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
42         final ResponseFactory response = new ResponseFactory();
43         FutureCallbackTx.addCallback(future, RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE, response);
44         return response.build();
45     }
46
47     /**
48      * Delete data via transaction. Return error if data to delete does not exist.
49      *
50      * @param transactionChain
51      *             transaction chain
52      * @param readWriteTx
53      *             read and write transaction
54      * @param path
55      *             path of data to delete
56      * @return {@link CheckedFuture}
57      */
58     private static CheckedFuture<Void, TransactionCommitFailedException> submitData(
59             final DOMTransactionChain transactionChain, final DOMDataReadWriteTransaction readWriteTx,
60             final YangInstanceIdentifier path) {
61         TransactionUtil.checkItemExists(transactionChain, readWriteTx, LogicalDatastoreType.CONFIGURATION, path,
62                 RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE);
63         readWriteTx.delete(LogicalDatastoreType.CONFIGURATION, path);
64         return readWriteTx.submit();
65     }
66 }