ed624fb838008704db91ec0152f45da60bbaf689
[policy/apex-pdp.git] /
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 com.google.gson.Gson;
24
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import javax.inject.Inject;
28 import javax.inject.Provider;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.core.Response;
33
34 import org.glassfish.grizzly.http.server.Request;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
39
40 /**
41  * This class is the REST end point for event simulator REST calls.
42  */
43 @Path("/")
44 public class EventGeneratorEndpoint {
45     // Get a reference to the logger
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventGeneratorEndpoint.class);
47
48     // Parameters for event generation
49     private static EventGeneratorParameters parameters = new EventGeneratorParameters();
50
51     // The map of event batches sent in the test
52     private static ConcurrentHashMap<Integer, EventBatch> batchMap = new ConcurrentHashMap<>();
53
54     // Flag indicating that event processing has finished
55     private static boolean finished = false;
56
57     // The current HTTP request
58     private final Provider<Request> httpRequest;
59
60     /**
61      * Inject the HTTP request with a constructor.
62      * @param httpRequest the current request
63      */
64     @Inject
65     public EventGeneratorEndpoint(final Provider<Request> httpRequest) {
66         this.httpRequest = httpRequest;
67     }
68
69     /**
70      * Set the parameters for the end point.
71      *
72      * @param incomingParameters the new parameters
73      */
74     public static void setParameters(EventGeneratorParameters incomingParameters) {
75         synchronized (parameters) {
76             parameters = incomingParameters;
77         }
78     }
79
80     /**
81      * Get event generator statistics.
82      *
83      * @return the response
84      */
85     @Path("/Stats")
86     @GET
87     public Response serviceGetStats() {
88         return Response.status(200).entity(new EventGeneratorStats(batchMap).getStatsAsJsonString()).build();
89     }
90
91     /**
92      * Generate a single event.
93      *
94      * @return the event
95      */
96     @Path("/GetEvents")
97     @GET
98     public Response getEvents() {
99         ThreadUtilities.sleep(parameters.getDelayBetweenBatches());
100
101         // Check if event generation is finished
102         if (isFinished()) {
103             return Response.status(204).build();
104         }
105
106         // A batch count of 0 means to continue to handle events for ever
107         if (parameters.getBatchCount() > 0 && batchMap.size() >= parameters.getBatchCount()) {
108             setFinished(true);
109             return Response.status(204).build();
110         }
111
112         EventBatch batch = new EventBatch(parameters.getBatchSize(), getApexClient());
113         batchMap.put(batch.getBatchNumber(), batch);
114
115         return Response.status(200).entity(batch.getBatchAsJsonString()).build();
116     }
117
118     /**
119      * Get a single response to an event.
120      *
121      * @param jsonString the json string
122      * @return the response
123      */
124     @Path("/PostEvent")
125     @POST
126     public Response postEventResponse(final String jsonString) {
127         final OutputEvent outputEvent = new Gson().fromJson(jsonString, OutputEvent.class);
128
129         EventBatch batch = batchMap.get(outputEvent.findBatchNumber());
130
131         if (batch == null) {
132             String errorMessage = "no input event found for received output event " + outputEvent;
133             LOGGER.warn(errorMessage);
134             return Response.status(409).build();
135         }
136
137         batch.handleResponse(outputEvent);
138         return Response.status(200).build();
139     }
140
141     /**
142      * Get the name, address, and port of the Apex client getting the events.
143      *
144      * @return the Apex client
145      */
146     private String getApexClient() {
147         return httpRequest.get().getRemoteHost() + '(' + httpRequest.get().getRemoteAddr() + "):"
148                         + httpRequest.get().getRemotePort();
149     }
150
151     /**
152      * Get event generation statistics.
153      * @return the statistics on event generation
154      */
155     protected static String getEventGenerationStats() {
156         return new EventGeneratorStats(batchMap).getStatsAsJsonString();
157     }
158
159     /**
160      * Clear event generation statistics.
161      */
162     protected static void clearEventGenerationStats() {
163         batchMap.clear();
164     }
165
166     /**
167      * Check if event generation has finished.
168      * @return true if event generation has finished
169      */
170     protected static boolean isFinished() {
171         return finished;
172     }
173
174     protected static void setFinished(boolean finished) {
175         EventGeneratorEndpoint.finished = finished;
176     }
177 }