Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / BluePrintProcessingGRPCHandler.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
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.cds.blueprintsprocessor.selfservice.api
19
20 import io.grpc.stub.StreamObserver
21 import kotlinx.coroutines.runBlocking
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.toJava
24 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
25 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
26 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
27 import org.slf4j.LoggerFactory
28 import org.springframework.security.access.prepost.PreAuthorize
29 import org.springframework.stereotype.Service
30
31 @Service
32 open class BluePrintProcessingGRPCHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
33                                           private val executionServiceHandler: ExecutionServiceHandler)
34     : BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
35     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
36
37     @PreAuthorize("hasRole('USER')")
38     override fun process(
39             responseObserver: StreamObserver<ExecutionServiceOutput>): StreamObserver<ExecutionServiceInput> {
40
41         return object : StreamObserver<ExecutionServiceInput> {
42             override fun onNext(executionServiceInput: ExecutionServiceInput) {
43                 try {
44                     runBlocking {
45                         executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
46                     }
47                 } catch (e: Exception) {
48                     onError(e)
49                 }
50             }
51
52             override fun onError(error: Throwable) {
53                 log.debug("Fail to process message", error)
54                 responseObserver.onError(io.grpc.Status.INTERNAL
55                         .withDescription(error.message)
56                         .asException())
57             }
58
59             override fun onCompleted() {
60                 log.info("Completed")
61             }
62         }
63     }
64 }