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