2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 Bell Canada.
4 * Modifications Copyright © 2019 IBM.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
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
43 open class BluePrintManagementGRPCHandler(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44 private val blueprintsProcessorCatalogService: BluePrintCatalogService)
45 : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
47 private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
49 @PreAuthorize("hasRole('USER')")
50 override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver:
51 StreamObserver<BluePrintManagementOutput>) {
54 log.info("request(${request.commonHeader.requestId})")
55 val uploadId = UUID.randomUUID().toString()
57 val cbaFile = normalizedFile(bluePrintLoadConfiguration.blueprintArchivePath, uploadId, "cba-zip")
59 saveToDisk(request, cbaFile)
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))
67 deleteDir(bluePrintLoadConfiguration.blueprintArchivePath, uploadId)
68 deleteDir(bluePrintLoadConfiguration.blueprintWorkingPath, uploadId)
73 @PreAuthorize("hasRole('USER')")
74 override fun removeBlueprint(request: BluePrintRemoveInput, responseObserver:
75 StreamObserver<BluePrintManagementOutput>) {
78 val blueprintName = request.blueprintName
79 val blueprintVersion = request.blueprintVersion
80 val blueprint = "blueprint $blueprintName:$blueprintVersion"
82 log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
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))
95 private fun saveToDisk(request: BluePrintUploadInput, cbaFile: File) {
96 log.info("request(${request.commonHeader.requestId}): Writing CBA File under :${cbaFile.absolutePath}")
99 cbaFile.parentFile.reCreateDirs()
102 cbaFile.writeBytes(request.fileChunk.chunk.toByteArray()).apply {
103 log.info("request(${request.commonHeader.requestId}): CBA file(${cbaFile.absolutePath} written successfully")
108 private fun successStatus(message: String, header: CommonHeader): BluePrintManagementOutput =
109 BluePrintManagementOutput.newBuilder()
110 .setCommonHeader(header)
111 .setStatus(Status.newBuilder()
112 .setTimestamp(currentTimestamp())
118 private fun failStatus(message: String, e: Exception): StatusException {
119 log.error(message, e)
120 return io.grpc.Status.INTERNAL
121 .withDescription(message)