Rename restconf-nb-rfc8040 to restconf-nb
[netconf.git] / restconf / restconf-nb / 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.restconf.common.errors.RestconfDocumentedException;
15 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
16 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
17 import org.opendaylight.yangtools.yang.common.ErrorTag;
18 import org.opendaylight.yangtools.yang.common.ErrorType;
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         // Hidden on purpose
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         final RestconfTransaction transaction = strategy.prepareWriteExecution();
42         try {
43             transaction.delete(path);
44         } catch (RestconfDocumentedException e) {
45             // close transaction if any and pass exception further
46             transaction.cancel();
47             throw e;
48         }
49         final FluentFuture<? extends CommitInfo> future = transaction.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, 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 isExistsFuture if checked data exists
61      * @param path           Path to be checked
62      * @param operationType  Type of operation (READ, POST, PUT, DELETE...)
63      */
64     public static void checkItemExists(final FluentFuture<Boolean> isExistsFuture,
65                                        final YangInstanceIdentifier path,
66                                        final String operationType) {
67         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
68         FutureCallbackTx.addCallback(isExistsFuture, operationType, response);
69
70         if (!response.result) {
71             LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
72             throw new RestconfDocumentedException(
73                 "Data does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, path);
74         }
75     }
76 }