86531597a5f1443af95931c9e7ed6c3f03da13f8
[policy/clamp.git] /
1 /*-
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
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.clamp.controlloop.runtime.main.rest;
22
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;
47
48 /**
49  * This class handles REST endpoints for CL Statistics monitoring.
50  */
51 @RestController
52 public class MonitoringQueryController extends AbstractRestController {
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringQueryController.class);
55     private final MonitoringProvider provider;
56
57     /**
58      * Create Monitoring Controller.
59      *
60      * @param provider the MonitoringProvider
61      */
62     public MonitoringQueryController(MonitoringProvider provider) {
63         this.provider = provider;
64     }
65
66     /**
67      * Queries details of control loop participants statistics.
68      *
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
76      */
77     // @formatter:off
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,
83         tags = {
84             "Clamp control loop Monitoring API"
85         },
86         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
87         responseHeaders = {
88             @ResponseHeader(
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)},
97         extensions = {
98             @Extension
99                 (
100                     name = EXTENSION_NAME,
101                     properties = {
102                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
103                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
104                     }
105                 )
106         }
107     )
108     @ApiResponses(
109         value = {
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)
113         }
114     )
115     // @formatter:on
116     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatistics(
117             @RequestHeader(
118                     name = REQUEST_ID_NAME,
119                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
120             @ApiParam(value = "Control Loop participant name") @RequestParam(
121                     value = "name",
122                     required = false) final String name,
123             @ApiParam(value = "Control Loop participant version", required = false) @RequestParam(
124                     value = "version",
125                     required = false) final String version,
126             @ApiParam(value = "Record count", required = false) @RequestParam(
127                     value = "recordCount",
128                     required = false,
129                     defaultValue = "0") final int recordCount,
130             @ApiParam(value = "start time", required = false) @RequestParam(
131                     value = "startTime",
132                     required = false) final String startTime,
133             @ApiParam(value = "end time", required = false) @RequestParam(
134                     value = "endTime",
135                     required = false) final String endTime) {
136
137         try {
138             Instant startTimestamp = null;
139             Instant endTimestamp = null;
140
141             if (startTime != null) {
142                 startTimestamp = Instant.parse(startTime);
143             }
144             if (endTime != null) {
145                 endTimestamp = Instant.parse(endTime);
146             }
147             return ResponseEntity.ok().body(provider.fetchFilteredParticipantStatistics(name, version, recordCount,
148                     startTimestamp, endTimestamp));
149
150         } catch (PfModelRuntimeException e) {
151             LOGGER.warn("Monitoring of participants statistics failed", e);
152             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
153         }
154
155     }
156
157     /**
158      * Queries details of all participant statistics per control loop.
159      *
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
164      */
165     // @formatter:off
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,
171         tags = {
172             "Clamp control loop Monitoring API"
173         },
174         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
175         responseHeaders = {
176             @ResponseHeader(
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)},
185         extensions = {
186             @Extension
187                 (
188                     name = EXTENSION_NAME,
189                     properties = {
190                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
191                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
192                     }
193                 )
194         })
195     @ApiResponses(
196         value = {
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)
200         }
201     )
202     // @formatter:on
203     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatisticsPerControlLoop(
204             @RequestHeader(
205                     name = REQUEST_ID_NAME,
206                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
207             @ApiParam(value = "Control Loop name", required = true) @RequestParam(
208                     value = "name",
209                     required = false) final String name,
210             @ApiParam(value = "Control Loop version", required = true) @RequestParam(
211                     value = "version",
212                     required = false) final String version) {
213
214         try {
215             return ResponseEntity.ok().body(provider.fetchParticipantStatsPerControlLoop(name, version));
216
217         } catch (PfModelRuntimeException e) {
218             LOGGER.warn("Monitoring of Cl participant statistics failed", e);
219             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
220         }
221
222     }
223
224     /**
225      * Queries details of all control loop element statistics per control loop.
226      *
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
231      */
232     // @formatter:off
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,
238         tags = {
239             "Clamp control loop Monitoring API"
240         },
241         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
242         responseHeaders = {
243             @ResponseHeader(
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)},
252         extensions = {
253             @Extension
254                 (
255                     name = EXTENSION_NAME,
256                     properties = {
257                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
258                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
259                     }
260                 )
261         })
262     @ApiResponses(
263         value = {
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)
267         }
268     )
269     // @formatter:on
270     public ResponseEntity<ClElementStatisticsList> queryElementStatisticsPerControlLoop(
271             @RequestHeader(
272                     name = REQUEST_ID_NAME,
273                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
274             @ApiParam(value = "Control Loop name", required = true) @RequestParam(
275                     value = "name",
276                     required = false) final String name,
277             @ApiParam(value = "Control Loop version", required = true) @RequestParam(
278                     value = "version",
279                     required = false) final String version) {
280
281         try {
282             return ResponseEntity.ok().body(provider.fetchClElementStatsPerControlLoop(name, version));
283
284         } catch (PfModelRuntimeException e) {
285             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
286             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
287         }
288
289     }
290
291     /**
292      * Queries details of all control loop element statistics per control loop.
293      *
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
302      */
303     // @formatter:off
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,
309         tags = {
310             "Clamp control loop Monitoring API"
311         },
312         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
313         responseHeaders = {
314             @ResponseHeader(
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)},
323         extensions = {
324             @Extension
325                 (
326                     name = EXTENSION_NAME,
327                     properties = {
328                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
329                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
330                     }
331                 )
332         })
333     @ApiResponses(
334         value = {
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)
338         }
339     )
340     // @formatter:on
341     public ResponseEntity<ClElementStatisticsList> queryElementStatistics(
342             @RequestHeader(
343                     name = REQUEST_ID_NAME,
344                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
345             @ApiParam(value = "Participant name", required = true) @RequestParam(
346                     value = "name",
347                     required = false) final String name,
348             @ApiParam(value = "Participant version", required = true) @RequestParam(
349                     value = "version",
350                     required = false) final String version,
351             @ApiParam(value = "Record count", required = false) @RequestParam(
352                     value = "recordCount",
353                     required = false,
354                     defaultValue = "0") final int recordCount,
355             @ApiParam(value = "Control Loop element id", required = false) @RequestParam(
356                     value = "id",
357                     required = false) final String id,
358             @ApiParam(value = "start time", required = false) @RequestParam(
359                     value = "startTime",
360                     required = false) final String startTime,
361             @ApiParam(value = "end time", required = false) @RequestParam(
362                     value = "endTime",
363                     required = false) final String endTime) {
364
365         try {
366             Instant startTimestamp = null;
367             Instant endTimestamp = null;
368
369             if (startTime != null) {
370                 startTimestamp = Instant.parse(startTime);
371             }
372             if (endTime != null) {
373                 endTimestamp = Instant.parse(endTime);
374             }
375             return ResponseEntity.ok().body(provider.fetchFilteredClElementStatistics(name, version, id, startTimestamp,
376                     endTimestamp, recordCount));
377
378         } catch (PfModelRuntimeException | PfModelException e) {
379             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
380             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
381         }
382
383     }
384
385 }