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) {
64 if (e is BluePrintProcessorException) handleWithErrorCatalog(e) else handleError(e)
66 ph.arriveAndDeregister()
70 override fun onError(error: Throwable) {
71 log.error("Terminating stream error:", error)
74 fun handleError(error: Exception) {
75 responseObserver.onError(
77 .withDescription(error.errorMessageOrDefault())
78 .withCause(error.errorCauseOrDefault())
83 fun handleWithErrorCatalog(error: BluePrintProcessorException) {
84 if (error.protocol == "") {
85 error.grpc(ErrorCatalogCodes.GENERIC_FAILURE)
87 val errorPayload = errorCatalogService.errorPayload(error)
88 val grpcCode = Status.fromCodeValue(errorPayload.code)
89 responseObserver.onError(
91 .withDescription(errorPayload.message)
92 .withCause(error.errorCauseOrDefault())
97 override fun onCompleted() {
105 val name = "BluePrintProcessingGRPCHandler"
106 log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
107 ph.arriveAndAwaitAdvance()
108 log.info("Done waiting in $name")