c4ce5bb056fda02567f9b95305a612d6fe66bdba
[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 lombok.RequiredArgsConstructor;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
36 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
37 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
38 import org.onap.policy.models.base.PfModelException;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.RequestHeader;
43 import org.springframework.web.bind.annotation.RequestParam;
44 import org.springframework.web.bind.annotation.RestController;
45
46 /**
47  * This class handles REST endpoints for CL Statistics monitoring.
48  */
49 @RestController
50 @RequiredArgsConstructor
51 public class MonitoringQueryController extends AbstractRestController {
52
53     private static final String TAGS = "Clamp Control Loop Monitoring API";
54     private final MonitoringProvider provider;
55
56     /**
57      * Queries details of control loop participants statistics.
58      *
59      * @param requestId request ID used in ONAP logging
60      * @param name the name of the participant to get, null for all participants statistics
61      * @param version the version of the participant to get, null for all participants with the given name
62      * @param recordCount the record count to be fetched
63      * @param startTime the time from which to get statistics
64      * @param endTime the time to which to get statistics
65      * @return the participant statistics
66      */
67     // @formatter:off
68     @GetMapping(value = "/monitoring/participant",
69             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
70     @ApiOperation(value = "Query details of the requested participant stats",
71         notes = "Queries details of the requested participant stats, returning all participant stats",
72         response = ParticipantStatisticsList.class,
73         tags = {TAGS},
74         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
75         responseHeaders = {
76             @ResponseHeader(
77                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
78                 response = String.class),
79             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
80                 response = String.class),
81             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
82                 response = String.class),
83             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
84                 response = UUID.class)},
85         extensions = {
86             @Extension
87                 (
88                     name = EXTENSION_NAME,
89                     properties = {
90                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
91                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
92                     }
93                 )
94         }
95     )
96     @ApiResponses(
97         value = {
98             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
99             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
100             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
101         }
102     )
103     // @formatter:on
104     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatistics(
105             @RequestHeader(
106                     name = REQUEST_ID_NAME,
107                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
108             @ApiParam(value = "Control Loop participant name") @RequestParam(
109                     value = "name",
110                     required = false) final String name,
111             @ApiParam(value = "Control Loop participant version", required = false) @RequestParam(
112                     value = "version",
113                     required = false) final String version,
114             @ApiParam(value = "Record count", required = false) @RequestParam(
115                     value = "recordCount",
116                     required = false,
117                     defaultValue = "0") final int recordCount,
118             @ApiParam(value = "start time", required = false) @RequestParam(
119                     value = "startTime",
120                     required = false) final String startTime,
121             @ApiParam(value = "end time", required = false) @RequestParam(
122                     value = "endTime",
123                     required = false) final String endTime) {
124
125         Instant startTimestamp = null;
126         Instant endTimestamp = null;
127
128         if (startTime != null) {
129             startTimestamp = Instant.parse(startTime);
130         }
131         if (endTime != null) {
132             endTimestamp = Instant.parse(endTime);
133         }
134         return ResponseEntity.ok().body(
135                 provider.fetchFilteredParticipantStatistics(name, version, recordCount, startTimestamp, endTimestamp));
136     }
137
138     /**
139      * Queries details of all participant statistics per control loop.
140      *
141      * @param requestId request ID used in ONAP logging
142      * @param name the name of the control loop
143      * @param version version of the control loop
144      * @return the control loop element statistics
145      */
146     // @formatter:off
147     @GetMapping(value = "/monitoring/participants/controlloop",
148             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
149     @ApiOperation(value = "Query details of all the participant stats in a control loop",
150         notes = "Queries details of the participant stats, returning all participant stats",
151         response = ClElementStatisticsList.class,
152         tags = {TAGS},
153         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
154         responseHeaders = {
155             @ResponseHeader(
156                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
157                 response = String.class),
158             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
159                 response = String.class),
160             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
161                 response = String.class),
162             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
163                 response = UUID.class)},
164         extensions = {
165             @Extension
166                 (
167                     name = EXTENSION_NAME,
168                     properties = {
169                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
170                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
171                     }
172                 )
173         })
174     @ApiResponses(
175         value = {
176             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
177             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
178             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
179         }
180     )
181     // @formatter:on
182     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatisticsPerControlLoop(
183             @RequestHeader(
184                     name = REQUEST_ID_NAME,
185                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
186             @ApiParam(value = "Control Loop name", required = true) @RequestParam(
187                     value = "name",
188                     required = false) final String name,
189             @ApiParam(value = "Control Loop version", required = true) @RequestParam(
190                     value = "version",
191                     required = false) final String version) {
192
193         return ResponseEntity.ok().body(provider.fetchParticipantStatsPerControlLoop(name, version));
194     }
195
196     /**
197      * Queries details of all control loop element statistics per control loop.
198      *
199      * @param requestId request ID used in ONAP logging
200      * @param name the name of the control loop
201      * @param version version of the control loop
202      * @return the control loop element statistics
203      */
204     // @formatter:off
205     @GetMapping(value = "/monitoring/clelements/controlloop",
206             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
207     @ApiOperation(value = "Query details of the requested cl element stats in a control loop",
208         notes = "Queries details of the requested cl element stats, returning all clElement stats",
209         response = ClElementStatisticsList.class,
210         tags = {TAGS},
211         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
212         responseHeaders = {
213             @ResponseHeader(
214                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
215                 response = String.class),
216             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
217                 response = String.class),
218             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
219                 response = String.class),
220             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
221                 response = UUID.class)},
222         extensions = {
223             @Extension
224                 (
225                     name = EXTENSION_NAME,
226                     properties = {
227                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
228                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
229                     }
230                 )
231         })
232     @ApiResponses(
233         value = {
234             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
235             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
236             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
237         }
238     )
239     // @formatter:on
240     public ResponseEntity<ClElementStatisticsList> queryElementStatisticsPerControlLoop(
241             @RequestHeader(
242                     name = REQUEST_ID_NAME,
243                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
244             @ApiParam(value = "Control Loop name", required = true) @RequestParam(
245                     value = "name",
246                     required = false) final String name,
247             @ApiParam(value = "Control Loop version", required = true) @RequestParam(
248                     value = "version",
249                     required = false) final String version) {
250
251         return ResponseEntity.ok().body(provider.fetchClElementStatsPerControlLoop(name, version));
252     }
253
254     /**
255      * Queries details of all control loop element statistics per control loop.
256      *
257      * @param requestId request ID used in ONAP logging
258      * @param name the name of the control loop
259      * @param version version of the control loop
260      * @param id Id of the control loop element
261      * @param recordCount the record count to be fetched
262      * @param startTime the time from which to get statistics
263      * @param endTime the time to which to get statistics
264      * @return the control loop element statistics
265      * @throws PfModelException on errors getting details of all control loop element statistics per control loop
266      */
267     // @formatter:off
268     @GetMapping(value = "/monitoring/clelement",
269             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
270     @ApiOperation(value = "Query details of the requested cl element stats",
271         notes = "Queries details of the requested cl element stats, returning all clElement stats",
272         response = ClElementStatisticsList.class,
273         tags = {TAGS},
274         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
275         responseHeaders = {
276             @ResponseHeader(
277                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
278                 response = String.class),
279             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
280                 response = String.class),
281             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
282                 response = String.class),
283             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
284                 response = UUID.class)},
285         extensions = {
286             @Extension
287                 (
288                     name = EXTENSION_NAME,
289                     properties = {
290                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
291                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
292                     }
293                 )
294         })
295     @ApiResponses(
296         value = {
297             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
298             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
299             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
300         }
301     )
302     // @formatter:on
303     public ResponseEntity<ClElementStatisticsList> queryElementStatistics(
304             @RequestHeader(
305                     name = REQUEST_ID_NAME,
306                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
307             @ApiParam(value = "Participant name", required = true) @RequestParam(
308                     value = "name",
309                     required = false) final String name,
310             @ApiParam(value = "Participant version", required = true) @RequestParam(
311                     value = "version",
312                     required = false) final String version,
313             @ApiParam(value = "Record count", required = false) @RequestParam(
314                     value = "recordCount",
315                     required = false,
316                     defaultValue = "0") final int recordCount,
317             @ApiParam(value = "Control Loop element id", required = false) @RequestParam(
318                     value = "id",
319                     required = false) final String id,
320             @ApiParam(value = "start time", required = false) @RequestParam(
321                     value = "startTime",
322                     required = false) final String startTime,
323             @ApiParam(value = "end time", required = false) @RequestParam(
324                     value = "endTime",
325                     required = false) final String endTime)
326             throws PfModelException {
327
328         Instant startTimestamp = null;
329         Instant endTimestamp = null;
330
331         if (startTime != null) {
332             startTimestamp = Instant.parse(startTime);
333         }
334         if (endTime != null) {
335             endTimestamp = Instant.parse(endTime);
336         }
337         return ResponseEntity.ok().body(provider.fetchFilteredClElementStatistics(name, version, id, startTimestamp,
338                 endTimestamp, recordCount));
339     }
340
341 }