CSIT Fix for SDC-2585
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / log / wrappers / LoggerSdcUtilBase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.common.log.wrappers;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.ws.rs.container.ContainerRequestContext;
29 import java.util.List;
30 import java.util.StringTokenizer;
31
32 import static java.net.HttpURLConnection.HTTP_BAD_METHOD;
33 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
34 import static java.net.HttpURLConnection.HTTP_CLIENT_TIMEOUT;
35 import static java.net.HttpURLConnection.HTTP_CONFLICT;
36 import static java.net.HttpURLConnection.HTTP_ENTITY_TOO_LARGE;
37 import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
38 import static java.net.HttpURLConnection.HTTP_GONE;
39 import static java.net.HttpURLConnection.HTTP_LENGTH_REQUIRED;
40 import static java.net.HttpURLConnection.HTTP_NOT_ACCEPTABLE;
41 import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
42 import static java.net.HttpURLConnection.HTTP_PAYMENT_REQUIRED;
43 import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
44 import static java.net.HttpURLConnection.HTTP_PROXY_AUTH;
45 import static java.net.HttpURLConnection.HTTP_REQ_TOO_LONG;
46 import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
47 import static java.net.HttpURLConnection.HTTP_UNSUPPORTED_TYPE;
48
49 /**
50  * Created by dd4296 on 12/20/2017.
51  * <p>
52  * base class for metric and audit log logging
53  * holding the specific logic for data extraction
54  */
55 public class LoggerSdcUtilBase {
56
57     private static final int SUCCESS_ERROR_CODE_LIMIT = 399;
58     private static final int BUSINESS_PROCESS_ERROR_BOUNDRY = 499;
59     protected static Logger log = LoggerFactory.getLogger(LoggerSdcUtilBase.class.getName());
60
61     String getRequestIDfromHeaders(List<Object> requestHeader) {
62         // this method gets list of type object.
63         // toString method returns the RequestId with brackets.
64         String requestHeaderString = requestHeader.toString();
65         return requestHeaderString.replace("[", "").replace("]", "");
66     }
67
68
69     // this method translates http error code to ECOMP Logger Error code
70     // this is a naive translation and is not a result of any documented format ECOMP specification
71     protected EcompLoggerErrorCode convertHttpCodeToErrorCode(int httpResponseCode) {
72         if (isSuccessError(httpResponseCode)) {
73             return EcompLoggerErrorCode.SUCCESS;
74         }
75
76         if (isSchemaError(httpResponseCode)) {
77             return EcompLoggerErrorCode.SCHEMA_ERROR;
78         }
79         if (isDataError(httpResponseCode)) {
80             return EcompLoggerErrorCode.DATA_ERROR;
81         }
82         if (isPermissionsError(httpResponseCode)) {
83             return EcompLoggerErrorCode.PERMISSION_ERROR;
84         }
85         if (isTimeoutOrAvailabilityError(httpResponseCode)) {
86             return EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR;
87         }
88         if (isBusinessProcessError(httpResponseCode)) {
89             return EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
90         }
91         return EcompLoggerErrorCode.UNKNOWN_ERROR;
92     }
93
94     private boolean isTimeoutOrAvailabilityError(int httpResponseCode) {
95
96         switch (httpResponseCode) {
97             case HTTP_BAD_REQUEST:
98             case HTTP_UNAUTHORIZED:
99             case HTTP_NOT_FOUND:
100             case HTTP_CLIENT_TIMEOUT:
101             case HTTP_GONE:
102                 return true;
103             default:
104                 return false;
105         }
106
107     }
108
109     private boolean isPermissionsError(int httpResponseCode) {
110
111         switch (httpResponseCode) {
112             case HTTP_PAYMENT_REQUIRED:
113             case HTTP_FORBIDDEN:
114             case HTTP_BAD_METHOD:
115             case HTTP_PROXY_AUTH:
116                 return true;
117
118             default:
119                 return false;
120         }
121     }
122
123     private boolean isDataError(int httpResponseCode) {
124
125         switch (httpResponseCode) {
126             case HTTP_NOT_ACCEPTABLE:
127             case HTTP_LENGTH_REQUIRED:
128             case HTTP_PRECON_FAILED:
129             case HTTP_REQ_TOO_LONG:
130             case HTTP_ENTITY_TOO_LARGE:
131             case HTTP_UNSUPPORTED_TYPE:
132                 return true;
133
134             default:
135                 return false;
136         }
137     }
138
139     private boolean isSchemaError(int httpResponseCode) {
140         return HTTP_CONFLICT == httpResponseCode;
141     }
142
143     private boolean isSuccessError(int httpResponseCode) {
144         return httpResponseCode < SUCCESS_ERROR_CODE_LIMIT;
145     }
146
147     private boolean isBusinessProcessError(int httpResponseCode) {
148         return httpResponseCode > BUSINESS_PROCESS_ERROR_BOUNDRY;
149     }
150
151     protected String getPartnerName(String userAgent, String userId, String url) {
152
153         if (!StringUtils.isEmpty(userId)) {
154             return userId;
155         }
156
157         String urlUser = getUserIdFromUrl(url);
158
159         if (!StringUtils.isEmpty(urlUser)) {
160             return urlUser;
161         }
162
163         String userAgentName = getUserIdFromUserAgent(userAgent);
164
165         if (!StringUtils.isEmpty(userAgentName)) {
166             return userAgentName;
167         }
168
169         return "";
170     }
171
172     private String getUserIdFromUserAgent(String userAgent) {
173         if (userAgent != null && userAgent.length() > 0) {
174             if (userAgent.toLowerCase().contains("firefox")) {
175                 return "fireFox_FE";
176             }
177
178             if (userAgent.toLowerCase().contains("msie")) {
179                 return "explorer_FE";
180             }
181
182             if (userAgent.toLowerCase().contains("chrome")) {
183                 return "chrome_FE";
184             }
185
186             return userAgent;
187         }
188         return null;
189     }
190
191     private String getUserIdFromUrl(String url) {
192         if (url != null && url.toLowerCase().contains("user")) {
193             StringTokenizer st = new StringTokenizer(url, "/");
194             while (st.hasMoreElements()) {
195                 if ("user".equalsIgnoreCase(st.nextToken())) {
196                     return st.nextToken();
197                 }
198             }
199         }
200         return null;
201     }
202
203     protected String getUrl(ContainerRequestContext requestContext) {
204         String url = "";
205
206         try {
207             if (requestContext.getUriInfo() != null && requestContext.getUriInfo().getRequestUri() != null) {
208                 url = requestContext.getUriInfo().getRequestUri().toURL().toString();
209             }
210         } catch (Exception ex) {
211             log.error("failed to get url from request context ", ex);
212         }
213
214         return url;
215     }
216
217     protected String getServiceName(ContainerRequestContext requestContext) {
218         return (requestContext.getUriInfo().getRequestUri().toString())
219                 .replace(requestContext.getUriInfo().getBaseUri().toString(), "/");
220     }
221 }