Replace non-Javadoc comments with inheritDocs
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / main / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventGenerator.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.util.Arrays;
26
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;
35
36 /**
37  * This class is the main class of a REST server that generates sample events.
38  */
39 public class EventGenerator {
40     // Get a reference to the logger
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGenerator.class);
42
43     // Parameters for event generation
44     private final EventGeneratorParameters parameters;
45
46     // The HTTP server we are running
47     private final HttpServer eventGeneratorServer;
48
49     /**
50      * Instantiates a new event generator with the given parameters.
51      *
52      * @param parameters the parameters for the event generator
53      */
54     public EventGenerator(final EventGeneratorParameters parameters) {
55         this.parameters = parameters;
56
57         // Set the parameters in the event generator endpoint
58         EventGeneratorEndpoint.clearEventGenerationStats();
59         EventGeneratorEndpoint.setParameters(parameters);
60
61         // Add a shutdown hook to shut down the rest services when the process is exiting
62         Runtime.getRuntime().addShutdownHook(new Thread(new EventGeneratorShutdownHook()));
63
64         LOGGER.info("Event generator REST server starting");
65
66         final ResourceConfig rc = new ResourceConfig(EventGeneratorEndpoint.class);
67         eventGeneratorServer = GrizzlyHttpServerFactory.createHttpServer(getBaseUri(), rc);
68
69         // Wait for the HTTP server to come up
70         while (!eventGeneratorServer.isStarted()) {
71             ThreadUtilities.sleep(50);
72         }
73
74         LOGGER.info("Event generator REST server started");
75     }
76
77     /**
78      * Get the current event generation statistics.
79      *
80      * @return the statistics as a JSON string
81      */
82     public String getEventGenerationStats() {
83         return EventGeneratorEndpoint.getEventGenerationStats();
84     }
85
86     /**
87      * Check if event generation is finished.
88      *
89      * @return true if event generation is finished
90      */
91     public boolean isFinished() {
92         return EventGeneratorEndpoint.isFinished();
93     }
94
95     /**
96      * Tear down the event generator.
97      */
98     public void tearDown() {
99         LOGGER.info("Event generator shutting down");
100
101         eventGeneratorServer.shutdown();
102
103         if (parameters.getOutFile() != null) {
104             try {
105                 TextFileUtils.putStringAsTextFile(getEventGenerationStats(), parameters.getOutFile());
106             }
107             catch (IOException ioe) {
108                 LOGGER.warn("could not output statistics to file \"" + parameters.getOutFile() + "\"", ioe);
109             }
110         }
111
112         LOGGER.info("Event generator shut down");
113     }
114
115     /**
116      * Get the base URI for the server.
117      *
118      * @return the base URI
119      */
120     private URI getBaseUri() {
121         String baseUri = "http://" + parameters.getHost() + ':' + parameters.getPort() + '/' + "/EventGenerator";
122         return URI.create(baseUri);
123     }
124
125     /**
126      * This class is a shutdown hook for the Apex editor command.
127      */
128     private class EventGeneratorShutdownHook implements Runnable {
129         /**
130          * {@inheritDoc}.
131          */
132         @Override
133         public void run() {
134             tearDown();
135         }
136     }
137
138     /**
139      * The main method.
140      *
141      * @param args the arguments
142      * @throws Exception the exception
143      */
144     public static void main(final String[] args) {
145         LOGGER.info("Starting event generator with arguments: " + Arrays.toString(args));
146
147         EventGeneratorParameterHandler parameterHandler = new EventGeneratorParameterHandler();
148
149         EventGeneratorParameters parameters = null;
150
151         try {
152             parameters = parameterHandler.parse(args);
153         }
154         catch (ParseException pe) {
155             LOGGER.trace("Event generator start exception", pe);
156             LOGGER.info("Start of event generator failed: {}", pe.getMessage());
157             return;
158         }
159
160         // Null parameters means we print help
161         if (parameters == null) {
162             LOGGER.info(parameterHandler.getHelp(EventGenerator.class.getName()));
163             return;
164         }
165
166         // Start the event generator
167         EventGenerator eventGenerator = new EventGenerator(parameters);
168         LOGGER.info("Event generator started");
169
170         // Wait for event generation to finish
171         while (!eventGenerator.isFinished()) {
172             ThreadUtilities.sleep(200);
173         }
174
175
176         // Shut down the server
177         eventGenerator.tearDown();
178
179         LOGGER.info("Event generator statistics\n" + eventGenerator.getEventGenerationStats());
180
181         LOGGER.info("Event generator finished");
182     }
183 }