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.main.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 org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
35 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
36 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.RequestHeader;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
49 * This class handles REST endpoints for CL Statistics monitoring.
52 public class MonitoringQueryController extends AbstractRestController {
54 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringQueryController.class);
55 private static final String TAGS = "Clamp Control Loop Monitoring API";
56 private final MonitoringProvider provider;
59 * Create Monitoring Controller.
61 * @param provider the MonitoringProvider
63 public MonitoringQueryController(MonitoringProvider provider) {
64 this.provider = provider;
68 * Queries details of control loop participants statistics.
70 * @param requestId request ID used in ONAP logging
71 * @param name the name of the participant to get, null for all participants statistics
72 * @param version the version of the participant to get, null for all participants with the given name
73 * @param recordCount the record count to be fetched
74 * @param startTime the time from which to get statistics
75 * @param endTime the time to which to get statistics
76 * @return the participant statistics
79 @GetMapping(value = "/monitoring/participant",
80 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
81 @ApiOperation(value = "Query details of the requested participant stats",
82 notes = "Queries details of the requested participant stats, returning all participant stats",
83 response = ParticipantStatisticsList.class,
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 ResponseEntity<ParticipantStatisticsList> queryParticipantStatistics(
117 name = REQUEST_ID_NAME,
118 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
119 @ApiParam(value = "Control Loop participant name") @RequestParam(
121 required = false) final String name,
122 @ApiParam(value = "Control Loop participant version", required = false) @RequestParam(
124 required = false) final String version,
125 @ApiParam(value = "Record count", required = false) @RequestParam(
126 value = "recordCount",
128 defaultValue = "0") final int recordCount,
129 @ApiParam(value = "start time", required = false) @RequestParam(
131 required = false) final String startTime,
132 @ApiParam(value = "end time", required = false) @RequestParam(
134 required = false) final String endTime) {
137 Instant startTimestamp = null;
138 Instant endTimestamp = null;
140 if (startTime != null) {
141 startTimestamp = Instant.parse(startTime);
143 if (endTime != null) {
144 endTimestamp = Instant.parse(endTime);
146 return ResponseEntity.ok().body(provider.fetchFilteredParticipantStatistics(name, version, recordCount,
147 startTimestamp, endTimestamp));
149 } catch (PfModelRuntimeException e) {
150 LOGGER.warn("Monitoring of participants statistics failed", e);
151 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
157 * Queries details of all participant statistics per control loop.
159 * @param requestId request ID used in ONAP logging
160 * @param name the name of the control loop
161 * @param version version of the control loop
162 * @return the control loop element statistics
165 @GetMapping(value = "/monitoring/participants/controlloop",
166 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
167 @ApiOperation(value = "Query details of all the participant stats in a control loop",
168 notes = "Queries details of the participant stats, returning all participant stats",
169 response = ClElementStatisticsList.class,
171 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
174 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
175 response = String.class),
176 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
177 response = String.class),
178 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
179 response = String.class),
180 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
181 response = UUID.class)},
185 name = EXTENSION_NAME,
187 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
188 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
194 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
195 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
196 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
200 public ResponseEntity<ParticipantStatisticsList> queryParticipantStatisticsPerControlLoop(
202 name = REQUEST_ID_NAME,
203 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
204 @ApiParam(value = "Control Loop name", required = true) @RequestParam(
206 required = false) final String name,
207 @ApiParam(value = "Control Loop version", required = true) @RequestParam(
209 required = false) final String version) {
212 return ResponseEntity.ok().body(provider.fetchParticipantStatsPerControlLoop(name, version));
214 } catch (PfModelRuntimeException e) {
215 LOGGER.warn("Monitoring of Cl participant statistics failed", e);
216 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
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
230 @GetMapping(value = "/monitoring/clelements/controlloop",
231 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
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 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
239 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
240 response = String.class),
241 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
242 response = String.class),
243 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
244 response = String.class),
245 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
246 response = UUID.class)},
250 name = EXTENSION_NAME,
252 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
253 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
259 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
260 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
261 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
265 public ResponseEntity<ClElementStatisticsList> queryElementStatisticsPerControlLoop(
267 name = REQUEST_ID_NAME,
268 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
269 @ApiParam(value = "Control Loop name", required = true) @RequestParam(
271 required = false) final String name,
272 @ApiParam(value = "Control Loop version", required = true) @RequestParam(
274 required = false) final String version) {
277 return ResponseEntity.ok().body(provider.fetchClElementStatsPerControlLoop(name, version));
279 } catch (PfModelRuntimeException e) {
280 LOGGER.warn("Monitoring of Cl Element statistics failed", e);
281 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
287 * Queries details of all control loop element statistics per control loop.
289 * @param requestId request ID used in ONAP logging
290 * @param name the name of the control loop
291 * @param version version of the control loop
292 * @param id Id of the control loop element
293 * @param recordCount the record count to be fetched
294 * @param startTime the time from which to get statistics
295 * @param endTime the time to which to get statistics
296 * @return the control loop element statistics
299 @GetMapping(value = "/monitoring/clelement",
300 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
301 @ApiOperation(value = "Query details of the requested cl element stats",
302 notes = "Queries details of the requested cl element stats, returning all clElement stats",
303 response = ClElementStatisticsList.class,
305 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
308 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
309 response = String.class),
310 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
311 response = String.class),
312 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
313 response = String.class),
314 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
315 response = UUID.class)},
319 name = EXTENSION_NAME,
321 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
322 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
328 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
329 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
330 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
334 public ResponseEntity<ClElementStatisticsList> queryElementStatistics(
336 name = REQUEST_ID_NAME,
337 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
338 @ApiParam(value = "Participant name", required = true) @RequestParam(
340 required = false) final String name,
341 @ApiParam(value = "Participant version", required = true) @RequestParam(
343 required = false) final String version,
344 @ApiParam(value = "Record count", required = false) @RequestParam(
345 value = "recordCount",
347 defaultValue = "0") final int recordCount,
348 @ApiParam(value = "Control Loop element id", required = false) @RequestParam(
350 required = false) final String id,
351 @ApiParam(value = "start time", required = false) @RequestParam(
353 required = false) final String startTime,
354 @ApiParam(value = "end time", required = false) @RequestParam(
356 required = false) final String endTime) {
359 Instant startTimestamp = null;
360 Instant endTimestamp = null;
362 if (startTime != null) {
363 startTimestamp = Instant.parse(startTime);
365 if (endTime != null) {
366 endTimestamp = Instant.parse(endTime);
368 return ResponseEntity.ok().body(provider.fetchFilteredClElementStatistics(name, version, id, startTimestamp,
369 endTimestamp, recordCount));
371 } catch (PfModelRuntimeException | PfModelException e) {
372 LOGGER.warn("Monitoring of Cl Element statistics failed", e);
373 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();