lower code smells
[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  * 
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.onap.portalapp.validation.DataValidator;
47 import org.onap.portalapp.validation.SecureString;
48 import org.slf4j.MDC;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.GetMapping;
52 import org.springframework.web.bind.annotation.RequestParam;
53 import org.springframework.web.bind.annotation.RestController;
54
55 import com.att.eelf.configuration.Configuration;
56
57 import org.onap.portalapp.controller.EPRestrictedBaseController;
58 import org.onap.portalapp.portal.domain.EPUser;
59 import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
60 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
61 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
62 import org.onap.portalapp.portal.utils.EcompPortalUtils;
63 import org.onap.portalapp.portal.utils.PortalConstants;
64 import org.onap.portalapp.util.EPUserUtils;
65 import org.onap.portalsdk.core.domain.AuditLog;
66 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
67 import org.onap.portalsdk.core.service.AuditService;
68 import org.onap.portalsdk.core.util.SystemProperties;
69
70 @RestController
71 @RequestMapping("/portalApi/auditLog")
72 public class AuditLogController extends EPRestrictedBaseController {
73         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardController.class);
74         private static final DataValidator dataValidator = new DataValidator();
75
76         private AuditService auditService;
77         @Autowired
78         public AuditLogController(AuditService auditService) {
79                 this.auditService = auditService;
80         }
81
82         /**
83          * Store audit log of the specified access type.
84          *
85          * @param request
86          *            HttpServletRequest
87          * @param affectedAppId
88          *            App ID
89          * @param type
90          *            Access type
91          * @param comment
92          *            Comment
93          */
94         @GetMapping(value = "/store", produces = "application/json")
95         public void auditLog(HttpServletRequest request, @RequestParam String affectedAppId, @RequestParam String type,
96                         @RequestParam String comment) {
97                 logger.debug(EELFLoggerDelegate.debugLogger, "auditLog: appId {}, type {}, comment {}", affectedAppId, type,
98                                 comment);
99                 String cdType = null;
100
101                 SecureString secureString0 = new SecureString(affectedAppId);
102                 SecureString secureString1 = new SecureString(type);
103                 SecureString secureString2 = new SecureString(comment);
104                 if (  !dataValidator.isValid(secureString0)
105                         ||!dataValidator.isValid(secureString1)
106                         ||!dataValidator.isValid(secureString2)){
107                         return;
108                 }
109
110                 try {
111                         EPUser user = EPUserUtils.getUserSession(request);
112                         /* Check type of Activity CD */
113                         switch (type) {
114                                 case "app":
115                                         cdType = AuditLog.CD_ACTIVITY_APP_ACCESS;
116                                         break;
117                                 case "tab":
118                                         cdType = AuditLog.CD_ACTIVITY_TAB_ACCESS;
119                                         break;
120                                 case "functional":
121                                         cdType = AuditLog.CD_ACTIVITY_FUNCTIONAL_ACCESS;
122                                         break;
123                                 case "leftMenu":
124                                         cdType = AuditLog.CD_ACTIVITY_LEFT_MENU_ACCESS;
125                                         break;
126                                 default:
127                                         logger.error(EELFLoggerDelegate.errorLogger,
128                                                 "Storing auditLog failed! Activity CD type is not correct.");
129                                         break;
130                         }
131                         /* Store the audit log only if it contains valid Activity CD */
132                         if (cdType != null) {
133                                 AuditLog auditLog = new AuditLog();
134                                 auditLog.setActivityCode(cdType);
135                                 /*
136                                  * Check affectedAppId and comment and see if these two values
137                                  * are valid
138                                  */
139                                 if (comment != null && !comment.isEmpty() && !"undefined".equals(comment))
140                                         auditLog.setComments(
141                                                         EcompPortalUtils.truncateString(comment, PortalConstants.AUDIT_LOG_COMMENT_SIZE));
142                                 if (affectedAppId != null && !affectedAppId.isEmpty() && !"undefined".equals(affectedAppId))
143                                         auditLog.setAffectedRecordId(affectedAppId);
144                                 long userId = EPUserUtils.getUserId(request);
145                                 auditLog.setUserId(userId);
146                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
147                                 MDC.put(EPCommonSystemProperties.PARTNER_NAME, EPCommonSystemProperties.ECOMP_PORTAL_FE);
148                                 MDC.put(Configuration.MDC_SERVICE_NAME, EPCommonSystemProperties.ECOMP_PORTAL_BE);
149                                 if (MDC.get(Configuration.MDC_KEY_REQUEST_ID) == null) {
150                                         String requestId = UUID.randomUUID().toString();
151                                         MDC.put(Configuration.MDC_KEY_REQUEST_ID, requestId);
152                                 }
153                                 // Log file
154                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
155                                 SimpleDateFormat ecompLogDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
156                                 String beginDateTime = MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
157                                 Date beginDate = ecompLogDateFormat.parse(beginDateTime);
158                                 auditService.logActivity(auditLog, null);
159                                 String endDateTime = MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
160                                 Date endDate = ecompLogDateFormat.parse(endDateTime);
161                                 String timeDifference = String.format("%d", endDate.getTime() - beginDate.getTime());
162                                 MDC.put(SystemProperties.MDC_TIMER, timeDifference);
163                                 MDC.put(EPCommonSystemProperties.STATUS_CODE, "COMPLETE");
164                                 logger.info(EELFLoggerDelegate.auditLogger, EPLogUtil.formatAuditLogMessage(
165                                                 "AuditLogController.auditLog", cdType, user.getOrgUserId(), affectedAppId, comment));
166                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
167                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
168                         }
169                 } catch (Exception e) {
170                         logger.error(EELFLoggerDelegate.errorLogger, "auditLog failed", e);
171                         MDC.put(EPCommonSystemProperties.STATUS_CODE, "ERROR");
172                 } finally{
173                         MDC.remove(Configuration.MDC_SERVICE_NAME);
174                         MDC.remove(EPCommonSystemProperties.PARTNER_NAME);
175                         MDC.remove(SystemProperties.MDC_TIMER);
176                         MDC.remove(Configuration.MDC_KEY_REQUEST_ID);
177                         MDC.remove(EPCommonSystemProperties.STATUS_CODE);
178                 }
179         }
180
181 }