1 package org.openecomp.sdc.common.log.wrappers;
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;
8 import javax.ws.rs.container.ContainerRequestContext;
10 import java.util.StringTokenizer;
12 import static java.net.HttpURLConnection.*;
15 * Created by dd4296 on 12/20/2017.
17 * base class for metric and audit log logging
18 * holding the specific logic for data extraction
20 public class LoggerSdcUtilBase {
22 protected static Logger log = LoggerFactory.getLogger(LoggerSdcUtilBase.class.getName());
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("]","");
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;
40 if (isSchemaError(httpResponseCode)) {
41 return EcompLoggerErrorCode.SCHEMA_ERROR;
43 if (isDataError(httpResponseCode)) {
44 return EcompLoggerErrorCode.DATA_ERROR;
46 if (isPermissionsError(httpResponseCode)) {
47 return EcompLoggerErrorCode.PERMISSION_ERROR;
49 if (isTimeoutOrAvailabilityError(httpResponseCode)) {
50 return EcompLoggerErrorCode.AVAILABILITY_TIMEOUTS_ERROR;
52 if (isBusinessProcessError(httpResponseCode)) {
53 return EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
55 return EcompLoggerErrorCode.UNKNOWN_ERROR;
58 private boolean isTimeoutOrAvailabilityError(int httpResponseCode) {
60 switch (httpResponseCode) {
61 case HTTP_BAD_REQUEST:
62 case HTTP_UNAUTHORIZED:
64 case HTTP_CLIENT_TIMEOUT:
73 private boolean isPermissionsError(int httpResponseCode) {
75 switch (httpResponseCode) {
76 case HTTP_PAYMENT_REQUIRED:
86 private boolean isDataError(int httpResponseCode) {
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:
101 private boolean isSchemaError(int httpResponseCode) {
102 return HTTP_CONFLICT == httpResponseCode;
105 private boolean isSuccessError(int httpResponseCode) {
106 return httpResponseCode < 399;
109 private boolean isBusinessProcessError(int httpResponseCode) {
110 return httpResponseCode > 499;
113 protected String getPartnerName(String userAgent, String userId, String url, String xOnapPartnerName) {
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)) {
120 String urlUser = getUserIdFromUrl(url);
121 if (isFound(urlUser)) {
125 //Otherwise, if X-ONAP-PartnerName was provided, then log that
126 if (isFound(xOnapPartnerName)){
127 return xOnapPartnerName;
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;
139 private String getUserIdFromUserAgent(String userAgent) {
140 if (userAgent != null && userAgent.length() > 0) {
141 if (userAgent.toLowerCase().contains("firefox")) {
145 if (userAgent.toLowerCase().contains("msie")) {
146 return "explorer_FE";
149 if (userAgent.toLowerCase().contains("chrome")) {
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();
170 protected String getUrl(ContainerRequestContext requestContext) {
174 if (requestContext.getUriInfo() != null && requestContext.getUriInfo().getRequestUri() != null) {
175 url = requestContext.getUriInfo().getRequestUri().toURL().toString();
177 } catch (Exception ex) {
178 log.error("failed to get url from request context ", ex);
184 protected String getServiceName(ContainerRequestContext requestContext) {
185 return (requestContext.getUriInfo().getRequestUri().toString())
186 .replace(requestContext.getUriInfo().getBaseUri().toString(), "/");
189 private boolean isFound(String value) {
190 if (StringUtils.isNotEmpty(value)) {