2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Bell Canada. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.pap.main.rest;
21 import io.swagger.annotations.ApiOperation;
22 import io.swagger.annotations.ApiParam;
23 import io.swagger.annotations.ApiResponse;
24 import io.swagger.annotations.ApiResponses;
25 import io.swagger.annotations.Authorization;
26 import io.swagger.annotations.Extension;
27 import io.swagger.annotations.ExtensionProperty;
28 import io.swagger.annotations.ResponseHeader;
29 import java.time.Instant;
30 import java.util.Collection;
31 import java.util.UUID;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.pap.concepts.PolicyAudit;
35 import org.onap.policy.pap.main.service.PolicyAuditService;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.RequestHeader;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RequestParam;
43 import org.springframework.web.bind.annotation.RestController;
46 * Class to provide REST end points for PAP component to retrieve the audit information for
47 * various operations on policies.
50 @RequestMapping(path = "/policy/pap/v1")
51 @RequiredArgsConstructor
52 public class PolicyAuditControllerV1 extends PapRestControllerV1 {
54 public static final String NO_AUDIT_RECORD_FOUND = "No records found matching the input parameters";
56 private final PolicyAuditService policyAuditService;
59 * Queries audit information of all policies.
61 * @param requestId request ID used in ONAP logging
62 * @param recordCount number of records to fetch
63 * @param startTime the starting time for the query in epoch timestamp
64 * @param endTime the ending time for the query in epoch timestamp
66 * @throws PfModelException the exception
69 @GetMapping("policies/audit")
70 @ApiOperation(value = "Queries audit information for all the policies",
71 notes = "Queries audit information for all the policies, "
72 + "returning audit information for all the policies in the database",
73 responseContainer = "List", response = PolicyAudit.class,
74 tags = {"Policy Audit"},
75 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
77 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
78 response = String.class),
79 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
80 response = String.class),
81 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
82 response = String.class),
83 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
84 response = UUID.class)},
86 @Extension(name = EXTENSION_NAME,
88 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
89 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
92 @ApiResponses(value = {
93 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
94 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
95 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
98 public ResponseEntity<Collection<PolicyAudit>> getAllAuditRecords(
99 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
101 value = REQUEST_ID_NAME) final UUID requestId,
102 @ApiParam(value = "Record count between 1-100") @RequestParam(
105 value = "recordCount") final int recordCount,
106 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
108 value = "startTime") final Long startTime,
109 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
111 value = "endTime") final Long endTime)
112 throws PfModelException {
114 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(policyAuditService
115 .getAuditRecords(recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
119 * Queries audit information of policies in a specific PdpGroup.
121 * @param requestId request ID used in ONAP logging
122 * @param recordCount number of records to fetch
123 * @param startTime the starting time for the query in epoch timestamp
124 * @param endTime the ending time for the query in epoch timestamp
125 * @param pdpGroupName the pdp group name for the query
127 * @throws PfModelException the exception
130 @GetMapping("policies/audit/{pdpGroupName}")
131 @ApiOperation(value = "Queries audit information for all the policies in a PdpGroup",
132 notes = "Queries audit information for all the policies in a PdpGroup, "
133 + "returning audit information for all the policies belonging to the PdpGroup",
134 responseContainer = "List", response = PolicyAudit.class,
135 tags = {"Policy Audit"},
136 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
138 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
139 response = String.class),
140 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
141 response = String.class),
142 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
143 response = String.class),
144 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
145 response = UUID.class)},
147 @Extension(name = EXTENSION_NAME,
149 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
150 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
153 @ApiResponses(value = {
154 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
155 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
156 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
159 public ResponseEntity<Object> getAuditRecordsByGroup(
160 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
162 value = REQUEST_ID_NAME) final UUID requestId,
163 @ApiParam(value = "Record count between 1-100") @RequestParam(
166 value = "recordCount") final int recordCount,
167 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
169 value = "startTime") final Long startTime,
170 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
172 value = "endTime") final Long endTime,
173 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName) throws PfModelException {
175 return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(pdpGroupName, recordCount,
176 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
180 * Queries audit information of a specific version of a policy in a PdpGroup.
182 * @param requestId request ID used in ONAP logging
183 * @param recordCount number of records to fetch
184 * @param startTime the starting time for the query in epoch timestamp
185 * @param endTime the ending time for the query in epoch timestamp
186 * @param pdpGroupName the pdp group name for the query
187 * @param policyName name of the Policy
188 * @param policyVersion version of the Policy
190 * @throws PfModelException the exception
193 @GetMapping("policies/audit/{pdpGroupName}/{policyName}/{policyVersion}")
194 @ApiOperation(value = "Queries audit information for a specific version of a policy in a PdpGroup",
195 notes = "Queries audit information for a specific version of a policy in a PdpGroup,"
196 + " returning audit information for the policy belonging to the PdpGroup",
197 response = PolicyAudit.class,
198 tags = {"Policy Audit"},
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)
223 public ResponseEntity<Object> getAuditRecordsOfPolicy(
224 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
226 value = REQUEST_ID_NAME) final UUID requestId,
227 @ApiParam(value = "Record count between 1-100", required = false) @RequestParam(
230 value = "recordCount") final int recordCount,
231 @ApiParam(value = "Start time in epoch timestamp", required = false) @RequestParam(
233 value = "startTime") final Long startTime,
234 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
236 value = "endTime") final Long endTime,
237 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
238 @ApiParam(value = "Policy Name") @PathVariable("policyName") String policyName,
239 @ApiParam(value = "Policy Version") @PathVariable(value = "policyVersion") String policyVersion)
240 throws PfModelException {
242 return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(pdpGroupName, policyName,
243 policyVersion, recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
247 * Queries audit information of a specific version of a policy.
249 * @param requestId request ID used in ONAP logging
250 * @param recordCount number of records to fetch
251 * @param startTime the starting time for the query in epoch timestamp
252 * @param endTime the ending time for the query in epoch timestamp
253 * @param policyName name of the Policy
254 * @param policyVersion version of the Policy
256 * @throws PfModelException the exception
259 @GetMapping("policies/audit/{policyName}/{policyVersion}")
260 @ApiOperation(value = "Queries audit information for a specific version of a policy",
261 notes = "Queries audit information for a specific version of a policy,"
262 + " returning audit information for the policy",
263 response = PolicyAudit.class,
264 tags = {"Policy Audit"},
265 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
267 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
268 response = String.class),
269 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
270 response = String.class),
271 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
272 response = String.class),
273 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
274 response = UUID.class)},
276 @Extension(name = EXTENSION_NAME,
278 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
279 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
282 @ApiResponses(value = {
283 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
284 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
285 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
289 public ResponseEntity<Object> getAuditRecordsOfPolicy(
290 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
292 value = REQUEST_ID_NAME) final UUID requestId,
293 @ApiParam(value = "Record count between 1-100") @RequestParam(
296 value = "recordCount") final int recordCount,
297 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
299 value = "startTime") final Long startTime,
300 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
302 value = "endTime") final Long endTime,
303 @ApiParam(value = "Policy Name") @PathVariable(required = true, value = "policyName") String policyName,
305 value = "Policy Version") @PathVariable(required = true, value = "policyVersion") String policyVersion)
306 throws PfModelException {
308 return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(policyName, policyVersion,
309 recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
312 private ResponseEntity<Object> makeOkOrNotFoundResponse(UUID requestId, Collection<PolicyAudit> result) {
313 if (result.isEmpty()) {
314 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
315 .body(NO_AUDIT_RECORD_FOUND);
317 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
321 private Instant convertEpochtoInstant(Long epochSecond) {
322 return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));