2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
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;
24 * Util class for delete specific data in config DS.
26 public final class DeleteDataTransactionUtil {
27 private static final Logger LOG = LoggerFactory.getLogger(DeleteDataTransactionUtil.class);
28 public static final String DELETE_TX_TYPE = "DELETE";
30 private DeleteDataTransactionUtil() {
31 throw new UnsupportedOperationException("Util class.");
35 * Delete data from DS via transaction.
37 * @param strategy object that perform the actual DS operations
38 * @return {@link Response}
40 public static Response deleteData(final RestconfStrategy strategy, final YangInstanceIdentifier path) {
41 strategy.prepareReadWriteExecution();
43 strategy.delete(LogicalDatastoreType.CONFIGURATION, path);
44 } catch (RestconfDocumentedException e) {
45 // close transaction if any and pass exception further
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();
57 * Check if items already exists at specified {@code path}. Throws {@link RestconfDocumentedException} if
58 * data does NOT already exists.
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...)
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);
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);