7ac95003e56176108db7c0b8bd59081bbde154f1
[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 static final String TAGS = "Clamp Control Loop Monitoring API";
56     private final MonitoringProvider provider;
57
58     /**
59      * Create Monitoring Controller.
60      *
61      * @param provider the MonitoringProvider
62      */
63     public MonitoringQueryController(MonitoringProvider provider) {
64         this.provider = provider;
65     }
66
67     /**
68      * Queries details of control loop participants statistics.
69      *
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
77      */
78     // @formatter:off
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,
84         tags = {TAGS},
85         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
86         responseHeaders = {
87             @ResponseHeader(
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)},
96         extensions = {
97             @Extension
98                 (
99                     name = EXTENSION_NAME,
100                     properties = {
101                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
102                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
103                     }
104                 )
105         }
106     )
107     @ApiResponses(
108         value = {
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)
112         }
113     )
114     // @formatter:on
115     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatistics(
116             @RequestHeader(
117                     name = REQUEST_ID_NAME,
118                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
119             @ApiParam(value = "Control Loop participant name") @RequestParam(
120                     value = "name",
121                     required = false) final String name,
122             @ApiParam(value = "Control Loop participant version", required = false) @RequestParam(
123                     value = "version",
124                     required = false) final String version,
125             @ApiParam(value = "Record count", required = false) @RequestParam(
126                     value = "recordCount",
127                     required = false,
128                     defaultValue = "0") final int recordCount,
129             @ApiParam(value = "start time", required = false) @RequestParam(
130                     value = "startTime",
131                     required = false) final String startTime,
132             @ApiParam(value = "end time", required = false) @RequestParam(
133                     value = "endTime",
134                     required = false) final String endTime) {
135
136         try {
137             Instant startTimestamp = null;
138             Instant endTimestamp = null;
139
140             if (startTime != null) {
141                 startTimestamp = Instant.parse(startTime);
142             }
143             if (endTime != null) {
144                 endTimestamp = Instant.parse(endTime);
145             }
146             return ResponseEntity.ok().body(provider.fetchFilteredParticipantStatistics(name, version, recordCount,
147                     startTimestamp, endTimestamp));
148
149         } catch (PfModelRuntimeException e) {
150             LOGGER.warn("Monitoring of participants statistics failed", e);
151             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
152         }
153
154     }
155
156     /**
157      * Queries details of all participant statistics per control loop.
158      *
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
163      */
164     // @formatter:off
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,
170         tags = {TAGS},
171         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
172         responseHeaders = {
173             @ResponseHeader(
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)},
182         extensions = {
183             @Extension
184                 (
185                     name = EXTENSION_NAME,
186                     properties = {
187                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
188                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
189                     }
190                 )
191         })
192     @ApiResponses(
193         value = {
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)
197         }
198     )
199     // @formatter:on
200     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatisticsPerControlLoop(
201             @RequestHeader(
202                     name = REQUEST_ID_NAME,
203                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
204             @ApiParam(value = "Control Loop name", required = true) @RequestParam(
205                     value = "name",
206                     required = false) final String name,
207             @ApiParam(value = "Control Loop version", required = true) @RequestParam(
208                     value = "version",
209                     required = false) final String version) {
210
211         try {
212             return ResponseEntity.ok().body(provider.fetchParticipantStatsPerControlLoop(name, version));
213
214         } catch (PfModelRuntimeException e) {
215             LOGGER.warn("Monitoring of Cl participant statistics failed", e);
216             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
217         }
218
219     }
220
221     /**
222      * Queries details of all control loop element statistics per control loop.
223      *
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
228      */
229     // @formatter:off
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,
235         tags = {TAGS},
236         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
237         responseHeaders = {
238             @ResponseHeader(
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)},
247         extensions = {
248             @Extension
249                 (
250                     name = EXTENSION_NAME,
251                     properties = {
252                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
253                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
254                     }
255                 )
256         })
257     @ApiResponses(
258         value = {
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)
262         }
263     )
264     // @formatter:on
265     public ResponseEntity<ClElementStatisticsList> queryElementStatisticsPerControlLoop(
266             @RequestHeader(
267                     name = REQUEST_ID_NAME,
268                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
269             @ApiParam(value = "Control Loop name", required = true) @RequestParam(
270                     value = "name",
271                     required = false) final String name,
272             @ApiParam(value = "Control Loop version", required = true) @RequestParam(
273                     value = "version",
274                     required = false) final String version) {
275
276         try {
277             return ResponseEntity.ok().body(provider.fetchClElementStatsPerControlLoop(name, version));
278
279         } catch (PfModelRuntimeException e) {
280             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
281             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
282         }
283
284     }
285
286     /**
287      * Queries details of all control loop element statistics per control loop.
288      *
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
297      */
298     // @formatter:off
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,
304         tags = {TAGS},
305         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
306         responseHeaders = {
307             @ResponseHeader(
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)},
316         extensions = {
317             @Extension
318                 (
319                     name = EXTENSION_NAME,
320                     properties = {
321                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
322                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
323                     }
324                 )
325         })
326     @ApiResponses(
327         value = {
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)
331         }
332     )
333     // @formatter:on
334     public ResponseEntity<ClElementStatisticsList> queryElementStatistics(
335             @RequestHeader(
336                     name = REQUEST_ID_NAME,
337                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
338             @ApiParam(value = "Participant name", required = true) @RequestParam(
339                     value = "name",
340                     required = false) final String name,
341             @ApiParam(value = "Participant version", required = true) @RequestParam(
342                     value = "version",
343                     required = false) final String version,
344             @ApiParam(value = "Record count", required = false) @RequestParam(
345                     value = "recordCount",
346                     required = false,
347                     defaultValue = "0") final int recordCount,
348             @ApiParam(value = "Control Loop element id", required = false) @RequestParam(
349                     value = "id",
350                     required = false) final String id,
351             @ApiParam(value = "start time", required = false) @RequestParam(
352                     value = "startTime",
353                     required = false) final String startTime,
354             @ApiParam(value = "end time", required = false) @RequestParam(
355                     value = "endTime",
356                     required = false) final String endTime) {
357
358         try {
359             Instant startTimestamp = null;
360             Instant endTimestamp = null;
361
362             if (startTime != null) {
363                 startTimestamp = Instant.parse(startTime);
364             }
365             if (endTime != null) {
366                 endTimestamp = Instant.parse(endTime);
367             }
368             return ResponseEntity.ok().body(provider.fetchFilteredClElementStatistics(name, version, id, startTimestamp,
369                     endTimestamp, recordCount));
370
371         } catch (PfModelRuntimeException | PfModelException e) {
372             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
373             return ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode()).build();
374         }
375
376     }
377
378 }