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