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