2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts
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
32 private val log = logger(MockBluePrintProcessingServer::class)
34 class MockBluePrintProcessingServer : BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
36 override fun process(responseObserver: StreamObserver<ExecutionServiceOutput>): StreamObserver<ExecutionServiceInput> {
38 return object : StreamObserver<ExecutionServiceInput> {
39 override fun onNext(executionServiceInput: ExecutionServiceInput) {
41 "Received requestId(${executionServiceInput.commonHeader.requestId}) " +
42 "subRequestId(${executionServiceInput.commonHeader.subRequestId})"
45 launch(MDCContext()) {
46 responseObserver.onNext(buildNotification(executionServiceInput))
47 responseObserver.onNext(buildResponse(executionServiceInput))
48 log.info("message has sent successfully...")
51 responseObserver.onCompleted()
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)
63 override fun onCompleted() {
69 private fun buildNotification(input: ExecutionServiceInput): ExecutionServiceOutput {
70 val status = Status.newBuilder()
71 .setEventType(EventType.EVENT_COMPONENT_NOTIFICATION)
73 return ExecutionServiceOutput.newBuilder()
74 .setCommonHeader(input.commonHeader)
75 .setActionIdentifiers(input.actionIdentifiers)
80 private fun buildResponse(input: ExecutionServiceInput): ExecutionServiceOutput {
82 val status = Status.newBuilder().setCode(200)
83 .setEventType(EventType.EVENT_COMPONENT_EXECUTED)
85 return ExecutionServiceOutput.newBuilder()
86 .setCommonHeader(input.commonHeader)
87 .setActionIdentifiers(input.actionIdentifiers)
93 /** For Integration testing stat this server */
96 val server = ServerBuilder
98 .intercept(GrpcServerLoggingInterceptor())
99 .addService(MockBluePrintProcessingServer())
102 log.info("GRPC Serve started(${server.isShutdown}) on port(${server.port})...")
103 server.awaitTermination()
104 } catch (e: Exception) {