6b3f0a014608d6a5c4dd7c9f48b1d8ed2e13ef1d
[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     private 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         checkItemExists(strategy, LogicalDatastoreType.CONFIGURATION, path, DELETE_TX_TYPE);
43         strategy.delete(LogicalDatastoreType.CONFIGURATION, path);
44         final FluentFuture<? extends CommitInfo> future = strategy.commit();
45         final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
46         //This method will close transactionChain if any
47         FutureCallbackTx.addCallback(future, DELETE_TX_TYPE, response, strategy.getTransactionChain());
48         return response.build();
49     }
50
51     /**
52      * Check if items already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
53      * data does NOT already exists.
54      *
55      * @param strategy      Object that perform the actual DS operations
56      * @param store         Datastore
57      * @param path          Path to be checked
58      * @param operationType Type of operation (READ, POST, PUT, DELETE...)
59      */
60     private static void checkItemExists(final RestconfStrategy strategy,
61                                         final LogicalDatastoreType store, final YangInstanceIdentifier path,
62                                         final String operationType) {
63         final FluentFuture<Boolean> future = strategy.exists(store, path);
64         final FutureDataFactory<Boolean> response = new FutureDataFactory<>();
65
66         FutureCallbackTx.addCallback(future, operationType, response);
67
68         if (!response.result) {
69             // close transaction
70             strategy.cancel();
71             // throw error
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 }