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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.simulators;
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;
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;
39 public class CdsSimulator {
41 private final int port;
43 private final Server server;
46 * Constructs the object, but does not start it.
48 * @param host host name of the server
49 * @param port port of the server
51 public CdsSimulator(String host, int port) {
54 BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() {
57 public StreamObserver<ExecutionServiceInput> process(
58 final StreamObserver<ExecutionServiceOutput> responseObserver) {
60 return new StreamObserver<ExecutionServiceInput>() {
63 public void onNext(final ExecutionServiceInput executionServiceInput) {
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());
72 } catch (InvalidProtocolBufferException e) {
73 throw new SimulatorRuntimeException("Cannot convert ExecutionServiceOutput output", e);
75 } catch (IOException e) {
76 throw new SimulatorRuntimeException("Cannot read ExecutionServiceOutput from file", e);
81 public void onError(final Throwable throwable) {
82 responseObserver.onError(throwable);
86 public void onCompleted() {
87 responseObserver.onCompleted();
93 server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl)
97 public void start() throws IOException {