Limit statistics record count
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.Authorization;
29 import io.swagger.annotations.Extension;
30 import io.swagger.annotations.ExtensionProperty;
31 import io.swagger.annotations.ResponseHeader;
32 import java.util.Map;
33 import java.util.UUID;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.core.Response;
40 import org.onap.policy.models.base.PfModelException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Class to provide REST endpoints for PAP component statistics.
46  *
47  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48  */
49 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsRestControllerV1.class);
52     private static final String GET_STATISTICS_ERR_MSG = "get pdpStatistics failed";
53
54     /**
55      * get statistics of PAP.
56      *
57      *
58      * @return a response
59      */
60     @GET
61     @Path("statistics")
62     @ApiOperation(value = "Fetch current statistics",
63             notes = "Returns current statistics of the Policy Administration component",
64             response = StatisticsReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE))
65     @ApiResponses(value = {
66         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
67         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
68         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
69     public Response statistics(
70             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
71         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
72                 .entity(new StatisticsRestProvider().fetchCurrentStatistics())
73                 .build();
74     }
75
76     /**
77      * get all statistics of PDP groups.
78      *
79      *
80      * @return a response
81      */
82     @GET
83     @Path("pdps/statistics")
84     @ApiOperation(value = "Fetch  statistics for all PDP Groups and subgroups in the system",
85             notes = "Returns for all PDP Groups and subgroups statistics of the Policy Administration component",
86             response = Map.class, tags = {"Policy Administration (PAP) API"},
87                     authorizations = @Authorization(value = AUTHORIZATION_TYPE),
88                     responseHeaders = {
89                         @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
90                                     response = String.class),
91                         @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
92                                     response = String.class),
93                         @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
94                                     response = String.class),
95                         @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
96                                     response = UUID.class)},
97                     extensions = {
98                         @Extension(name = EXTENSION_NAME,
99                             properties = {
100                                 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
101                                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
102                             })
103                         })
104
105     @ApiResponses(value = {
106         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
107         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
108         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
109     })
110     public Response pdpStatistics(
111             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
112             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
113         try {
114             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
115                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(null, null, null, recordCount))
116                     .build();
117         } catch (final PfModelException exp) {
118             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
119             return addLoggingHeaders(
120                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
121                             .build();
122         }
123     }
124
125     /**
126      * get all statistics of a PDP group.
127      *
128      * @param groupName name of the PDP group
129      * @return a response
130      */
131     @GET
132     @Path("pdps/statistics/{group}")
133     @ApiOperation(value = "Fetch current statistics for given PDP Group",
134             notes = "Returns statistics for given PDP Group of the Policy Administration component",
135             response = Map.class, tags = {"Policy Administration (PAP) API"},
136             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
137             responseHeaders = {
138                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
139                             response = String.class),
140                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
141                             response = String.class),
142                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
143                             response = String.class),
144                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
145                             response = UUID.class)},
146             extensions = {
147                 @Extension(name = EXTENSION_NAME,
148                     properties = {
149                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
150                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
151                     })
152                 })
153     @ApiResponses(value = {
154         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
155         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
156         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
157     })
158     public Response pdpGroupStatistics(
159             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
160             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
161             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
162         try {
163             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
164                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, null, null, recordCount))
165                     .build();
166         } catch (final PfModelException exp) {
167             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
168             return addLoggingHeaders(
169                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
170                             .build();
171         }
172     }
173
174     /**
175      * get all statistics of sub PDP group.
176      *
177      * @param groupName name of the PDP group
178      * @param subType type of the sub PDP group
179      * @return a response
180      */
181     @GET
182     @Path("pdps/statistics/{group}/{type}")
183     @ApiOperation(value = "Fetch statistics for the specified subgroup",
184             notes = "Returns  statistics for the specified subgroup of the Policy Administration component",
185             response = Map.class, tags = {"Policy Administration (PAP) API"},
186             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
187             responseHeaders = {
188                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
189                             response = String.class),
190                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
191                             response = String.class),
192                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
193                             response = String.class),
194                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
195                             response = UUID.class)},
196             extensions = {
197                 @Extension(name = EXTENSION_NAME,
198                     properties = {
199                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
200                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
201                     })
202                 })
203     @ApiResponses(value = {
204         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
205         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
206         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
207     })
208     public Response pdpSubGroupStatistics(
209             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
210             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
211             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType,
212             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
213         try {
214             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
215                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, subType, null,
216                                     recordCount))
217                     .build();
218         } catch (final PfModelException exp) {
219             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
220             return addLoggingHeaders(
221                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
222                             .build();
223         }
224     }
225
226     /**
227      * get all statistics of one PDP.
228      *
229      * @param groupName name of the PDP group
230      * @param subType type of the sub PDP group
231      * @param pdpName the name of the PDP
232      * @param recordCount the count of the query response, optional, default return all statistics stored
233      * @return a response
234      */
235     @GET
236     @Path("pdps/statistics/{group}/{type}/{pdp}")
237     @ApiOperation(value = "Fetch statistics for the specified pdp",
238             notes = "Returns  statistics for the specified pdp of the Policy Administration component",
239             response = Map.class,
240             tags = {"Policy Administration (PAP) API"},
241             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
242             responseHeaders = {
243                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
244                             response = String.class),
245                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
246                             response = String.class),
247                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
248                             response = String.class),
249                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
250                             response = UUID.class)},
251             extensions = {
252                 @Extension(name = EXTENSION_NAME,
253                     properties = {
254                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
255                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
256                     })
257                 })
258     @ApiResponses(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     public Response pdpInstanceStatistics(
264             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
265             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
266             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType,
267             @ApiParam(value = "PDP Instance name", required = true) @PathParam("pdp") final String pdpName,
268             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
269         try {
270             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
271                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, subType, pdpName,
272                             recordCount))
273                     .build();
274         } catch (final PfModelException exp) {
275             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
276             return addLoggingHeaders(
277                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
278                             .build();
279         }
280     }
281 }