2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
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.apex.testsuites.performance.benchmark.eventgenerator;
24 import com.google.gson.Gson;
26 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.atomic.AtomicReference;
29 import javax.inject.Inject;
30 import javax.inject.Provider;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.core.Response;
36 import org.glassfish.grizzly.http.server.Request;
37 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
38 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
43 * This class is the REST end point for event simulator REST calls.
46 public class EventGeneratorEndpoint {
48 // Get a reference to the logger
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGeneratorEndpoint.class);
51 // Parameters for event generation
52 private static AtomicReference<EventGeneratorParameters> parameters = new AtomicReference<>(
53 new EventGeneratorParameters());
55 // The map of event batches sent in the test
56 private static ConcurrentHashMap<Integer, EventBatch> batchMap = new ConcurrentHashMap<>();
58 // Flag indicating that event processing has finished
59 private static boolean finished = false;
61 // The current HTTP request
62 private final Provider<Request> httpRequest;
65 * Inject the HTTP request with a constructor.
67 * @param httpRequest the current request
70 public EventGeneratorEndpoint(final Provider<Request> httpRequest) {
71 this.httpRequest = httpRequest;
75 * Set the parameters for the end point.
77 * @param incomingParameters the new parameters
79 public static void setParameters(EventGeneratorParameters incomingParameters) {
80 parameters.set(incomingParameters);
84 * Get event generator statistics.
86 * @return the response
90 public Response serviceGetStats() {
91 return Response.status(200).entity(new EventGeneratorStats(batchMap).getStatsAsJsonString()).build();
95 * Generate a single event.
101 public Response getEvents() {
102 ThreadUtilities.sleep(parameters.get().getDelayBetweenBatches());
104 // Check if event generation is finished
106 return Response.status(204).build();
109 // A batch count of 0 means to continue to handle events for ever
110 if (parameters.get().getBatchCount() > 0 && batchMap.size() >= parameters.get().getBatchCount()) {
112 return Response.status(204).build();
115 EventBatch batch = new EventBatch(parameters.get().getBatchSize(), getApexClient());
116 batchMap.put(batch.getBatchNumber(), batch);
118 return Response.status(200).entity(batch.getBatchAsJsonString()).build();
122 * Get a single response to an event.
124 * @param jsonString the json string
125 * @return the response
129 public Response postEventResponse(final String jsonString) {
130 final OutputEvent outputEvent = new Gson().fromJson(jsonString, OutputEvent.class);
132 EventBatch batch = batchMap.get(outputEvent.findBatchNumber());
135 String errorMessage = "no input event found for received output event " + outputEvent;
136 LOGGER.warn(errorMessage);
137 return Response.status(409).build();
140 batch.handleResponse(outputEvent);
141 return Response.status(200).build();
145 * Get the name, address, and port of the Apex client getting the events.
147 * @return the Apex client
149 private String getApexClient() {
150 return httpRequest.get().getRemoteHost() + '(' + httpRequest.get().getRemoteAddr() + "):" + httpRequest.get()
155 * Get event generation statistics.
157 * @return the statistics on event generation
159 protected static String getEventGenerationStats() {
160 return new EventGeneratorStats(batchMap).getStatsAsJsonString();
164 * Clear event generation statistics.
166 protected static void clearEventGenerationStats() {
171 * Check if event generation has finished.
173 * @return true if event generation has finished
175 protected static boolean isFinished() {
179 protected static void setFinished(boolean finished) {
180 EventGeneratorEndpoint.finished = finished;