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 final MonitoringProvider provider;
58 * Create Monitoring Controller.
60 * @param provider the MonitoringProvider
62 public MonitoringQueryController(MonitoringProvider provider) {
63 this.provider = provider;
67 * Queries details of control loop participants statistics.
69 * @param requestId request ID used in ONAP logging
70 * @param name the name of the participant to get, null for all participants statistics
71 * @param version the version of the participant to get, null for all participants with the given name
72 * @param recordCount the record count to be fetched
73 * @param startTime the time from which to get statistics
74 * @param endTime the time to which to get statistics
75 * @return the participant statistics
78 @GetMapping(value = "/monitoring/participant",
79 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
80 @ApiOperation(value = "Query details of the requested participant stats",
81 notes = "Queries details of the requested participant stats, returning all participant stats",
82 response = ParticipantStatisticsList.class,
84 "Clamp control loop Monitoring API"
86 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
89 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
90 response = String.class),
91 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
92 response = String.class),
93 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
94 response = String.class),
95 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
96 response = UUID.class)},
100 name = EXTENSION_NAME,
102 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
103 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
110 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
111 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
112 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
116 public ResponseEntity<ParticipantStatisticsList> queryParticipantStatistics(
118 name = REQUEST_ID_NAME,
119 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
120 @ApiParam(value = "Control Loop participant name") @RequestParam(
122 required = false) final String name,
123 @ApiParam(value = "Control Loop participant version", required = false) @RequestParam(
125 required = false) final String version,
126 @ApiParam(value = "Record count", required = false) @RequestParam(
127 value = "recordCount",
129 defaultValue = "0") final int recordCount,
130 @ApiParam(value = "start time", required = false) @RequestParam(
132 required = false) final String startTime,
133 @ApiParam(value = "end time", required = false) @RequestParam(
135 required = false) final String endTime) {
138 Instant startTimestamp = null;
139 Instant endTimestamp = null;
141 if (startTime != null) {
142 startTimestamp = Instant.parse(startTime);
144 if (endTime != null) {
145 endTimestamp = Instant.parse(endTime);
147 return ResponseEntity.ok().body(provider.fetchFilteredParticipantStatistics(name, version, recordCount,
148 startTimestamp, endTimestamp));
150 } catch (PfModelRuntimeException e) {
151 LOGGER.warn("Monitoring of participants statistics failed", e);
152 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
158 * Queries details of all participant statistics per control loop.
160 * @param requestId request ID used in ONAP logging
161 * @param name the name of the control loop
162 * @param version version of the control loop
163 * @return the control loop element statistics
166 @GetMapping(value = "/monitoring/participants/controlloop",
167 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
168 @ApiOperation(value = "Query details of all the participant stats in a control loop",
169 notes = "Queries details of the participant stats, returning all participant stats",
170 response = ClElementStatisticsList.class,
172 "Clamp control loop Monitoring API"
174 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
177 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
178 response = String.class),
179 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
180 response = String.class),
181 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
182 response = String.class),
183 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
184 response = UUID.class)},
188 name = EXTENSION_NAME,
190 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
191 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
197 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
198 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
199 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
203 public ResponseEntity<ParticipantStatisticsList> queryParticipantStatisticsPerControlLoop(
205 name = REQUEST_ID_NAME,
206 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
207 @ApiParam(value = "Control Loop name", required = true) @RequestParam(
209 required = false) final String name,
210 @ApiParam(value = "Control Loop version", required = true) @RequestParam(
212 required = false) final String version) {
215 return ResponseEntity.ok().body(provider.fetchParticipantStatsPerControlLoop(name, version));
217 } catch (PfModelRuntimeException e) {
218 LOGGER.warn("Monitoring of Cl participant statistics failed", e);
219 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
225 * Queries details of all control loop element statistics per control loop.
227 * @param requestId request ID used in ONAP logging
228 * @param name the name of the control loop
229 * @param version version of the control loop
230 * @return the control loop element statistics
233 @GetMapping(value = "/monitoring/clelements/controlloop",
234 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
235 @ApiOperation(value = "Query details of the requested cl element stats in a control loop",
236 notes = "Queries details of the requested cl element stats, returning all clElement stats",
237 response = ClElementStatisticsList.class,
239 "Clamp control loop Monitoring API"
241 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
244 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
245 response = String.class),
246 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
247 response = String.class),
248 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
249 response = String.class),
250 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
251 response = UUID.class)},
255 name = EXTENSION_NAME,
257 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
258 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
264 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
265 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
266 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
270 public ResponseEntity<ClElementStatisticsList> queryElementStatisticsPerControlLoop(
272 name = REQUEST_ID_NAME,
273 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
274 @ApiParam(value = "Control Loop name", required = true) @RequestParam(
276 required = false) final String name,
277 @ApiParam(value = "Control Loop version", required = true) @RequestParam(
279 required = false) final String version) {
282 return ResponseEntity.ok().body(provider.fetchClElementStatsPerControlLoop(name, version));
284 } catch (PfModelRuntimeException e) {
285 LOGGER.warn("Monitoring of Cl Element statistics failed", e);
286 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
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
304 @GetMapping(value = "/monitoring/clelement",
305 produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
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 ResponseEntity<ClElementStatisticsList> queryElementStatistics(
343 name = REQUEST_ID_NAME,
344 required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
345 @ApiParam(value = "Participant name", required = true) @RequestParam(
347 required = false) final String name,
348 @ApiParam(value = "Participant version", required = true) @RequestParam(
350 required = false) final String version,
351 @ApiParam(value = "Record count", required = false) @RequestParam(
352 value = "recordCount",
354 defaultValue = "0") final int recordCount,
355 @ApiParam(value = "Control Loop element id", required = false) @RequestParam(
357 required = false) final String id,
358 @ApiParam(value = "start time", required = false) @RequestParam(
360 required = false) final String startTime,
361 @ApiParam(value = "end time", required = false) @RequestParam(
363 required = false) final String endTime) {
366 Instant startTimestamp = null;
367 Instant endTimestamp = null;
369 if (startTime != null) {
370 startTimestamp = Instant.parse(startTime);
372 if (endTime != null) {
373 endTimestamp = Instant.parse(endTime);
375 return ResponseEntity.ok().body(provider.fetchFilteredClElementStatistics(name, version, id, startTimestamp,
376 endTimestamp, recordCount));
378 } catch (PfModelRuntimeException | PfModelException e) {
379 LOGGER.warn("Monitoring of Cl Element statistics failed", e);
380 return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();