Move PAP database provider to spring boot default
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / StatisticsRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiParam;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import io.swagger.annotations.Authorization;
30 import io.swagger.annotations.Extension;
31 import io.swagger.annotations.ExtensionProperty;
32 import io.swagger.annotations.ResponseHeader;
33 import java.time.Instant;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37 import lombok.RequiredArgsConstructor;
38 import org.onap.policy.models.pdp.concepts.PdpStatistics;
39 import org.onap.policy.pap.main.service.PdpStatisticsService;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.RequestHeader;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47
48 /**
49  * Class to provide REST endpoints for PAP component statistics.
50  *
51  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
52  */
53 @RestController
54 @RequestMapping(path = "/policy/pap/v1")
55 @RequiredArgsConstructor
56 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
57
58     private final PdpStatisticsService pdpStatisticsService;
59
60     /**
61      * get statistics of PAP.
62      *
63      *
64      * @return a response
65      */
66     @GetMapping("statistics")
67     @ApiOperation(value = "Fetch current statistics",
68             notes = "Returns current statistics of the Policy Administration component",
69             response = StatisticsReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE))
70     @ApiResponses(value = {
71         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
72         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
73         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
74     public ResponseEntity<StatisticsReport> statistics(
75         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
76             required = false,
77             value = REQUEST_ID_NAME) final UUID requestId) {
78         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
79             .body(pdpStatisticsService.fetchCurrentStatistics());
80     }
81
82     /**
83      * get all statistics of PDP groups.
84      *
85      * @return a response
86      */
87     @GetMapping("pdps/statistics")
88     @ApiOperation(value = "Fetch  statistics for all PDP Groups and subgroups in the system",
89             notes = "Returns for all PDP Groups and subgroups statistics of the Policy Administration component",
90             response = Map.class, tags = {"PDP Statistics"},
91                     authorizations = @Authorization(value = AUTHORIZATION_TYPE),
92                     responseHeaders = {
93                         @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
94                                     response = String.class),
95                         @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
96                                     response = String.class),
97                         @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
98                                     response = String.class),
99                         @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
100                                     response = UUID.class)},
101                     extensions = {
102                         @Extension(name = EXTENSION_NAME,
103                             properties = {
104                                 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
105                                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
106                             })
107                         })
108
109     @ApiResponses(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     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpStatistics(
115         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
116             required = false,
117             value = REQUEST_ID_NAME) final UUID requestId,
118             @ApiParam(value = "Record Count") @RequestParam(
119                 defaultValue = "10", required = false,
120                 value = "recordCount") final int recordCount,
121             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
122                                 required = false,
123                                 value = "startTime") final Long startTime,
124             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
125                                 required = false,
126                                 value = "endTime") final Long endTime) {
127         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(pdpStatisticsService
128             .fetchDatabaseStatistics(recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
129     }
130
131     /**
132      * get all statistics of a PDP group.
133      *
134      * @param groupName name of the PDP group
135      * @return a response
136      */
137     @GetMapping("pdps/statistics/{group}")
138     @ApiOperation(value = "Fetch current statistics for given PDP Group",
139             notes = "Returns statistics for given PDP Group of the Policy Administration component",
140             response = Map.class, tags = {"PDP Statistics"},
141             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
142             responseHeaders = {
143                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
144                             response = String.class),
145                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
146                             response = String.class),
147                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
148                             response = String.class),
149                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
150                             response = UUID.class)},
151             extensions = {
152                 @Extension(name = EXTENSION_NAME,
153                     properties = {
154                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
155                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
156                     })
157                 })
158     @ApiResponses(value = {
159         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
160         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
161         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
162     })
163     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpGroupStatistics(
164         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
165             required = false,
166             value = REQUEST_ID_NAME) final UUID requestId,
167             @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
168             @ApiParam(value = "Record Count") @RequestParam(
169                 defaultValue = "10", required = false,
170                 value = "recordCount") final int recordCount,
171             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
172                                 required = false,
173                                 value = "startTime") final Long startTime,
174             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
175                                 required = false,
176                                 value = "endTime") final Long endTime) {
177         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
178             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, recordCount, convertEpochtoInstant(startTime),
179                 convertEpochtoInstant(endTime)));
180     }
181
182     /**
183      * get all statistics of sub PDP group.
184      *
185      * @param groupName name of the PDP group
186      * @param subType type of the sub PDP group
187      * @return a response
188      */
189     @GetMapping("pdps/statistics/{group}/{type}")
190     @ApiOperation(value = "Fetch statistics for the specified subgroup",
191             notes = "Returns  statistics for the specified subgroup of the Policy Administration component",
192             response = Map.class, tags = {"PDP Statistics"},
193             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
194             responseHeaders = {
195                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
196                             response = String.class),
197                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
198                             response = String.class),
199                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
200                             response = String.class),
201                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
202                             response = UUID.class)},
203             extensions = {
204                 @Extension(name = EXTENSION_NAME,
205                     properties = {
206                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
207                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
208                     })
209                 })
210     @ApiResponses(value = {
211         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
212         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
213         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
214     })
215     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpSubGroupStatistics(
216         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
217             required = false,
218             value = REQUEST_ID_NAME) final UUID requestId,
219             @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
220             @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
221             @ApiParam(value = "Record Count") @RequestParam(
222                 defaultValue = "10", required = false,
223                 value = "recordCount") final int recordCount,
224             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
225                                 required = false,
226                                 value = "startTime") final Long startTime,
227             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
228                                 required = false,
229                                 value = "endTime") final Long endTime) {
230         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
231             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, recordCount,
232                 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
233     }
234
235     /**
236      * get all statistics of one PDP.
237      *
238      * @param groupName name of the PDP group
239      * @param subType type of the sub PDP group
240      * @param pdpName the name of the PDP
241      * @param recordCount the count of the query response, optional, default return all statistics stored
242      * @return a response
243      */
244     @GetMapping("pdps/statistics/{group}/{type}/{pdp}")
245     @ApiOperation(value = "Fetch statistics for the specified pdp",
246             notes = "Returns  statistics for the specified pdp of the Policy Administration component",
247             response = Map.class,
248             tags = {"PDP Statistics"},
249             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
250             responseHeaders = {
251                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
252                             response = String.class),
253                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
254                             response = String.class),
255                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
256                             response = String.class),
257                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
258                             response = UUID.class)},
259             extensions = {
260                 @Extension(name = EXTENSION_NAME,
261                     properties = {
262                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
263                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
264                     })
265                 })
266     @ApiResponses(value = {
267         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
268         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
269         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
270     })
271     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpInstanceStatistics(
272         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
273             required = false,
274             value = REQUEST_ID_NAME) final UUID requestId,
275             @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
276             @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
277             @ApiParam(value = "PDP Instance name") @PathVariable("pdp") final String pdpName,
278             @ApiParam(value = "Record Count") @RequestParam(
279                 defaultValue = "10", required = false,
280                 value = "recordCount") final int recordCount,
281             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
282                                 required = false,
283                                 value = "startTime") final Long startTime,
284             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
285                                 required = false,
286                                 value = "endTime") final Long endTime) {
287         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
288             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, pdpName, recordCount,
289                 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
290     }
291
292     private Instant convertEpochtoInstant(Long epochSecond) {
293         return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
294     }
295 }