e8fb71a67e546ff30cab65b186b90e31eb08423c
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / AuditLogController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
42 import java.util.UUID;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.slf4j.MDC;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.web.bind.annotation.RestController;
52
53 import com.att.eelf.configuration.Configuration;
54
55 import org.onap.portalapp.controller.EPRestrictedBaseController;
56 import org.onap.portalapp.portal.domain.EPUser;
57 import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
58 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
59 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
60 import org.onap.portalapp.portal.utils.EcompPortalUtils;
61 import org.onap.portalapp.portal.utils.PortalConstants;
62 import org.onap.portalapp.util.EPUserUtils;
63 import org.onap.portalsdk.core.domain.AuditLog;
64 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
65 import org.onap.portalsdk.core.service.AuditService;
66 import org.onap.portalsdk.core.util.SystemProperties;
67
68 @RestController
69 @RequestMapping("/portalApi/auditLog")
70 public class AuditLogController extends EPRestrictedBaseController {
71         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardController.class);
72
73         @Autowired
74         private AuditService auditService;
75
76         /**
77          * Store audit log of the specified access type.
78          * 
79          * @param request
80          *            HttpServletRequest
81          * @param affectedAppId
82          *            App ID
83          * @param type
84          *            Access type
85          * @param comment
86          *            Comment
87          */
88         @RequestMapping(value = "/store", method = RequestMethod.GET, produces = "application/json")
89         public void auditLog(HttpServletRequest request, @RequestParam String affectedAppId, @RequestParam String type,
90                         @RequestParam String comment) {
91                 logger.debug(EELFLoggerDelegate.debugLogger, "auditLog: appId {}, type {], comment {}", affectedAppId, type,
92                                 comment);
93                 String cd_type = null;
94                 try {
95                         EPUser user = EPUserUtils.getUserSession(request);
96                         /* Check type of Activity CD */
97                         if (type.equals("app")) {
98                                 cd_type = AuditLog.CD_ACTIVITY_APP_ACCESS;
99                         } else if (type.equals("tab")) {
100                                 cd_type = AuditLog.CD_ACTIVITY_TAB_ACCESS;
101                         } else if (type.equals("functional")) {
102                                 cd_type = AuditLog.CD_ACTIVITY_FUNCTIONAL_ACCESS;
103                         } else if (type.equals("leftMenu")) {
104                                 cd_type = AuditLog.CD_ACTIVITY_LEFT_MENU_ACCESS;
105                         } else {
106                                 logger.error(EELFLoggerDelegate.errorLogger,
107                                                 "Storing auditLog failed! Activity CD type is not correct.");
108                         }
109                         /* Store the audit log only if it contains valid Activity CD */
110                         if (cd_type != null) {
111                                 AuditLog auditLog = new AuditLog();
112                                 auditLog.setActivityCode(cd_type);
113                                 /*
114                                  * Check affectedAppId and comment and see if these two values
115                                  * are valid
116                                  */
117                                 if (comment != null && !comment.equals("") && !comment.equals("undefined"))
118                                         auditLog.setComments(
119                                                         EcompPortalUtils.truncateString(comment, PortalConstants.AUDIT_LOG_COMMENT_SIZE));
120                                 if (affectedAppId != null && !affectedAppId.equals("") && !affectedAppId.equals("undefined"))
121                                         auditLog.setAffectedRecordId(affectedAppId);
122                                 long userId = EPUserUtils.getUserId(request);
123                                 auditLog.setUserId(userId);
124                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
125                                 MDC.put(EPCommonSystemProperties.PARTNER_NAME, EPCommonSystemProperties.ECOMP_PORTAL_FE);
126                                 MDC.put(Configuration.MDC_SERVICE_NAME, EPCommonSystemProperties.ECOMP_PORTAL_BE);
127                                 if (MDC.get(Configuration.MDC_KEY_REQUEST_ID) == null) {
128                                         String requestId = UUID.randomUUID().toString();
129                                         MDC.put(Configuration.MDC_KEY_REQUEST_ID, requestId);
130                                 }
131                                 // Log file
132                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
133                                 SimpleDateFormat ecompLogDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
134                                 String beginDateTime = MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
135                                 Date beginDate = ecompLogDateFormat.parse(beginDateTime);
136                                 auditService.logActivity(auditLog, null);
137                                 String endDateTime = MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
138                                 Date endDate = ecompLogDateFormat.parse(endDateTime);
139                                 String timeDifference = String.format("%d", endDate.getTime() - beginDate.getTime());
140                                 MDC.put(SystemProperties.MDC_TIMER, timeDifference);
141                                 MDC.put(EPCommonSystemProperties.STATUS_CODE, "COMPLETE");
142                                 logger.info(EELFLoggerDelegate.auditLogger, EPLogUtil.formatAuditLogMessage(
143                                                 "AuditLogController.auditLog", cd_type, user.getOrgUserId(), affectedAppId, comment));
144                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
145                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
146                         }
147                 } catch (Exception e) {
148                         logger.error(EELFLoggerDelegate.errorLogger, "auditLog failed", e);
149                         MDC.put(EPCommonSystemProperties.STATUS_CODE, "ERROR");
150                 } finally{
151                         MDC.remove(Configuration.MDC_SERVICE_NAME);
152                         MDC.remove(EPCommonSystemProperties.PARTNER_NAME);
153                         MDC.remove(SystemProperties.MDC_TIMER);
154                         MDC.remove(Configuration.MDC_KEY_REQUEST_ID);
155                         MDC.remove(EPCommonSystemProperties.STATUS_CODE);
156                 }
157         }
158
159 }