2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
25 import com.google.gson.Gson;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.atomic.AtomicReference;
28 import javax.inject.Inject;
29 import javax.inject.Provider;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.core.Response;
34 import lombok.AccessLevel;
37 import org.glassfish.grizzly.http.server.Request;
38 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
39 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
40 import org.slf4j.ext.XLogger;
41 import org.slf4j.ext.XLoggerFactory;
44 * This class is the REST end point for event simulator REST calls.
47 public class EventGeneratorEndpoint {
49 // Get a reference to the logger
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGeneratorEndpoint.class);
52 // Parameters for event generation
53 private static AtomicReference<EventGeneratorParameters> parameters = new AtomicReference<>(
54 new EventGeneratorParameters());
56 // The map of event batches sent in the test
57 private static ConcurrentHashMap<Integer, EventBatch> batchMap = new ConcurrentHashMap<>();
59 // Flag indicating that event processing has finished
60 @Getter(AccessLevel.PROTECTED)
61 @Setter(AccessLevel.PROTECTED)
62 private static boolean finished = false;
64 // The current HTTP request
65 private final Provider<Request> httpRequest;
68 * Inject the HTTP request with a constructor.
70 * @param httpRequest the current request
73 public EventGeneratorEndpoint(final Provider<Request> httpRequest) {
74 this.httpRequest = httpRequest;
78 * Set the parameters for the end point.
80 * @param incomingParameters the new parameters
82 public static void setParameters(EventGeneratorParameters incomingParameters) {
83 parameters.set(incomingParameters);
87 * Get event generator statistics.
89 * @return the response
93 public Response serviceGetStats() {
94 return Response.status(200).entity(new EventGeneratorStats(batchMap).getStatsAsJsonString()).build();
98 * Generate a single event.
104 public Response getEvents() {
105 ThreadUtilities.sleep(parameters.get().getDelayBetweenBatches());
107 // Check if event generation is finished
109 return Response.status(204).build();
112 // A batch count of 0 means to continue to handle events for ever
113 if (parameters.get().getBatchCount() > 0 && batchMap.size() >= parameters.get().getBatchCount()) {
115 return Response.status(204).build();
118 EventBatch batch = new EventBatch(parameters.get().getBatchSize(), getApexClient());
119 batchMap.put(batch.getBatchNumber(), batch);
121 return Response.status(200).entity(batch.getBatchAsJsonString()).build();
125 * Get a single response to an event.
127 * @param jsonString the json string
128 * @return the response
132 public Response postEventResponse(final String jsonString) {
133 final OutputEvent outputEvent = new Gson().fromJson(jsonString, OutputEvent.class);
135 EventBatch batch = batchMap.get(outputEvent.findBatchNumber());
138 String errorMessage = "no input event found for received output event " + outputEvent;
139 LOGGER.warn(errorMessage);
140 return Response.status(409).build();
143 batch.handleResponse(outputEvent);
144 return Response.status(200).build();
148 * Get the name, address, and port of the Apex client getting the events.
150 * @return the Apex client
152 private String getApexClient() {
153 return httpRequest.get().getRemoteHost() + '(' + httpRequest.get().getRemoteAddr() + "):" + httpRequest.get()
158 * Get event generation statistics.
160 * @return the statistics on event generation
162 protected static String getEventGenerationStats() {
163 return new EventGeneratorStats(batchMap).getStatsAsJsonString();
167 * Clear event generation statistics.
169 protected static void clearEventGenerationStats() {