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.base.PfModelRuntimeException;
43 import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Class to provide REST endpoints for PAP component statistics.
50 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
52 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
54 private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsRestControllerV1.class);
55 private static final String GET_STATISTICS_ERR_MSG = "get pdpStatistics failed";
58 * get statistics of PAP.
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())
80 * get all statistics of PDP groups.
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),
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)},
101 @Extension(name = EXTENSION_NAME,
103 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
104 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
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))
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)
137 * get all statistics of a PDP group.
139 * @param groupName name of the PDP group
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),
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)},
158 @Extension(name = EXTENSION_NAME,
160 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
161 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
178 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
179 .entity(new StatisticsRestProvider().fetchDatabaseStatistics(PdpFilterParameters.builder()
181 .recordNum(recordCount)
182 .startTime(convertEpochtoInstant(startTime))
183 .endTime(convertEpochtoInstant(endTime))
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)
195 * get all statistics of sub PDP group.
197 * @param groupName name of the PDP group
198 * @param subType type of the sub PDP group
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),
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)},
217 @Extension(name = EXTENSION_NAME,
219 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
220 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
238 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
239 .entity(new StatisticsRestProvider().fetchDatabaseStatistics(PdpFilterParameters.builder()
242 .recordNum(recordCount)
243 .startTime(convertEpochtoInstant(startTime))
244 .endTime(convertEpochtoInstant(endTime))
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)
256 * get all statistics of one PDP.
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
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),
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)},
281 @Extension(name = EXTENSION_NAME,
283 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
284 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
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)
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) {
303 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
304 .entity(new StatisticsRestProvider().fetchDatabaseStatistics(
305 PdpFilterParameters.builder()
309 .recordNum(recordCount)
310 .startTime(convertEpochtoInstant(startTime))
311 .endTime(convertEpochtoInstant(endTime))
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)
322 private Instant convertEpochtoInstant(Long epochSecond) {
323 return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));