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
21 import io.grpc.stub.StreamObserver
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.toJava
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
27 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
28 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
29 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
30 import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault
31 import org.onap.ccsdk.cds.error.catalog.core.utils.errorMessageOrDefault
32 import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogService
33 import org.slf4j.LoggerFactory
34 import org.springframework.security.access.prepost.PreAuthorize
35 import org.springframework.stereotype.Service
36 import java.util.concurrent.Phaser
37 import javax.annotation.PreDestroy
40 open class BluePrintProcessingGRPCHandler(
41 private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
42 private val executionServiceHandler: ExecutionServiceHandler,
43 private val errorCatalogService: ErrorCatalogService
45 BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
47 private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
49 private val ph = Phaser(1)
51 @PreAuthorize("hasRole('USER')")
53 responseObserver: StreamObserver<ExecutionServiceOutput>
54 ): StreamObserver<ExecutionServiceInput> {
56 return object : StreamObserver<ExecutionServiceInput> {
57 override fun onNext(executionServiceInput: ExecutionServiceInput) {
61 executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
63 } catch (e: Exception) {
66 ph.arriveAndDeregister()
70 override fun onError(error: Throwable) {
71 log.debug("Fail to process message", error)
72 if (error is BluePrintProcessorException) onErrorCatalog(error) else onError(error)
75 fun onError(error: Exception) {
76 responseObserver.onError(
78 .withDescription(error.errorMessageOrDefault())
79 .withCause(error.errorCauseOrDefault())
84 fun onErrorCatalog(error: BluePrintProcessorException) {
85 if (error.protocol == "") {
86 error.grpc(ErrorCatalogCodes.GENERIC_FAILURE)
88 val errorPayload = errorCatalogService.errorPayload(error)
89 val grpcCode = Status.fromCodeValue(errorPayload.code)
90 responseObserver.onError(
92 .withDescription(errorPayload.message)
93 .withCause(error.errorCauseOrDefault())
98 override fun onCompleted() {
106 val name = "BluePrintProcessingGRPCHandler"
107 log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
108 ph.arriveAndAwaitAdvance()
109 log.info("Done waiting in $name")