53c19c5fe96fce213f6a983f777b5c43733ea8d9
[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.restconf.restful.transaction.TransactionVarsWrapper;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17
18 /**
19  * Util class for delete specific data in config DS.
20  *
21  */
22 public final class DeleteDataTransactionUtil {
23
24     private DeleteDataTransactionUtil() {
25         throw new UnsupportedOperationException("Util class.");
26     }
27
28     /**
29      * Delete data from DS via transaction.
30      *
31      * @param transactionNode
32      *            - Wrapper for data of transaction
33      * @return {@link Response}
34      */
35     public static Response deleteData(final TransactionVarsWrapper transactionNode) {
36         final CheckedFuture<Void, TransactionCommitFailedException> future = submitData(
37                 transactionNode.getTransaction(), transactionNode.getInstanceIdentifier().getInstanceIdentifier());
38         final ResponseFactory response = new ResponseFactory();
39         FutureCallbackTx.addCallback(future, RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE, response);
40         return response.build();
41     }
42
43     /**
44      * Delete data via transaction. Return error if data to delete does not exist.
45      *
46      * @param readWriteTx
47      *            - read and write transaction
48      * @param path
49      *            - path of data to delete
50      * @return {@link CheckedFuture}
51      */
52     private static CheckedFuture<Void, TransactionCommitFailedException> submitData(
53             final DOMDataReadWriteTransaction readWriteTx, final YangInstanceIdentifier path) {
54         TransactionUtil.checkItemExists(readWriteTx, LogicalDatastoreType.CONFIGURATION, path,
55                 RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE);
56         readWriteTx.delete(LogicalDatastoreType.CONFIGURATION, path);
57         return readWriteTx.submit();
58     }
59 }