Migrate pap startup & controllers to spring boot
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyAuditControllerV1.java
1 /*-
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
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.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;
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 PolicyAuditProvider provider;
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)
115             .body(provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
116                 .fromDate(convertEpochtoInstant(startTime)).toDate(convertEpochtoInstant(endTime)).build()));
117     }
118
119     /**
120      * Queries audit information of policies in a specific PdpGroup.
121      *
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
127      * @return a response
128      * @throws PfModelException the exception
129      */
130     // @formatter:off
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),
138         responseHeaders = {
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)},
147         extensions = {
148             @Extension(name = EXTENSION_NAME,
149                 properties = {
150                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
151                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
152                 })
153             })
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)
158     })
159     // @formatter:on
160     public ResponseEntity<Object> getAuditRecordsByGroup(
161         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
162             required = false,
163             value = REQUEST_ID_NAME) final UUID requestId,
164         @ApiParam(value = "Record count between 1-100") @RequestParam(
165             defaultValue = "10",
166             required = false,
167             value = "recordCount") final int recordCount,
168         @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
169             required = false,
170             value = "startTime") final Long startTime,
171         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
172             required = false,
173             value = "endTime") final Long endTime,
174         @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName) throws PfModelException {
175
176         return makeOkOrNotFoundResponse(requestId,
177             provider.getAuditRecords(
178                 AuditFilter.builder().recordNum(recordCount).fromDate((convertEpochtoInstant(startTime)))
179                     .toDate(convertEpochtoInstant(endTime)).pdpGroup(pdpGroupName).build()));
180     }
181
182     /**
183      * Queries audit information of a specific version of a policy in a PdpGroup.
184      *
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
192      * @return a response
193      * @throws PfModelException the exception
194      */
195     // @formatter:off
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),
203         responseHeaders = {
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)},
212         extensions = {
213             @Extension(name = EXTENSION_NAME,
214                 properties = {
215                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
216                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
217                 })
218             })
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)
223     })
224     // @formatter:on
225
226     public ResponseEntity<Object> getAuditRecordsOfPolicy(
227         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
228             required = false,
229             value = REQUEST_ID_NAME) final UUID requestId,
230         @ApiParam(value = "Record count between 1-100", required = false) @RequestParam(
231             defaultValue = "10",
232             required = false,
233             value = "recordCount") final int recordCount,
234         @ApiParam(value = "Start time in epoch timestamp", required = false) @RequestParam(
235             required = false,
236             value = "startTime") final Long startTime,
237         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
238             required = false,
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 {
244
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()));
249     }
250
251     /**
252      * Queries audit information of a specific version of a policy.
253      *
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
260      * @return a response
261      * @throws PfModelException the exception
262      */
263     // @formatter:off
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),
271         responseHeaders = {
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)},
280         extensions = {
281             @Extension(name = EXTENSION_NAME,
282                 properties = {
283                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
284                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
285                 })
286             })
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)
291     })
292     // @formatter:on
293
294     public ResponseEntity<Object> getAuditRecordsOfPolicy(
295         @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
296             required = false,
297             value = REQUEST_ID_NAME) final UUID requestId,
298         @ApiParam(value = "Record count between 1-100") @RequestParam(
299             defaultValue = "10",
300             required = false,
301             value = "recordCount") final int recordCount,
302         @ApiParam(value = "Start time in epoch timestamp") @RequestParam(
303             required = false,
304             value = "startTime") final Long startTime,
305         @ApiParam(value = "End time in epoch timestamp") @RequestParam(
306             required = false,
307             value = "endTime") final Long endTime,
308         @ApiParam(value = "Policy Name") @PathVariable(required = true, value = "policyName") String policyName,
309         @ApiParam(
310             value = "Policy Version") @PathVariable(required = true, value = "policyVersion") String policyVersion)
311         throws PfModelException {
312
313         return makeOkOrNotFoundResponse(requestId,
314             provider
315                 .getAuditRecords(AuditFilter.builder().recordNum(recordCount).fromDate(convertEpochtoInstant(startTime))
316                     .toDate(convertEpochtoInstant(endTime)).name(policyName).version(policyVersion).build()));
317     }
318
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);
323         } else {
324             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
325         }
326     }
327
328     private Instant convertEpochtoInstant(Long epochSecond) {
329         return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
330     }
331 }