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 java.io.IOException;
26 import java.nio.file.InvalidPathException;
27 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 LOGGER.info("Starting event generator with arguments: " + Arrays.toString(args));
148 EventGeneratorParameterHandler parameterHandler = new EventGeneratorParameterHandler();
150 EventGeneratorParameters parameters = null;
153 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);
175 // Shut down the server
176 eventGenerator.tearDown();
178 LOGGER.info("Event generator statistics\n" + eventGenerator.getEventGenerationStats());
180 LOGGER.info("Event generator finished");