25828b15e18afbc887a52370696c4458b7371a5f
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / service / MockTLSBluePrintProcessingServer.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.service
18
19 import io.grpc.stub.StreamObserver
20 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.GRPCLibConstants
21 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.TLSAuthGrpcServerProperties
22 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.interceptor.GrpcServerLoggingInterceptor
23 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
24 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status
25 import org.onap.ccsdk.cds.controllerblueprints.core.logger
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
30 val log = logger(MockTLSBluePrintProcessingServer::class)
31
32 /** For Integration testing stat this server, Set the working path to run this method */
33 fun main() {
34     try {
35         val tlsAuthGrpcServerProperties = TLSAuthGrpcServerProperties().apply {
36             port = 50052
37             type = GRPCLibConstants.TYPE_TLS_AUTH
38             certChain = "src/test/resources/tls-manual/py-executor-chain.pem"
39             privateKey = "src/test/resources/tls-manual/py-executor-key.pem"
40         }
41         val server = TLSAuthGrpcServerService(tlsAuthGrpcServerProperties).serverBuilder()
42             .intercept(GrpcServerLoggingInterceptor())
43             .addService(MockTLSBluePrintProcessingServer())
44             .build()
45         server.start()
46         log.info("GRPC Serve started(${server.isShutdown}) on port(${server.port})...")
47         server.awaitTermination()
48     } catch (e: Exception) {
49         log.error("Failed to start tls grpc integration server", e)
50     }
51 }
52
53 class MockTLSBluePrintProcessingServer : BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
54     override fun process(responseObserver: StreamObserver<ExecutionServiceOutput>): StreamObserver<ExecutionServiceInput> {
55
56         return object : StreamObserver<ExecutionServiceInput> {
57             override fun onNext(executionServiceInput: ExecutionServiceInput) {
58                 log.info(
59                     "Received requestId(${executionServiceInput.commonHeader.requestId})  " +
60                             "subRequestId(${executionServiceInput.commonHeader.subRequestId})"
61                 )
62                 responseObserver.onNext(buildResponse(executionServiceInput))
63                 responseObserver.onCompleted()
64             }
65
66             override fun onError(error: Throwable) {
67                 log.debug("Fail to process message", error)
68                 responseObserver.onError(
69                     io.grpc.Status.INTERNAL
70                         .withDescription(error.message)
71                         .asException()
72                 )
73             }
74
75             override fun onCompleted() {
76                 log.info("Completed")
77             }
78         }
79     }
80
81     private fun buildResponse(input: ExecutionServiceInput): ExecutionServiceOutput {
82         val status = Status.newBuilder().setCode(200)
83             .setEventType(EventType.EVENT_COMPONENT_EXECUTED)
84             .build()
85         return ExecutionServiceOutput.newBuilder()
86             .setCommonHeader(input.commonHeader)
87             .setActionIdentifiers(input.actionIdentifiers)
88             .setStatus(status)
89             .build()
90     }
91 }