2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
23 import com.google.gson.Gson;
25 import java.util.concurrent.ConcurrentHashMap;
27 import javax.inject.Inject;
28 import javax.inject.Provider;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.core.Response;
34 import org.glassfish.grizzly.http.server.Request;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * This class is the REST end point for event simulator REST calls.
44 public class EventGeneratorEndpoint {
45 // Get a reference to the logger
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGeneratorEndpoint.class);
48 // Parameters for event generation
49 private static EventGeneratorParameters parameters = new EventGeneratorParameters();
51 // The map of event batches sent in the test
52 private static ConcurrentHashMap<Integer, EventBatch> batchMap = new ConcurrentHashMap<>();
54 // Flag indicating that event processing has finished
55 private static boolean finished = false;
57 // The current HTTP request
58 private final Provider<Request> httpRequest;
61 * Inject the HTTP request with a constructor.
62 * @param httpRequest the current request
65 public EventGeneratorEndpoint(final Provider<Request> httpRequest) {
66 this.httpRequest = httpRequest;
70 * Set the parameters for the end point.
72 * @param incomingParameters the new parameters
74 public static void setParameters(EventGeneratorParameters incomingParameters) {
75 synchronized (parameters) {
76 parameters = incomingParameters;
81 * Get event generator statistics.
83 * @return the response
87 public Response serviceGetStats() {
88 return Response.status(200).entity(new EventGeneratorStats(batchMap).getStatsAsJsonString()).build();
92 * Generate a single event.
98 public Response getEvents() {
99 ThreadUtilities.sleep(parameters.getDelayBetweenBatches());
101 // Check if event generation is finished
103 return Response.status(204).build();
106 // A batch count of 0 means to continue to handle events for ever
107 if (parameters.getBatchCount() > 0 && batchMap.size() >= parameters.getBatchCount()) {
109 return Response.status(204).build();
112 EventBatch batch = new EventBatch(parameters.getBatchSize(), getApexClient());
113 batchMap.put(batch.getBatchNumber(), batch);
115 return Response.status(200).entity(batch.getBatchAsJsonString()).build();
119 * Get a single response to an event.
121 * @param jsonString the json string
122 * @return the response
126 public Response postEventResponse(final String jsonString) {
127 final OutputEvent outputEvent = new Gson().fromJson(jsonString, OutputEvent.class);
129 EventBatch batch = batchMap.get(outputEvent.findBatchNumber());
132 String errorMessage = "no input event found for received output event " + outputEvent;
133 LOGGER.warn(errorMessage);
134 return Response.status(409).build();
137 batch.handleResponse(outputEvent);
138 return Response.status(200).build();
142 * Get the name, address, and port of the Apex client getting the events.
144 * @return the Apex client
146 private String getApexClient() {
147 return httpRequest.get().getRemoteHost() + '(' + httpRequest.get().getRemoteAddr() + "):"
148 + httpRequest.get().getRemotePort();
152 * Get event generation statistics.
153 * @return the statistics on event generation
155 protected static String getEventGenerationStats() {
156 return new EventGeneratorStats(batchMap).getStatsAsJsonString();
160 * Clear event generation statistics.
162 protected static void clearEventGenerationStats() {
167 * Check if event generation has finished.
168 * @return true if event generation has finished
170 protected static boolean isFinished() {
174 protected static void setFinished(boolean finished) {
175 EventGeneratorEndpoint.finished = finished;