From d4288759f290404fdbd1308b427e3a9b67574f83 Mon Sep 17 00:00:00 2001 From: Jakub Toth Date: Tue, 31 May 2016 10:41:25 +0200 Subject: [PATCH] Bug 5528 - Delete data impl Change-Id: I47955a4f6563c3ae5ab5d5db38823530bf013cba Signed-off-by: Jakub Toth --- .../impl/RestconfDataServiceImpl.java | 17 +++++- .../utils/DeleteDataTransactionUtil.java | 59 +++++++++++++++++++ .../restful/utils/ResponseFactory.java | 28 +++++---- .../utils/RestconfDataServiceConstant.java | 12 ++++ .../utils/parser/ParserIdentifier.java | 4 ++ 5 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/DeleteDataTransactionUtil.java diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/services/impl/RestconfDataServiceImpl.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/services/impl/RestconfDataServiceImpl.java index efde43a83d..ca412292ef 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/services/impl/RestconfDataServiceImpl.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/services/impl/RestconfDataServiceImpl.java @@ -24,6 +24,7 @@ import org.opendaylight.restconf.handlers.SchemaContextHandler; import org.opendaylight.restconf.handlers.TransactionChainHandler; import org.opendaylight.restconf.restful.services.api.RestconfDataService; import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper; +import org.opendaylight.restconf.restful.utils.DeleteDataTransactionUtil; import org.opendaylight.restconf.restful.utils.PostDataTransactionUtil; import org.opendaylight.restconf.restful.utils.PutDataTransactionUtil; import org.opendaylight.restconf.restful.utils.ReadDataTransactionUtil; @@ -120,7 +121,21 @@ public class RestconfDataServiceImpl implements RestconfDataService { @Override public Response deleteData(final String identifier) { - throw new UnsupportedOperationException("Not yet implemented."); + final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get()); + final InstanceIdentifierContext instanceIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, + schemaContextRef.get()); + + final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint(); + DOMDataReadWriteTransaction transaction = null; + if (mountPoint == null) { + transaction = this.transactionChainHandler.get().newReadWriteTransaction(); + } else { + transaction = transactionOfMountPoint(mountPoint); + } + + final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint, + transaction); + return DeleteDataTransactionUtil.deleteData(transactionNode); } @Override diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/DeleteDataTransactionUtil.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/DeleteDataTransactionUtil.java new file mode 100644 index 0000000000..9cc7d0fe9e --- /dev/null +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/DeleteDataTransactionUtil.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.restconf.restful.utils; + +import com.google.common.util.concurrent.CheckedFuture; +import javax.ws.rs.core.Response; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; +import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +/** + * Util class for delete specific data in config DS. + * + */ +public final class DeleteDataTransactionUtil { + + private DeleteDataTransactionUtil() { + throw new UnsupportedOperationException("Util class."); + } + + /** + * Delete data from DS via transaction. + * + * @param transactionNode + * - Wrapper for data of transaction + * @return {@link Response} + */ + + public static Response deleteData(final TransactionVarsWrapper transactionNode) { + final CheckedFuture future = submitData( + transactionNode.getTransaction(), transactionNode.getInstanceIdentifier().getInstanceIdentifier()); + final ResponseFactory response = new ResponseFactory(); + FutureCallbackTx.addCallback(future, transactionNode.getTransaction(), + RestconfDataServiceConstant.DeleteData.DELETE_TX_TYPE, response); + return response.build(); + } + + /** + * Delete data via transaction + * + * @param writeTx + * - write transaction + * @param path + * - path of data to delete + * @return {@link CheckedFuture} + */ + private static CheckedFuture submitData( + final DOMDataWriteTransaction writeTx, final YangInstanceIdentifier path) { + writeTx.delete(LogicalDatastoreType.CONFIGURATION, path); + return writeTx.submit(); + } +} diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/ResponseFactory.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/ResponseFactory.java index d3f9a68453..5d8c4f56aa 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/ResponseFactory.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/ResponseFactory.java @@ -16,26 +16,28 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; final class ResponseFactory extends FutureDataFactory implements Builder { - private final NormalizedNode readData; - private final URI location; - + private final ResponseBuilder responseBuilder; ResponseFactory(final NormalizedNode readData) { - this.readData = readData; - this.location = null; + final Status status = prepareStatus(readData); + this.responseBuilder = Response.status(status); } ResponseFactory(final NormalizedNode readData, final URI location) { - this.readData = readData; - this.location = location; + final Status status = prepareStatus(readData); + this.responseBuilder = Response.status(status); + this.responseBuilder.location(location); + } + + ResponseFactory() { + this.responseBuilder = Response.status(Status.OK); } @Override public Response build() { - final Status status = this.readData != null ? Status.OK : Status.CREATED; - final ResponseBuilder responseBuilder = Response.status(status); - if (this.location != null) { - responseBuilder.location(this.location); - } - return responseBuilder.build(); + return this.responseBuilder.build(); + } + + private Status prepareStatus(final NormalizedNode readData) { + return readData != null ? Status.OK : Status.CREATED; } } diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/RestconfDataServiceConstant.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/RestconfDataServiceConstant.java index 19bb87d863..0045ade534 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/RestconfDataServiceConstant.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/RestconfDataServiceConstant.java @@ -79,4 +79,16 @@ public final class RestconfDataServiceConstant { throw new UnsupportedOperationException("Util class."); } } + + /** + * Constants for data to delete + * + */ + public final class DeleteData { + public static final String DELETE_TX_TYPE = "DELETE"; + + private DeleteData() { + throw new UnsupportedOperationException("Util class."); + } + } } diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/ParserIdentifier.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/ParserIdentifier.java index 6fb58f5a2b..9b9b8d33c4 100644 --- a/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/ParserIdentifier.java +++ b/restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/ParserIdentifier.java @@ -42,6 +42,10 @@ public final class ParserIdentifier { private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class); + private ParserIdentifier() { + throw new UnsupportedOperationException("Util class."); + } + /** * Make {@link InstanceIdentifierContext} from identifier. * -- 2.36.6