2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
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 PolicyAuditProvider provider;
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)
115 .body(provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
116 .fromDate(convertEpochtoInstant(startTime)).toDate(convertEpochtoInstant(endTime)).build()));
120 * Queries audit information of policies in a specific PdpGroup.
122 * @param requestId request ID used in ONAP logging
123 * @param recordCount number of records to fetch
124 * @param startTime the starting time for the query in epoch timestamp
125 * @param endTime the ending time for the query in epoch timestamp
126 * @param pdpGroupName the pdp group name for the query
128 * @throws PfModelException the exception
131 @GetMapping("policies/audit/{pdpGroupName}")
132 @ApiOperation(value = "Queries audit information for all the policies in a PdpGroup",
133 notes = "Queries audit information for all the policies in a PdpGroup, "
134 + "returning audit information for all the policies belonging to the PdpGroup",
135 responseContainer = "List", response = PolicyAudit.class,
136 tags = {"Policy Audit"},
137 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
139 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
140 response = String.class),
141 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
142 response = String.class),
143 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
144 response = String.class),
145 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
146 response = UUID.class)},
148 @Extension(name = EXTENSION_NAME,
150 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
151 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
154 @ApiResponses(value = {
155 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
156 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
157 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
160 public ResponseEntity<Object> getAuditRecordsByGroup(
161 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
163 value = REQUEST_ID_NAME) final UUID requestId,
164 @ApiParam(value = "Record count between 1-100") @RequestParam(
167 value = "recordCount") final int recordCount,
168 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
170 value = "startTime") final Long startTime,
171 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
173 value = "endTime") final Long endTime,
174 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName) throws PfModelException {
176 return makeOkOrNotFoundResponse(requestId,
177 provider.getAuditRecords(
178 AuditFilter.builder().recordNum(recordCount).fromDate((convertEpochtoInstant(startTime)))
179 .toDate(convertEpochtoInstant(endTime)).pdpGroup(pdpGroupName).build()));
183 * Queries audit information of a specific version of a policy in a PdpGroup.
185 * @param requestId request ID used in ONAP logging
186 * @param recordCount number of records to fetch
187 * @param startTime the starting time for the query in epoch timestamp
188 * @param endTime the ending time for the query in epoch timestamp
189 * @param pdpGroupName the pdp group name for the query
190 * @param policyName name of the Policy
191 * @param policyVersion version of the Policy
193 * @throws PfModelException the exception
196 @GetMapping("policies/audit/{pdpGroupName}/{policyName}/{policyVersion}")
197 @ApiOperation(value = "Queries audit information for a specific version of a policy in a PdpGroup",
198 notes = "Queries audit information for a specific version of a policy in a PdpGroup,"
199 + " returning audit information for the policy belonging to the PdpGroup",
200 response = PolicyAudit.class,
201 tags = {"Policy Audit"},
202 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
204 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
205 response = String.class),
206 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
207 response = String.class),
208 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
209 response = String.class),
210 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
211 response = UUID.class)},
213 @Extension(name = EXTENSION_NAME,
215 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
216 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
219 @ApiResponses(value = {
220 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
221 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
222 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
226 public ResponseEntity<Object> getAuditRecordsOfPolicy(
227 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
229 value = REQUEST_ID_NAME) final UUID requestId,
230 @ApiParam(value = "Record count between 1-100", required = false) @RequestParam(
233 value = "recordCount") final int recordCount,
234 @ApiParam(value = "Start time in epoch timestamp", required = false) @RequestParam(
236 value = "startTime") final Long startTime,
237 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
239 value = "endTime") final Long endTime,
240 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
241 @ApiParam(value = "Policy Name") @PathVariable("policyName") String policyName,
242 @ApiParam(value = "Policy Version") @PathVariable(value = "policyVersion") String policyVersion)
243 throws PfModelException {
245 return makeOkOrNotFoundResponse(requestId,
246 provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
247 .fromDate(convertEpochtoInstant(startTime)).toDate(convertEpochtoInstant(endTime))
248 .pdpGroup(pdpGroupName).name(policyName).version(policyVersion).build()));
252 * Queries audit information of a specific version of a policy.
254 * @param requestId request ID used in ONAP logging
255 * @param recordCount number of records to fetch
256 * @param startTime the starting time for the query in epoch timestamp
257 * @param endTime the ending time for the query in epoch timestamp
258 * @param policyName name of the Policy
259 * @param policyVersion version of the Policy
261 * @throws PfModelException the exception
264 @GetMapping("policies/audit/{policyName}/{policyVersion}")
265 @ApiOperation(value = "Queries audit information for a specific version of a policy",
266 notes = "Queries audit information for a specific version of a policy,"
267 + " returning audit information for the policy",
268 response = PolicyAudit.class,
269 tags = {"Policy Audit"},
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)
294 public ResponseEntity<Object> getAuditRecordsOfPolicy(
295 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
297 value = REQUEST_ID_NAME) final UUID requestId,
298 @ApiParam(value = "Record count between 1-100") @RequestParam(
301 value = "recordCount") final int recordCount,
302 @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
304 value = "startTime") final Long startTime,
305 @ApiParam(value = "End time in epoch timestamp") @RequestParam(
307 value = "endTime") final Long endTime,
308 @ApiParam(value = "Policy Name") @PathVariable(required = true, value = "policyName") String policyName,
310 value = "Policy Version") @PathVariable(required = true, value = "policyVersion") String policyVersion)
311 throws PfModelException {
313 return makeOkOrNotFoundResponse(requestId,
315 .getAuditRecords(AuditFilter.builder().recordNum(recordCount).fromDate(convertEpochtoInstant(startTime))
316 .toDate(convertEpochtoInstant(endTime)).name(policyName).version(policyVersion).build()));
319 private ResponseEntity<Object> makeOkOrNotFoundResponse(UUID requestId, Collection<PolicyAudit> result) {
320 if (result.isEmpty()) {
321 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
322 .body(NO_AUDIT_RECORD_FOUND);
324 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
328 private Instant convertEpochtoInstant(Long epochSecond) {
329 return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));