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 com.google.protobuf.util.JsonFormat
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.asJsonString
31 import org.onap.ccsdk.cds.controllerblueprints.core.emptyTONull
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.currentTimestamp
33 import org.onap.ccsdk.cds.controllerblueprints.management.api.*
34 import org.slf4j.LoggerFactory
35 import org.springframework.security.access.prepost.PreAuthorize
36 import org.springframework.stereotype.Service
38 //TODO("Convert to coroutines handler")
40 open class BluePrintManagementGRPCHandler(private val bluePrintModelHandler: BluePrintModelHandler)
41 : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
43 private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
45 @PreAuthorize("hasRole('USER')")
46 override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver:
47 StreamObserver<BluePrintManagementOutput>) {
50 //TODO("catch if request id is missing")
51 log.info("request(${request.commonHeader.requestId})")
53 /** Get the file byte array */
54 val byteArray = request.fileChunk.chunk.toByteArray()
55 /** Get the Upload Action */
56 val uploadAction = request.actionIdentifiers?.actionName.emptyTONull()
57 ?: UploadAction.DRAFT.toString()
60 UploadAction.DRAFT.toString() -> {
61 val blueprintModel = bluePrintModelHandler.upload(byteArray, false)
62 responseObserver.onNext(successStatus(request.commonHeader, blueprintModel.asJsonString()))
64 UploadAction.PUBLISH.toString() -> {
65 val blueprintModel = bluePrintModelHandler.upload(byteArray, true)
66 responseObserver.onNext(successStatus(request.commonHeader, blueprintModel.asJsonString()))
68 UploadAction.VALIDATE.toString() -> {
69 //TODO("Not Implemented")
70 responseObserver.onNext(failStatus(request.commonHeader,
71 "Upload action($uploadAction) not implemented",
72 BluePrintProcessorException("Not Implemented")))
74 UploadAction.ENRICH.toString() -> {
75 val enrichedByteArray = bluePrintModelHandler.enrichBlueprintFileSource(byteArray)
76 responseObserver.onNext(outputWithFileBytes(request.commonHeader, enrichedByteArray))
79 responseObserver.onNext(failStatus(request.commonHeader,
80 "Upload action($uploadAction) not implemented",
81 BluePrintProcessorException("Not implemented")))
84 } catch (e: Exception) {
85 responseObserver.onNext(failStatus(request.commonHeader,
86 "request(${request.commonHeader.requestId}): Failed to upload CBA", e))
88 responseObserver.onCompleted()
93 @PreAuthorize("hasRole('USER')")
94 override fun downloadBlueprint(request: BluePrintDownloadInput,
95 responseObserver: StreamObserver<BluePrintManagementOutput>) {
97 val blueprintName = request.actionIdentifiers.blueprintName
98 val blueprintVersion = request.actionIdentifiers.blueprintVersion
99 val blueprint = "blueprint $blueprintName:$blueprintVersion"
101 /** Get the Search Action */
102 val searchAction = request.actionIdentifiers?.actionName.emptyTONull()
103 ?: DownloadAction.SEARCH.toString()
105 log.info("request(${request.commonHeader.requestId}): Received download $blueprint")
107 when (searchAction) {
108 DownloadAction.SEARCH.toString() -> {
109 val downloadByteArray = bluePrintModelHandler.download(blueprintName, blueprintVersion)
110 responseObserver.onNext(outputWithFileBytes(request.commonHeader, downloadByteArray))
113 responseObserver.onNext(failStatus(request.commonHeader,
114 "Search action($searchAction) not implemented",
115 BluePrintProcessorException("Not implemented")))
118 } catch (e: Exception) {
119 responseObserver.onNext(failStatus(request.commonHeader,
120 "request(${request.commonHeader.requestId}): Failed to delete $blueprint", e))
122 responseObserver.onCompleted()
127 @PreAuthorize("hasRole('USER')")
128 override fun removeBlueprint(request: BluePrintRemoveInput, responseObserver:
129 StreamObserver<BluePrintManagementOutput>) {
132 val blueprintName = request.blueprintName
133 val blueprintVersion = request.blueprintVersion
134 val blueprint = "blueprint $blueprintName:$blueprintVersion"
136 log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
138 bluePrintModelHandler.deleteBlueprintModel(blueprintName, blueprintVersion)
139 responseObserver.onNext(successStatus(request.commonHeader))
140 } catch (e: Exception) {
141 responseObserver.onNext(failStatus(request.commonHeader,
142 "request(${request.commonHeader.requestId}): Failed to delete $blueprint", e))
144 responseObserver.onCompleted()
149 private fun outputWithFileBytes(header: CommonHeader, byteArray: ByteArray): BluePrintManagementOutput =
150 BluePrintManagementOutput.newBuilder()
151 .setCommonHeader(header)
152 .setFileChunk(FileChunk.newBuilder().setChunk(ByteString.copyFrom(byteArray)))
153 .setStatus(Status.newBuilder()
154 .setTimestamp(currentTimestamp())
155 .setMessage(BluePrintConstants.STATUS_SUCCESS)
160 private fun successStatus(header: CommonHeader, propertyContent: String? = null): BluePrintManagementOutput {
161 // Populate Response Payload
162 val propertiesBuilder = BluePrintManagementOutput.newBuilder().propertiesBuilder
163 propertyContent?.let {
164 JsonFormat.parser().merge(propertyContent, propertiesBuilder)
166 return BluePrintManagementOutput.newBuilder()
167 .setCommonHeader(header)
168 .setProperties(propertiesBuilder.build())
169 .setStatus(Status.newBuilder()
170 .setTimestamp(currentTimestamp())
171 .setMessage(BluePrintConstants.STATUS_SUCCESS)
177 private fun failStatus(header: CommonHeader, message: String, e: Exception): BluePrintManagementOutput {
178 log.error(message, e)
179 return BluePrintManagementOutput.newBuilder()
180 .setCommonHeader(header)
181 .setStatus(Status.newBuilder()
182 .setTimestamp(currentTimestamp())
183 .setMessage(BluePrintConstants.STATUS_FAILURE)
184 .setErrorMessage(message)
188 // return io.grpc.Status.INTERNAL
189 // .withDescription(message)