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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest;
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;
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;
37 * Class to manage operations related to audit of policies.
39 * @author Adheli Tavares (adheli.tavares@est.tech)
42 public class PolicyAuditManager {
43 private static final Logger logger = LoggerFactory.getLogger(PolicyAuditManager.class);
46 * Set of policies to be audited.
48 @Getter(value = AccessLevel.PROTECTED)
49 private List<PolicyAudit> auditRecords = new ArrayList<>();
51 private PolicyModelsProvider provider;
54 * Default constructor.
56 public PolicyAuditManager(PolicyModelsProvider provider) {
57 this.provider = provider;
61 * Builds an audit object.
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
70 public PolicyAudit buildAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, AuditAction action,
72 return PolicyAudit.builder().action(action).pdpGroup(pdpGroup).pdpType(pdpType).policy(policyId)
73 .timestamp(Instant.now().truncatedTo(ChronoUnit.SECONDS)).user(user).build();
77 * Add deployments to the list of audits.
79 * @param policyId policy under deploy
80 * @param pdpGroup PdpGroup
81 * @param pdpType PDP type
82 * @param user user whom triggered the deploy
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));
90 * Add deployments to the list of audits.
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
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));
103 * Create audit registers in DB.
104 * If an exception happens, list is not cleared up, exception is logged.
106 public void saveRecordsToDb() {
107 if (!auditRecords.isEmpty()) {
108 logger.info("sending audit records to database: {}", auditRecords);
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);