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