Merge "Add datetime format to audit api's"
[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.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Class to provide REST endpoints for PAP component statistics.
47  *
48  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
49  */
50 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsRestControllerV1.class);
53     private static final String GET_STATISTICS_ERR_MSG = "get pdpStatistics failed";
54
55     /**
56      * get statistics of PAP.
57      *
58      *
59      * @return a response
60      */
61     @GET
62     @Path("statistics")
63     @ApiOperation(value = "Fetch current statistics",
64             notes = "Returns current statistics of the Policy Administration component",
65             response = StatisticsReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE))
66     @ApiResponses(value = {
67         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
68         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
69         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
70     public Response statistics(
71             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
72         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
73                 .entity(new StatisticsRestProvider().fetchCurrentStatistics())
74                 .build();
75     }
76
77     /**
78      * get all statistics of PDP groups.
79      *
80      *
81      * @return a response
82      */
83     @GET
84     @Path("pdps/statistics")
85     @ApiOperation(value = "Fetch  statistics for all PDP Groups and subgroups in the system",
86             notes = "Returns for all PDP Groups and subgroups statistics of the Policy Administration component",
87             response = Map.class, tags = {"Policy Administration (PAP) API"},
88                     authorizations = @Authorization(value = AUTHORIZATION_TYPE),
89                     responseHeaders = {
90                         @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
91                                     response = String.class),
92                         @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
93                                     response = String.class),
94                         @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
95                                     response = String.class),
96                         @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
97                                     response = UUID.class)},
98                     extensions = {
99                         @Extension(name = EXTENSION_NAME,
100                             properties = {
101                                 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
102                                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
103                             })
104                         })
105
106     @ApiResponses(value = {
107         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
108         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
109         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
110     })
111     public Response pdpStatistics(
112             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
113             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
114         try {
115             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
116                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(
117                                     PdpFilterParameters.builder().recordNum(recordCount).build()))
118                     .build();
119         } catch (final PfModelException exp) {
120             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
121             return addLoggingHeaders(
122                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
123                             .build();
124         }
125     }
126
127     /**
128      * get all statistics of a PDP group.
129      *
130      * @param groupName name of the PDP group
131      * @return a response
132      */
133     @GET
134     @Path("pdps/statistics/{group}")
135     @ApiOperation(value = "Fetch current statistics for given PDP Group",
136             notes = "Returns statistics for given PDP Group of the Policy Administration component",
137             response = Map.class, tags = {"Policy Administration (PAP) API"},
138             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
139             responseHeaders = {
140                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
141                             response = String.class),
142                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
143                             response = String.class),
144                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
145                             response = String.class),
146                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
147                             response = UUID.class)},
148             extensions = {
149                 @Extension(name = EXTENSION_NAME,
150                     properties = {
151                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
152                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
153                     })
154                 })
155     @ApiResponses(value = {
156         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
157         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
158         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
159     })
160     public Response pdpGroupStatistics(
161             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
162             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
163             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
164         try {
165             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
166                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(PdpFilterParameters.builder()
167                                     .group(groupName).recordNum(recordCount).build()))
168                     .build();
169         } catch (final PfModelException exp) {
170             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
171             return addLoggingHeaders(
172                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
173                             .build();
174         }
175     }
176
177     /**
178      * get all statistics of sub PDP group.
179      *
180      * @param groupName name of the PDP group
181      * @param subType type of the sub PDP group
182      * @return a response
183      */
184     @GET
185     @Path("pdps/statistics/{group}/{type}")
186     @ApiOperation(value = "Fetch statistics for the specified subgroup",
187             notes = "Returns  statistics for the specified subgroup of the Policy Administration component",
188             response = Map.class, tags = {"Policy Administration (PAP) API"},
189             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
190             responseHeaders = {
191                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
192                             response = String.class),
193                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
194                             response = String.class),
195                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
196                             response = String.class),
197                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
198                             response = UUID.class)},
199             extensions = {
200                 @Extension(name = EXTENSION_NAME,
201                     properties = {
202                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
203                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
204                     })
205                 })
206     @ApiResponses(value = {
207         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
208         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
209         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
210     })
211     public Response pdpSubGroupStatistics(
212             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
213             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
214             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType,
215             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
216         try {
217             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
218                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(PdpFilterParameters.builder()
219                                     .group(groupName).subGroup(subType).recordNum(recordCount).build()))
220                     .build();
221         } catch (final PfModelException exp) {
222             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
223             return addLoggingHeaders(
224                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
225                             .build();
226         }
227     }
228
229     /**
230      * get all statistics of one PDP.
231      *
232      * @param groupName name of the PDP group
233      * @param subType type of the sub PDP group
234      * @param pdpName the name of the PDP
235      * @param recordCount the count of the query response, optional, default return all statistics stored
236      * @return a response
237      */
238     @GET
239     @Path("pdps/statistics/{group}/{type}/{pdp}")
240     @ApiOperation(value = "Fetch statistics for the specified pdp",
241             notes = "Returns  statistics for the specified pdp of the Policy Administration component",
242             response = Map.class,
243             tags = {"Policy Administration (PAP) API"},
244             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
245             responseHeaders = {
246                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
247                             response = String.class),
248                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
249                             response = String.class),
250                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
251                             response = String.class),
252                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
253                             response = UUID.class)},
254             extensions = {
255                 @Extension(name = EXTENSION_NAME,
256                     properties = {
257                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
258                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
259                     })
260                 })
261     @ApiResponses(value = {
262         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
263         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
264         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
265     })
266     public Response pdpInstanceStatistics(
267             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
268             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
269             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType,
270             @ApiParam(value = "PDP Instance name", required = true) @PathParam("pdp") final String pdpName,
271             @ApiParam(value = "Record Count", required = false) @QueryParam("recordCount") final int recordCount) {
272         try {
273             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
274                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(
275                                     PdpFilterParameters.builder().group(groupName).subGroup(subType)
276                                                     .name(pdpName).recordNum(recordCount).build()))
277                     .build();
278         } catch (final PfModelException exp) {
279             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
280             return addLoggingHeaders(
281                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
282                             .build();
283         }
284     }
285 }