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 java.io.IOException;
27 import java.nio.file.InvalidPathException;
28 import java.util.Arrays;
29 import org.apache.commons.cli.ParseException;
30 import org.glassfish.grizzly.http.server.HttpServer;
31 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
32 import org.glassfish.jersey.server.ResourceConfig;
33 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
34 import org.onap.policy.common.utils.resources.TextFileUtils;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
39 * This class is the main class of a REST server that generates sample events.
41 public class EventGenerator {
42 // Get a reference to the logger
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGenerator.class);
45 // Parameters for event generation
46 private final EventGeneratorParameters parameters;
48 // The HTTP server we are running
49 private final HttpServer eventGeneratorServer;
52 * Instantiates a new event generator with the given parameters.
54 * @param parameters the parameters for the event generator
56 public EventGenerator(final EventGeneratorParameters parameters) {
57 this.parameters = parameters;
59 // Set the parameters in the event generator endpoint
60 EventGeneratorEndpoint.clearEventGenerationStats();
61 EventGeneratorEndpoint.setParameters(parameters);
63 // Add a shutdown hook to shut down the rest services when the process is exiting
64 Runtime.getRuntime().addShutdownHook(new Thread(new EventGeneratorShutdownHook()));
66 LOGGER.info("Event generator REST server starting");
68 final ResourceConfig rc = new ResourceConfig(EventGeneratorEndpoint.class);
69 eventGeneratorServer = GrizzlyHttpServerFactory.createHttpServer(getBaseUri(), rc);
71 // Wait for the HTTP server to come up
72 while (!eventGeneratorServer.isStarted()) {
73 ThreadUtilities.sleep(50);
76 LOGGER.info("Event generator REST server started");
80 * Get the current event generation statistics.
82 * @return the statistics as a JSON string
84 public String getEventGenerationStats() {
85 return EventGeneratorEndpoint.getEventGenerationStats();
89 * Check if event generation is finished.
91 * @return true if event generation is finished
93 public boolean isFinished() {
94 return EventGeneratorEndpoint.isFinished();
98 * Tear down the event generator.
100 public void tearDown() {
101 LOGGER.info("Event generator shutting down");
103 eventGeneratorServer.shutdown();
105 if (parameters.getOutFile() != null) {
107 TextFileUtils.putStringAsTextFile(getEventGenerationStats(), parameters.getOutFile());
108 } catch (IOException | InvalidPathException ioe) {
109 LOGGER.warn("could not output statistics to file \"" + parameters.getOutFile() + "\"", ioe);
113 LOGGER.info("Event generator shut down");
117 * Get the base URI for the server.
119 * @return the base URI
121 private URI getBaseUri() {
122 String baseUri = "http://" + parameters.getHost() + ':' + parameters.getPort() + '/' + "/EventGenerator";
123 return URI.create(baseUri);
127 * This class is a shutdown hook for the Apex editor command.
129 private class EventGeneratorShutdownHook implements Runnable {
142 * @param args the arguments
143 * @throws Exception the exception
145 public static void main(final String[] args) {
146 if (LOGGER.isInfoEnabled()) {
147 LOGGER.info("Starting event generator with arguments: {}", Arrays.toString(args));
150 EventGeneratorParameterHandler parameterHandler = new EventGeneratorParameterHandler();
152 EventGeneratorParameters parameters = null;
155 parameters = parameterHandler.parse(args);
156 } catch (ParseException pe) {
157 LOGGER.trace("Event generator start exception", pe);
158 LOGGER.info("Start of event generator failed: {}", pe.getMessage());
162 // Null parameters means we print help
163 if (parameters == null) {
164 LOGGER.info(parameterHandler.getHelp(EventGenerator.class.getName()));
168 // Start the event generator
169 EventGenerator eventGenerator = new EventGenerator(parameters);
170 LOGGER.info("Event generator started");
172 // Wait for event generation to finish
173 while (!eventGenerator.isFinished()) {
174 ThreadUtilities.sleep(200);
177 // Shut down the server
178 eventGenerator.tearDown();
180 LOGGER.info("Event generator statistics\n" + eventGenerator.getEventGenerationStats());
182 LOGGER.info("Event generator finished");