Bug 5528 - Delete data impl 34/39634/23
authorJakub Toth <jatoth@cisco.com>
Tue, 31 May 2016 08:41:25 +0000 (10:41 +0200)
committerJakub Toth <jatoth@cisco.com>
Tue, 28 Jun 2016 12:23:45 +0000 (12:23 +0000)
Change-Id: I47955a4f6563c3ae5ab5d5db38823530bf013cba
Signed-off-by: Jakub Toth <jatoth@cisco.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/services/impl/RestconfDataServiceImpl.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/DeleteDataTransactionUtil.java [new file with mode: 0644]
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/ResponseFactory.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/RestconfDataServiceConstant.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/ParserIdentifier.java

index efde43a83d341dc904df01bb34a7ca5262760cb6..ca412292efdf48c80d639e13c377134afedede13 100644 (file)
@@ -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 (file)
index 0000000..9cc7d0f
--- /dev/null
@@ -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<Void, TransactionCommitFailedException> 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<Void, TransactionCommitFailedException> submitData(
+            final DOMDataWriteTransaction writeTx, final YangInstanceIdentifier path) {
+        writeTx.delete(LogicalDatastoreType.CONFIGURATION, path);
+        return writeTx.submit();
+    }
+}
index d3f9a68453df4d522b1765b87b7e51a9a53684fb..5d8c4f56aa63124915eebf5755c37c179f99f88e 100644 (file)
@@ -16,26 +16,28 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
 final class ResponseFactory extends FutureDataFactory<Void> implements Builder<Response> {
 
-    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;
     }
 }
index 19bb87d863fd2272c28a38f0969d6cdd4f7eea44..0045ade5348b46e1744b075520c7b50ecde862cd 100644 (file)
@@ -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.");
+        }
+    }
 }
index 6fb58f5a2b923677ccceead051460b8b7bc9ea89..9b9b8d33c43514291fb8e84542c2d64f010455e5 100644 (file)
@@ -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.
      *