Remove unused files with company keywords
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / AuditLogController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.slf4j.MDC;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.bind.annotation.RequestParam;
29 import org.springframework.web.bind.annotation.RestController;
30
31 import org.openecomp.portalsdk.core.domain.AuditLog;
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalsdk.core.service.AuditService;
34 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
35 import org.openecomp.portalapp.portal.domain.EPUser;
36 import org.openecomp.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
37 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
38 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
39 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
40 import org.openecomp.portalapp.portal.utils.PortalConstants;
41 import org.openecomp.portalapp.util.EPUserUtils;
42
43 @RestController
44 @RequestMapping("/portalApi/auditLog")
45 public class AuditLogController extends EPRestrictedBaseController {
46         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardController.class);
47
48         @Autowired
49         private AuditService auditService;
50
51         /**
52          * Store audit log of the specified access type.
53          * 
54          * @param request
55          * @param affectedAppId
56          * @param type
57          * @param comment
58          */
59         @RequestMapping(value = "/store", method = RequestMethod.GET, produces = "application/json")
60         public void auditLog(HttpServletRequest request, @RequestParam String affectedAppId, @RequestParam String type,
61                         @RequestParam String comment) {
62                 logger.debug(EELFLoggerDelegate.debugLogger, "auditLog: appId {}, type {], comment {}", 
63                                 affectedAppId, type, comment);
64                 String cd_type = null;
65                 try {                                   
66                         EPUser user = EPUserUtils.getUserSession(request);
67                         /* Check type of Activity CD */
68                         if (type.equals("app")) {
69                                 cd_type = AuditLog.CD_ACTIVITY_APP_ACCESS;
70                         } else if (type.equals("tab")) {
71                                 cd_type = AuditLog.CD_ACTIVITY_TAB_ACCESS;
72                         } else if (type.equals("functional")) {
73                                 cd_type = AuditLog.CD_ACTIVITY_FUNCTIONAL_ACCESS;
74                         } else if (type.equals("leftMenu")) {
75                                 cd_type = AuditLog.CD_ACTIVITY_LEFT_MENU_ACCESS;
76                         } else {
77                                 logger.error(EELFLoggerDelegate.errorLogger, "Storing auditLog failed! Activity CD type is not correct.");
78                         }
79                         /* Store the audit log only if it contains valid Activity CD */
80                         if (cd_type != null) {
81                                 AuditLog auditLog = new AuditLog();
82                                 auditLog.setActivityCode(cd_type);
83                                 /*
84                                  * Check affectedAppId and comment and see if these two values are
85                                  * valid
86                                  */
87                                 if (comment != null && !comment.equals("") && !comment.equals("undefined"))
88                                         auditLog.setComments(EcompPortalUtils.truncateString(comment, PortalConstants.AUDIT_LOG_COMMENT_SIZE));
89                                 if (affectedAppId != null && !affectedAppId.equals("") && !affectedAppId.equals("undefined"))
90                                         auditLog.setAffectedRecordId(affectedAppId);
91                                 long userId = EPUserUtils.getUserId(request);
92                                 auditLog.setUserId(userId);
93                                 auditService.logActivity(auditLog, null);
94
95                                 // Log file
96                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP,EPEELFLoggerAdvice.getCurrentDateTimeUTC());          
97                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP,EPEELFLoggerAdvice.getCurrentDateTimeUTC());
98                                 logger.info(EELFLoggerDelegate.auditLogger, EPLogUtil.formatAuditLogMessage("AuditLogController.auditLog", 
99                                                 cd_type, user.getOrgUserId(), affectedAppId, comment)); 
100                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
101                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
102                         }
103                 } catch (Exception e) {
104                         logger.error(EELFLoggerDelegate.errorLogger, "auditLog failed", e);
105                 }
106         }
107
108 }