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 java.io.IOException;
25 import java.util.Arrays;
27 import org.apache.commons.cli.ParseException;
28 import org.glassfish.grizzly.http.server.HttpServer;
29 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.model.utilities.TextFileUtils;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * This class is the main class of a REST server that generates sample events.
39 public class EventGenerator {
40 // Get a reference to the logger
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGenerator.class);
43 // Parameters for event generation
44 private final EventGeneratorParameters parameters;
46 // The HTTP server we are running
47 private final HttpServer eventGeneratorServer;
50 * Instantiates a new event generator with the given parameters.
52 * @param parameters the parameters for the event generator
54 public EventGenerator(final EventGeneratorParameters parameters) {
55 this.parameters = parameters;
57 // Set the parameters in the event generator endpoint
58 EventGeneratorEndpoint.clearEventGenerationStats();
59 EventGeneratorEndpoint.setParameters(parameters);
61 // Add a shutdown hook to shut down the rest services when the process is exiting
62 Runtime.getRuntime().addShutdownHook(new Thread(new EventGeneratorShutdownHook()));
64 LOGGER.info("Event generator REST server starting");
66 final ResourceConfig rc = new ResourceConfig(EventGeneratorEndpoint.class);
67 eventGeneratorServer = GrizzlyHttpServerFactory.createHttpServer(getBaseUri(), rc);
69 // Wait for the HTTP server to come up
70 while (!eventGeneratorServer.isStarted()) {
71 ThreadUtilities.sleep(50);
74 LOGGER.info("Event generator REST server started");
78 * Get the current event generation statistics.
80 * @return the statistics as a JSON string
82 public String getEventGenerationStats() {
83 return EventGeneratorEndpoint.getEventGenerationStats();
87 * Check if event generation is finished.
89 * @return true if event generation is finished
91 public boolean isFinished() {
92 return EventGeneratorEndpoint.isFinished();
96 * Tear down the event generator.
98 public void tearDown() {
99 LOGGER.info("Event generator shutting down");
101 eventGeneratorServer.shutdown();
103 if (parameters.getOutFile() != null) {
105 TextFileUtils.putStringAsTextFile(getEventGenerationStats(), parameters.getOutFile());
107 catch (IOException ioe) {
108 LOGGER.warn("could not output statistics to file \"" + parameters.getOutFile() + "\"", ioe);
112 LOGGER.info("Event generator shut down");
116 * Get the base URI for the server.
118 * @return the base URI
120 private URI getBaseUri() {
121 String baseUri = "http://" + parameters.getHost() + ':' + parameters.getPort() + '/' + "/EventGenerator";
122 return URI.create(baseUri);
126 * This class is a shutdown hook for the Apex editor command.
128 private class EventGeneratorShutdownHook implements Runnable {
141 * @param args the arguments
142 * @throws Exception the exception
144 public static void main(final String[] args) {
145 LOGGER.info("Starting event generator with arguments: " + Arrays.toString(args));
147 EventGeneratorParameterHandler parameterHandler = new EventGeneratorParameterHandler();
149 EventGeneratorParameters parameters = null;
152 parameters = parameterHandler.parse(args);
154 catch (ParseException pe) {
155 LOGGER.trace("Event generator start exception", pe);
156 LOGGER.info("Start of event generator failed: {}", pe.getMessage());
160 // Null parameters means we print help
161 if (parameters == null) {
162 LOGGER.info(parameterHandler.getHelp(EventGenerator.class.getName()));
166 // Start the event generator
167 EventGenerator eventGenerator = new EventGenerator(parameters);
168 LOGGER.info("Event generator started");
170 // Wait for event generation to finish
171 while (!eventGenerator.isFinished()) {
172 ThreadUtilities.sleep(200);
176 // Shut down the server
177 eventGenerator.tearDown();
179 LOGGER.info("Event generator statistics\n" + eventGenerator.getEventGenerationStats());
181 LOGGER.info("Event generator finished");