Formatting Code base with ktlint
[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.core.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 import java.util.concurrent.Phaser
31 import javax.annotation.PreDestroy
32
33 @Service
34 open class BluePrintProcessingGRPCHandler(
35     private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
36     private val executionServiceHandler: ExecutionServiceHandler
37 ) :
38     BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
39
40     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
41
42     private val ph = Phaser(1)
43
44     @PreAuthorize("hasRole('USER')")
45     override fun process(
46         responseObserver: StreamObserver<ExecutionServiceOutput>
47     ): StreamObserver<ExecutionServiceInput> {
48
49         return object : StreamObserver<ExecutionServiceInput> {
50             override fun onNext(executionServiceInput: ExecutionServiceInput) {
51                 try {
52                     ph.register()
53                     runBlocking {
54                         executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
55                     }
56                 } catch (e: Exception) {
57                     onError(e)
58                 } finally {
59                     ph.arriveAndDeregister()
60                 }
61             }
62
63             override fun onError(error: Throwable) {
64                 log.debug("Fail to process message", error)
65                 responseObserver.onError(
66                     io.grpc.Status.INTERNAL
67                         .withDescription(error.message)
68                         .asException()
69                 )
70             }
71
72             override fun onCompleted() {
73                 log.info("Completed")
74             }
75         }
76     }
77
78     @PreDestroy
79     fun preDestroy() {
80         val name = "BluePrintProcessingGRPCHandler"
81         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
82         ph.arriveAndAwaitAdvance()
83         log.info("Done waiting in $name")
84     }
85 }