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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
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;
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;
38 * Class to manage operations related to audit of policies.
40 * @author Adheli Tavares (adheli.tavares@est.tech)
43 public class PolicyAuditManager {
44 private static final Logger logger = LoggerFactory.getLogger(PolicyAuditManager.class);
47 * Set of policies to be audited.
49 @Getter(value = AccessLevel.PROTECTED)
50 private List<PolicyAudit> auditRecords = new ArrayList<>();
52 private PolicyAuditService policyAuditService;
55 * Constructs the object.
57 * @param policyAuditService the service
59 public PolicyAuditManager(PolicyAuditService policyAuditService) {
60 this.policyAuditService = policyAuditService;
65 * Builds an audit object.
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
74 public PolicyAudit buildAudit(ToscaConceptIdentifier policyId, String pdpGroup, String pdpType, AuditAction action,
76 return PolicyAudit.builder().action(action).pdpGroup(pdpGroup).pdpType(pdpType).policy(policyId)
77 .timestamp(Instant.now().truncatedTo(ChronoUnit.SECONDS)).user(user).build();
81 * Add deployments to the list of audits.
83 * @param policyId policy under deploy
84 * @param pdpGroup PdpGroup
85 * @param pdpType PDP type
86 * @param user user whom triggered the deploy
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));
94 * Add deployments to the list of audits.
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
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));
107 * Create audit registers in DB.
108 * If an exception happens, list is not cleared up, exception is logged.
110 public void saveRecordsToDb() {
111 if (!auditRecords.isEmpty()) {
112 logger.info("sending audit records to database: {}", auditRecords);
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);