Merge "Send pdp-update if PDP response doesn't match DB"
[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 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.DefaultValue;
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.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     private static final int NO_COUNT_LIMIT = 0;
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 = {"Policy Administration (PAP) API"},
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         try {
115             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
116                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(null, null, null, NO_COUNT_LIMIT))
117                     .build();
118         } catch (final PfModelException exp) {
119             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
120             return addLoggingHeaders(
121                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
122                             .build();
123         }
124     }
125
126     /**
127      * get all statistics of a PDP group.
128      *
129      * @param groupName name of the PDP group
130      * @return a response
131      */
132     @GET
133     @Path("pdps/statistics/{group}")
134     @ApiOperation(value = "Fetch current statistics for given PDP Group",
135             notes = "Returns statistics for given PDP Group of the Policy Administration component",
136             response = Map.class, tags = {"Policy Administration (PAP) API"},
137             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
138             responseHeaders = {
139                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
140                             response = String.class),
141                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
142                             response = String.class),
143                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
144                             response = String.class),
145                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
146                             response = UUID.class)},
147             extensions = {
148                 @Extension(name = EXTENSION_NAME,
149                     properties = {
150                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
151                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
152                     })
153                 })
154     @ApiResponses(value = {
155         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
156         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
157         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
158     })
159     public Response pdpGroupStatistics(
160             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
161             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName) {
162         try {
163             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
164                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, null, null, NO_COUNT_LIMIT))
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         try {
213             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
214                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, subType, null,
215                             NO_COUNT_LIMIT))
216                     .build();
217         } catch (final PfModelException exp) {
218             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
219             return addLoggingHeaders(
220                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
221                             .build();
222         }
223     }
224
225     /**
226      * get all statistics of one PDP.
227      *
228      * @param groupName name of the PDP group
229      * @param subType type of the sub PDP group
230      * @param pdpName the name of the PDP
231      * @param recordCount the count of the query response, optional, default return all statistics stored
232      * @return a response
233      */
234     @GET
235     @Path("pdps/statistics/{group}/{type}/{pdp}")
236     @ApiOperation(value = "Fetch statistics for the specified pdp",
237             notes = "Returns  statistics for the specified pdp of the Policy Administration component",
238             response = Map.class,
239             tags = {"Policy Administration (PAP) API"},
240             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
241             responseHeaders = {
242                 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
243                             response = String.class),
244                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
245                             response = String.class),
246                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
247                             response = String.class),
248                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
249                             response = UUID.class)},
250             extensions = {
251                 @Extension(name = EXTENSION_NAME,
252                     properties = {
253                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
254                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
255                     })
256                 })
257     @ApiResponses(value = {
258         @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
259         @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
260         @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
261     })
262     public Response pdpInstanceStatistics(
263             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
264             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
265             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType,
266             @ApiParam(value = "PDP Instance name", required = true) @PathParam("pdp") final String pdpName,
267             @ApiParam(value = "Record Count",
268                     required = false) @DefaultValue("0") @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 }