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