Merge "Add CDS simulator to policy-models"
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / CdsSimulator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import com.google.protobuf.InvalidProtocolBufferException;
25 import com.google.protobuf.util.JsonFormat;
26 import io.grpc.Server;
27 import io.grpc.netty.NettyServerBuilder;
28 import io.grpc.stub.StreamObserver;
29 import java.io.IOException;
30 import java.net.InetSocketAddress;
31 import java.nio.charset.StandardCharsets;
32 import lombok.Getter;
33 import org.apache.commons.io.IOUtils;
34 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
35 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
36 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
37 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder;
38
39 public class CdsSimulator {
40     @Getter
41     private final int port;
42
43     private final Server server;
44
45     /**
46      * Constructs the object, but does not start it.
47      *
48      * @param host host name of the server
49      * @param port port of the server
50      */
51     public CdsSimulator(String host, int port) {
52         this.port = port;
53
54         BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() {
55
56             @Override
57             public StreamObserver<ExecutionServiceInput> process(
58                             final StreamObserver<ExecutionServiceOutput> responseObserver) {
59
60                 return new StreamObserver<ExecutionServiceInput>() {
61
62                     @Override
63                     public void onNext(final ExecutionServiceInput executionServiceInput) {
64                         try {
65                             String responseString = IOUtils.toString(
66                                             getClass().getResource("cds/CreateSubscriptionResponseEvent.json"),
67                                             StandardCharsets.UTF_8);
68                             Builder builder = ExecutionServiceOutput.newBuilder();
69                             JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder);
70                             responseObserver.onNext(builder.build());
71
72                         } catch (InvalidProtocolBufferException e) {
73                             throw new SimulatorRuntimeException("Cannot convert ExecutionServiceOutput output", e);
74
75                         } catch (IOException e) {
76                             throw new SimulatorRuntimeException("Cannot read ExecutionServiceOutput from file", e);
77                         }
78                     }
79
80                     @Override
81                     public void onError(final Throwable throwable) {
82                         responseObserver.onError(throwable);
83                     }
84
85                     @Override
86                     public void onCompleted() {
87                         responseObserver.onCompleted();
88                     }
89                 };
90             }
91         };
92
93         server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl)
94                         .build();
95     }
96
97     public void start() throws IOException {
98         server.start();
99     }
100
101     public void stop() {
102         server.shutdown();
103     }
104 }