Resolve duplicate application property values
[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  * Modifications Copyright © 2019 IBM.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
20
21 import io.grpc.StatusException
22 import io.grpc.stub.StreamObserver
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.currentTimestamp
25 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status
27 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
28 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
31 import org.onap.ccsdk.cds.controllerblueprints.core.reCreateDirs
32 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput
33 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc
34 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintRemoveInput
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput
36 import org.slf4j.LoggerFactory
37 import org.springframework.security.access.prepost.PreAuthorize
38 import org.springframework.stereotype.Service
39 import java.io.File
40 import java.util.*
41
42 @Service
43 open class BluePrintManagementGRPCHandler(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44                                           private val blueprintsProcessorCatalogService: BluePrintCatalogService)
45     : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
46
47     private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
48
49     @PreAuthorize("hasRole('USER')")
50     override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver:
51     StreamObserver<BluePrintManagementOutput>) {
52         runBlocking {
53
54             log.info("request(${request.commonHeader.requestId})")
55             val uploadId = UUID.randomUUID().toString()
56             try {
57                 val cbaFile = normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath, uploadId, "cba-zip")
58
59                 saveToDisk(request, cbaFile)
60
61                 val blueprintId = blueprintsProcessorCatalogService.saveToDatabase(uploadId, cbaFile)
62                 responseObserver.onNext(successStatus("Successfully uploaded CBA($blueprintId)...", request.commonHeader))
63                 responseObserver.onCompleted()
64             } catch (e: Exception) {
65                 responseObserver.onError(failStatus("request(${request.commonHeader.requestId}): Failed to upload CBA", e))
66             } finally {
67                 deleteDir(bluePrintLoadConfiguration.blueprintArchivePath, uploadId)
68                 deleteDir(bluePrintLoadConfiguration.blueprintWorkingPath, uploadId)
69             }
70         }
71     }
72
73     @PreAuthorize("hasRole('USER')")
74     override fun removeBlueprint(request: BluePrintRemoveInput, responseObserver:
75     StreamObserver<BluePrintManagementOutput>) {
76
77         runBlocking {
78             val blueprintName = request.blueprintName
79             val blueprintVersion = request.blueprintVersion
80             val blueprint = "blueprint $blueprintName:$blueprintVersion"
81
82             log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
83
84
85             try {
86                 blueprintsProcessorCatalogService.deleteFromDatabase(blueprintName, blueprintVersion)
87                 responseObserver.onNext(successStatus("Successfully deleted $blueprint", request.commonHeader))
88                 responseObserver.onCompleted()
89             } catch (e: Exception) {
90                 responseObserver.onError(failStatus("request(${request.commonHeader.requestId}): Failed to delete $blueprint", e))
91             }
92         }
93     }
94
95     private fun saveToDisk(request: BluePrintUploadInput, cbaFile: File) {
96         log.info("request(${request.commonHeader.requestId}): Writing CBA File under :${cbaFile.absolutePath}")
97
98         // Recreate Folder
99         cbaFile.parentFile.reCreateDirs()
100
101         // Write the File
102         cbaFile.writeBytes(request.fileChunk.chunk.toByteArray()).apply {
103             log.info("request(${request.commonHeader.requestId}): CBA file(${cbaFile.absolutePath} written successfully")
104         }
105
106     }
107
108     private fun successStatus(message: String, header: CommonHeader): BluePrintManagementOutput =
109             BluePrintManagementOutput.newBuilder()
110                     .setCommonHeader(header)
111                     .setStatus(Status.newBuilder()
112                             .setTimestamp(currentTimestamp())
113                             .setMessage(message)
114                             .setCode(200)
115                             .build())
116                     .build()
117
118     private fun failStatus(message: String, e: Exception): StatusException {
119         log.error(message, e)
120         return io.grpc.Status.INTERNAL
121                 .withDescription(message)
122                 .withCause(e)
123                 .asException()
124     }
125 }