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