Merge "Add wrapper function for Cli and Netconf executor"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / BluePrintManagementGRPCHandler.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api
20
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
36
37 @Service
38 open class BluePrintManagementGRPCHandler(private val bluePrintModelHandler: BluePrintModelHandler)
39     : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() {
40
41     private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java)
42
43     @PreAuthorize("hasRole('USER')")
44     override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver:
45     StreamObserver<BluePrintManagementOutput>) {
46
47         runBlocking {
48             log.info("request(${request.commonHeader.requestId})")
49             try {
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()
55
56                 when (uploadAction) {
57                     UploadAction.DRAFT.toString() -> {
58                         val blueprintModel = bluePrintModelHandler.upload(byteArray, false)
59                         responseObserver.onNext(successStatus(request.commonHeader))
60                     }
61                     UploadAction.PUBLISH.toString() -> {
62                         val blueprintModel = bluePrintModelHandler.upload(byteArray, true)
63                         responseObserver.onNext(successStatus(request.commonHeader))
64                     }
65                     UploadAction.VALIDATE.toString() -> {
66                         //TODO("Not Implemented")
67                         responseObserver.onError(failStatus("Not Implemented",
68                                 BluePrintProcessorException("Not Implemented")))
69                     }
70                     UploadAction.ENRICH.toString() -> {
71                         val enrichedByteArray = bluePrintModelHandler.enrichBlueprintFileSource(byteArray)
72                         responseObserver.onNext(enrichmentStatus(request.commonHeader, enrichedByteArray))
73                     }
74                     else -> {
75                         responseObserver.onError(failStatus("Upload action($uploadAction) not implemented",
76                                 BluePrintProcessorException("Upload action($uploadAction) not implemented")))
77                     }
78                 }
79                 responseObserver.onCompleted()
80             } catch (e: Exception) {
81                 responseObserver.onError(failStatus("request(${request.commonHeader.requestId}): Failed to upload CBA", e))
82             }
83         }
84     }
85
86     @PreAuthorize("hasRole('USER')")
87     override fun removeBlueprint(request: BluePrintRemoveInput, responseObserver:
88     StreamObserver<BluePrintManagementOutput>) {
89
90         runBlocking {
91             val blueprintName = request.blueprintName
92             val blueprintVersion = request.blueprintVersion
93             val blueprint = "blueprint $blueprintName:$blueprintVersion"
94
95             log.info("request(${request.commonHeader.requestId}): Received delete $blueprint")
96             try {
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))
102             }
103         }
104     }
105
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)
113                             .setCode(200)
114                             .build())
115                     .build()
116
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)
123                             .setCode(200)
124                             .build())
125                     .build()
126
127     private fun failStatus(message: String, e: Exception): StatusException {
128         log.error(message, e)
129         return io.grpc.Status.INTERNAL
130                 .withDescription(message)
131                 .withCause(e)
132                 .asException()
133     }
134 }