2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
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.clamp.controlloop.runtime.monitoring.rest;
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.time.Instant;
32 import java.util.UUID;
33 import javax.ws.rs.DefaultValue;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.core.Response;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
41 import org.onap.policy.clamp.controlloop.runtime.main.rest.RestController;
42 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringHandler;
43 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * This class handles REST endpoints for CL Statistics monitoring.
52 public class MonitoringQueryController extends RestController {
54 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringQueryController.class);
55 private final MonitoringProvider provider;
58 * Create Monitoring Controller.
60 public MonitoringQueryController() {
61 this.provider = MonitoringHandler.getInstance().getMonitoringProvider();
66 * Queries details of control loop participants statistics.
68 * @param requestId request ID used in ONAP logging
69 * @param name the name of the participant to get, null for all participants statistics
70 * @param version the version of the participant to get, null for all participants with the given name
71 * @param recordCount the record count to be fetched
72 * @param startTime the time from which to get statistics
73 * @param endTime the time to which to get statistics
74 * @return the participant statistics
78 @Path("/monitoring/participant")
79 @ApiOperation(value = "Query details of the requested participant stats",
80 notes = "Queries details of the requested participant stats, returning all participant stats",
81 response = ParticipantStatisticsList.class,
83 "Clamp control loop Monitoring API"
85 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
88 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
89 response = String.class),
90 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
91 response = String.class),
92 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
93 response = String.class),
94 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
95 response = UUID.class)},
99 name = EXTENSION_NAME,
101 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
102 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
109 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
110 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
111 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
115 public Response queryParticipantStatistics(@HeaderParam(REQUEST_ID_NAME)
116 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
117 @ApiParam(value = "Control Loop participant name", required = true)
118 @QueryParam("name") final String name,
119 @ApiParam(value = "Control Loop participant version", required = true)
120 @QueryParam("version") final String version,
121 @ApiParam(value = "Record count", required = false) @DefaultValue("0")
122 @QueryParam("recordCount") final int recordCount,
123 @ApiParam(value = "start time", required = false)
124 @QueryParam("startTime") final String startTime,
125 @ApiParam(value = "end time", required = false)
126 @QueryParam("endTime") final String endTime) {
129 Instant startTimestamp = null;
130 Instant endTimestamp = null;
132 if (startTime != null) {
133 startTimestamp = Instant.parse(startTime);
135 if (endTime != null) {
136 endTimestamp = Instant.parse(endTime);
138 ParticipantStatisticsList response = provider.fetchFilteredParticipantStatistics(name, version, recordCount,
139 startTimestamp, endTimestamp);
140 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
144 } catch (PfModelRuntimeException e) {
145 LOGGER.warn("Monitoring of participants statistics failed", e);
146 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
153 * Queries details of all participant statistics per control loop.
155 * @param requestId request ID used in ONAP logging
156 * @param name the name of the control loop
157 * @param version version of the control loop
158 * @return the control loop element statistics
162 @Path("/monitoring/participants/controlloop")
163 @ApiOperation(value = "Query details of all the participant stats in a control loop",
164 notes = "Queries details of the participant stats, returning all participant stats",
165 response = ClElementStatisticsList.class,
167 "Clamp control loop Monitoring API"
169 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
172 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
173 response = String.class),
174 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
175 response = String.class),
176 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
177 response = String.class),
178 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
179 response = UUID.class)},
183 name = EXTENSION_NAME,
185 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
186 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
192 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
193 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
194 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
198 public Response queryParticipantStatisticsPerControlLoop(@HeaderParam(REQUEST_ID_NAME)
199 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
200 @ApiParam(value = "Control Loop name", required = true)
201 @QueryParam("name") final String name,
202 @ApiParam(value = "Control Loop version", required = true)
203 @QueryParam("version") final String version) {
206 ParticipantStatisticsList response = provider.fetchParticipantStatsPerControlLoop(name, version);
207 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
211 } catch (PfModelRuntimeException e) {
212 LOGGER.warn("Monitoring of Cl participant statistics failed", e);
213 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
222 * Queries details of all control loop element statistics per control loop.
224 * @param requestId request ID used in ONAP logging
225 * @param name the name of the control loop
226 * @param version version of the control loop
227 * @return the control loop element statistics
231 @Path("/monitoring/clelements/controlloop")
232 @ApiOperation(value = "Query details of the requested cl element stats in a control loop",
233 notes = "Queries details of the requested cl element stats, returning all clElement stats",
234 response = ClElementStatisticsList.class,
236 "Clamp control loop Monitoring API"
238 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
241 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
242 response = String.class),
243 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
244 response = String.class),
245 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
246 response = String.class),
247 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
248 response = UUID.class)},
252 name = EXTENSION_NAME,
254 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
255 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
261 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
262 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
263 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
267 public Response queryElementStatisticsPerControlLoop(@HeaderParam(REQUEST_ID_NAME)
268 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
269 @ApiParam(value = "Control Loop name", required = true)
270 @QueryParam("name") final String name,
271 @ApiParam(value = "Control Loop version", required = true)
272 @QueryParam("version") final String version) {
275 ClElementStatisticsList response = provider.fetchClElementStatsPerControlLoop(name, version);
276 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
280 } catch (PfModelRuntimeException e) {
281 LOGGER.warn("Monitoring of Cl Element statistics failed", e);
282 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
292 * Queries details of all control loop element statistics per control loop.
294 * @param requestId request ID used in ONAP logging
295 * @param name the name of the control loop
296 * @param version version of the control loop
297 * @param id Id of the control loop element
298 * @param recordCount the record count to be fetched
299 * @param startTime the time from which to get statistics
300 * @param endTime the time to which to get statistics
301 * @return the control loop element statistics
305 @Path("/monitoring/clelement")
306 @ApiOperation(value = "Query details of the requested cl element stats",
307 notes = "Queries details of the requested cl element stats, returning all clElement stats",
308 response = ClElementStatisticsList.class,
310 "Clamp control loop Monitoring API"
312 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
315 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
316 response = String.class),
317 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
318 response = String.class),
319 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
320 response = String.class),
321 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
322 response = UUID.class)},
326 name = EXTENSION_NAME,
328 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
329 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
335 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
336 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
337 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
341 public Response queryElementStatistics(@HeaderParam(REQUEST_ID_NAME)
342 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
343 @ApiParam(value = "Participant name", required = true)
344 @QueryParam("name") final String name,
345 @ApiParam(value = "Participant version", required = true)
346 @QueryParam("version") final String version,
347 @ApiParam(value = "Record count", required = false)
348 @DefaultValue("0") @QueryParam("recordCount") final int recordCount,
349 @ApiParam(value = "Control Loop element id", required = false)
350 @QueryParam("id") final String id,
351 @ApiParam(value = "start time", required = false)
352 @QueryParam("startTime") final String startTime,
353 @ApiParam(value = "end time", required = false)
354 @QueryParam("endTime") final String endTime) {
357 Instant startTimestamp = null;
358 Instant endTimestamp = null;
360 if (startTime != null) {
361 startTimestamp = Instant.parse(startTime);
363 if (endTime != null) {
364 endTimestamp = Instant.parse(endTime);
366 ClElementStatisticsList response = provider.fetchFilteredClElementStatistics(name, version, id,
367 startTimestamp, endTimestamp, recordCount);
368 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
372 } catch (PfModelRuntimeException | PfModelException e) {
373 LOGGER.warn("Monitoring of Cl Element statistics failed", e);
374 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),