Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / auditing / impl / AuditingManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.auditing.impl;
24
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
27 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
28 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
29 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
30 import org.openecomp.sdc.common.log.elements.LogFieldsMdcHandler;
31 import org.openecomp.sdc.common.log.enums.LogLevel;
32 import org.openecomp.sdc.common.log.enums.Severity;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.common.log.wrappers.LoggerSdcAudit;
35 import org.slf4j.MarkerFactory;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class AuditingManager {
40
41     private static final Logger log = Logger.getLogger(AuditingManager.class.getName());
42
43     private final AuditCassandraDao cassandraDao;
44     private final ConfigurationProvider configurationProvider;
45
46     public AuditingManager(AuditCassandraDao cassandraDao, ConfigurationProvider configurationProvider) {
47         this.cassandraDao = cassandraDao;
48         this.configurationProvider = configurationProvider;
49     }
50
51     public String auditEvent(AuditEventFactory factory) {
52         if (configurationProvider.getConfiguration().isDisableAudit()) {
53             return null;
54         }
55         String msg = factory.getLogMessage();
56         logAuditEvent(msg);
57
58         saveEventToCassandra(factory.getDbEvent());
59         return msg;
60     }
61
62     public String auditEvent(AuditEventFactory factory, LoggerSdcAudit audit) {
63         String msg = auditEvent(factory);
64         logAuditEvent(msg, audit, factory.getDbEvent().getRequestId());
65         return msg;
66     }
67
68     private void logAuditEvent(String msg, LoggerSdcAudit audit, String requestId) {
69         if(audit != null) {
70             audit.logEntry(LogLevel.INFO, Severity.OK, msg,
71                     MarkerFactory.getMarker(ONAPLogConstants.Markers.ENTRY.getName()), requestId);
72         }
73     }
74
75     private void saveEventToCassandra(AuditingGenericEvent event) {
76         CassandraOperationStatus result = cassandraDao.saveRecord(event);
77         if (result != CassandraOperationStatus.OK) {
78             log.warn("Failed to persist to cassandra auditing event: {}", result.name());
79         }
80     }
81
82     private void logAuditEvent(final String formattedString) {
83         log.trace("logAuditEvent - start");
84         log.debug(formattedString);
85         LogFieldsMdcHandler.getInstance()
86                 .setAuditMessage(formattedString);
87         if (!LoggerSdcAudit.isFlowBeingTakenCare()){
88             log.debug("MOVED FROM AUDIT LOG: {}", formattedString);
89         }
90         log.trace("logAuditEvent - end");
91     }
92
93 }