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