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 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.base.PfModelException;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestHeader;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
50 * Class to provide REST endpoints for PAP component statistics.
52 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
55 @RequestMapping(path = "/policy/pap/v1")
56 @RequiredArgsConstructor
57 public class StatisticsRestControllerV1 extends PapRestControllerV1 {
59 private final StatisticsRestProvider provider;
62 * get statistics of PAP.
67 @GetMapping("statistics")
68 @ApiOperation(value = "Fetch current statistics",
69 notes = "Returns current statistics of the Policy Administration component",
70 response = StatisticsReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE))
71 @ApiResponses(value = {
72 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
73 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
74 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
75 public ResponseEntity<StatisticsReport> statistics(
76 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
78 value = REQUEST_ID_NAME) final UUID requestId) {
79 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
80 .body(provider.fetchCurrentStatistics());
84 * get all statistics of PDP groups.
88 * @throws PfModelException the exception
90 @GetMapping("pdps/statistics")
91 @ApiOperation(value = "Fetch statistics for all PDP Groups and subgroups in the system",
92 notes = "Returns for all PDP Groups and subgroups statistics of the Policy Administration component",
93 response = Map.class, tags = {"PDP Statistics"},
94 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
96 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
97 response = String.class),
98 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
99 response = String.class),
100 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
101 response = String.class),
102 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
103 response = UUID.class)},
105 @Extension(name = EXTENSION_NAME,
107 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
108 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
112 @ApiResponses(value = {
113 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
114 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
115 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
117 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpStatistics(
118 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
120 value = REQUEST_ID_NAME) final UUID requestId,
121 @ApiParam(value = "Record Count") @RequestParam(
122 defaultValue = "10", required = false,
123 value = "recordCount") final int recordCount,
124 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
126 value = "startTime") final Long startTime,
127 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
129 value = "endTime") final Long endTime) throws PfModelException {
130 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
131 .body(provider.fetchDatabaseStatistics(PdpFilterParameters.builder().recordNum(recordCount)
132 .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
136 * get all statistics of a PDP group.
138 * @param groupName name of the PDP group
140 * @throws PfModelException the exception
142 @GetMapping("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 ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpGroupStatistics(
169 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
171 value = REQUEST_ID_NAME) final UUID requestId,
172 @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
173 @ApiParam(value = "Record Count") @RequestParam(
174 defaultValue = "10", required = false,
175 value = "recordCount") final int recordCount,
176 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
178 value = "startTime") final Long startTime,
179 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
181 value = "endTime") final Long endTime) throws PfModelException {
182 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
183 .body(provider.fetchDatabaseStatistics(PdpFilterParameters.builder().group(groupName).recordNum(recordCount)
184 .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
188 * get all statistics of sub PDP group.
190 * @param groupName name of the PDP group
191 * @param subType type of the sub PDP group
193 * @throws PfModelException the exception
195 @GetMapping("pdps/statistics/{group}/{type}")
196 @ApiOperation(value = "Fetch statistics for the specified subgroup",
197 notes = "Returns statistics for the specified subgroup of the Policy Administration component",
198 response = Map.class, tags = {"PDP Statistics"},
199 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
201 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
202 response = String.class),
203 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
204 response = String.class),
205 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
206 response = String.class),
207 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
208 response = UUID.class)},
210 @Extension(name = EXTENSION_NAME,
212 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
213 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
216 @ApiResponses(value = {
217 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
218 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
219 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
221 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpSubGroupStatistics(
222 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
224 value = REQUEST_ID_NAME) final UUID requestId,
225 @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
226 @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
227 @ApiParam(value = "Record Count") @RequestParam(
228 defaultValue = "10", required = false,
229 value = "recordCount") final int recordCount,
230 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
232 value = "startTime") final Long startTime,
233 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
235 value = "endTime") final Long endTime) throws PfModelException {
236 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
237 .body(provider.fetchDatabaseStatistics(
238 PdpFilterParameters.builder().group(groupName).subGroup(subType).recordNum(recordCount)
239 .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
243 * get all statistics of one PDP.
245 * @param groupName name of the PDP group
246 * @param subType type of the sub PDP group
247 * @param pdpName the name of the PDP
248 * @param recordCount the count of the query response, optional, default return all statistics stored
250 * @throws PfModelException the exception
252 @GetMapping("pdps/statistics/{group}/{type}/{pdp}")
253 @ApiOperation(value = "Fetch statistics for the specified pdp",
254 notes = "Returns statistics for the specified pdp of the Policy Administration component",
255 response = Map.class,
256 tags = {"PDP Statistics"},
257 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
259 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
260 response = String.class),
261 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
262 response = String.class),
263 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
264 response = String.class),
265 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
266 response = UUID.class)},
268 @Extension(name = EXTENSION_NAME,
270 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
271 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
274 @ApiResponses(value = {
275 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
276 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
277 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
279 public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpInstanceStatistics(
280 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
282 value = REQUEST_ID_NAME) final UUID requestId,
283 @ApiParam(value = "PDP Group Name") @PathVariable("group") final String groupName,
284 @ApiParam(value = "PDP SubGroup type") @PathVariable("type") final String subType,
285 @ApiParam(value = "PDP Instance name") @PathVariable("pdp") final String pdpName,
286 @ApiParam(value = "Record Count") @RequestParam(
287 defaultValue = "10", required = false,
288 value = "recordCount") final int recordCount,
289 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
291 value = "startTime") final Long startTime,
292 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
294 value = "endTime") final Long endTime) throws PfModelException {
295 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
296 .body(provider.fetchDatabaseStatistics(
297 PdpFilterParameters.builder().group(groupName).subGroup(subType).name(pdpName).recordNum(recordCount)
298 .startTime(convertEpochtoInstant(startTime)).endTime(convertEpochtoInstant(endTime)).build()));
301 private Instant convertEpochtoInstant(Long epochSecond) {
302 return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));