c722173ec82e142905db4d6bcb5085bb08e04445
[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.Status
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.blueprintsprocessor.grpc.service.mdcGrpcCoroutineScope
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
28 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
29 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
30 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
31 import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault
32 import org.onap.ccsdk.cds.error.catalog.core.utils.errorMessageOrDefault
33 import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogService
34 import org.slf4j.LoggerFactory
35 import org.springframework.security.access.prepost.PreAuthorize
36 import org.springframework.stereotype.Service
37 import java.util.concurrent.Phaser
38 import javax.annotation.PreDestroy
39
40 @Service
41 open class BluePrintProcessingGRPCHandler(
42     private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
43     private val executionServiceHandler: ExecutionServiceHandler,
44     private val errorCatalogService: ErrorCatalogService
45 ) :
46     BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
47
48     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
49
50     private val ph = Phaser(1)
51
52     @PreAuthorize("hasRole('USER')")
53     override fun process(
54         responseObserver: StreamObserver<ExecutionServiceOutput>
55     ): StreamObserver<ExecutionServiceInput> {
56
57         return object : StreamObserver<ExecutionServiceInput> {
58             override fun onNext(executionServiceInput: ExecutionServiceInput) {
59                 try {
60                     ph.register()
61                     runBlocking {
62                         mdcGrpcCoroutineScope(executionServiceInput) {
63                             executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
64                         }
65                     }
66                 } catch (e: Exception) {
67                     if (e is BluePrintProcessorException) handleWithErrorCatalog(e) else handleError(e)
68                 } finally {
69                     ph.arriveAndDeregister()
70                 }
71             }
72
73             override fun onError(error: Throwable) {
74                 log.error("Terminating stream error:", error)
75             }
76
77             fun handleError(error: Exception) {
78                 responseObserver.onError(
79                     Status.INTERNAL
80                         .withDescription(error.errorMessageOrDefault())
81                         .withCause(error.errorCauseOrDefault())
82                         .asException()
83                 )
84             }
85
86             fun handleWithErrorCatalog(error: BluePrintProcessorException) {
87                 if (error.protocol == "") {
88                     error.grpc(ErrorCatalogCodes.GENERIC_FAILURE)
89                 }
90                 val errorPayload = errorCatalogService.errorPayload(error)
91                 val grpcCode = Status.fromCodeValue(errorPayload.code)
92                 responseObserver.onError(
93                     grpcCode
94                         .withDescription(errorPayload.message)
95                         .withCause(error.errorCauseOrDefault())
96                         .asException()
97                 )
98             }
99
100             override fun onCompleted() {
101                 log.info("Completed")
102             }
103         }
104     }
105
106     @PreDestroy
107     fun preDestroy() {
108         val name = "BluePrintProcessingGRPCHandler"
109         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
110         ph.arriveAndAwaitAdvance()
111         log.info("Done waiting in $name")
112     }
113 }