re base code
[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  */
20
21 package org.openecomp.sdc.be.auditing.impl;
22
23 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
27 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
28 import org.openecomp.sdc.be.dao.impl.AuditingDao;
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.wrappers.Logger;
32 import org.openecomp.sdc.common.log.wrappers.LoggerSdcAudit;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 public class AuditingManager {
37
38     private static final Logger log = Logger.getLogger(AuditingManager.class.getName());
39
40     private final AuditingDao auditingDao;
41     private final AuditCassandraDao cassandraDao;
42
43     public AuditingManager(AuditingDao auditingDao, AuditCassandraDao cassandraDao) {
44         this.auditingDao = auditingDao;
45         this.cassandraDao = cassandraDao;
46     }
47
48
49     public String auditEvent(AuditEventFactory factory) {
50         if (ConfigurationManager.getConfigurationManager().getConfiguration().isDisableAudit()) {
51             return null;
52         }
53         String msg = factory.getLogMessage();
54         logAuditEvent(msg);
55
56         //TODO - remove this method after we got rid of ES
57         saveEventToElasticSearch(factory);
58         saveEventToCassandra(factory.getDbEvent());
59         return msg;
60     }
61
62     private void saveEventToCassandra(AuditingGenericEvent event) {
63         CassandraOperationStatus result = cassandraDao.saveRecord(event);
64         if (!result.equals(CassandraOperationStatus.OK)) {
65             log.warn("Failed to persist to cassandra auditing event: {}", result.name());
66         }
67     }
68
69     private void saveEventToElasticSearch(AuditEventFactory factory) {
70         ActionStatus addRecordStatus = auditingDao.addRecord(factory.getDbEvent(), factory.getAuditingEsType());
71         if (!addRecordStatus.equals(ActionStatus.OK)) {
72             log.warn("Failed to persist auditing event: {}", addRecordStatus.name());
73         }
74     }
75
76     private void logAuditEvent(final String formattedString) {
77         log.trace("logAuditEvent - start");
78         log.debug(formattedString);
79         LogFieldsMdcHandler.getInstance()
80                 .setAuditMessage(formattedString);
81         if (!LoggerSdcAudit.isFlowBeingTakenCare()){
82             log.debug("MOVED FROM AUDIT LOG: {}", formattedString);
83         }
84         log.trace("logAuditEvent - end");
85     }
86
87 }