2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiParam;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import io.swagger.annotations.Authorization;
30 import io.swagger.annotations.Extension;
31 import io.swagger.annotations.ExtensionProperty;
32 import io.swagger.annotations.ResponseHeader;
33 import java.time.Instant;
34 import java.util.List;
36 import java.util.UUID;
37 import lombok.RequiredArgsConstructor;
38 import org.onap.policy.models.pdp.concepts.PdpStatistics;
39 import org.onap.policy.pap.main.service.PdpStatisticsService;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.RequestHeader;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
49 * Class to provide REST endpoints for PAP component statistics.
51 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
54 @RequestMapping(path = "/policy/pap/v1")
55 @RequiredArgsConstructor
56 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
58 private final PdpStatisticsService pdpStatisticsService;
61 * get statistics of PAP.
66 @GetMapping("statistics")
67 @ApiOperation(value = "Fetch current statistics",
68 notes = "Returns current statistics of the Policy Administration component",
69 response = StatisticsReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE))
70 @ApiResponses(value = {
71 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
72 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
73 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
74 public ResponseEntity<StatisticsReport> statistics(
75 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
77 value = REQUEST_ID_NAME) final UUID requestId) {
78 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
79 .body(pdpStatisticsService.fetchCurrentStatistics());
83 * get all statistics of PDP groups.
87 @GetMapping("pdps/statistics")
88 @ApiOperation(value = "Fetch statistics for all PDP Groups and subgroups in the system",
89 notes = "Returns for all PDP Groups and subgroups statistics of the Policy Administration component",
90 response = Map.class, tags = {"PDP Statistics"},
91 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
93 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
94 response = String.class),
95 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
96 response = String.class),
97 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
98 response = String.class),
99 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
100 response = UUID.class)},
102 @Extension(name = EXTENSION_NAME,
104 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
105 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
109 @ApiResponses(value = {
110 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
111 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
112 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
114 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpStatistics(
115 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
117 value = REQUEST_ID_NAME) final UUID requestId,
118 @ApiParam(value = "Record Count") @RequestParam(
119 defaultValue = "10", required = false,
120 value = "recordCount") final int recordCount,
121 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
123 value = "startTime") final Long startTime,
124 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
126 value = "endTime") final Long endTime) {
127 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(pdpStatisticsService
128 .fetchDatabaseStatistics(recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
132 * get all statistics of a PDP group.
134 * @param groupName name of the PDP group
137 @GetMapping("pdps/statistics/{group}")
138 @ApiOperation(value = "Fetch current statistics for given PDP Group",
139 notes = "Returns statistics for given PDP Group of the Policy Administration component",
140 response = Map.class, tags = {"PDP Statistics"},
141 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
143 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
144 response = String.class),
145 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
146 response = String.class),
147 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
148 response = String.class),
149 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
150 response = UUID.class)},
152 @Extension(name = EXTENSION_NAME,
154 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
155 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
158 @ApiResponses(value = {
159 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
160 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
161 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
163 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpGroupStatistics(
164 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
166 value = REQUEST_ID_NAME) final UUID requestId,
167 @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
168 @ApiParam(value = "Record Count") @RequestParam(
169 defaultValue = "10", required = false,
170 value = "recordCount") final int recordCount,
171 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
173 value = "startTime") final Long startTime,
174 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
176 value = "endTime") final Long endTime) {
177 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
178 .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, recordCount, convertEpochtoInstant(startTime),
179 convertEpochtoInstant(endTime)));
183 * get all statistics of sub PDP group.
185 * @param groupName name of the PDP group
186 * @param subType type of the sub PDP group
189 @GetMapping("pdps/statistics/{group}/{type}")
190 @ApiOperation(value = "Fetch statistics for the specified subgroup",
191 notes = "Returns statistics for the specified subgroup of the Policy Administration component",
192 response = Map.class, tags = {"PDP Statistics"},
193 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
195 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
196 response = String.class),
197 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
198 response = String.class),
199 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
200 response = String.class),
201 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
202 response = UUID.class)},
204 @Extension(name = EXTENSION_NAME,
206 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
207 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
210 @ApiResponses(value = {
211 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
212 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
213 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
215 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpSubGroupStatistics(
216 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
218 value = REQUEST_ID_NAME) final UUID requestId,
219 @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
220 @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
221 @ApiParam(value = "Record Count") @RequestParam(
222 defaultValue = "10", required = false,
223 value = "recordCount") final int recordCount,
224 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
226 value = "startTime") final Long startTime,
227 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
229 value = "endTime") final Long endTime) {
230 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
231 .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, recordCount,
232 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
236 * get all statistics of one PDP.
238 * @param groupName name of the PDP group
239 * @param subType type of the sub PDP group
240 * @param pdpName the name of the PDP
241 * @param recordCount the count of the query response, optional, default return all statistics stored
244 @GetMapping("pdps/statistics/{group}/{type}/{pdp}")
245 @ApiOperation(value = "Fetch statistics for the specified pdp",
246 notes = "Returns statistics for the specified pdp of the Policy Administration component",
247 response = Map.class,
248 tags = {"PDP Statistics"},
249 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
251 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
252 response = String.class),
253 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
254 response = String.class),
255 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
256 response = String.class),
257 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
258 response = UUID.class)},
260 @Extension(name = EXTENSION_NAME,
262 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
263 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
266 @ApiResponses(value = {
267 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
268 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
269 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
271 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpInstanceStatistics(
272 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
274 value = REQUEST_ID_NAME) final UUID requestId,
275 @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
276 @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
277 @ApiParam(value = "PDP Instance name") @PathVariable("pdp") final String pdpName,
278 @ApiParam(value = "Record Count") @RequestParam(
279 defaultValue = "10", required = false,
280 value = "recordCount") final int recordCount,
281 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
283 value = "startTime") final Long startTime,
284 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
286 value = "endTime") final Long endTime) {
287 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
288 .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, pdpName, recordCount,
289 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
292 private Instant convertEpochtoInstant(Long epochSecond) {
293 return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));