17191f31d71c43d77340a68ff0cb3b10a4921c95
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
18
19 import io.grpc.stub.StreamObserver
20 import org.apache.commons.io.FileUtils
21 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
22 import org.onap.ccsdk.apps.controllerblueprints.management.api.*
23 import org.slf4j.LoggerFactory
24 import org.springframework.stereotype.Service
25 import java.io.File
26
27 @Service
28 class BluePrintManagementGRPCHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration)
29     : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
30
31     private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
32
33     override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver: StreamObserver<BluePrintUploadOutput>) {
34         val response = BluePrintUploadOutput.newBuilder().setCommonHeader(request.commonHeader).build()
35         try {
36             val blueprintName = request.blueprintName
37             val blueprintVersion = request.blueprintVersion
38             val filePath = "${bluePrintCoreConfiguration.archivePath}/$blueprintName/$blueprintVersion"
39             val blueprintDir = File(filePath)
40
41             log.info("Re-creating blueprint directory(${blueprintDir.absolutePath})")
42             FileUtils.deleteDirectory(blueprintDir)
43             FileUtils.forceMkdir(blueprintDir)
44
45             val file = File("${blueprintDir.absolutePath}/$blueprintName.zip")
46             log.info("Writing CBA File under :${file.absolutePath}")
47
48             val fileChunk = request.fileChunk
49
50             file.writeBytes(fileChunk.chunk.toByteArray()).apply {
51                 log.info("CBA file(${file.absolutePath} written successfully")
52             }
53         } catch (e: Exception) {
54             log.error("failed to upload file ", e)
55         }
56         responseObserver.onNext(response)
57         responseObserver.onCompleted()
58     }
59
60     override fun removeBlueprint(request: BluePrintRemoveInput?, responseObserver: StreamObserver<BluePrintRemoveOutput>?) {
61         //TODO
62     }
63 }