Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / BluePrintManagementGRPCHandler.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19
20 import io.grpc.StatusException
21 import io.grpc.stub.StreamObserver
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.currentTimestamp
24 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
25 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
27 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementInput
28 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput
29 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc
30 import org.slf4j.LoggerFactory
31 import org.springframework.security.access.prepost.PreAuthorize
32 import org.springframework.stereotype.Service
33 import java.io.File
34
35 @Service
36 open class BluePrintManagementGRPCHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
37                                      private val bluePrintCatalogService: BluePrintCatalogService)
38     : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
39
40     private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
41
42     @PreAuthorize("hasRole('USER')")
43     override fun uploadBlueprint(request: BluePrintManagementInput, responseObserver: StreamObserver<BluePrintManagementOutput>) {
44         val blueprintName = request.blueprintName
45         val blueprintVersion = request.blueprintVersion
46         val blueprint = "blueprint $blueprintName:$blueprintVersion"
47
48         log.info("request(${request.commonHeader.requestId}): Received upload $blueprint")
49
50         val blueprintArchivedFilePath = "${bluePrintCoreConfiguration.archivePath}/$blueprintName/$blueprintVersion/$blueprintName.zip"
51         try {
52             val blueprintArchivedFile = File(blueprintArchivedFilePath)
53
54             saveToDisk(request, blueprintArchivedFile)
55             val blueprintId = bluePrintCatalogService.saveToDatabase(blueprintArchivedFile)
56
57             File("${bluePrintCoreConfiguration.archivePath}/$blueprintName").deleteRecursively()
58
59             responseObserver.onNext(successStatus("Successfully uploaded $blueprint with id($blueprintId)", request.commonHeader))
60             responseObserver.onCompleted()
61         } catch (e: Exception) {
62             failStatus("request(${request.commonHeader.requestId}): Failed to upload $blueprint at path $blueprintArchivedFilePath", e)
63         }
64     }
65
66     @PreAuthorize("hasRole('USER')")
67     override fun removeBlueprint(request: BluePrintManagementInput, responseObserver: StreamObserver<BluePrintManagementOutput>) {
68         val blueprintName = request.blueprintName
69         val blueprintVersion = request.blueprintVersion
70         val blueprint = "blueprint $blueprintName:$blueprintVersion"
71
72         log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
73
74         try {
75             bluePrintCatalogService.deleteFromDatabase(blueprintName, blueprintVersion)
76             responseObserver.onNext(successStatus("Successfully deleted $blueprint", request.commonHeader))
77             responseObserver.onCompleted()
78         } catch (e: Exception) {
79             failStatus("request(${request.commonHeader.requestId}): Failed to delete $blueprint", e)
80         }
81     }
82
83     private fun saveToDisk(request: BluePrintManagementInput, blueprintDir: File) {
84         log.info("request(${request.commonHeader.requestId}): Writing CBA File under :${blueprintDir.absolutePath}")
85         if (blueprintDir.exists()) {
86             log.info("request(${request.commonHeader.requestId}): Re-creating blueprint directory(${blueprintDir.absolutePath})")
87             //FileUtils.deleteDirectory(blueprintDir.parentFile)
88             blueprintDir.parentFile.deleteRecursively()
89         }
90         blueprintDir.parentFile.mkdirs()
91         //FileUtils.forceMkdir(blueprintDir.parentFile)
92         blueprintDir.writeBytes(request.fileChunk.chunk.toByteArray()).apply {
93             log.info("request(${request.commonHeader.requestId}): CBA file(${blueprintDir.absolutePath} written successfully")
94         }
95
96     }
97
98     private fun successStatus(message: String, header: CommonHeader): BluePrintManagementOutput =
99             BluePrintManagementOutput.newBuilder()
100                     .setCommonHeader(header)
101                     .setStatus(Status.newBuilder()
102                             .setTimestamp(currentTimestamp())
103                             .setMessage(message)
104                             .setCode(200)
105                             .build())
106                     .build()
107
108     private fun failStatus(message: String, e: Exception): StatusException {
109         log.error(message, e)
110         return io.grpc.Status.INTERNAL
111                 .withDescription(message)
112                 .withCause(e)
113                 .asException()
114     }
115 }