c8e38bc54d51a943b901ec4d5361fcbd258061a9
[policy/apex-pdp.git] / examples / examples-grpc / src / test / java / org / onap / policy / apex / examples / grpc / GrpcTestDummyGrpcServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.examples.grpc;
22
23 import com.google.protobuf.InvalidProtocolBufferException;
24 import com.google.protobuf.util.JsonFormat;
25
26 import io.grpc.Server;
27 import io.grpc.netty.NettyServerBuilder;
28 import io.grpc.stub.StreamObserver;
29
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34
35 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
36 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
37 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
38 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder;
39 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
40
41 /**
42  * The Class GrpcTestDummyGrpcServer creates a dummy gRPC server to mimic a CDS implementation.
43  */
44 public class GrpcTestDummyGrpcServer {
45     private Server server;
46
47     /**
48      * Dummy server for gRPC.
49      *
50      * @param host hostname of the server
51      * @param port port of the server
52      */
53     public GrpcTestDummyGrpcServer(String host, int port) {
54         // Implement the dummy gRPC server
55         BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() {
56             @Override
57             public StreamObserver<ExecutionServiceInput>
58                 process(final StreamObserver<ExecutionServiceOutput> responseObserver) {
59                 return new StreamObserver<ExecutionServiceInput>() {
60                     @Override
61                     public void onNext(final ExecutionServiceInput executionServiceInput) {
62                         String responseString = "";
63                         try {
64                             responseString = Files.readString(Paths.get(
65                                 "src/main/resources/examples/events/APEXgRPC/CreateSubscriptionResponseEvent.json"));
66                         } catch (IOException e) {
67                             throw new ApexEventRuntimeException("Cannot read executionServiceOutput from file", e);
68                         }
69                         ExecutionServiceOutput executionServiceOutput;
70                         Builder builder = ExecutionServiceOutput.newBuilder();
71                         try {
72                             JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder);
73                             executionServiceOutput = builder.build();
74                             responseObserver.onNext(executionServiceOutput);
75                         } catch (InvalidProtocolBufferException e) {
76                             throw new ApexEventRuntimeException(
77                                 "Output string cannot be converted to ExecutionServiceOutput type for gRPC request."
78                                     + e);
79                         }
80                     }
81
82                     @Override
83                     public void onError(final Throwable throwable) {
84                         responseObserver.onError(throwable);
85                     }
86
87                     @Override
88                     public void onCompleted() {
89                         responseObserver.onCompleted();
90                     }
91                 };
92             }
93         };
94         server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl)
95             .build();
96     }
97
98     public void start() throws IOException {
99         server.start();
100     }
101
102     public void stop() {
103         server.shutdown();
104     }
105 }