Update snapshot and/or references of policy/models to latest snapshots
[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-2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.simulators;
24
25 import com.google.protobuf.InvalidProtocolBufferException;
26 import com.google.protobuf.util.JsonFormat;
27 import io.grpc.Server;
28 import io.grpc.netty.NettyServerBuilder;
29 import io.grpc.stub.StreamObserver;
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.time.Instant;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.atomic.AtomicInteger;
35 import lombok.Getter;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType;
38 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
39 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
40 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
41 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
42 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder;
43 import org.onap.policy.common.utils.resources.ResourceUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class CdsSimulator implements Runnable {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(CdsSimulator.class);
50
51     @Getter
52     private final int port;
53
54     private final Server server;
55
56     private final String resourceLocation;
57
58     private AtomicInteger countOfEvents = new AtomicInteger(1);
59
60     /**
61      * Constructs the object, but does not start it.
62      *
63      * @param host host name of the server
64      * @param port port of the server
65      */
66     public CdsSimulator(String host, int port) {
67         this(host, port, "org/onap/policy/simulators/cds/", 0, 0);
68     }
69
70     /**
71      * Constructs the object, but does not start it.
72      *
73      * @param host host name of the server
74      * @param port port of the server
75      * @param countOfSuccesfulEvents number of successive successful events
76      * @param requestedResponseDelayMs time for the request to be processed
77      */
78     public CdsSimulator(String host, int port, String resourceLocation, int countOfSuccesfulEvents,
79         long requestedResponseDelayMs) {
80         this.port = port;
81         this.resourceLocation = resourceLocation;
82
83         BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() {
84
85             @Override
86             public StreamObserver<ExecutionServiceInput> process(
87                 final StreamObserver<ExecutionServiceOutput> responseObserver) {
88
89                 return new StreamObserver<ExecutionServiceInput>() {
90
91                     @Override
92                     public void onNext(final ExecutionServiceInput executionServiceInput) {
93                         LOGGER.info("Received request input to CDS: {}", executionServiceInput);
94                         try {
95                             var builder = getResponse(executionServiceInput, countOfSuccesfulEvents);
96                             TimeUnit.MILLISECONDS.sleep(requestedResponseDelayMs);
97                             responseObserver.onNext(builder.build());
98                         } catch (InvalidProtocolBufferException e) {
99                             throw new SimulatorRuntimeException("Cannot convert ExecutionServiceOutput output", e);
100                         } catch (InterruptedException e) {
101                             Thread.currentThread().interrupt();
102                             throw new SimulatorRuntimeException("Execution Interrupted", e);
103                         }
104                     }
105
106                     @Override
107                     public void onError(final Throwable throwable) {
108                         responseObserver.onError(throwable);
109                     }
110
111                     @Override
112                     public void onCompleted() {
113                         responseObserver.onCompleted();
114                     }
115                 };
116             }
117         };
118
119         server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl)
120             .build();
121     }
122
123     /**
124      * Start the server.
125      *
126      * @throws IOException IO exception.
127      */
128     public void start() throws IOException {
129         server.start();
130         // The grpc server uses daemon threads by default. Hence the application will exit as soon the main thread
131         // completes. So, wrap the server in a non-daemon thread and call awaitTermination to keep the thread alive
132         // until the server is terminated.
133         new Thread(this).start();
134     }
135
136     /**
137      * Stop the server.
138      */
139     public void stop() {
140         server.shutdown();
141     }
142
143     /**
144      * Constructs the ResponseString on the basis of request.
145      *
146      * @param executionServiceInput service input
147      * @param countOfSuccesfulEvents number of successive successful events
148      * @return builder for ExecutionServiceOutput response
149      * @throws InvalidProtocolBufferException when response string cannot be converted
150      */
151     public Builder getResponse(ExecutionServiceInput executionServiceInput, int countOfSuccesfulEvents)
152         throws InvalidProtocolBufferException {
153         var resourceName = "DefaultResponseEvent";
154         if (!StringUtils.isBlank(executionServiceInput.getActionIdentifiers().getActionName())) {
155             var actionIdentifiers = executionServiceInput.getActionIdentifiers();
156             resourceName = actionIdentifiers.getBlueprintName() + "-" + actionIdentifiers.getActionName();
157         }
158         if (countOfSuccesfulEvents > 0 && countOfEvents.getAndIncrement() % countOfSuccesfulEvents == 0) {
159             // generating the failure response
160             resourceName = resourceName + "-error.json";
161         } else {
162             resourceName = resourceName + ".json";
163         }
164         LOGGER.info("Fetching response from {}", resourceName);
165         var responseString = ResourceUtils.getResourceAsString(resourceLocation + resourceName);
166         var builder = ExecutionServiceOutput.newBuilder();
167         if (null == responseString) {
168             LOGGER.info("Expected response file {} not found in {}", resourceName, resourceLocation);
169             var actionIdentifiers = executionServiceInput.getActionIdentifiers();
170             builder.setCommonHeader(executionServiceInput.getCommonHeader());
171             builder.setActionIdentifiers(actionIdentifiers);
172             builder.setPayload(executionServiceInput.getPayload());
173             builder.setStatus(Status.newBuilder().setCode(500).setMessage("failure")
174                 .setErrorMessage("failed to get  get cba file name(" + actionIdentifiers.getBlueprintName()
175                     + "), version(" + actionIdentifiers.getBlueprintVersion() + ") from db : file check failed.")
176                 .setEventType(EventType.EVENT_COMPONENT_FAILURE).setTimestamp(Instant.now().toString()));
177         } else {
178             LOGGER.debug("Returning response from CDS Simulator: {}", responseString);
179             JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder);
180         }
181         return builder;
182     }
183
184     @Override
185     public void run() {
186         try {
187             server.awaitTermination();
188         } catch (InterruptedException e) {
189             LOGGER.info("gRPC server is terminated");
190             Thread.currentThread().interrupt();
191         }
192     }
193 }