54e3964bd9de4afa66baea0a046785da944b3ce6
[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.restconf.nb.rfc8040.handlers.TransactionChainHandler;
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 FluentFuture<? extends CommitInfo> future = submitData(transactionNode.getTransactionChainHandler(),
39                 transactionNode.getInstanceIdentifier().getInstanceIdentifier());
40         final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
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 transactionChainHandler
49      *             transaction chain handler
50      * @param readWriteTx
51      *             read and write transaction
52      * @param path
53      *             path of data to delete
54      * @return {@link FluentFuture}
55      */
56     private static FluentFuture<? extends CommitInfo> submitData(
57             final TransactionChainHandler transactionChainHandler, final YangInstanceIdentifier path) {
58         final DOMDataTreeReadWriteTransaction readWriteTx = transactionChainHandler.get().newReadWriteTransaction();
59         TransactionUtil.checkItemExists(transactionChainHandler, readWriteTx, LogicalDatastoreType.CONFIGURATION, path,
60                 RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE);
61         readWriteTx.delete(LogicalDatastoreType.CONFIGURATION, path);
62         return readWriteTx.commit();
63     }
64 }