a2101856a8a5942115b07eaaad03b6228ba2b679
[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.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
38
39 @Service
40 open class BluePrintProcessingGRPCHandler(
41     private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
42     private val executionServiceHandler: ExecutionServiceHandler,
43     private val errorCatalogService: ErrorCatalogService
44 ) :
45     BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
46
47     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
48
49     private val ph = Phaser(1)
50
51     @PreAuthorize("hasRole('USER')")
52     override fun process(
53         responseObserver: StreamObserver<ExecutionServiceOutput>
54     ): StreamObserver<ExecutionServiceInput> {
55
56         return object : StreamObserver<ExecutionServiceInput> {
57             override fun onNext(executionServiceInput: ExecutionServiceInput) {
58                 try {
59                     ph.register()
60                     runBlocking {
61                         executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
62                     }
63                 } catch (e: Exception) {
64                     if (e is BluePrintProcessorException) handleWithErrorCatalog(e) else handleError(e)
65                 } finally {
66                     ph.arriveAndDeregister()
67                 }
68             }
69
70             override fun onError(error: Throwable) {
71                 log.error("Terminating stream error:", error)
72             }
73
74             fun handleError(error: Exception) {
75                 responseObserver.onError(
76                     Status.INTERNAL
77                         .withDescription(error.errorMessageOrDefault())
78                         .withCause(error.errorCauseOrDefault())
79                         .asException()
80                 )
81             }
82
83             fun handleWithErrorCatalog(error: BluePrintProcessorException) {
84                 if (error.protocol == "") {
85                     error.grpc(ErrorCatalogCodes.GENERIC_FAILURE)
86                 }
87                 val errorPayload = errorCatalogService.errorPayload(error)
88                 val grpcCode = Status.fromCodeValue(errorPayload.code)
89                 responseObserver.onError(
90                     grpcCode
91                         .withDescription(errorPayload.message)
92                         .withCause(error.errorCauseOrDefault())
93                         .asException()
94                 )
95             }
96
97             override fun onCompleted() {
98                 log.info("Completed")
99             }
100         }
101     }
102
103     @PreDestroy
104     fun preDestroy() {
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")
109     }
110 }