Migrate pap startup & controllers to spring boot
[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 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.base.PfModelException;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestHeader;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
48
49 /**
50  * Class to provide REST endpoints for PAP component statistics.
51  *
52  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
53  */
54 @RestController
55 @RequestMapping(path = "/policy/pap/v1")
56 @RequiredArgsConstructor
57 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
58
59     private final StatisticsRestProvider provider;
60
61     /**
62      * get statistics of PAP.
63      *
64      *
65      * @return a response
66      */
67     @GetMapping("statistics")
68     @ApiOperation(value = "Fetch current statistics",
69             notes = "Returns current statistics of the Policy Administration component",
70             response = StatisticsReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE))
71     @ApiResponses(value = {
72         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
73         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
74         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
75     public ResponseEntity<StatisticsReport> statistics(
76         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
77             required = false,
78             value = REQUEST_ID_NAME) final UUID requestId) {
79         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
80             .body(provider.fetchCurrentStatistics());
81     }
82
83     /**
84      * get all statistics of PDP groups.
85      *
86      *
87      * @return a response
88      * @throws PfModelException the exception
89      */
90     @GetMapping("pdps/statistics")
91     @ApiOperation(value = "Fetch  statistics for all PDP Groups and subgroups in the system",
92             notes = "Returns for all PDP Groups and subgroups statistics of the Policy Administration component",
93             response = Map.class, tags = {"PDP Statistics"},
94                     authorizations = @Authorization(value = AUTHORIZATION_TYPE),
95                     responseHeaders = {
96                         @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
97                                     response = String.class),
98                         @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
99                                     response = String.class),
100                         @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
101                                     response = String.class),
102                         @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
103                                     response = UUID.class)},
104                     extensions = {
105                         @Extension(name = EXTENSION_NAME,
106                             properties = {
107                                 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
108                                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
109                             })
110                         })
111
112     @ApiResponses(value = {
113         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
114         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
115         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
116     })
117     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpStatistics(
118         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
119             required = false,
120             value = REQUEST_ID_NAME) final UUID requestId,
121             @ApiParam(value = "Record Count") @RequestParam(
122                 defaultValue = "10", required = false,
123                 value = "recordCount") final int recordCount,
124             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
125                                 required = false,
126                                 value = "startTime") final Long startTime,
127             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
128                                 required = false,
129                                 value = "endTime") final Long endTime) throws PfModelException {
130         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
131             .body(provider.fetchDatabaseStatistics(PdpFilterParameters.builder().recordNum(recordCount)
132                 .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
133     }
134
135     /**
136      * get all statistics of a PDP group.
137      *
138      * @param groupName name of the PDP group
139      * @return a response
140      * @throws PfModelException the exception
141      */
142     @GetMapping("pdps/statistics/{group}")
143     @ApiOperation(value = "Fetch current statistics for given PDP Group",
144             notes = "Returns statistics for given PDP Group of the Policy Administration component",
145             response = Map.class, tags = {"PDP Statistics"},
146             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
147             responseHeaders = {
148                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
149                             response = String.class),
150                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
151                             response = String.class),
152                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
153                             response = String.class),
154                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
155                             response = UUID.class)},
156             extensions = {
157                 @Extension(name = EXTENSION_NAME,
158                     properties = {
159                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
160                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
161                     })
162                 })
163     @ApiResponses(value = {
164         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
165         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
166         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
167     })
168     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpGroupStatistics(
169         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
170             required = false,
171             value = REQUEST_ID_NAME) final UUID requestId,
172             @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
173             @ApiParam(value = "Record Count") @RequestParam(
174                 defaultValue = "10", required = false,
175                 value = "recordCount") final int recordCount,
176             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
177                                 required = false,
178                                 value = "startTime") final Long startTime,
179             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
180                                 required = false,
181                                 value = "endTime") final Long endTime) throws PfModelException {
182         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
183             .body(provider.fetchDatabaseStatistics(PdpFilterParameters.builder().group(groupName).recordNum(recordCount)
184                 .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
185     }
186
187     /**
188      * get all statistics of sub PDP group.
189      *
190      * @param groupName name of the PDP group
191      * @param subType type of the sub PDP group
192      * @return a response
193      * @throws PfModelException the exception
194      */
195     @GetMapping("pdps/statistics/{group}/{type}")
196     @ApiOperation(value = "Fetch statistics for the specified subgroup",
197             notes = "Returns  statistics for the specified subgroup of the Policy Administration component",
198             response = Map.class, tags = {"PDP Statistics"},
199             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
200             responseHeaders = {
201                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
202                             response = String.class),
203                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
204                             response = String.class),
205                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
206                             response = String.class),
207                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
208                             response = UUID.class)},
209             extensions = {
210                 @Extension(name = EXTENSION_NAME,
211                     properties = {
212                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
213                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
214                     })
215                 })
216     @ApiResponses(value = {
217         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
218         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
219         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
220     })
221     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpSubGroupStatistics(
222         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
223             required = false,
224             value = REQUEST_ID_NAME) final UUID requestId,
225             @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
226             @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
227             @ApiParam(value = "Record Count") @RequestParam(
228                 defaultValue = "10", required = false,
229                 value = "recordCount") final int recordCount,
230             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
231                                 required = false,
232                                 value = "startTime") final Long startTime,
233             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
234                                 required = false,
235                                 value = "endTime") final Long endTime) throws PfModelException {
236         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
237             .body(provider.fetchDatabaseStatistics(
238                 PdpFilterParameters.builder().group(groupName).subGroup(subType).recordNum(recordCount)
239                     .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
240     }
241
242     /**
243      * get all statistics of one PDP.
244      *
245      * @param groupName name of the PDP group
246      * @param subType type of the sub PDP group
247      * @param pdpName the name of the PDP
248      * @param recordCount the count of the query response, optional, default return all statistics stored
249      * @return a response
250      * @throws PfModelException the exception
251      */
252     @GetMapping("pdps/statistics/{group}/{type}/{pdp}")
253     @ApiOperation(value = "Fetch statistics for the specified pdp",
254             notes = "Returns  statistics for the specified pdp of the Policy Administration component",
255             response = Map.class,
256             tags = {"PDP Statistics"},
257             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
258             responseHeaders = {
259                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
260                             response = String.class),
261                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
262                             response = String.class),
263                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
264                             response = String.class),
265                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
266                             response = UUID.class)},
267             extensions = {
268                 @Extension(name = EXTENSION_NAME,
269                     properties = {
270                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
271                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
272                     })
273                 })
274     @ApiResponses(value = {
275         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
276         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
277         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
278     })
279     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpInstanceStatistics(
280         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
281             required = false,
282             value = REQUEST_ID_NAME) final UUID requestId,
283             @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
284             @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
285             @ApiParam(value = "PDP Instance name") @PathVariable("pdp") final String pdpName,
286             @ApiParam(value = "Record Count") @RequestParam(
287                 defaultValue = "10", required = false,
288                 value = "recordCount") final int recordCount,
289             @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
290                                 required = false,
291                                 value = "startTime") final Long startTime,
292             @ApiParam(value = "End time in epoch timestamp") @RequestParam(
293                                 required = false,
294                                 value = "endTime") final Long endTime) throws PfModelException {
295         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
296             .body(provider.fetchDatabaseStatistics(
297                 PdpFilterParameters.builder().group(groupName).subGroup(subType).name(pdpName).recordNum(recordCount)
298                     .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
299     }
300
301     private Instant convertEpochtoInstant(Long epochSecond) {
302         return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
303     }
304 }