Fixing Blueprint Typo's and docs
[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 class MockBluePrintProcessingServer : BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
35
36     override fun process(responseObserver: StreamObserver<ExecutionServiceOutput>): StreamObserver<ExecutionServiceInput> {
37
38         return object : StreamObserver<ExecutionServiceInput> {
39             override fun onNext(executionServiceInput: ExecutionServiceInput) {
40                 log.info(
41                     "Received requestId(${executionServiceInput.commonHeader.requestId})  " +
42                         "subRequestId(${executionServiceInput.commonHeader.subRequestId})"
43                 )
44                 runBlocking {
45                     launch(MDCContext()) {
46                         responseObserver.onNext(buildNotification(executionServiceInput))
47                         responseObserver.onNext(buildResponse(executionServiceInput))
48                         log.info("message has sent successfully...")
49                     }
50                 }
51                 responseObserver.onCompleted()
52             }
53
54             override fun onError(error: Throwable) {
55                 log.debug("Fail to process message", error)
56                 responseObserver.onError(
57                     io.grpc.Status.INTERNAL
58                         .withDescription(error.message)
59                         .asException()
60                 )
61             }
62
63             override fun onCompleted() {
64                 log.info("Completed")
65             }
66         }
67     }
68
69     private fun buildNotification(input: ExecutionServiceInput): ExecutionServiceOutput {
70         val status = Status.newBuilder()
71             .setEventType(EventType.EVENT_COMPONENT_NOTIFICATION)
72             .build()
73         return ExecutionServiceOutput.newBuilder()
74             .setCommonHeader(input.commonHeader)
75             .setActionIdentifiers(input.actionIdentifiers)
76             .setStatus(status)
77             .build()
78     }
79
80     private fun buildResponse(input: ExecutionServiceInput): ExecutionServiceOutput {
81
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 }
92
93 /** For Integration testing stat this server */
94 fun main() {
95     try {
96         val server = ServerBuilder
97             .forPort(50052)
98             .intercept(GrpcServerLoggingInterceptor())
99             .addService(MockBluePrintProcessingServer())
100             .build()
101         server.start()
102         log.info("GRPC Serve started(${server.isShutdown}) on port(${server.port})...")
103         server.awaitTermination()
104     } catch (e: Exception) {
105         e.printStackTrace()
106     }
107 }