Merge "validate and save PdpStatistisc"
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / StatisticsRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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 = {@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 = {@Extension(name = EXTENSION_NAME,
99                             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
100                                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
101
102     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
103             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
104             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
105     public Response pdpStatistics(
106             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
107         try {
108             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
109                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(null, null, null, NO_COUNT_LIMIT))
110                     .build();
111         } catch (final PfModelException exp) {
112             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
113             return addLoggingHeaders(
114                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
115                             .build();
116         }
117     }
118
119     /**
120      * get all statistics of a PDP group.
121      *
122      * @param groupName name of the PDP group
123      * @return a response
124      */
125     @GET
126     @Path("pdps/statistics/{group}")
127     @ApiOperation(value = "Fetch current statistics for given PDP Group",
128             notes = "Returns statistics for given PDP Group of the Policy Administration component",
129             response = Map.class, tags = {"Policy Administration (PAP) API"},
130             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
131             responseHeaders = {
132                     @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
133                             response = String.class),
134                     @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
135                             response = String.class),
136                     @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
137                             response = String.class),
138                     @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
139                             response = UUID.class)},
140             extensions = {@Extension(name = EXTENSION_NAME,
141                     properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
142                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
143     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
144             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
145             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
146     public Response pdpGroupStatistics(
147             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
148             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName) {
149         try {
150             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
151                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, null, null, NO_COUNT_LIMIT))
152                     .build();
153         } catch (final PfModelException exp) {
154             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
155             return addLoggingHeaders(
156                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
157                             .build();
158         }
159     }
160
161     /**
162      * get all statistics of sub PDP group.
163      *
164      * @param groupName name of the PDP group
165      * @param subType type of the sub PDP group
166      * @return a response
167      */
168     @GET
169     @Path("pdps/statistics/{group}/{type}")
170     @ApiOperation(value = "Fetch statistics for the specified subgroup",
171             notes = "Returns  statistics for the specified subgroup of the Policy Administration component",
172             response = Map.class, tags = {"Policy Administration (PAP) API"},
173             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
174             responseHeaders = {
175                     @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
176                             response = String.class),
177                     @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
178                             response = String.class),
179                     @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
180                             response = String.class),
181                     @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
182                             response = UUID.class)},
183             extensions = {@Extension(name = EXTENSION_NAME,
184                     properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
185                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
186     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
187             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
188             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
189     public Response pdpSubGroupStatistics(
190             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
191             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
192             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType) {
193         try {
194             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
195                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, subType, null,
196                             NO_COUNT_LIMIT))
197                     .build();
198         } catch (final PfModelException exp) {
199             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
200             return addLoggingHeaders(
201                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
202                             .build();
203         }
204     }
205
206     /**
207      * get all statistics of one PDP.
208      *
209      * @param groupName name of the PDP group
210      * @param subType type of the sub PDP group
211      * @param pdpName the name of the PDP
212      * @param recordCount the count of the query response, optional, default return all statistics stored
213      * @return a response
214      */
215     @GET
216     @Path("pdps/statistics/{group}/{type}/{pdp}")
217     @ApiOperation(value = "Fetch statistics for the specified pdp",
218             notes = "Returns  statistics for the specified pdp of the Policy Administration component",
219             response = Map.class,
220             tags = {"Policy Administration (PAP) API"},
221             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
222             responseHeaders = {
223                     @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
224                             response = String.class),
225                     @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
226                             response = String.class),
227                     @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
228                             response = String.class),
229                     @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
230                             response = UUID.class)},
231             extensions = {@Extension(name = EXTENSION_NAME,
232                     properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
233                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
234     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
235             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
236             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
237
238     public Response pdpInstanceStatistics(
239             @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
240             @ApiParam(value = "PDP Group Name", required = true) @PathParam("group") final String groupName,
241             @ApiParam(value = "PDP SubGroup type", required = true) @PathParam("type") final String subType,
242             @ApiParam(value = "PDP Instance name", required = true) @PathParam("pdp") final String pdpName,
243             @ApiParam(value = "Record Count",
244                     required = false) @DefaultValue("0") @QueryParam("recordCount") final int recordCount) {
245         try {
246             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
247                     .entity(new StatisticsRestProvider().fetchDatabaseStatistics(groupName, subType, pdpName,
248                             recordCount))
249                     .build();
250         } catch (final PfModelException exp) {
251             LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
252             return addLoggingHeaders(
253                     addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
254                             .build();
255         }
256     }
257 }