2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
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
34 open class BluePrintProcessingGRPCHandler(
35 private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
36 private val executionServiceHandler: ExecutionServiceHandler
38 BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
40 private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
42 private val ph = Phaser(1)
44 @PreAuthorize("hasRole('USER')")
46 responseObserver: StreamObserver<ExecutionServiceOutput>
47 ): StreamObserver<ExecutionServiceInput> {
49 return object : StreamObserver<ExecutionServiceInput> {
50 override fun onNext(executionServiceInput: ExecutionServiceInput) {
54 executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
56 } catch (e: Exception) {
59 ph.arriveAndDeregister()
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)
72 override fun onCompleted() {
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")