Move PAP database provider to spring boot default
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyAuditManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import java.time.Instant;
25 import java.time.temporal.ChronoUnit;
26 import java.util.ArrayList;
27 import java.util.List;
28 import lombok.AccessLevel;
29 import lombok.Getter;
30 import org.onap.policy.models.pap.concepts.PolicyAudit;
31 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.onap.policy.pap.main.service.PolicyAuditService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Class to manage operations related to audit of policies.
39  *
40  * @author Adheli Tavares (adheli.tavares@est.tech)
41  *
42  */
43 public class PolicyAuditManager {
44     private static final Logger logger = LoggerFactory.getLogger(PolicyAuditManager.class);
45
46     /*
47      * Set of policies to be audited.
48      */
49     @Getter(value = AccessLevel.PROTECTED)
50     private List<PolicyAudit> auditRecords = new ArrayList<>();
51
52     private PolicyAuditService policyAuditService;
53
54     /**
55      * Constructs the object.
56      *
57      * @param policyAuditService the service
58      */
59     public PolicyAuditManager(PolicyAuditService policyAuditService) {
60         this.policyAuditService = policyAuditService;
61     }
62
63
64     /**
65      * Builds an audit object.
66      *
67      * @param policyId policy under action
68      * @param pdpGroup pdpGroup which the policy is related to
69      * @param pdpType pdp type
70      * @param action which action was taken on policy
71      * @param user which user started the action
72      * @return PolicyAudit object
73      */
74     public PolicyAudit buildAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, AuditAction action,
75             String user) {
76         return PolicyAudit.builder().action(action).pdpGroup(pdpGroup).pdpType(pdpType).policy(policyId)
77                 .timestamp(Instant.now().truncatedTo(ChronoUnit.SECONDS)).user(user).build();
78     }
79
80     /**
81      * Add deployments to the list of audits.
82      *
83      * @param policyId policy under deploy
84      * @param pdpGroup PdpGroup
85      * @param pdpType PDP type
86      * @param user user whom triggered the deploy
87      */
88     public void addDeploymentAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, String user) {
89         logger.info("Registering a deploy for policy {}", policyId);
90         auditRecords.add(buildAudit(policyId, pdpGroup, pdpType, AuditAction.DEPLOYMENT, user));
91     }
92
93     /**
94      * Add deployments to the list of audits.
95      *
96      * @param policyId policy under undeploy
97      * @param pdpGroup pdpGroup which the policy is related to
98      * @param pdpType PDP type
99      * @param user user whom triggered the undeploy
100      */
101     public void addUndeploymentAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, String user) {
102         logger.info("Registering an undeploy for policy {}", policyId);
103         auditRecords.add(buildAudit(policyId, pdpGroup, pdpType, AuditAction.UNDEPLOYMENT, user));
104     }
105
106     /**
107      * Create audit registers in DB.
108      * If an exception happens, list is not cleared up, exception is logged.
109      */
110     public void saveRecordsToDb() {
111         if (!auditRecords.isEmpty()) {
112             logger.info("sending audit records to database: {}", auditRecords);
113             try {
114                 policyAuditService.createAuditRecords(auditRecords);
115                 auditRecords.clear();
116             } catch (RuntimeException excpt) {
117                 // not throwing the exception to not stop the main request.
118                 logger.error("Failed saving the audit records in DB.", excpt);
119             }
120         }
121     }
122 }