Implement GRPC download cab. 71/95271/2
authorBrinda Santh <brindasanth@in.ibm.com>
Mon, 9 Sep 2019 20:44:39 +0000 (16:44 -0400)
committerBrinda Santh Muthuramalingam <brindasanth@in.ibm.com>
Thu, 12 Sep 2019 01:22:27 +0000 (01:22 +0000)
Change-Id: I7c872b7e6e20590668c68b92ed221752a9413bd8
Issue-ID: CCSDK-1682
Signed-off-by: Brinda Santh <brindasanth@in.ibm.com>
ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/BluePrintManagementGRPCHandler.kt
ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/handler/BluePrintModelHandler.kt
ms/blueprintsprocessor/modules/inbounds/designer-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/BluePrintManagementGRPCHandlerTest.kt

index 08250ed..a3bf370 100644 (file)
@@ -73,7 +73,7 @@ open class BluePrintManagementGRPCHandler(private val bluePrintModelHandler: Blu
                     }
                     UploadAction.ENRICH.toString() -> {
                         val enrichedByteArray = bluePrintModelHandler.enrichBlueprintFileSource(byteArray)
-                        responseObserver.onNext(enrichmentStatus(request.commonHeader, enrichedByteArray))
+                        responseObserver.onNext(outputWithFileBytes(request.commonHeader, enrichedByteArray))
                     }
                     else -> {
                         responseObserver.onNext(failStatus(request.commonHeader,
@@ -90,6 +90,40 @@ open class BluePrintManagementGRPCHandler(private val bluePrintModelHandler: Blu
         }
     }
 
+    @PreAuthorize("hasRole('USER')")
+    override fun downloadBlueprint(request: BluePrintDownloadInput,
+                                   responseObserver: StreamObserver<BluePrintManagementOutput>) {
+        runBlocking {
+            val blueprintName = request.actionIdentifiers.blueprintName
+            val blueprintVersion = request.actionIdentifiers.blueprintVersion
+            val blueprint = "blueprint $blueprintName:$blueprintVersion"
+
+            /** Get the Search Action */
+            val searchAction = request.actionIdentifiers?.actionName.emptyTONull()
+                    ?: DownloadAction.SEARCH.toString()
+
+            log.info("request(${request.commonHeader.requestId}): Received download $blueprint")
+            try {
+                when (searchAction) {
+                    DownloadAction.SEARCH.toString() -> {
+                        val downloadByteArray = bluePrintModelHandler.download(blueprintName, blueprintVersion)
+                        responseObserver.onNext(outputWithFileBytes(request.commonHeader, downloadByteArray))
+                    }
+                    else -> {
+                        responseObserver.onNext(failStatus(request.commonHeader,
+                                "Search action($searchAction) not implemented",
+                                BluePrintProcessorException("Not implemented")))
+                    }
+                }
+            } catch (e: Exception) {
+                responseObserver.onNext(failStatus(request.commonHeader,
+                        "request(${request.commonHeader.requestId}): Failed to delete $blueprint", e))
+            } finally {
+                responseObserver.onCompleted()
+            }
+        }
+    }
+
     @PreAuthorize("hasRole('USER')")
     override fun removeBlueprint(request: BluePrintRemoveInput, responseObserver:
     StreamObserver<BluePrintManagementOutput>) {
@@ -112,7 +146,7 @@ open class BluePrintManagementGRPCHandler(private val bluePrintModelHandler: Blu
         }
     }
 
-    private fun enrichmentStatus(header: CommonHeader, byteArray: ByteArray): BluePrintManagementOutput =
+    private fun outputWithFileBytes(header: CommonHeader, byteArray: ByteArray): BluePrintManagementOutput =
             BluePrintManagementOutput.newBuilder()
                     .setCommonHeader(header)
                     .setFileChunk(FileChunk.newBuilder().setChunk(ByteString.copyFrom(byteArray)))
index 212ffd9..526e92c 100644 (file)
@@ -123,19 +123,14 @@ open class BluePrintModelHandler(private val blueprintsProcessorCatalogService:
     @Throws(BluePrintException::class)
     open fun downloadBlueprintModelFileByNameAndVersion(name: String,
                                                         version: String): ResponseEntity<Resource> {
-        val blueprintModel: BlueprintModel
         try {
-            blueprintModel = getBlueprintModelByNameAndVersion(name, version)
+            val archiveByteArray = download(name, version)
+            val fileName = "${name}_$version.zip"
+            return prepareResourceEntity(fileName, archiveByteArray)
         } catch (e: BluePrintException) {
             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
                     String.format("Error while " + "downloading the CBA file: %s", e.message), e)
         }
-
-        val fileName = blueprintModel.id + ".zip"
-        val file = blueprintModel.blueprintModelContent?.content
-                ?: throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
-                        String.format("Error while downloading the CBA file: couldn't get model content"))
-        return prepareResourceEntity(fileName, file)
     }
 
     /**
@@ -153,7 +148,7 @@ open class BluePrintModelHandler(private val blueprintsProcessorCatalogService:
             throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value, String.format("Error while " + "downloading the CBA file: %s", e.message), e)
         }
 
-        val fileName = blueprintModel.id + ".zip"
+        val fileName = "${blueprintModel.artifactName}_${blueprintModel.artifactVersion}.zip"
         val file = blueprintModel.blueprintModelContent?.content
                 ?: throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
                         String.format("Error while downloading the CBA file: couldn't get model content"))
@@ -319,6 +314,20 @@ open class BluePrintModelHandler(private val blueprintsProcessorCatalogService:
         }
     }
 
+    /** Common CBA download function for RestController and GRPC Handler, the [fileSource] may be
+     * byteArray or File Part type.*/
+    open fun download(name: String, version: String): ByteArray {
+        try {
+            val blueprintModel = getBlueprintModelByNameAndVersion(name, version)
+            return blueprintModel.blueprintModelContent?.content
+                    ?: throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
+                            String.format("Error while downloading the CBA file: couldn't get model content"))
+        } catch (e: BluePrintException) {
+            throw BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.value,
+                    String.format("Error while " + "downloading the CBA file: %s", e.message), e)
+        }
+    }
+
     /** Common CBA Enrich function for RestController and GRPC Handler, the [fileSource] may be
      * byteArray or File Part type.*/
     open suspend fun enrichBlueprintFileSource(fileSource: Any): ByteArray {
index 9f1bd9c..691cfd7 100644 (file)
@@ -68,7 +68,7 @@ class BluePrintManagementGRPCHandlerTest {
     }
 
     @Test
-    fun `test upload blueprint`() {
+    fun `test upload and download blueprint`() {
         val blockingStub = BluePrintManagementServiceGrpc.newBlockingStub(grpcServerRule.channel)
         val id = "123_upload"
         val req = createUploadInputRequest(id, UploadAction.PUBLISH.toString())
@@ -78,6 +78,16 @@ class BluePrintManagementGRPCHandlerTest {
         assertTrue(output.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
                 "failed to get success status")
         assertEquals(id, output.commonHeader.requestId)
+
+        val downloadId = "123_download"
+        val downloadReq = createDownloadInputRequest(downloadId, DownloadAction.SEARCH.toString())
+
+        val downloadOutput = blockingStub.downloadBlueprint(downloadReq)
+        assertEquals(200, downloadOutput.status.code)
+        assertTrue(downloadOutput.status.message.contentEquals(BluePrintConstants.STATUS_SUCCESS),
+                "failed to get success status")
+        assertNotNull(downloadOutput.fileChunk?.chunk, "failed to get cba file chunks")
+        assertEquals(downloadId, downloadOutput.commonHeader.requestId)
     }
 
     @Test
@@ -146,6 +156,23 @@ class BluePrintManagementGRPCHandlerTest {
                 .build()
     }
 
+    private fun createDownloadInputRequest(id: String, action: String): BluePrintDownloadInput {
+        val commonHeader = CommonHeader
+                .newBuilder()
+                .setTimestamp("2012-04-23T18:25:43.511Z")
+                .setOriginatorId("System")
+                .setRequestId(id)
+                .setSubRequestId("1234-56").build()
+
+        return BluePrintDownloadInput.newBuilder()
+                .setCommonHeader(commonHeader)
+                .setActionIdentifiers(ActionIdentifiers.newBuilder()
+                        .setBlueprintName("baseconfiguration")
+                        .setBlueprintVersion("1.0.0")
+                        .setActionName(action).build())
+                .build()
+    }
+
     private fun createRemoveInputRequest(id: String): BluePrintRemoveInput {
         val commonHeader = CommonHeader
                 .newBuilder()