9eeef2be82d9ede7dff799a295237a9aec48c1e0
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyAuditControllerV1.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.pap.main.rest;
20
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;
44
45 /**
46  * Class to provide REST end points for PAP component to retrieve the audit information for
47  * various operations on policies.
48  */
49 @RestController
50 @RequestMapping(path = "/policy/pap/v1")
51 @RequiredArgsConstructor
52 public class PolicyAuditControllerV1 extends PapRestControllerV1 {
53
54     public static final String NO_AUDIT_RECORD_FOUND = "No records found matching the input parameters";
55
56     private final PolicyAuditService policyAuditService;
57
58     /**
59      * Queries audit information of all policies.
60      *
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
65      * @return a response
66      * @throws PfModelException the exception
67      */
68     // @formatter:off
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),
76         responseHeaders = {
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)},
85         extensions = {
86             @Extension(name = EXTENSION_NAME,
87                 properties = {
88                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
89                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
90                 })
91             })
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)
96     })
97     // @formatter:on
98     public ResponseEntity<Collection<PolicyAudit>> getAllAuditRecords(
99         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
100             required = false,
101             value = REQUEST_ID_NAME) final UUID requestId,
102         @ApiParam(value = "Record count between 1-100") @RequestParam(
103             defaultValue = "10",
104             required = false,
105             value = "recordCount") final int recordCount,
106         @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
107             required = false,
108             value = "startTime") final Long startTime,
109         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
110             required = false,
111             value = "endTime") final Long endTime)
112         throws PfModelException {
113
114         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(policyAuditService
115             .getAuditRecords(recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
116     }
117
118     /**
119      * Queries audit information of policies in a specific PdpGroup.
120      *
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
126      * @return a response
127      * @throws PfModelException the exception
128      */
129     // @formatter:off
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),
137         responseHeaders = {
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)},
146         extensions = {
147             @Extension(name = EXTENSION_NAME,
148                 properties = {
149                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
150                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
151                 })
152             })
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)
157     })
158     // @formatter:on
159     public ResponseEntity<Object> getAuditRecordsByGroup(
160         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
161             required = false,
162             value = REQUEST_ID_NAME) final UUID requestId,
163         @ApiParam(value = "Record count between 1-100") @RequestParam(
164             defaultValue = "10",
165             required = false,
166             value = "recordCount") final int recordCount,
167         @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
168             required = false,
169             value = "startTime") final Long startTime,
170         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
171             required = false,
172             value = "endTime") final Long endTime,
173         @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName) throws PfModelException {
174
175         return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(pdpGroupName, recordCount,
176             convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
177     }
178
179     /**
180      * Queries audit information of a specific version of a policy in a PdpGroup.
181      *
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
189      * @return a response
190      * @throws PfModelException the exception
191      */
192     // @formatter:off
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),
200         responseHeaders = {
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)},
209         extensions = {
210             @Extension(name = EXTENSION_NAME,
211                 properties = {
212                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
213                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
214                 })
215             })
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)
220     })
221     // @formatter:on
222
223     public ResponseEntity<Object> getAuditRecordsOfPolicy(
224         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
225             required = false,
226             value = REQUEST_ID_NAME) final UUID requestId,
227         @ApiParam(value = "Record count between 1-100", required = false) @RequestParam(
228             defaultValue = "10",
229             required = false,
230             value = "recordCount") final int recordCount,
231         @ApiParam(value = "Start time in epoch timestamp", required = false) @RequestParam(
232             required = false,
233             value = "startTime") final Long startTime,
234         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
235             required = false,
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 {
241
242         return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(pdpGroupName, policyName,
243             policyVersion, recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
244     }
245
246     /**
247      * Queries audit information of a specific version of a policy.
248      *
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
255      * @return a response
256      * @throws PfModelException the exception
257      */
258     // @formatter:off
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),
266         responseHeaders = {
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)},
275         extensions = {
276             @Extension(name = EXTENSION_NAME,
277                 properties = {
278                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
279                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
280                 })
281             })
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)
286     })
287     // @formatter:on
288
289     public ResponseEntity<Object> getAuditRecordsOfPolicy(
290         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
291             required = false,
292             value = REQUEST_ID_NAME) final UUID requestId,
293         @ApiParam(value = "Record count between 1-100") @RequestParam(
294             defaultValue = "10",
295             required = false,
296             value = "recordCount") final int recordCount,
297         @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
298             required = false,
299             value = "startTime") final Long startTime,
300         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
301             required = false,
302             value = "endTime") final Long endTime,
303         @ApiParam(value = "Policy Name") @PathVariable(required = true, value = "policyName") String policyName,
304         @ApiParam(
305             value = "Policy Version") @PathVariable(required = true, value = "policyVersion") String policyVersion)
306         throws PfModelException {
307
308         return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(policyName, policyVersion,
309             recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
310     }
311
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);
316         } else {
317             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
318         }
319     }
320
321     private Instant convertEpochtoInstant(Long epochSecond) {
322         return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
323     }
324 }