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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
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;
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;
47 * Class to provide REST endpoints for PAP component statistics.
49 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
51 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
53 private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsRestControllerV1.class);
54 private static final String GET_STATISTICS_ERR_MSG = "get pdpStatistics failed";
57 * get statistics of PAP.
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())
79 * get all statistics of PDP groups.
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),
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)},
100 @Extension(name = EXTENSION_NAME,
102 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
103 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
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))
127 } catch (final PfModelException exp) {
128 LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
129 return addLoggingHeaders(
130 addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
136 * get all statistics of a PDP group.
138 * @param groupName name of the PDP group
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),
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)},
157 @Extension(name = EXTENSION_NAME,
159 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
160 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
177 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
178 .entity(new StatisticsRestProvider().fetchDatabaseStatistics(PdpFilterParameters.builder()
180 .recordNum(recordCount)
181 .startTime(convertEpochtoInstant(startTime))
182 .endTime(convertEpochtoInstant(endTime))
185 } catch (final PfModelException exp) {
186 LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
187 return addLoggingHeaders(
188 addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
194 * get all statistics of sub PDP group.
196 * @param groupName name of the PDP group
197 * @param subType type of the sub PDP group
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),
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)},
216 @Extension(name = EXTENSION_NAME,
218 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
219 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
237 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
238 .entity(new StatisticsRestProvider().fetchDatabaseStatistics(PdpFilterParameters.builder()
241 .recordNum(recordCount)
242 .startTime(convertEpochtoInstant(startTime))
243 .endTime(convertEpochtoInstant(endTime))
246 } catch (final PfModelException exp) {
247 LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
248 return addLoggingHeaders(
249 addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
255 * get all statistics of one PDP.
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
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),
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)},
280 @Extension(name = EXTENSION_NAME,
282 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
283 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
302 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
303 .entity(new StatisticsRestProvider().fetchDatabaseStatistics(
304 PdpFilterParameters.builder()
308 .recordNum(recordCount)
309 .startTime(convertEpochtoInstant(startTime))
310 .endTime(convertEpochtoInstant(endTime))
313 } catch (final PfModelException exp) {
314 LOGGER.info(GET_STATISTICS_ERR_MSG, exp);
315 return addLoggingHeaders(
316 addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
321 private Instant convertEpochtoInstant(Long epochSecond) {
322 return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));