Merge "PolicyAudit creation when deploy/undeploy triggered."
[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  * ================================================================================
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import java.time.Instant;
24 import java.time.temporal.ChronoUnit;
25 import java.util.ArrayList;
26 import java.util.List;
27 import lombok.AccessLevel;
28 import lombok.Getter;
29 import org.onap.policy.models.pap.concepts.PolicyAudit;
30 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
31 import org.onap.policy.models.provider.PolicyModelsProvider;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Class to manage operations related to audit of policies.
38  *
39  * @author Adheli Tavares (adheli.tavares@est.tech)
40  *
41  */
42 public class PolicyAuditManager {
43     private static final Logger logger = LoggerFactory.getLogger(PolicyAuditManager.class);
44
45     /*
46      * Set of policies to be audited.
47      */
48     @Getter(value = AccessLevel.PROTECTED)
49     private List<PolicyAudit> auditRecords = new ArrayList<>();
50
51     private PolicyModelsProvider provider;
52
53     /**
54      * Default constructor.
55      */
56     public PolicyAuditManager(PolicyModelsProvider provider) {
57         this.provider = provider;
58     }
59
60     /**
61      * Builds an audit object.
62      *
63      * @param policyId policy under action
64      * @param pdpGroup pdpGroup which the policy is related to
65      * @param pdpType pdp type
66      * @param action which action was taken on policy
67      * @param user which user started the action
68      * @return PolicyAudit object
69      */
70     public PolicyAudit buildAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, AuditAction action,
71             String user) {
72         return PolicyAudit.builder().action(action).pdpGroup(pdpGroup).pdpType(pdpType).policy(policyId)
73                 .timestamp(Instant.now().truncatedTo(ChronoUnit.SECONDS)).user(user).build();
74     }
75
76     /**
77      * Add deployments to the list of audits.
78      *
79      * @param policyId policy under deploy
80      * @param pdpGroup PdpGroup
81      * @param pdpType PDP type
82      * @param user user whom triggered the deploy
83      */
84     public void addDeploymentAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, String user) {
85         logger.info("Registering a deploy for policy {}", policyId);
86         auditRecords.add(buildAudit(policyId, pdpGroup, pdpType, AuditAction.DEPLOYMENT, user));
87     }
88
89     /**
90      * Add deployments to the list of audits.
91      *
92      * @param policyId policy under undeploy
93      * @param pdpGroup pdpGroup which the policy is related to
94      * @param pdpType PDP type
95      * @param user user whom triggered the undeploy
96      */
97     public void addUndeploymentAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, String user) {
98         logger.info("Registering an undeploy for policy {}", policyId);
99         auditRecords.add(buildAudit(policyId, pdpGroup, pdpType, AuditAction.UNDEPLOYMENT, user));
100     }
101
102     /**
103      * Create audit registers in DB.
104      * If an exception happens, list is not cleared up, exception is logged.
105      */
106     public void saveRecordsToDb() {
107         if (!auditRecords.isEmpty()) {
108             logger.info("sending audit records to database: {}", auditRecords);
109             try {
110                 provider.createAuditRecords(auditRecords);
111                 auditRecords.clear();
112             } catch (RuntimeException excpt) {
113                 // not throwing the exception to not stop the main request.
114                 logger.error("Failed saving the audit records in DB.", excpt);
115             }
116         }
117     }
118 }