2cd7a810e867d79fb96fcf3843b24a90dcca79e7
[ccsdk/cds.git] /
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.apps.blueprintsprocessor.selfservice.api
19
20 import io.grpc.StatusException
21 import io.grpc.stub.StreamObserver
22 import org.apache.commons.io.FileUtils
23 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
24 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.currentTimestamp
25 import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
26 import org.onap.ccsdk.apps.controllerblueprints.common.api.Status
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
28 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementInput
29 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementOutput
30 import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementServiceGrpc
31 import org.slf4j.LoggerFactory
32 import org.springframework.stereotype.Service
33 import java.io.File
34
35 @Service
36 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     override fun uploadBlueprint(request: BluePrintManagementInput, responseObserver: StreamObserver<BluePrintManagementOutput>) {
43         val blueprintName = request.blueprintName
44         val blueprintVersion = request.blueprintVersion
45         val blueprint = "blueprint $blueprintName:$blueprintVersion"
46
47         log.info("request(${request.commonHeader.requestId}): Received upload $blueprint")
48
49         val blueprintArchivedFilePath = "${bluePrintCoreConfiguration.archivePath}/$blueprintName/$blueprintVersion/$blueprintName.zip"
50         try {
51             val blueprintArchivedFile = File(blueprintArchivedFilePath)
52
53             saveToDisk(request, blueprintArchivedFile)
54             val blueprintId = bluePrintCatalogService.saveToDatabase(blueprintArchivedFile)
55
56             File("${bluePrintCoreConfiguration.archivePath}/$blueprintName").deleteRecursively()
57
58             responseObserver.onNext(successStatus("Successfully uploaded $blueprint with id($blueprintId)", request.commonHeader))
59             responseObserver.onCompleted()
60         } catch (e: Exception) {
61             failStatus("request(${request.commonHeader.requestId}): Failed to upload $blueprint at path $blueprintArchivedFilePath", e)
62         }
63     }
64
65     override fun removeBlueprint(request: BluePrintManagementInput, responseObserver: StreamObserver<BluePrintManagementOutput>) {
66         val blueprintName = request.blueprintName
67         val blueprintVersion = request.blueprintVersion
68         val blueprint = "blueprint $blueprintName:$blueprintVersion"
69
70         log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
71
72         try {
73             bluePrintCatalogService.deleteFromDatabase(blueprintName, blueprintVersion)
74             responseObserver.onNext(successStatus("Successfully deleted $blueprint", request.commonHeader))
75             responseObserver.onCompleted()
76         } catch (e: Exception) {
77             failStatus("request(${request.commonHeader.requestId}): Failed to delete $blueprint", e)
78         }
79     }
80
81     private fun saveToDisk(request: BluePrintManagementInput, blueprintDir: File) {
82         log.debug("request(${request.commonHeader.requestId}): Writing CBA File under :${blueprintDir.absolutePath}")
83         if (blueprintDir.exists()) {
84             log.debug("request(${request.commonHeader.requestId}): Re-creating blueprint directory(${blueprintDir.absolutePath})")
85             FileUtils.deleteDirectory(blueprintDir.parentFile)
86         }
87         FileUtils.forceMkdir(blueprintDir.parentFile)
88         blueprintDir.writeBytes(request.fileChunk.chunk.toByteArray()).apply {
89             log.debug("request(${request.commonHeader.requestId}): CBA file(${blueprintDir.absolutePath} written successfully")
90         }
91     }
92
93     private fun successStatus(message: String, header: CommonHeader): BluePrintManagementOutput =
94             BluePrintManagementOutput.newBuilder()
95                     .setCommonHeader(header)
96                     .setStatus(Status.newBuilder()
97                             .setTimestamp(currentTimestamp())
98                             .setMessage(message)
99                             .setCode(200)
100                             .build())
101                     .build()
102
103     private fun failStatus(message: String, e: Exception): StatusException {
104         log.error(message, e)
105         return io.grpc.Status.INTERNAL
106                 .withDescription(message)
107                 .withCause(e)
108                 .asException()
109     }
110 }