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