Changes for checkstyle 8.32
[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 import io.grpc.Server;
26 import io.grpc.netty.NettyServerBuilder;
27 import io.grpc.stub.StreamObserver;
28 import java.io.IOException;
29 import java.net.InetSocketAddress;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
33 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
34 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
35 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder;
36 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
37
38 /**
39  * The Class GrpcTestDummyGrpcServer creates a dummy gRPC server to mimic a CDS implementation.
40  */
41 public class GrpcTestDummyGrpcServer {
42     private Server server;
43
44     /**
45      * Dummy server for gRPC.
46      *
47      * @param host hostname of the server
48      * @param port port of the server
49      */
50     public GrpcTestDummyGrpcServer(String host, int port) {
51         // Implement the dummy gRPC server
52         BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() {
53             @Override
54             public StreamObserver<ExecutionServiceInput>
55                 process(final StreamObserver<ExecutionServiceOutput> responseObserver) {
56                 return new StreamObserver<ExecutionServiceInput>() {
57                     @Override
58                     public void onNext(final ExecutionServiceInput executionServiceInput) {
59                         String responseString = "";
60                         try {
61                             responseString = Files.readString(Paths.get(
62                                 "src/main/resources/examples/events/APEXgRPC/CreateSubscriptionResponseEvent.json"));
63                         } catch (IOException e) {
64                             throw new ApexEventRuntimeException("Cannot read executionServiceOutput from file", e);
65                         }
66                         ExecutionServiceOutput executionServiceOutput;
67                         Builder builder = ExecutionServiceOutput.newBuilder();
68                         try {
69                             JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder);
70                             executionServiceOutput = builder.build();
71                             responseObserver.onNext(executionServiceOutput);
72                         } catch (InvalidProtocolBufferException e) {
73                             throw new ApexEventRuntimeException(
74                                 "Output string cannot be converted to ExecutionServiceOutput type for gRPC request."
75                                     + e);
76                         }
77                     }
78
79                     @Override
80                     public void onError(final Throwable throwable) {
81                         responseObserver.onError(throwable);
82                     }
83
84                     @Override
85                     public void onCompleted() {
86                         responseObserver.onCompleted();
87                     }
88                 };
89             }
90         };
91         server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl)
92             .build();
93     }
94
95     public void start() throws IOException {
96         server.start();
97     }
98
99     public void stop() {
100         server.shutdown();
101     }
102 }