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.designer.api
21 import com.google.protobuf.ByteString
22 import io.grpc.StatusException
23 import io.grpc.stub.StreamObserver
24 import kotlinx.coroutines.runBlocking
25 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.BluePrintModelHandler
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
27 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.emptyTONull
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.currentTimestamp
32 import org.onap.ccsdk.cds.controllerblueprints.management.api.*
33 import org.slf4j.LoggerFactory
34 import org.springframework.security.access.prepost.PreAuthorize
35 import org.springframework.stereotype.Service
38 open class BluePrintManagementGRPCHandler(private val bluePrintModelHandler: BluePrintModelHandler)
39 : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
41 private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
43 @PreAuthorize("hasRole('USER')")
44 override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver:
45 StreamObserver<BluePrintManagementOutput>) {
48 log.info("request(${request.commonHeader.requestId})")
50 /** Get the file byte array */
51 val byteArray = request.fileChunk.chunk.toByteArray()
52 /** Get the Upload Action */
53 val uploadAction = request.actionIdentifiers?.actionName.emptyTONull()
54 ?: UploadAction.DRAFT.toString()
57 UploadAction.DRAFT.toString() -> {
58 val blueprintModel = bluePrintModelHandler.upload(byteArray, false)
59 responseObserver.onNext(successStatus(request.commonHeader))
61 UploadAction.PUBLISH.toString() -> {
62 val blueprintModel = bluePrintModelHandler.upload(byteArray, true)
63 responseObserver.onNext(successStatus(request.commonHeader))
65 UploadAction.VALIDATE.toString() -> {
66 //TODO("Not Implemented")
67 responseObserver.onError(failStatus("Not Implemented",
68 BluePrintProcessorException("Not Implemented")))
70 UploadAction.ENRICH.toString() -> {
71 val enrichedByteArray = bluePrintModelHandler.enrichBlueprintFileSource(byteArray)
72 responseObserver.onNext(enrichmentStatus(request.commonHeader, enrichedByteArray))
75 responseObserver.onError(failStatus("Upload action($uploadAction) not implemented",
76 BluePrintProcessorException("Upload action($uploadAction) not implemented")))
79 responseObserver.onCompleted()
80 } catch (e: Exception) {
81 responseObserver.onError(failStatus("request(${request.commonHeader.requestId}): Failed to upload CBA", e))
86 @PreAuthorize("hasRole('USER')")
87 override fun removeBlueprint(request: BluePrintRemoveInput, responseObserver:
88 StreamObserver<BluePrintManagementOutput>) {
91 val blueprintName = request.blueprintName
92 val blueprintVersion = request.blueprintVersion
93 val blueprint = "blueprint $blueprintName:$blueprintVersion"
95 log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
97 bluePrintModelHandler.deleteBlueprintModel(blueprintName, blueprintVersion)
98 responseObserver.onNext(successStatus(request.commonHeader))
99 responseObserver.onCompleted()
100 } catch (e: Exception) {
101 responseObserver.onError(failStatus("request(${request.commonHeader.requestId}): Failed to delete $blueprint", e))
106 private fun enrichmentStatus(header: CommonHeader, byteArray: ByteArray): BluePrintManagementOutput =
107 BluePrintManagementOutput.newBuilder()
108 .setCommonHeader(header)
109 .setFileChunk(FileChunk.newBuilder().setChunk(ByteString.copyFrom(byteArray)))
110 .setStatus(Status.newBuilder()
111 .setTimestamp(currentTimestamp())
112 .setMessage(BluePrintConstants.STATUS_SUCCESS)
117 private fun successStatus(header: CommonHeader): BluePrintManagementOutput =
118 BluePrintManagementOutput.newBuilder()
119 .setCommonHeader(header)
120 .setStatus(Status.newBuilder()
121 .setTimestamp(currentTimestamp())
122 .setMessage(BluePrintConstants.STATUS_SUCCESS)
127 private fun failStatus(message: String, e: Exception): StatusException {
128 log.error(message, e)
129 return io.grpc.Status.INTERNAL
130 .withDescription(message)