6bffffdb5e3811b310bd8c175c30c1f92b5f8d8a
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / MockBluePrintProcessingServer.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.services.execution.scripts
18
19 import io.grpc.ServerBuilder
20 import io.grpc.stub.StreamObserver
21 import kotlinx.coroutines.launch
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.interceptor.GrpcServerLoggingInterceptor
24 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
25 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status
26 import org.onap.ccsdk.cds.controllerblueprints.core.MDCContext
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
29 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
30 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
31
32 private val log = logger(MockBluePrintProcessingServer::class)
33
34
35 class MockBluePrintProcessingServer : BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
36
37     override fun process(responseObserver: StreamObserver<ExecutionServiceOutput>): StreamObserver<ExecutionServiceInput> {
38
39         return object : StreamObserver<ExecutionServiceInput> {
40             override fun onNext(executionServiceInput: ExecutionServiceInput) {
41                 log.info("Received requestId(${executionServiceInput.commonHeader.requestId})  " +
42                         "subRequestId(${executionServiceInput.commonHeader.subRequestId})")
43                 runBlocking {
44                     launch(MDCContext()) {
45                         responseObserver.onNext(buildNotification(executionServiceInput))
46                         responseObserver.onNext(buildResponse(executionServiceInput))
47                         log.info("message has sent successfully...")
48                     }
49                 }
50                 responseObserver.onCompleted()
51             }
52
53             override fun onError(error: Throwable) {
54                 log.debug("Fail to process message", error)
55                 responseObserver.onError(io.grpc.Status.INTERNAL
56                         .withDescription(error.message)
57                         .asException())
58             }
59
60             override fun onCompleted() {
61                 log.info("Completed")
62             }
63         }
64     }
65
66
67     private fun buildNotification(input: ExecutionServiceInput): ExecutionServiceOutput {
68         val status = Status.newBuilder()
69                 .setEventType(EventType.EVENT_COMPONENT_NOTIFICATION)
70                 .build()
71         return ExecutionServiceOutput.newBuilder()
72                 .setCommonHeader(input.commonHeader)
73                 .setActionIdentifiers(input.actionIdentifiers)
74                 .setStatus(status)
75                 .build()
76     }
77
78     private fun buildResponse(input: ExecutionServiceInput): ExecutionServiceOutput {
79
80         val status = Status.newBuilder().setCode(200)
81                 .setEventType(EventType.EVENT_COMPONENT_EXECUTED)
82                 .build()
83         return ExecutionServiceOutput.newBuilder()
84                 .setCommonHeader(input.commonHeader)
85                 .setActionIdentifiers(input.actionIdentifiers)
86                 .setStatus(status)
87                 .build()
88
89     }
90 }
91
92 /** For Integration testing stat this server */
93 fun main() {
94     try {
95         val server = ServerBuilder
96                 .forPort(50052)
97                 .intercept(GrpcServerLoggingInterceptor())
98                 .addService(MockBluePrintProcessingServer())
99                 .build()
100         server.start()
101         log.info("GRPC Serve started(${server.isShutdown}) on port(${server.port})...")
102         server.awaitTermination()
103     } catch (e: Exception) {
104         e.printStackTrace()
105     }
106
107 }