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