30c1d5dc9d512e21c82798e880d8362e45d1a6a7
[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.acm.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.acm.runtime.main.web.AbstractRestController;
35 import org.onap.policy.clamp.acm.runtime.monitoring.MonitoringProvider;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementStatisticsList;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantStatisticsList;
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 ACM Statistics monitoring.
48  */
49 @RestController
50 @RequiredArgsConstructor
51 public class MonitoringQueryController extends AbstractRestController {
52
53     private static final String TAGS = "Clamp Automation Composition Monitoring API";
54     private final MonitoringProvider provider;
55
56     /**
57      * Queries details of automation composition 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(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
106         @ApiParam(value = "Automation composition  participant name") @RequestParam(
107             value = "name",
108             required = false) final String name,
109         @ApiParam(value = "Automation composition  participant version", required = false) @RequestParam(
110             value = "version",
111             required = false) final String version,
112         @ApiParam(value = "Record count", required = false) @RequestParam(
113             value = "recordCount",
114             required = false,
115             defaultValue = "0") final int recordCount,
116         @ApiParam(value = "start time", required = false) @RequestParam(
117             value = "startTime",
118             required = false) final String startTime,
119         @ApiParam(value = "end time", required = false) @RequestParam(
120             value = "endTime",
121             required = false) final String endTime) {
122
123         Instant startTimestamp = null;
124         Instant endTimestamp = null;
125
126         if (startTime != null) {
127             startTimestamp = Instant.parse(startTime);
128         }
129         if (endTime != null) {
130             endTimestamp = Instant.parse(endTime);
131         }
132         return ResponseEntity.ok().body(
133             provider.fetchFilteredParticipantStatistics(name, version, recordCount, startTimestamp, endTimestamp));
134     }
135
136     /**
137      * Queries details of all participant statistics per automation composition.
138      *
139      * @param requestId request ID used in ONAP logging
140      * @param name the name of the automation composition
141      * @param version version of the automation composition
142      * @return the automation composition element statistics
143      */
144     // @formatter:off
145     @GetMapping(value = "/monitoring/participants/automationcomposition",
146             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
147     @ApiOperation(value = "Query details of all the participant stats in a automation composition",
148         notes = "Queries details of the participant stats, returning all participant stats",
149         response = AcElementStatisticsList.class,
150         tags = {TAGS},
151         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
152         responseHeaders = {
153             @ResponseHeader(
154                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
155                 response = String.class),
156             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
157                 response = String.class),
158             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
159                 response = String.class),
160             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
161                 response = UUID.class)},
162         extensions = {
163             @Extension
164                 (
165                     name = EXTENSION_NAME,
166                     properties = {
167                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
168                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
169                     }
170                 )
171         })
172     @ApiResponses(
173         value = {
174             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
175             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
176             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
177         }
178     )
179     // @formatter:on
180     public ResponseEntity<ParticipantStatisticsList> queryParticipantStatisticsPerAutomationComposition(
181         @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
182         @ApiParam(value = "Automation composition  name", required = true) @RequestParam(
183             value = "name",
184             required = false) final String name,
185         @ApiParam(value = "Automation composition  version", required = true) @RequestParam(
186             value = "version",
187             required = false) final String version) {
188
189         return ResponseEntity.ok().body(provider.fetchParticipantStatsPerAutomationComposition(name, version));
190     }
191
192     /**
193      * Queries details of all automation composition element statistics per automation composition.
194      *
195      * @param requestId request ID used in ONAP logging
196      * @param name the name of the automation composition
197      * @param version version of the automation composition
198      * @return the automation composition element statistics
199      */
200     // @formatter:off
201     @GetMapping(value = "/monitoring/acelements/automationcomposition",
202             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
203     @ApiOperation(value = "Query details of the requested acElement stats in a automation composition",
204         notes = "Queries details of the requested acElement stats, returning all acElement stats",
205         response = AcElementStatisticsList.class,
206         tags = {TAGS},
207         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
208         responseHeaders = {
209             @ResponseHeader(
210                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
211                 response = String.class),
212             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
213                 response = String.class),
214             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
215                 response = String.class),
216             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
217                 response = UUID.class)},
218         extensions = {
219             @Extension
220                 (
221                     name = EXTENSION_NAME,
222                     properties = {
223                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
224                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
225                     }
226                 )
227         })
228     @ApiResponses(
229         value = {
230             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
231             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
232             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
233         }
234     )
235     // @formatter:on
236     public ResponseEntity<AcElementStatisticsList> queryElementStatisticsPerAutomationComposition(
237         @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
238         @ApiParam(value = "Automation composition  name", required = true) @RequestParam(
239             value = "name",
240             required = false) final String name,
241         @ApiParam(value = "Automation composition  version", required = true) @RequestParam(
242             value = "version",
243             required = false) final String version) {
244
245         return ResponseEntity.ok().body(provider.fetchAcElementStatsPerAutomationComposition(name, version));
246     }
247
248     /**
249      * Queries details of all automation composition element statistics per automation composition.
250      *
251      * @param requestId request ID used in ONAP logging
252      * @param name the name of the automation composition
253      * @param version version of the automation composition
254      * @param id Id of the automation composition element
255      * @param recordCount the record count to be fetched
256      * @param startTime the time from which to get statistics
257      * @param endTime the time to which to get statistics
258      * @return the automation composition element statistics
259      * @throws PfModelException on errors getting details of all automation composition element statistics per
260      *         automation composition
261      */
262     // @formatter:off
263     @GetMapping(value = "/monitoring/acelement",
264             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
265     @ApiOperation(value = "Query details of the requested acElement stats",
266         notes = "Queries details of the requested acElement stats, returning all acElement stats",
267         response = AcElementStatisticsList.class,
268         tags = {TAGS},
269         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
270         responseHeaders = {
271             @ResponseHeader(
272                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
273                 response = String.class),
274             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
275                 response = String.class),
276             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
277                 response = String.class),
278             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
279                 response = UUID.class)},
280         extensions = {
281             @Extension
282                 (
283                     name = EXTENSION_NAME,
284                     properties = {
285                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
286                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
287                     }
288                 )
289         })
290     @ApiResponses(
291         value = {
292             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
293             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
294             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
295         }
296     )
297     // @formatter:on
298     public ResponseEntity<AcElementStatisticsList> queryElementStatistics(
299         @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
300         @ApiParam(value = "Participant name", required = true) @RequestParam(
301             value = "name",
302             required = false) final String name,
303         @ApiParam(value = "Participant version", required = true) @RequestParam(
304             value = "version",
305             required = false) final String version,
306         @ApiParam(value = "Record count", required = false) @RequestParam(
307             value = "recordCount",
308             required = false,
309             defaultValue = "0") final int recordCount,
310         @ApiParam(value = "Automation composition  element id", required = false) @RequestParam(
311             value = "id",
312             required = false) final String id,
313         @ApiParam(value = "start time", required = false) @RequestParam(
314             value = "startTime",
315             required = false) final String startTime,
316         @ApiParam(value = "end time", required = false) @RequestParam(
317             value = "endTime",
318             required = false) final String endTime)
319         throws PfModelException {
320
321         Instant startTimestamp = null;
322         Instant endTimestamp = null;
323
324         if (startTime != null) {
325             startTimestamp = Instant.parse(startTime);
326         }
327         if (endTime != null) {
328             endTimestamp = Instant.parse(endTime);
329         }
330         return ResponseEntity.ok().body(
331             provider.fetchFilteredAcElementStatistics(name, version, id, startTimestamp, endTimestamp, recordCount));
332     }
333
334 }