From e2c77490982e448fb94477cebc5b7d4b8110321d Mon Sep 17 00:00:00 2001 From: "Smokowski, Steve (ss835w)" Date: Tue, 4 Sep 2018 15:31:38 -0400 Subject: Client Logging Update Client Logging to use interceptor Move CXF to own project due to dependency hell Issue-ID: SO-947 Change-Id: I2597ff6d32231b8625b04ef63ab5e8f589e2545c Signed-off-by: Smokowski, Steve (ss835w) --- common/pom.xml | 16 +- .../main/java/org/onap/so/client/RestClient.java | 55 ++-- .../java/org/onap/so/client/RestClientSSL.java | 10 +- .../so/client/adapter/rest/AdapterRestClient.java | 2 +- .../org/onap/so/client/policy/LoggingFilter.java | 157 ---------- .../cxf/interceptor/SOAPLoggingInInterceptor.java | 96 ------ .../cxf/interceptor/SOAPLoggingOutInterceptor.java | 63 ---- .../logging/jaxrs/filter/JaxRsClientLogging.java | 163 +++++----- .../logging/jaxrs/filter/PayloadLoggingFilter.java | 159 ++++++++++ .../main/java/org/onap/so/utils/TargetEntity.java | 15 +- .../onap/so/adapter_utils/tests/MsoLoggerTest.java | 331 --------------------- .../java/org/onap/so/client/grm/GRMClientTest.java | 118 +++----- common/src/test/resources/logback-test.xml | 189 +++--------- common/src/test/resources/mso-bad.json | 20 -- common/src/test/resources/mso.json | 21 -- common/src/test/resources/mso.properties | 28 -- common/src/test/resources/mso2.json | 20 -- common/src/test/resources/mso2.properties | 22 -- 18 files changed, 373 insertions(+), 1112 deletions(-) delete mode 100644 common/src/main/java/org/onap/so/client/policy/LoggingFilter.java delete mode 100644 common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingInInterceptor.java delete mode 100644 common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingOutInterceptor.java create mode 100644 common/src/main/java/org/onap/so/logging/jaxrs/filter/PayloadLoggingFilter.java delete mode 100644 common/src/test/java/org/onap/so/adapter_utils/tests/MsoLoggerTest.java delete mode 100644 common/src/test/resources/mso-bad.json delete mode 100644 common/src/test/resources/mso.json delete mode 100644 common/src/test/resources/mso.properties delete mode 100644 common/src/test/resources/mso2.json delete mode 100644 common/src/test/resources/mso2.properties (limited to 'common') diff --git a/common/pom.xml b/common/pom.xml index 490f2c782e..d3f460e563 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -102,16 +102,7 @@ org.apache.cxf cxf-rt-rs-client ${cxf.version} - - - org.apache.cxf - cxf-rt-bindings-soap - ${cxf.version} - - - org.apache.cxf - cxf-rt-transports-http - ${cxf.version} + test com.shazam @@ -147,11 +138,6 @@ org.slf4j slf4j-api - - janino - janino - 2.5.15 - org.springframework.boot spring-boot-configuration-processor diff --git a/common/src/main/java/org/onap/so/client/RestClient.java b/common/src/main/java/org/onap/so/client/RestClient.java index cb8e1f0a91..007c666ac2 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -36,7 +36,6 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.function.Predicate; - import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation.Builder; @@ -46,29 +45,28 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriBuilder; - import org.onap.so.client.policy.CommonObjectMapperProvider; -import org.onap.so.client.policy.LoggingFilter; -import org.onap.so.logger.MsoLogger; import org.onap.so.logging.jaxrs.filter.JaxRsClientLogging; +import org.onap.so.logging.jaxrs.filter.PayloadLoggingFilter; import org.onap.so.utils.CryptoUtils; import org.onap.so.utils.TargetEntity; -import org.slf4j.MDC; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; - import net.jodah.failsafe.Failsafe; import net.jodah.failsafe.RetryPolicy; public abstract class RestClient { - public static final String ECOMP_COMPONENT_NAME = "MSO"; + private static final String APPLICATION_MERGE_PATCH_JSON = "application/merge-patch+json"; + + public static final String ECOMP_COMPONENT_NAME = "MSO"; private static final int MAX_PAYLOAD_SIZE = 1024 * 1024; private WebTarget webTarget; protected final Map headerMap; - protected final MsoLogger msoLogger; + protected final Logger logger = LoggerFactory.getLogger(RestClient.class); protected URL host; protected Optional path; protected String accept; @@ -79,11 +77,6 @@ public abstract class RestClient { protected RestClient(RestProperties props, Optional path) { - msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, RestClient.class); - this.requestId = MDC.get(MsoLogger.REQUEST_ID); - if (requestId == null) { - requestId = ""; - } headerMap = new HashMap<>(); try { host = props.getEndpoint(); @@ -99,24 +92,14 @@ public abstract class RestClient { this(props, path); this.accept = accept; this.contentType = contentType; - this.requestId = MDC.get(MsoLogger.REQUEST_ID); - if (requestId == null) { - requestId = ""; - } this.props = props; } protected RestClient(URL host, String contentType) { headerMap = new HashMap<>(); - - msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, RestClient.class); this.path = Optional.empty(); this.host = host; this.contentType = contentType; - this.requestId = MDC.get(MsoLogger.REQUEST_ID); - if (requestId == null) { - requestId = ""; - } this.props = new DefaultProperties(host); } @@ -143,15 +126,13 @@ public abstract class RestClient { if (webTarget == null) { initializeClient(getClient()); - } - Builder builder = webTarget.request(); - initializeHeaderMap(headerMap); - - headerMap.put("X-ECOMP-RequestID", requestId); - for (Entry entry : headerMap.entrySet()) { - builder.header(entry.getKey(), entry.getValue()); - } - return builder; + } + Builder builder = webTarget.request(); + initializeHeaderMap(headerMap); + for (Entry entry : headerMap.entrySet()) { + builder.header(entry.getKey(), entry.getValue()); + } + return builder; } protected WebTarget getWebTarget() { @@ -179,7 +160,7 @@ public abstract class RestClient { String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(decryptedAuth); headerMap.put("Authorization", authHeaderValue); } catch (GeneralSecurityException e) { - msoLogger.error(e.getMessage(), e); + logger.error(e.getMessage(), e); } } @@ -192,7 +173,7 @@ public abstract class RestClient { } protected String getMergeContentType() { - return "application/merge-patch+json"; + return APPLICATION_MERGE_PATCH_JSON; } protected Client getClient() { @@ -203,11 +184,11 @@ public abstract class RestClient { protected void initializeClient(Client client) { if (this.enableLogging()) { - client.register(new LoggingFilter(this.getMaxPayloadSize())); + client.register(new PayloadLoggingFilter(this.getMaxPayloadSize())); } CommonObjectMapperProvider provider = this.getCommonObjectMapperProvider(); client.register(new JacksonJsonProvider(provider.getMapper())); - + jaxRsClientLogging = new JaxRsClientLogging(); jaxRsClientLogging.setTargetService(getTargetEntity()); client.register(jaxRsClientLogging); diff --git a/common/src/main/java/org/onap/so/client/RestClientSSL.java b/common/src/main/java/org/onap/so/client/RestClientSSL.java index 8eaeee97ee..cb2839ae1f 100644 --- a/common/src/main/java/org/onap/so/client/RestClientSSL.java +++ b/common/src/main/java/org/onap/so/client/RestClientSSL.java @@ -32,7 +32,8 @@ import javax.ws.rs.client.ClientBuilder; public abstract class RestClientSSL extends RestClient { - public static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore"; + private static final String TRUE = "true"; + public static final String SSL_KEY_STORE_KEY = "javax.net.ssl.keyStore"; public static final String SSL_KEY_STORE_PASSWORD_KEY = "javax.net.ssl.keyStorePassword"; public static final String MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY = "mso.load.ssl.client.keystore"; @@ -51,19 +52,18 @@ public abstract class RestClientSSL extends RestClient { Client client = null; try { String loadSSLKeyStore = System.getProperty(RestClientSSL.MSO_LOAD_SSL_CLIENT_KEYSTORE_KEY); - if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase("true")) { + if(loadSSLKeyStore != null && loadSSLKeyStore.equalsIgnoreCase(TRUE)) { KeyStore ks = getKeyStore(); if(ks != null) { client = ClientBuilder.newBuilder().keyStore(ks, System.getProperty(RestClientSSL.SSL_KEY_STORE_PASSWORD_KEY)).build(); - this.msoLogger.debug("RestClientSSL not using default SSL context - setting keystore here."); + logger.debug("RestClientSSL not using default SSL context - setting keystore here."); return client; } } //Use default SSL context client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build(); - this.msoLogger.debug("RestClientSSL using default SSL context!"); + logger.info("RestClientSSL using default SSL context!"); } catch (NoSuchAlgorithmException e) { - //this.logger.error(MessageEnum.APIH_GENERAL_EXCEPTION, "AAI", "Client init", MsoLogger.ErrorCode.UnknownError, "could not create SSL client", e); throw new RuntimeException(e); } return client; diff --git a/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java b/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java index 2329a5acb9..9d72fdf85b 100644 --- a/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java +++ b/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java @@ -72,7 +72,7 @@ public class AdapterRestClient extends RestClient { encodedString = "Basic " + encodedString; return encodedString; } catch (GeneralSecurityException e) { - this.msoLogger.debug(e.getMessage()); + logger.error(e.getMessage(),e); return null; } } diff --git a/common/src/main/java/org/onap/so/client/policy/LoggingFilter.java b/common/src/main/java/org/onap/so/client/policy/LoggingFilter.java deleted file mode 100644 index 83cf08f77f..0000000000 --- a/common/src/main/java/org/onap/so/client/policy/LoggingFilter.java +++ /dev/null @@ -1,157 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.policy; - -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FilterOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - -import javax.annotation.Priority; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.client.ClientRequestContext; -import javax.ws.rs.client.ClientRequestFilter; -import javax.ws.rs.client.ClientResponseContext; -import javax.ws.rs.client.ClientResponseFilter; -import javax.ws.rs.ext.Provider; -import javax.ws.rs.ext.WriterInterceptor; -import javax.ws.rs.ext.WriterInterceptorContext; - -import org.onap.so.logger.MsoLogger; - - -@Provider -@Priority(0) -public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter, WriterInterceptor { - - private static final MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, LoggingFilter.class); - private static final String ENTITY_STREAM_PROPERTY = "LoggingFilter.entityStream"; - private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; - private final int maxEntitySize; - - public LoggingFilter() { - maxEntitySize = 1024 * 1024; - } - - public LoggingFilter(int maxPayloadSize) { - this.maxEntitySize = Integer.min(maxPayloadSize, 1024 * 1024); - } - - private void log(StringBuilder sb) { - logger.debug(sb.toString()); - } - - protected InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) - throws IOException { - if (!stream.markSupported()) { - stream = new BufferedInputStream(stream); - } - stream.mark(maxEntitySize + 1); - final byte[] entity = new byte[maxEntitySize + 1]; - final int entitySize = stream.read(entity); - if (entitySize != -1) { - b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset)); - } - if (entitySize > maxEntitySize) { - b.append("...more..."); - } - b.append('\n'); - stream.reset(); - return stream; - } - - @Override - public void filter(ClientRequestContext requestContext) throws IOException { - if (requestContext.hasEntity()) { - final OutputStream stream = new LoggingStream(requestContext.getEntityStream()); - requestContext.setEntityStream(stream); - requestContext.setProperty(ENTITY_STREAM_PROPERTY, stream); - } - String method = formatMethod(requestContext); - log(new StringBuilder("Making " + method + " request to: " + requestContext.getUri() + "\nRequest Headers: " + requestContext.getHeaders().toString())); - - } - - @Override - public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { - final StringBuilder sb = new StringBuilder(); - if (responseContext.hasEntity()) { - responseContext.setEntityStream(logInboundEntity(sb, responseContext.getEntityStream(), DEFAULT_CHARSET)); - String method = formatMethod(requestContext); - log(sb.insert(0, "Response from " + method + ": " + requestContext.getUri() + "\nResponse Headers: " + responseContext.getHeaders().toString())); - } - } - - @Override - public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { - final LoggingStream stream = (LoggingStream) context.getProperty(ENTITY_STREAM_PROPERTY); - context.proceed(); - if (stream != null) { - log(stream.getStringBuilder(DEFAULT_CHARSET)); - } - } - - private class LoggingStream extends FilterOutputStream { - - private final StringBuilder sb = new StringBuilder(); - private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - LoggingStream(OutputStream out) { - super(out); - } - - StringBuilder getStringBuilder(Charset charset) { - // write entity to the builder - final byte[] entity = baos.toByteArray(); - - sb.append(new String(entity, 0, entity.length, charset)); - if (entity.length > maxEntitySize) { - sb.append("...more..."); - } - sb.append('\n'); - - return sb; - } - - @Override - public void write(final int i) throws IOException { - if (baos.size() <= maxEntitySize) { - baos.write(i); - } - out.write(i); - } - } - - private String formatMethod(ClientRequestContext requestContext) { - String method = requestContext.getHeaderString("X-HTTP-Method-Override"); - if (method == null) { - method = requestContext.getMethod(); - } else { - method = requestContext.getMethod() + " (overridden to " + method + ")"; - } - - return method; - } -} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingInInterceptor.java b/common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingInInterceptor.java deleted file mode 100644 index 9aed537668..0000000000 --- a/common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingInInterceptor.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.onap.so.logging.cxf.interceptor; - - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.stream.Collectors; -import javax.servlet.http.HttpServletRequest; -import javax.xml.namespace.QName; -import org.apache.cxf.binding.soap.SoapMessage; -import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; -import org.apache.cxf.interceptor.Fault; -import org.apache.cxf.message.Message; -import org.apache.cxf.phase.Phase; -import org.apache.cxf.service.model.MessageInfo; -import org.apache.cxf.service.model.OperationInfo; -import org.apache.cxf.transport.http.AbstractHTTPDestination; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.onap.so.logging.jaxrs.filter.JaxRsFilterLogging; -import org.onap.so.logging.jaxrs.filter.MDCSetup; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class SOAPLoggingInInterceptor extends AbstractSoapInterceptor{ - - protected static Logger logger = LoggerFactory.getLogger(SOAPLoggingInInterceptor.class); - - @Autowired - MDCSetup mdcSetup; - - public SOAPLoggingInInterceptor() { - super(Phase.READ); - } - - @Override - public void handleMessage(SoapMessage message) throws Fault { - try { - - Map> headers = (Map>) message.get(Message.PROTOCOL_HEADERS); - HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST); - request.getRemoteAddr(); - - setRequestId(headers); - setInvocationId(headers); - setServiceName(message); - setMDCPartnerName(headers); - mdcSetup.setServerFQDN(); - mdcSetup.setClientIPAddress(request); - mdcSetup.setInstanceUUID(); - mdcSetup.setEntryTimeStamp(); - MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, "INPROGRESS"); - logger.info(ONAPLogConstants.Markers.ENTRY, "Entering"); - } catch (Exception e) { - logger.warn("Error in incoming SOAP Message Inteceptor", e); - } - } - - private void setServiceName(SoapMessage message) { - String requestURI = (String) message.get(Message.REQUEST_URI); - MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, requestURI); - } - - //CXF Appears to flatten headers to lower case - private void setMDCPartnerName(Map> headers){ - String partnerName=getValueOrDefault(headers, ONAPLogConstants.Headers.PARTNER_NAME.toLowerCase(),""); - MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME,partnerName); - } - - private void setInvocationId(Map> headers) { - String invocationId=getValueOrDefault(headers, ONAPLogConstants.Headers.INVOCATION_ID.toLowerCase(),UUID.randomUUID().toString()); - MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); - } - - private void setRequestId(Map> headers) { - String requestId=getValueOrDefault(headers, ONAPLogConstants.Headers.REQUEST_ID.toLowerCase(),UUID.randomUUID().toString()); - MDC.put(ONAPLogConstants.MDCs.REQUEST_ID,requestId); - } - - private String getValueOrDefault(Map> headers, String headerName, String defaultValue){ - String headerValue; - List headerList=headers.get(headerName); - if(headerList != null && !headerList.isEmpty()){ - headerValue= headerList.get(0); - if(headerValue == null || headerValue.isEmpty()) - headerValue = defaultValue; - }else - headerValue = defaultValue; - return headerValue; - } - -} diff --git a/common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingOutInterceptor.java b/common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingOutInterceptor.java deleted file mode 100644 index c50e505cd8..0000000000 --- a/common/src/main/java/org/onap/so/logging/cxf/interceptor/SOAPLoggingOutInterceptor.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.onap.so.logging.cxf.interceptor; - - -import java.util.Collections; -import java.util.Map; -import java.util.UUID; -import java.util.stream.Collectors; -import javax.servlet.http.HttpServletRequest; -import javax.xml.namespace.QName; -import org.apache.cxf.binding.soap.SoapMessage; -import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; -import org.apache.cxf.interceptor.Fault; -import org.apache.cxf.message.Message; -import org.apache.cxf.phase.Phase; -import org.apache.cxf.service.model.MessageInfo; -import org.apache.cxf.service.model.OperationInfo; -import org.apache.cxf.transport.http.AbstractHTTPDestination; -import org.onap.logging.ref.slf4j.ONAPLogConstants; -import org.onap.so.exceptions.MSOException; -import org.onap.so.logging.jaxrs.filter.JaxRsFilterLogging; -import org.onap.so.logging.jaxrs.filter.MDCSetup; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class SOAPLoggingOutInterceptor extends AbstractSoapInterceptor{ - - private static final String _500 = "500"; - - protected static Logger logger = LoggerFactory.getLogger(SOAPLoggingOutInterceptor.class); - - @Autowired - MDCSetup mdcSetup; - - public SOAPLoggingOutInterceptor() { - super(Phase.WRITE); - } - - @Override - public void handleMessage(SoapMessage message) throws Fault { - try { - Exception ex = message.getContent(Exception.class); - if (ex == null) { - MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.COMPLETED.toString()); - }else{ - int responseCode = 0; - responseCode = (int) message.get(Message.RESPONSE_CODE); - if(responseCode != 0 ) - MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(responseCode)); - else - MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, _500); - - MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString()); - } - logger.info(ONAPLogConstants.Markers.EXIT, "Exiting"); - } catch (Exception e) { - logger.warn("Error in incoming SOAP Message Inteceptor", e); - } - } -} diff --git a/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java b/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java index 2cb375bed8..49dc71e773 100644 --- a/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java +++ b/common/src/main/java/org/onap/so/logging/jaxrs/filter/JaxRsClientLogging.java @@ -22,104 +22,126 @@ package org.onap.so.logging.jaxrs.filter; import org.apache.commons.io.IOUtils; -import org.onap.so.logger.MsoLogger; +import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.onap.so.utils.TargetEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.slf4j.MDC; +import org.slf4j.MarkerFactory; import org.springframework.stereotype.Component; - +import javax.annotation.Priority; import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.client.ClientRequestFilter; import javax.ws.rs.client.ClientResponseContext; import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; - +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Providers; import java.io.*; - -import java.time.Instant; -import java.time.ZoneId; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.Locale; +import java.util.Map; import java.util.UUID; @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @Component +@Priority(0) public class JaxRsClientLogging implements ClientRequestFilter,ClientResponseFilter { - - private static MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA,JaxRsClientLogging.class); + + @Context + private Providers providers; - private TargetEntity targetEntity; + private static final String TRACE = "trace-#"; + private static final String SO = "SO"; + private static Logger logger = LoggerFactory.getLogger(JaxRsClientLogging.class); - public void setTargetService(TargetEntity targetEntity){ - this.targetEntity = targetEntity; + public void setTargetService(TargetEntity targetEntity){ + MDC.put("TargetEntity", targetEntity.toString()); } - @Override - public void filter(ClientRequestContext clientRequest) { + @Override + public void filter(ClientRequestContext clientRequest) { try{ - MultivaluedMap headers = clientRequest.getHeaders(); - - - Instant instant = Instant.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) - .withLocale( Locale.US ) - .withZone( ZoneId.systemDefault() ); - - String requestId = MDC.get(MsoLogger.REQUEST_ID); - if(requestId == null || requestId.isEmpty()){ - requestId = UUID.randomUUID().toString(); - logger.warnSimple(clientRequest.getUri().getPath(),"Could not Find Request ID Generating New One"); - } - - MDC.put(MsoLogger.METRIC_BEGIN_TIME, formatter.format(instant)); - MDC.put(MsoLogger.METRIC_START_TIME, String.valueOf(System.currentTimeMillis())); - MDC.put(MsoLogger.REQUEST_ID,requestId); - MDC.put(MsoLogger.TARGETSERVICENAME, clientRequest.getUri().toString()); + setupMDC(clientRequest); + setupHeaders(clientRequest); + logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke"); } catch (Exception e) { - logger.warnSimple("Error in incoming JAX-RS Inteceptor", e); - } - } + logger.warn("Error in incoming JAX-RS Inteceptor", e); + } + } + + private void setupHeaders(ClientRequestContext clientRequest) { + MultivaluedMap headers = clientRequest.getHeaders(); + headers.add(ONAPLogConstants.Headers.REQUEST_ID, extractRequestID(clientRequest)); + headers.add(ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + headers.add(ONAPLogConstants.Headers.PARTNER_NAME, SO); + } + + private void setupMDC(ClientRequestContext clientRequest) { + MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); + MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, clientRequest.getUri().toString()); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString()); + setInvocationId(); + MDC.put("TargetEntity",MDC.get("TargetEntity")); + } + private String extractRequestID(ClientRequestContext clientRequest) { + String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID); + if(requestId == null || requestId.isEmpty() || requestId.equals(TRACE)){ + requestId = UUID.randomUUID().toString(); + logger.warn("Could not Find Request ID Generating New One: {}",clientRequest.getUri().getPath()); + } + return requestId; + } + + private void setInvocationId() { + String invocationId = MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID); + if(invocationId == null || invocationId.isEmpty()) + invocationId =UUID.randomUUID().toString(); + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); + } - @Override - public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) { + + @Override + public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) { try { - Instant instant = Instant.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ) - .withLocale( Locale.US ) - .withZone( ZoneId.systemDefault() ); - String startTime= MDC.get(MsoLogger.METRIC_START_TIME); - - long elapsedTime = System.currentTimeMillis()-Long.parseLong(startTime); - String statusCode; - if(Response.Status.Family.familyOf(responseContext.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){ - statusCode=MsoLogger.COMPLETE; - }else{ - statusCode=MsoLogger.StatusCode.ERROR.toString(); - } - MultivaluedMap headers = responseContext.getHeaders(); - - String partnerName = headers.getFirst(MsoLogger.HEADER_FROM_APP_ID ); - if(partnerName == null || partnerName.isEmpty()) - partnerName="UNKNOWN"; - MDC.put(MsoLogger.RESPONSEDESC,getStringFromInputStream(responseContext)); - MDC.put(MsoLogger.STATUSCODE, statusCode); - MDC.put(MsoLogger.RESPONSECODE,String.valueOf(responseContext.getStatus())); - MDC.put(MsoLogger.METRIC_TIMER, String.valueOf(elapsedTime)); - MDC.put(MsoLogger.METRIC_END_TIME,formatter.format(instant)); - MDC.put(MsoLogger.PARTNERNAME,partnerName); - MDC.put(MsoLogger.TARGETENTITY, targetEntity.toString()); - logger.recordMetricEvent(); - } catch ( Exception e) { - logger.warnSimple("Error in outgoing JAX-RS Inteceptor", e); - } - } + String statusCode; + if(Response.Status.Family.familyOf(responseContext.getStatus()).equals(Response.Status.Family.SUCCESSFUL)){ + statusCode=ONAPLogConstants.ResponseStatus.COMPLETED.toString(); + }else{ + statusCode=ONAPLogConstants.ResponseStatus.ERROR.toString(); + } + MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(responseContext.getStatus())); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION,getStringFromInputStream(responseContext)); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode); + logger.info(MarkerFactory.getMarker("INVOKE_RETURN"), "InvokeReturn"); + clearClientMDCs(); + } catch ( Exception e) { + logger.warn("Error in outgoing JAX-RS Inteceptor", e); + } + } + + private void clearClientMDCs() { + MDC.remove(ONAPLogConstants.MDCs.INVOCATION_ID); + MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION); + MDC.remove(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE); + MDC.remove(ONAPLogConstants.MDCs.RESPONSE_DESCRIPTION); + MDC.remove(ONAPLogConstants.MDCs.RESPONSE_CODE); + } private static String getStringFromInputStream(ClientResponseContext clientResponseContext) { - InputStream is = clientResponseContext.getEntityStream(); - ByteArrayOutputStream boas = new ByteArrayOutputStream(); + InputStream is = clientResponseContext.getEntityStream(); + ByteArrayOutputStream boas = new ByteArrayOutputStream(); try { IOUtils.copy(is,boas); @@ -128,8 +150,9 @@ public class JaxRsClientLogging implements ClientRequestFilter,ClientResponseFil return boas.toString(); } catch (IOException e) { - logger.warnSimple("Failed to read response body", e); + logger.warn("Failed to read response body", e); } return "Unable to read input stream"; } + } diff --git a/common/src/main/java/org/onap/so/logging/jaxrs/filter/PayloadLoggingFilter.java b/common/src/main/java/org/onap/so/logging/jaxrs/filter/PayloadLoggingFilter.java new file mode 100644 index 0000000000..29264f4e42 --- /dev/null +++ b/common/src/main/java/org/onap/so/logging/jaxrs/filter/PayloadLoggingFilter.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.logging.jaxrs.filter; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +import javax.annotation.Priority; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.ext.Provider; +import javax.ws.rs.ext.WriterInterceptor; +import javax.ws.rs.ext.WriterInterceptorContext; + +import org.onap.so.logger.MsoLogger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +@Provider +@Priority(1) +public class PayloadLoggingFilter implements ClientRequestFilter, ClientResponseFilter, WriterInterceptor { + + private static final Logger logger = LoggerFactory.getLogger(PayloadLoggingFilter.class); + private static final String ENTITY_STREAM_PROPERTY = "LoggingFilter.entityStream"; + private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + private final int maxEntitySize; + + public PayloadLoggingFilter() { + maxEntitySize = 1024 * 1024; + } + + public PayloadLoggingFilter(int maxPayloadSize) { + this.maxEntitySize = Integer.min(maxPayloadSize, 1024 * 1024); + } + + private void log(StringBuilder sb) { + logger.debug(sb.toString()); + } + + protected InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) + throws IOException { + if (!stream.markSupported()) { + stream = new BufferedInputStream(stream); + } + stream.mark(maxEntitySize + 1); + final byte[] entity = new byte[maxEntitySize + 1]; + final int entitySize = stream.read(entity); + if (entitySize != -1) { + b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset)); + } + if (entitySize > maxEntitySize) { + b.append("...more..."); + } + b.append('\n'); + stream.reset(); + return stream; + } + + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + if (requestContext.hasEntity()) { + final OutputStream stream = new LoggingStream(requestContext.getEntityStream()); + requestContext.setEntityStream(stream); + requestContext.setProperty(ENTITY_STREAM_PROPERTY, stream); + } + String method = formatMethod(requestContext); + log(new StringBuilder("Making " + method + " request to: " + requestContext.getUri() + "\nRequest Headers: " + requestContext.getHeaders().toString())); + + } + + @Override + public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { + final StringBuilder sb = new StringBuilder(); + if (responseContext.hasEntity()) { + responseContext.setEntityStream(logInboundEntity(sb, responseContext.getEntityStream(), DEFAULT_CHARSET)); + String method = formatMethod(requestContext); + log(sb.insert(0, "Response from " + method + ": " + requestContext.getUri() + "\nResponse Headers: " + responseContext.getHeaders().toString())); + } + } + + @Override + public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { + final LoggingStream stream = (LoggingStream) context.getProperty(ENTITY_STREAM_PROPERTY); + context.proceed(); + if (stream != null) { + log(stream.getStringBuilder(DEFAULT_CHARSET)); + } + } + + private class LoggingStream extends FilterOutputStream { + + private final StringBuilder sb = new StringBuilder(); + private final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + LoggingStream(OutputStream out) { + super(out); + } + + StringBuilder getStringBuilder(Charset charset) { + // write entity to the builder + final byte[] entity = baos.toByteArray(); + + sb.append(new String(entity, 0, entity.length, charset)); + if (entity.length > maxEntitySize) { + sb.append("...more..."); + } + sb.append('\n'); + + return sb; + } + + @Override + public void write(final int i) throws IOException { + if (baos.size() <= maxEntitySize) { + baos.write(i); + } + out.write(i); + } + } + + private String formatMethod(ClientRequestContext requestContext) { + String method = requestContext.getHeaderString("X-HTTP-Method-Override"); + if (method == null) { + method = requestContext.getMethod(); + } else { + method = requestContext.getMethod() + " (overridden to " + method + ")"; + } + + return method; + } +} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/utils/TargetEntity.java b/common/src/main/java/org/onap/so/utils/TargetEntity.java index ad76e56a56..84dae957ba 100644 --- a/common/src/main/java/org/onap/so/utils/TargetEntity.java +++ b/common/src/main/java/org/onap/so/utils/TargetEntity.java @@ -20,13 +20,22 @@ package org.onap.so.utils; +import java.util.EnumSet; public enum TargetEntity { OPENSTACK_ADAPTER, BPMN, GRM ,AAI, DMAAP, POLICY, CATALOG_DB, REQUEST_DB, VNF_ADAPTER, SDNC_ADAPTER, NARAD; - private static final String PREFIX = "MSO"; + private static final String PREFIX = "SO"; + + public static EnumSet getSOInternalComponents() { + return EnumSet.of(OPENSTACK_ADAPTER, BPMN,CATALOG_DB,REQUEST_DB,VNF_ADAPTER,SDNC_ADAPTER); + } + @Override public String toString(){ - return TargetEntity.PREFIX + "." + this.name(); + if(getSOInternalComponents().contains(this)) + return TargetEntity.PREFIX + "." + this.name(); + else + return this.name(); } -} +} \ No newline at end of file diff --git a/common/src/test/java/org/onap/so/adapter_utils/tests/MsoLoggerTest.java b/common/src/test/java/org/onap/so/adapter_utils/tests/MsoLoggerTest.java deleted file mode 100644 index 29ac91c864..0000000000 --- a/common/src/test/java/org/onap/so/adapter_utils/tests/MsoLoggerTest.java +++ /dev/null @@ -1,331 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.adapter_utils.tests; - -import org.onap.so.entity.MsoRequest; -import org.onap.so.logger.MessageEnum; -import org.onap.so.logger.MsoLogger; -import org.onap.so.logger.MsoLogger.ErrorCode; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import org.slf4j.MDC; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.PrintWriter; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URL; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; - -/** - * This class implements all test methods of the MsoLogger features. - * - * - */ -public class MsoLoggerTest { - - static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, MsoLoggerTest.class); - - /** - * This method is called before any test occurs. It creates a fake tree from - * scratch - */ - @BeforeClass - public static final void prepare() { - - } - - @Before - public final void cleanErrorLogFile() throws FileNotFoundException { - URL url = this.getClass().getClassLoader().getResource("logback-test.xml"); - String logFile = url.getFile().substring(0, url.getFile().indexOf("test-classes")) - + "/MSO/Test/errorjboss.server.name_IS_UNDEFINED.log"; - PrintWriter asdcConfigFileWriter = new PrintWriter(logFile); - asdcConfigFileWriter.print(""); - asdcConfigFileWriter.flush(); - asdcConfigFileWriter.close(); - } - - @Before - public final void cleanMetricLogFile() throws FileNotFoundException { - URL url = this.getClass().getClassLoader().getResource("logback-test.xml"); - String logFile = url.getFile().substring(0, url.getFile().indexOf("test-classes")) - + "/MSO/Test/metricsjboss.server.name_IS_UNDEFINED.log"; - PrintWriter asdcConfigFileWriter = new PrintWriter(logFile); - asdcConfigFileWriter.print(""); - asdcConfigFileWriter.flush(); - asdcConfigFileWriter.close(); - } - - @Before - public final void cleanAuditLogFile() throws FileNotFoundException { - URL url = this.getClass().getClassLoader().getResource("logback-test.xml"); - String logFile = url.getFile().substring(0, url.getFile().indexOf("test-classes")) - + "/MSO/Test/auditjbo.server.name_IS_UNDEFINED.log"; - PrintWriter asdcConfigFileWriter = new PrintWriter(logFile); - asdcConfigFileWriter.print(""); - asdcConfigFileWriter.flush(); - asdcConfigFileWriter.close(); - } - - /** - * This method implements a test of getSeverifyLevel method. - */ - @Test - public final void testGetSeverityLevel() { - - try { - String levelInfo = (String) invokePriveMethod("getSeverityLevel", "INFO"); - Assert.assertEquals(levelInfo, "0"); - - String levelWarn = (String) invokePriveMethod("getSeverityLevel", "WARN"); - Assert.assertEquals(levelWarn, "1"); - - String levelERROR = (String) invokePriveMethod("getSeverityLevel", "ERROR"); - Assert.assertEquals(levelERROR, "2"); - - String levelDEBUG = (String) invokePriveMethod("getSeverityLevel", "DEBUG"); - Assert.assertEquals(levelDEBUG, "0"); - - String levelFATAL = (String) invokePriveMethod("getSeverityLevel", "FATAL"); - Assert.assertEquals(levelFATAL, "3"); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * This method implements a test of getFinalServiceName method. - */ - @Test - public final void testGetFinalServiceName() { - try { - String serviceName1 = (String) invokePriveMethod("getFinalServiceName", "testServiceName1"); - Assert.assertEquals(serviceName1, "testServiceName1"); - - MsoLogger.setServiceName("testServiceName2"); - String serviceName2 = (String) invokePriveMethod("getFinalServiceName", "testServiceName1"); - Assert.assertEquals(serviceName2, "testServiceName1"); - - String msgNull = null; - String serviceName3 = (String) invokePriveMethod("getFinalServiceName", msgNull); - Assert.assertEquals(serviceName3, "testServiceName2"); - - MsoLogger.resetServiceName(); - String serviceName4 = (String) invokePriveMethod("getFinalServiceName", msgNull); - Assert.assertEquals(serviceName4, "invoke0"); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public final void testPrepareMsg() { - try { - String msgNull = null; - MDC.clear(); - invokePrepareMsg("INFO", null, null); - - Assert.assertTrue(MDC.get(MsoLogger.REQUEST_ID).equals("trace-#") - && MDC.get(MsoLogger.SERVICE_INSTANCE_ID).equals("trace-#") - && MDC.get(MsoLogger.SERVICE_NAME).equals("invoke0") && MDC.get(MsoLogger.TIMER) == null - && MDC.get(MsoLogger.ALERT_SEVERITY).equals("0")); - - MsoLogger.setLoggerParameters("testRemoteIp", "testUser"); - MsoLogger.setLogContext("testReqId", "testSvcId"); - invokePrepareMsg("ERROR", "testServiceName3", null); - Assert.assertTrue(MDC.get(MsoLogger.REQUEST_ID).equals("testReqId") - && MDC.get(MsoLogger.SERVICE_INSTANCE_ID).equals("testSvcId") - && MDC.get(MsoLogger.SERVICE_NAME).equals("testServiceName3") && MDC.get(MsoLogger.TIMER) == null - && MDC.get(MsoLogger.ALERT_SEVERITY).equals("2")); - - MsoLogger.setServiceName("testServiceName2"); - invokePrepareMsg("WARN", msgNull, msgNull); - Assert.assertTrue(MDC.get(MsoLogger.REQUEST_ID).equals("testReqId") - && MDC.get(MsoLogger.SERVICE_INSTANCE_ID).equals("testSvcId") - && MDC.get(MsoLogger.SERVICE_NAME).equals("testServiceName2") && MDC.get(MsoLogger.TIMER) == null - && MDC.get(MsoLogger.ALERT_SEVERITY).equals("1")); - - MDC.clear(); - MsoRequest msoRequest = new MsoRequest(); - msoRequest.setRequestId("reqId2"); - msoRequest.setServiceInstanceId("servId2"); - MsoLogger.setLogContext(msoRequest); - invokePrepareMsg("FATAL", null, "123"); - Assert.assertTrue(MDC.get(MsoLogger.REQUEST_ID).equals("reqId2") - && MDC.get(MsoLogger.SERVICE_INSTANCE_ID).equals("servId2") - && MDC.get(MsoLogger.TIMER).equals("123") && MDC.get(MsoLogger.ALERT_SEVERITY).equals("3")); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * This method implements a test of log methods - */ - @Test - @Ignore - public final void testLogMethods() { - try { - MDC.clear(); - MsoLogger.setLogContext("reqId2", "servId2"); - MsoLogger.setServiceName("MSO.testServiceName"); - msoLogger.info(MessageEnum.LOGGER_UPDATE_SUC, "testLogger", "INFO", "DEBUG", "target entity", - "target service"); - msoLogger.warn(MessageEnum.GENERAL_WARNING, "warning test", "", "", MsoLogger.ErrorCode.UnknownError, - "warning test"); - msoLogger.error(MessageEnum.GENERAL_EXCEPTION, "target entity", "target service", - MsoLogger.ErrorCode.UnknownError, "error test"); - - // Fetch from the error log - URL url = this.getClass().getClassLoader().getResource("logback-test.xml"); - String logFile = url.getFile().substring(0, url.getFile().indexOf("test-classes")) - + "/MSO/Test/errorjboss.server.name_IS_UNDEFINED.log"; - - Path filePath = new File(logFile).toPath(); - Charset charset = Charset.defaultCharset(); - List stringList = Files.readAllLines(filePath, charset); - String[] stringArray = stringList.toArray(new String[] {}); - int size = stringArray.length; - - Assert.assertTrue(stringArray[size - 3] - .contains("|reqId2|main|MSO.testServiceName||target entity|target service|INFO|null||") - && stringArray[size - 3].contains( - "||MSO-GENERAL-5408I Successfully update Logger: testLogger from level INFO to level DEBUG")); - Assert.assertTrue(stringArray[size - 2] - .contains("|reqId2|main|MSO.testServiceName||||WARN|UnknownError|warning test|") - && stringArray[size - 2].contains("|MSO-GENERAL-5401W WARNING: warning test")); - Assert.assertTrue(stringArray[size - 1].contains( - "|reqId2|main|MSO.testServiceName||target entity|target service|ERROR|UnknownError|error test|") - && stringArray[size - 1].contains("|MSO-GENERAL-9401E Exception encountered")); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * This method implements a test of recordMetricEvent method. - * - * @throws IOException - */ - @Test - @Ignore - public final void testRecordMetricEvent() throws IOException { - MDC.clear(); - MsoLogger.setLogContext("reqId", "servId"); - msoLogger.recordMetricEvent(123456789L, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful", - "VNF", "createVNF", null); - MDC.put(MsoLogger.REMOTE_HOST, "127.0.0.1"); - MDC.put(MsoLogger.PARTNERNAME, "testUser"); - msoLogger.recordMetricEvent(123456789L, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.ServiceNotAvailable, - "Exception", "SDNC", "removeSDNC", "testVNF"); - - // Fetch from the metric log - URL url = this.getClass().getClassLoader().getResource("logback-test.xml"); - String logFile = url.getFile().substring(0, url.getFile().indexOf("test-classes")) - + "/MSO/Test/metricsjboss.server.name_IS_UNDEFINED.log"; - - Path filePath = new File(logFile).toPath(); - Charset charset = Charset.defaultCharset(); - List stringList = Files.readAllLines(filePath, charset); - String[] stringArray = stringList.toArray(new String[] {}); - msoLogger.error(MessageEnum.GENERAL_EXCEPTION, "", "", MsoLogger.ErrorCode.UnknownError, "test error msg"); - - Assert.assertTrue(stringArray[0] - .contains("|reqId|servId|main||testRecordMetricEvent||VNF|createVNF|COMPLETE|0|Successful|")); - // count the occurance of symbol "|" - Assert.assertTrue((stringArray[0].length() - stringArray[0].replace("|", "").length()) == 28); - Assert.assertTrue(stringArray[1] - .contains("|reqId|servId|main||testRecordMetricEvent|testUser|SDNC|removeSDNC|ERROR|501|Exception|") - && stringArray[1].contains("|127.0.0.1||||testVNF|||||")); - Assert.assertTrue((stringArray[1].length() - stringArray[1].replace("|", "").length()) == 28); - } - - /** - * This method implements a test of testRecordAuditEvent method. - */ - - // User reflection to invoke to avoid change the publicity of the method - private static String invokePrepareMsg(String arg1, String arg2, String arg3) { - Method method; - try { - method = MsoLogger.class.getDeclaredMethod("prepareMsg", String.class, String.class, String.class); - method.setAccessible(true); - return (String) method.invoke(msoLogger, arg1, arg2, arg3); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } - - // User reflection to invoke to avoid change the publicity of the method - private static Object invokePriveMethod(String methodName, String arg) { - Method method; - try { - method = MsoLogger.class.getDeclaredMethod(methodName, String.class); - method.setAccessible(true); - return method.invoke(msoLogger, arg); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } -} diff --git a/common/src/test/java/org/onap/so/client/grm/GRMClientTest.java b/common/src/test/java/org/onap/so/client/grm/GRMClientTest.java index 3cce6a626b..7b783762fa 100644 --- a/common/src/test/java/org/onap/so/client/grm/GRMClientTest.java +++ b/common/src/test/java/org/onap/so/client/grm/GRMClientTest.java @@ -24,9 +24,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static com.github.tomakehurst.wiremock.client.WireMock.*; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; - +import static org.junit.Assert.fail; import java.io.File; import java.nio.file.Files; import java.util.ArrayList; @@ -43,6 +45,7 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.onap.so.client.grm.beans.OperationalInfo; import org.onap.so.client.grm.beans.Property; import org.onap.so.client.grm.beans.ServiceEndPoint; @@ -54,8 +57,7 @@ import org.onap.so.client.grm.exceptions.GRMClientCallFailed; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.junit.WireMockRule; -import org.onap.so.logger.MsoLogger; -import org.onap.so.logger.MsoLogger.Catalog; + import org.onap.so.utils.TestAppender; public class GRMClientTest { @@ -66,6 +68,8 @@ public class GRMClientTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private static final String uuidRegex = "(?i)^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-5][0-9a-f]{3}-?[089ab][0-9a-f]{3}-?[0-9a-f]{12}$"; + @BeforeClass public static void setUp() throws Exception { System.setProperty("mso.config.path", "src/test/resources"); @@ -81,25 +85,45 @@ public class GRMClientTest { .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", MediaType.APPLICATION_JSON) - .withHeader("X-FromAppId", "GRM") .withBody(endpoints))); - MDC.put(MsoLogger.SERVICE_NAME, "my-value"); + MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, "/test"); GRMClient client = new GRMClient(); ServiceEndPointList sel = client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST"); List list = sel.getServiceEndPointList(); assertEquals(3, list.size()); - ILoggingEvent logEvent = TestAppender.events.get(0); - Map mdc = logEvent.getMDCPropertyMap(); - assertNotNull(mdc.get(MsoLogger.METRIC_BEGIN_TIME)); - assertNotNull(mdc.get(MsoLogger.METRIC_END_TIME)); - assertNotNull(mdc.get(MsoLogger.REQUEST_ID)); - assertNotNull(mdc.get(MsoLogger.METRIC_TIMER)); - assertEquals("200",mdc.get(MsoLogger.RESPONSECODE)); - assertEquals("GRM",mdc.get(MsoLogger.PARTNERNAME)); - assertEquals("expect value to not be overwritten by jax rs client interceptor", "my-value",mdc.get(MsoLogger.SERVICE_NAME)); - assertEquals("COMPLETE",mdc.get(MsoLogger.STATUSCODE)); - assertNotNull(mdc.get(MsoLogger.RESPONSEDESC)); + + boolean foundInvoke = false; + boolean foundInvokeReturn = false; + for(ILoggingEvent logEvent : TestAppender.events) + if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") && + logEvent.getMarker().getName().equals("INVOKE") + ){ + Map mdc = logEvent.getMDCPropertyMap(); + assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + assertEquals("GRM",mdc.get("TargetEntity")); + assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + foundInvoke=true; + }else if(logEvent.getLoggerName().equals("org.onap.so.logging.jaxrs.filter.JaxRsClientLogging") && + logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("INVOKE_RETURN")){ + Map mdc = logEvent.getMDCPropertyMap(); + assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID)); + assertEquals("200",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE)); + assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + foundInvokeReturn=true; + } + + if(!foundInvoke) + fail("INVOKE Marker not found"); + + if(!foundInvokeReturn) + fail("INVOKE RETURN Marker not found"); + + verify(postRequestedFor(urlEqualTo("/GRMLWPService/v1/serviceEndPoint/findRunning")) + .withHeader(ONAPLogConstants.Headers.INVOCATION_ID.toString(), matching(uuidRegex)) + .withHeader(ONAPLogConstants.Headers.REQUEST_ID.toString(), matching(uuidRegex)) + .withHeader(ONAPLogConstants.Headers.PARTNER_NAME.toString(), equalTo("SO"))); + TestAppender.events.clear(); } @Test @@ -116,68 +140,6 @@ public class GRMClientTest { client.findRunningServices("TEST.ECOMP_PSL.*", 1, "TEST"); } - @Ignore - @Test - public void testAdd() throws Exception { - - wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add")) - .willReturn(aResponse() - .withStatus(202) - .withHeader("Content-Type", MediaType.APPLICATION_JSON) - .withBody("test"))); - wireMockRule.addMockServiceRequestListener((request, response) -> { - System.out.println("URL Requested => " + request.getAbsoluteUrl()); - System.out.println("Request Body => " + request.getBodyAsString()); - System.out.println("Request Headers => " + request.getHeaders().toString()); - System.out.println("Response Status => " + response.getStatus()); - System.out.println("Response Body => " + response.getBodyAsString()); - }); - - Version ver = new Version(); - ver.setMajor(1); - ver.setMinor(0); - ver.setPatch("0"); - - ServiceEndPoint sep = new ServiceEndPoint(); - sep.setName("TEST.ECOMP_PSL.Inventory"); - sep.setVersion(ver); - sep.setHostAddress("127.0.0.1"); - sep.setListenPort("8080"); - sep.setLatitude("37.7022"); - sep.setLongitude("121.9358"); - sep.setContextPath("/"); - sep.setRouteOffer("TEST"); - - OperationalInfo operInfo = new OperationalInfo(); - operInfo.setCreatedBy("edge"); - operInfo.setUpdatedBy("edge"); - - sep.setOperationalInfo(operInfo); - - Property prop1 = new Property(); - prop1.setName("Environment"); - prop1.setValue("TEST"); - - Property prop2 = new Property(); - prop2.setName("cpfrun_cluster_name"); - prop2.setValue("testcase_cluster_no_cluster"); - - List props = new ArrayList(); - props.add(prop1); - props.add(prop2); - - sep.setProperties(props); - - ServiceEndPointRequest request = new ServiceEndPointRequest(); - request.setEnv("DEV"); - request.setServiceEndPoint(sep); - - System.out.println("Request in JSON: " + mapper.writeValueAsString(request)); - - GRMClient client = new GRMClient(); - client.addServiceEndPoint(request); - } - @Test public void testAddFail() throws Exception { wireMockRule.stubFor(post(urlPathEqualTo("/GRMLWPService/v1/serviceEndPoint/add")) diff --git a/common/src/test/resources/logback-test.xml b/common/src/test/resources/logback-test.xml index c4248bf985..772eeabeb6 100644 --- a/common/src/test/resources/logback-test.xml +++ b/common/src/test/resources/logback-test.xml @@ -1,8 +1,8 @@ - - - + + + + + + + + + + - - - - - - - - - - - - - - - + + + ${pattern} + + - - + - - + + + - - - - + + + - - - - - - ${logDirectory}/${auditLogName}${jboss.server.name}.log - - ${logDirectory}/${auditLogName}${jboss.server.name}.log.%d - - - - ${auditPattern} - - - - 500 - 0 - - + + + - - ${logDirectory}/${metricsLogName}${jboss.server.name}.log - - ${logDirectory}/${metricsLogName}${jboss.server.name}.log.%d - - - - - ${metricPattern} - - - - - - 500 - 0 - - - - - ${logDirectory}/${errorLogName}${jboss.server.name}.log - - ${logDirectory}/${errorLogName}${jboss.server.name}.log.%d - - - - INFO - - - ${errorPattern} - - - - - 500 - 0 - - - - - ${debugLogDirectory}/${debugLogName}${jboss.server.name}.log - - ${debugLogDirectory}/${debugLogName}${jboss.server.name}.log.%d - - - - ${debugPattern} - - - - - 500 - 0 - - true - - - - - %d{HH:mm:ss.SSS} [%thread] |%X{RequestId}| %-5level - %logger{1024} - %msg%n - - - + + + + + + + + - - - - - - - - - - - + + + + - - - - - - - - + + + + \ No newline at end of file diff --git a/common/src/test/resources/mso-bad.json b/common/src/test/resources/mso-bad.json deleted file mode 100644 index 1aaea50042..0000000000 --- a/common/src/test/resources/mso-bad.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "asdc-connections": { - "asdc-controller1": { - "asdcUser": "user1", - "asdcConsumerGroup": "consumer1", - "asdcConsumerId": "consumer1", - "asdcEnvironmentName": "PROD", - "asdcAddress": "localhost:8443", - "asdcPassword": "1c551b8b5ab91fcd5a0907b11c304199" - }, - "asdc-controller2": { - "asdcUser": "user2", - "asdcConsumerGroup": "consumer2", - "asdcConsumerId": "consumer2", - "asdcEnvironmentName": "E2E", - "asdcAddress": "localhost:8443", - "asdcPassword": "1c551b8b5ab91fcd5a0907b11c304199" - } - } -} \ No newline at end of file diff --git a/common/src/test/resources/mso.json b/common/src/test/resources/mso.json deleted file mode 100644 index e8ee4a9185..0000000000 --- a/common/src/test/resources/mso.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "asdc-connections":{ - "asdc-controller1":{ - "asdcUser": "user1", - "asdcConsumerGroup": "consumer1", - "asdcConsumerId": "consumer1", - "asdcEnvironmentName": "PROD", - "asdcAddress": "localhost:8443", - "asdcPassword": "1c551b8b5ab91fcd5a0907b11c304199" - }, - "asdc-controller2":{ - "asdcUser": "user2", - "asdcConsumerGroup": "consumer2", - "asdcConsumerId": "consumer2", - "asdcEnvironmentName": "E2E", - "asdcAddress": "localhost:8443", - "asdcPassword": "1c551b8b5ab91fcd5a0907b11c304199" - } - }, - "mso.properties.reload.time.minutes":2 -} diff --git a/common/src/test/resources/mso.properties b/common/src/test/resources/mso.properties deleted file mode 100644 index bb0e4ebf0d..0000000000 --- a/common/src/test/resources/mso.properties +++ /dev/null @@ -1,28 +0,0 @@ -### -# ============LICENSE_START======================================================= -# ECOMP MSO -# ================================================================================ -# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. -# ================================================================================ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============LICENSE_END========================================================= -### - -mso.properties.reload.time.minutes=2 -ecomp.mso.cloud.1.cloudId=MT -ecomp.mso.cloud.1.keystoneUrl=http://localhost:5000/v2.0 -ecomp.mso.cloud.1.msoId=John -ecomp.mso.cloud.1.publicNetId=FD205490A48D48475607C36B9AD902BF -ecomp.mso.cloud.1.test=1234 -ecomp.mso.cloud.1.boolean=true -ecomp.mso.cloud.1.enum=enum1 diff --git a/common/src/test/resources/mso2.json b/common/src/test/resources/mso2.json deleted file mode 100644 index bac30c495c..0000000000 --- a/common/src/test/resources/mso2.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "asdc-connections": { - "asdc-controller1": { - "asdcUser": "user1B", - "asdcConsumerGroup": "consumer1", - "asdcConsumerId": "consumer1", - "asdcEnvironmentName": "PROD", - "asdcAddress": "localhost:8443", - "asdcPassword": "1c551b8b5ab91fcd5a0907b11c304199" - }, - "asdc-controller2": { - "asdcUser": "user2B", - "asdcConsumerGroup": "consumer2", - "asdcConsumerId": "consumer2", - "asdcEnvironmentName": "E2E", - "asdcAddress": "localhost:8443", - "asdcPassword": "1c551b8b5ab91fcd5a0907b11c304199" - } - } -} \ No newline at end of file diff --git a/common/src/test/resources/mso2.properties b/common/src/test/resources/mso2.properties deleted file mode 100644 index 423346e953..0000000000 --- a/common/src/test/resources/mso2.properties +++ /dev/null @@ -1,22 +0,0 @@ -### -# ============LICENSE_START======================================================= -# ECOMP MSO -# ================================================================================ -# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. -# ================================================================================ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============LICENSE_END========================================================= -### - -mso.properties.reload.time.minutes=1 -ecomp.mso.cloud.1.cloudId=MT2 -- cgit 1.2.3-korg