Split transaction lifecycle
[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.restconf.common.errors.RestconfDocumentedException;
16 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
17 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
18 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
19 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Util class for delete specific data in config DS.
26  */
27 public final class DeleteDataTransactionUtil {
28     private static final Logger LOG = LoggerFactory.getLogger(DeleteDataTransactionUtil.class);
29     public static final String DELETE_TX_TYPE = "DELETE";
30
31     private DeleteDataTransactionUtil() {
32         throw new UnsupportedOperationException("Util class.");
33     }
34
35     /**
36      * Delete data from DS via transaction.
37      *
38      * @param strategy object that perform the actual DS operations
39      * @return {@link Response}
40      */
41     public static Response deleteData(final RestconfStrategy strategy, final YangInstanceIdentifier path) {
42         final RestconfTransaction transaction = strategy.prepareWriteExecution();
43         try {
44             transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
45         } catch (RestconfDocumentedException e) {
46             // close transaction if any and pass exception further
47             transaction.cancel();
48             throw e;
49         }
50         final FluentFuture<? extends CommitInfo> future = transaction.commit();
51         final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
52         //This method will close transactionChain if any
53         FutureCallbackTx.addCallback(future, DELETE_TX_TYPE, response, strategy, path);
54         return response.build();
55     }
56
57     /**
58      * Check if items already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
59      * data does NOT already exists.
60      *
61      * @param isExistsFuture if checked data exists
62      * @param path           Path to be checked
63      * @param operationType  Type of operation (READ, POST, PUT, DELETE...)
64      */
65     public static void checkItemExists(final FluentFuture<Boolean> isExistsFuture,
66                                        final YangInstanceIdentifier path,
67                                        final String operationType) {
68         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
69         FutureCallbackTx.addCallback(isExistsFuture, operationType, response);
70
71         if (!response.result) {
72             LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
73             throw new RestconfDocumentedException(
74                 "Data does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, path);
75         }
76     }
77 }