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;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.atomic.AtomicReference;
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;
33 import org.glassfish.grizzly.http.server.Request;
34 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * This class is the REST end point for event simulator REST calls.
43 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 AtomicReference<EventGeneratorParameters> parameters = new AtomicReference<>(
50 new EventGeneratorParameters());
52 // The map of event batches sent in the test
53 private static ConcurrentHashMap<Integer, EventBatch> batchMap = new ConcurrentHashMap<>();
55 // Flag indicating that event processing has finished
56 private static boolean finished = false;
58 // The current HTTP request
59 private final Provider<Request> httpRequest;
62 * Inject the HTTP request with a constructor.
64 * @param httpRequest the current request
67 public EventGeneratorEndpoint(final Provider<Request> httpRequest) {
68 this.httpRequest = httpRequest;
72 * Set the parameters for the end point.
74 * @param incomingParameters the new parameters
76 public static void setParameters(EventGeneratorParameters incomingParameters) {
77 parameters.set(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.get().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.get().getBatchCount() > 0 && batchMap.size() >= parameters.get().getBatchCount()) {
109 return Response.status(204).build();
112 EventBatch batch = new EventBatch(parameters.get().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() + "):" + httpRequest.get()
152 * Get event generation statistics.
154 * @return the statistics on event generation
156 protected static String getEventGenerationStats() {
157 return new EventGeneratorStats(batchMap).getStatsAsJsonString();
161 * Clear event generation statistics.
163 protected static void clearEventGenerationStats() {
168 * Check if event generation has finished.
170 * @return true if event generation has finished
172 protected static boolean isFinished() {
176 protected static void setFinished(boolean finished) {
177 EventGeneratorEndpoint.finished = finished;