diff options
author | Ittay Stern <ittay.stern@att.com> | 2019-09-12 07:47:32 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-09-12 07:47:32 +0000 |
commit | ca6f40516028602d3551856301cd9d7cda65f812 (patch) | |
tree | c114fcde5bd53e1b1aaaaa8625eaa553a7b043fa /vid-app-common/src/main/java | |
parent | 4996ccfb67d22533da5d5021284e41b6612e4aec (diff) | |
parent | 2265215c803291e029add2db7912c7b1e25e0a8e (diff) |
Merge "make Logging a service and inject it to SyncRestClient"
Diffstat (limited to 'vid-app-common/src/main/java')
10 files changed, 171 insertions, 127 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/PombaRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/PombaRestInterface.java index 2f69e397c..e9895e635 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/PombaRestInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/PombaRestInterface.java @@ -22,25 +22,31 @@ package org.onap.vid.aai; -import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.onap.vid.aai.util.*; -import org.onap.vid.utils.Logging; -import org.springframework.http.HttpMethod; +import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; +import java.util.UUID; import javax.ws.rs.client.Client; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.UUID; - -import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.vid.aai.util.AAIRestInterface; +import org.onap.vid.aai.util.HttpClientMode; +import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.aai.util.ServletRequestHelper; +import org.onap.vid.aai.util.SystemPropertyHelper; +import org.onap.vid.utils.Logging; +import org.springframework.http.HttpMethod; public class PombaRestInterface extends AAIRestInterface { private Client client = null; - public PombaRestInterface (HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) { - super(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper); + public PombaRestInterface (HttpsAuthClient httpsAuthClientFactory, + ServletRequestHelper servletRequestHelper, + SystemPropertyHelper systemPropertyHelper, + Logging loggingService) { + super(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService); } @Override @@ -64,7 +70,7 @@ public class PombaRestInterface extends AAIRestInterface { try { initRestClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload); final Response cres = client.target(url) .request() .accept(MediaType.APPLICATION_JSON) @@ -72,7 +78,7 @@ public class PombaRestInterface extends AAIRestInterface { .header(FROM_APP_ID_HEADER, fromAppId) .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) .post(Entity.entity(payload, MediaType.APPLICATION_JSON)); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres); if (cres.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName)); diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java index 92d8de757..034800516 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java @@ -21,7 +21,20 @@ package org.onap.vid.aai.util; +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; +import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; + import com.att.eelf.configuration.EELFLogger; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLEncoder; +import java.util.Optional; +import java.util.UUID; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; @@ -33,20 +46,6 @@ import org.onap.vid.utils.Unchecked; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URLEncoder; -import java.util.Optional; -import java.util.UUID; - -import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; -import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; - /** * The Class AAIRestInterface. @@ -68,6 +67,7 @@ public class AAIRestInterface { protected HttpsAuthClient httpsAuthClientFactory; private final ServletRequestHelper servletRequestHelper; private final SystemPropertyHelper systemPropertyHelper; + protected final Logging loggingService; protected static final String START_STRING = " start"; protected static final String TRANSACTION_ID_HEADER = "X-TransactionId"; @@ -75,10 +75,14 @@ public class AAIRestInterface { protected static final String SUCCESSFUL_API_MESSAGE = " REST api call was successful!"; protected static final String URL_DECLARATION = ", url="; - public AAIRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) { + public AAIRestInterface(HttpsAuthClient httpsAuthClientFactory, + ServletRequestHelper servletRequestHelper, + SystemPropertyHelper systemPropertyHelper, + Logging loggingService) { this.httpsAuthClientFactory = httpsAuthClientFactory; this.servletRequestHelper = servletRequestHelper; this.systemPropertyHelper = systemPropertyHelper; + this.loggingService = loggingService; initRestClient(); } @@ -86,10 +90,14 @@ public class AAIRestInterface { * For testing purpose */ AAIRestInterface(Optional<Client> client, - HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper){ + HttpsAuthClient httpsAuthClientFactory, + ServletRequestHelper servletRequestHelper, + SystemPropertyHelper systemPropertyHelper, + Logging loggingService){ this.httpsAuthClientFactory = httpsAuthClientFactory; this.servletRequestHelper = servletRequestHelper; this.systemPropertyHelper = systemPropertyHelper; + this.loggingService = loggingService; if (client != null && client.isPresent()){ this.client = client.get(); } @@ -182,7 +190,7 @@ public class AAIRestInterface { logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); logger.debug(EELFLoggerDelegate.debugLogger, url + " for the get REST API"); - Logging.logRequest(outgoingRequestsLogger, method, url, payload); + loggingService.logRequest(outgoingRequestsLogger, method, url, payload); final Response response; Invocation.Builder requestBuilder = client.target(url) @@ -201,7 +209,7 @@ public class AAIRestInterface { requestBuilder.build(method.name(), Entity.entity(payload, MediaType.APPLICATION_JSON)); response = restInvocation.invoke(); - Logging.logResponse(outgoingRequestsLogger, method, url, response); + loggingService.logResponse(outgoingRequestsLogger, method, url, response); if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { logger.debug(EELFLoggerDelegate.debugLogger, methodName + SUCCESSFUL_API_MESSAGE); @@ -242,7 +250,7 @@ public class AAIRestInterface { try { initRestClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); final Response cres = client.target(url) .request() .accept(MediaType.APPLICATION_JSON) @@ -250,7 +258,7 @@ public class AAIRestInterface { .header(FROM_APP_ID_HEADER, sourceID) .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) .delete(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres); if (cres.getStatusInfo().equals(Response.Status.NOT_FOUND)) { logger.debug(EELFLoggerDelegate.debugLogger, "Resource does not exist...: " + cres.getStatus() + ":" + cres.readEntity(String.class)); @@ -306,7 +314,7 @@ public class AAIRestInterface { Response response = null; try { initRestClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload); response = authenticateRequest(client.target(url) .request() .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) @@ -314,7 +322,7 @@ public class AAIRestInterface { .header(FROM_APP_ID_HEADER, fromAppId)) .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) .post(Entity.entity(payload, MediaType.APPLICATION_JSON)); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response); if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName)); diff --git a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java index 1de9715ee..9e139a02a 100644 --- a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java @@ -30,7 +30,6 @@ import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE; import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID; import static org.onap.vid.client.UnirestPatchKt.extractRawAsString; import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; -import static org.onap.vid.utils.Logging.logRequest; import com.att.eelf.configuration.EELFLogger; import com.google.common.collect.ImmutableMap; @@ -63,23 +62,25 @@ public class SdcRestClient implements AsdcClient { private String baseUrl; private String path; private String auth; - private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc"); + private static final EELFLogger LOGGER = Logging.getRequestsLogger("sdc"); private SyncRestClientInterface syncRestClient; + private Logging loggingService; - public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) { + public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client, Logging loggingService) { this.syncRestClient = client; this.auth = auth; this.baseUrl = baseUrl; this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH); + this.loggingService = loggingService; } @Override public Service getService(UUID uuid) throws AsdcCatalogException { String finalUrl = String.format(METADATA_URL_TEMPLATE, baseUrl, path, uuid); - logRequest(LOGGER, HttpMethod.GET, finalUrl); + loggingService.logRequest(LOGGER, HttpMethod.GET, finalUrl); return Try .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class)) @@ -94,7 +95,7 @@ public class SdcRestClient implements AsdcClient { HttpResponseWithRequestInfo<InputStream> responseWithRequestInfo = getServiceInputStream(uuid, false); if (responseWithRequestInfo.getResponse().getStatus()>399) { - Logging.logResponse(LOGGER, HttpMethod.GET, + loggingService.logRequest(LOGGER, HttpMethod.GET, responseWithRequestInfo.getRequestUrl(), responseWithRequestInfo.getResponse()); String body = extractRawAsString(responseWithRequestInfo.getResponse()); @@ -124,7 +125,7 @@ public class SdcRestClient implements AsdcClient { @Override public HttpResponseWithRequestInfo<InputStream> getServiceInputStream(UUID serviceUuid, boolean warpException) { String finalUrl = String.format(TOSCA_MODEL_URL_TEMPLATE, baseUrl, path, serviceUuid); - logRequest(LOGGER, HttpMethod.GET, finalUrl); + loggingService.logRequest(LOGGER, HttpMethod.GET, finalUrl); try { HttpResponse<InputStream> httpResponse = syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap()); return new HttpResponseWithRequestInfo<>(httpResponse, finalUrl, HttpMethod.GET); diff --git a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java index 50556e7ec..18f87223c 100644 --- a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClient.java @@ -23,6 +23,7 @@ package org.onap.vid.client; import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.onap.vid.client.UnirestPatchKt.patched; +import com.att.eelf.configuration.EELFLogger; import io.joshworks.restclient.http.HttpResponse; import io.joshworks.restclient.http.JsonNode; import io.joshworks.restclient.http.RestClient; @@ -42,6 +43,7 @@ import java.security.cert.CertificateException; import java.util.Map; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; +import org.apache.commons.lang3.ObjectUtils; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContexts; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; @@ -51,29 +53,38 @@ import org.eclipse.jetty.util.security.Password; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.properties.VidProperties; +import org.onap.vid.utils.Logging; public class SyncRestClient implements SyncRestClientInterface { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SyncRestClient.class); private static final String[] SUPPORTED_SSL_VERSIONS = {"TLSv1", "TLSv1.2"}; private static final String HTTPS_SCHEMA = "https://"; private static final String HTTP_SCHEMA = "http://"; + private final Logging loggingService; + private final EELFLogger outgoingRequestsLogger; private RestClient restClient; - public SyncRestClient() { - restClient = RestClient.newClient().objectMapper(defaultObjectMapper()).httpClient(defaultHttpClient()).build(); + public SyncRestClient(Logging loggingService) { + this(null, null, loggingService); } - public SyncRestClient(ObjectMapper objectMapper) { - restClient = RestClient.newClient().objectMapper(objectMapper).httpClient(defaultHttpClient()).build(); + public SyncRestClient(ObjectMapper objectMapper, Logging loggingService) { + this(null, objectMapper, loggingService); } - public SyncRestClient(CloseableHttpClient httpClient) { - restClient = RestClient.newClient().objectMapper(defaultObjectMapper()).httpClient(httpClient).build(); + public SyncRestClient(CloseableHttpClient httpClient, Logging loggingService) { + this(httpClient, null, loggingService); } - public SyncRestClient(CloseableHttpClient httpClient, ObjectMapper objectMapper) { - restClient = RestClient.newClient().objectMapper(objectMapper).httpClient(httpClient).build(); + public SyncRestClient(CloseableHttpClient httpClient, ObjectMapper objectMapper, Logging loggingService) { + restClient = RestClient + .newClient() + .objectMapper(ObjectUtils.defaultIfNull(objectMapper, defaultObjectMapper())) + .httpClient(ObjectUtils.defaultIfNull(httpClient , defaultHttpClient())) + .build(); + this.loggingService = loggingService; + this.outgoingRequestsLogger = Logging.getRequestsLogger("syncRestClient"); } @Override diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java b/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java index 20cf6f038..5b05caa7a 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java @@ -33,6 +33,7 @@ import org.onap.vid.mso.MsoProperties; import org.onap.vid.mso.rest.MsoRestClientNew; import org.onap.vid.services.CloudOwnerService; import org.onap.vid.services.CloudOwnerServiceImpl; +import org.onap.vid.utils.Logging; import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -48,10 +49,18 @@ public class MsoConfig { } @Bean - public MsoRestClientNew msoClient(ObjectMapper unirestObjectMapper, HttpsAuthClient httpsAuthClient, SystemPropertiesWrapper systemPropertiesWrapper){ + public MsoRestClientNew msoClient(ObjectMapper unirestObjectMapper, + HttpsAuthClient httpsAuthClient, + SystemPropertiesWrapper systemPropertiesWrapper, + Logging loggingService){ // Satisfy both interfaces -- MsoInterface and RestMsoImplementation - return new MsoRestClientNew(new SyncRestClient(unirestObjectMapper), SystemProperties.getProperty( - MsoProperties.MSO_SERVER_URL),httpsAuthClient, systemPropertiesWrapper); + return new MsoRestClientNew( + new SyncRestClient(unirestObjectMapper, loggingService), + SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL), + httpsAuthClient, + systemPropertiesWrapper, + loggingService + ); } diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java index b9908d1e3..99845f06d 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/WebConfig.java @@ -55,7 +55,6 @@ import org.onap.vid.asdc.parser.ToscaParserImpl2; import org.onap.vid.asdc.parser.VidNotionsBuilder; import org.onap.vid.asdc.rest.SdcRestClient; import org.onap.vid.client.SyncRestClient; -import org.onap.vid.client.SyncRestClientInterface; import org.onap.vid.properties.AsdcClientConfiguration; import org.onap.vid.properties.VidProperties; import org.onap.vid.scheduler.SchedulerService; @@ -67,6 +66,7 @@ import org.onap.vid.services.AaiServiceImpl; import org.onap.vid.services.ChangeManagementService; import org.onap.vid.services.PombaService; import org.onap.vid.services.PombaServiceImpl; +import org.onap.vid.utils.Logging; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -119,13 +119,19 @@ public class WebConfig { } @Bean(name = "aaiRestInterface") - public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) { - return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper); + public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory, + ServletRequestHelper servletRequestHelper, + SystemPropertyHelper systemPropertyHelper, + Logging loggingService) { + return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService); } @Bean - public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) { - return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper); + public PombaRestInterface getPombaRestInterface(HttpsAuthClient httpsAuthClientFactory, + ServletRequestHelper servletRequestHelper, + SystemPropertyHelper systemPropertyHelper, + Logging loggingService) { + return new PombaRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper, loggingService); } @Bean @@ -150,18 +156,13 @@ public class WebConfig { } @Bean - public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, SyncRestClientInterface syncRestClient) { + public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, Logging loggingService) { String auth = asdcClientConfiguration.getAsdcClientAuth(); String host = asdcClientConfiguration.getAsdcClientHost(); String protocol = asdcClientConfiguration.getAsdcClientProtocol(); int port = asdcClientConfiguration.getAsdcClientPort(); - return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth, syncRestClient); - } - - @Bean - public SyncRestClientInterface syncRestClient() { - return new SyncRestClient(); + return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth, new SyncRestClient(loggingService), loggingService); } @Bean @@ -190,8 +191,10 @@ public class WebConfig { } @Bean - public AaiOverTLSClientInterface aaiOverTLSClient(ObjectMapper unirestObjectMapper, SystemProperties systemProperties){ - return new AaiOverTLSClient(new SyncRestClient(unirestObjectMapper), new AaiOverTLSPropertySupplier()); + public AaiOverTLSClientInterface aaiOverTLSClient(ObjectMapper unirestObjectMapper, SystemProperties systemProperties, Logging loggingService){ + return new AaiOverTLSClient( + new SyncRestClient(unirestObjectMapper, loggingService), + new AaiOverTLSPropertySupplier()); } @Bean diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java index 17af75200..ee1eb0429 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java @@ -20,8 +20,21 @@ package org.onap.vid.mso; +import static org.onap.vid.utils.Logging.ONAP_REQUEST_ID_HEADER_KEY; +import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; +import static org.onap.vid.utils.Logging.getMethodCallerName; +import static org.onap.vid.utils.Logging.getMethodName; + import com.att.eelf.configuration.EELFLogger; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.Collections; +import java.util.Optional; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.Response; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpException; import org.eclipse.jetty.util.security.Password; @@ -37,22 +50,12 @@ import org.onap.vid.utils.SystemPropertiesWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.Response; -import java.util.Collections; -import java.util.Optional; - -import static org.onap.vid.utils.Logging.*; - /** * Created by pickjonathan on 26/06/2017. */ public class RestMsoImplementation implements RestInterface { + /** * The logger. */ @@ -65,6 +68,7 @@ public class RestMsoImplementation implements RestInterface { protected HttpsAuthClient httpsAuthClient; protected SystemPropertiesWrapper systemProperties; + protected final Logging loggingService; private static final String START_LOG = " start"; private static final String APPLICATION_JSON = "application/json"; @@ -81,9 +85,10 @@ public class RestMsoImplementation implements RestInterface { */ @Autowired - protected RestMsoImplementation(HttpsAuthClient httpsAuthClient, SystemPropertiesWrapper systemProperties){ + protected RestMsoImplementation(HttpsAuthClient httpsAuthClient, SystemPropertiesWrapper systemProperties, Logging loggingService){ this.httpsAuthClient=httpsAuthClient; this.systemProperties = systemProperties; + this.loggingService = loggingService; } @SuppressWarnings("Duplicates") @@ -145,13 +150,13 @@ public class RestMsoImplementation implements RestInterface { url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path; MultivaluedHashMap<String, Object> commonHeaders = initMsoClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); final Response cres = client.target(url) .request() .accept(APPLICATION_JSON) .headers(commonHeaders) .get(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); cres.bufferEntity(); status = cres.getStatus(); @@ -185,13 +190,13 @@ public class RestMsoImplementation implements RestInterface { logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " sending request to url= " + url); MultivaluedHashMap<String, Object> commonHeaders = initMsoClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); final Response cres = client.target(url) .request() .accept(APPLICATION_JSON) .headers(commonHeaders) .get(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); final RestObject<T> restObject = cresToRestObject(cres, clazz); int status = cres.getStatus(); @@ -219,7 +224,7 @@ public class RestMsoImplementation implements RestInterface { MultivaluedHashMap<String, Object> commonHeaders = initMsoClient(); url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path; - Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r); cres = client.target(url) .request() @@ -228,7 +233,7 @@ public class RestMsoImplementation implements RestInterface { //.entity(r) .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)) .invoke(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres); int status = cres.getStatus(); restObject.setStatusCode (status); @@ -310,7 +315,7 @@ public class RestMsoImplementation implements RestInterface { userId.ifPresent(id->commonHeaders.put("X-RequestorID", Collections.singletonList(id))); url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path; - Logging.logRequest(outgoingRequestsLogger, httpMethod, url, payload); + loggingService.logRequest(outgoingRequestsLogger, httpMethod, url, payload); // Change the content length final Invocation.Builder restBuilder = client.target(url) .request() @@ -322,7 +327,7 @@ public class RestMsoImplementation implements RestInterface { restBuilder.build(httpMethod.name(), Entity.entity(payload, MediaType.APPLICATION_JSON)); final Response cres = restInvocation.invoke(); - Logging.logResponse(outgoingRequestsLogger, httpMethod, url, cres); + loggingService.logResponse(outgoingRequestsLogger, httpMethod, url, cres); return cresToRestObject(cres, tClass); } catch (Exception e) { @@ -372,7 +377,7 @@ public class RestMsoImplementation implements RestInterface { MultivaluedHashMap<String, Object> commonHeaders = initMsoClient(); url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path; - Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, r); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, r); // Change the content length final Response cres = client.target(url) .request() @@ -381,7 +386,7 @@ public class RestMsoImplementation implements RestInterface { //.header("content-length", 201) .put(Entity.entity(r, MediaType.APPLICATION_JSON)); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres); + loggingService.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres); try { t = (T) cres.readEntity(t.getClass()); diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java b/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java index 4b8a974e3..b2ccde93b 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java @@ -77,8 +77,8 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf */ EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoRestClientNew.class); - public MsoRestClientNew(SyncRestClient client, String baseUrl, HttpsAuthClient authClient, SystemPropertiesWrapper systemPropertiesWrapper) { - super(authClient,systemPropertiesWrapper); + public MsoRestClientNew(SyncRestClient client, String baseUrl, HttpsAuthClient authClient, SystemPropertiesWrapper systemPropertiesWrapper, Logging loggingService) { + super(authClient,systemPropertiesWrapper, loggingService); this.client = client; this.baseUrl = baseUrl; this.commonHeaders = initCommonHeaders(); diff --git a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java index 001a8ae6d..8d45bee87 100644 --- a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java @@ -20,11 +20,17 @@ package org.onap.vid.scheduler; +import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; +import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; + import com.att.eelf.configuration.EELFLogger; -import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import io.joshworks.restclient.http.HttpResponse; +import java.util.Base64; +import java.util.Collections; +import java.util.Map; +import java.util.function.Function; import org.apache.http.HttpException; import org.eclipse.jetty.util.security.Password; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; @@ -36,17 +42,10 @@ import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.vid.mso.RestObject; import org.onap.vid.mso.RestObjectWithRequestInfo; import org.onap.vid.utils.Logging; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; -import java.util.Base64; -import java.util.Collections; -import java.util.Map; -import java.util.function.Function; - -import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; -import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; - @Service public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { @@ -57,14 +56,19 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { private Function<String, String> propertyGetter; private Map<String, String> commonHeaders; - public SchedulerRestInterface() { - this.propertyGetter = SystemProperties::getProperty; - } + private Logging loggingService; - public SchedulerRestInterface(Function<String, String> propertyGetter) { + public SchedulerRestInterface(Function<String, String> propertyGetter, Logging loggingService) { + this.loggingService = loggingService; this.propertyGetter = propertyGetter; } + @Autowired + public SchedulerRestInterface(Logging loggingService) { + this.loggingService = loggingService; + this.propertyGetter = SystemProperties::getProperty; + } + public void initRestClient() { logger.info("Starting to initialize rest client "); String authStringEnc = calcEncodedAuthString(); @@ -72,7 +76,7 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { commonHeaders = Maps.newHashMap(); commonHeaders.put("Authorization", "Basic " + authStringEnc); - syncRestClient = new SyncRestClient(); + syncRestClient = new SyncRestClient(loggingService); logger.info("\t<== Client Initialized \n"); } @@ -87,14 +91,14 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { String methodName = "Get"; url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path); initRestClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); Map<String, String> requestHeaders = ImmutableMap.<String, String>builder() .putAll(commonHeaders) .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()) .build(); final HttpResponse<String> response = syncRestClient.get(url, requestHeaders, Collections.emptyMap(), String.class); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, response); status = response.getStatus(); restObject.setStatusCode(status); rawData = response.getBody(); @@ -121,13 +125,13 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { public <T> void Delete(T t, String sourceID, String path, RestObject<T> restObject) { initRestClient(); String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); Map<String, String> requestHeaders = ImmutableMap.<String, String>builder() .putAll(commonHeaders) .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build(); final HttpResponse<T> response = (HttpResponse<T>) syncRestClient.delete(url, requestHeaders, t.getClass()); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response); + loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, response); int status = response.getStatus(); restObject.setStatusCode(status); diff --git a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java index 71478fcf1..77b7ee869 100644 --- a/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java +++ b/vid-app-common/src/main/java/org/onap/vid/utils/Logging.java @@ -20,37 +20,34 @@ package org.onap.vid.utils; +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; +import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCause; +import static org.apache.commons.lang3.exception.ExceptionUtils.getThrowableList; +import static org.onap.vid.utils.Streams.not; + import com.att.eelf.configuration.EELFLogger; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.google.common.collect.ImmutableList; import io.joshworks.restclient.http.HttpResponse; +import java.util.Arrays; +import java.util.Optional; +import java.util.UUID; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Response; import org.apache.commons.lang3.StringUtils; -import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.exceptions.GenericUncheckedException; import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.ProcessingException; -import javax.ws.rs.core.Response; -import java.util.Arrays; -import java.util.Optional; -import java.util.UUID; - -import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; -import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCause; -import static org.apache.commons.lang3.exception.ExceptionUtils.getThrowableList; -import static org.onap.vid.utils.Streams.not; - +@Service public class Logging { - private Logging() { - } - public static final String HTTP_REQUESTS_OUTGOING = "http.requests.outgoing."; public static final String REQUEST_ID_HEADER_KEY = SystemProperties.ECOMP_REQUEST_ID; @@ -84,7 +81,7 @@ public class Logging { return EELFLoggerDelegate.getLogger(HTTP_REQUESTS_OUTGOING +serverName); } - public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) { + public void logRequest(final EELFLogger logger, final HttpMethod method, final String url, final Object body) { if (!logger.isDebugEnabled()) { return; } @@ -103,11 +100,11 @@ public class Logging { } } - public static void logRequest(final EELFLogger logger, final HttpMethod method, final String url) { + public void logRequest(final EELFLogger logger, final HttpMethod method, final String url) { logger.debug("Sending {} {}", method.name(), url); } - public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) { + public <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response, final Class<T> entityClass) { if (!logger.isDebugEnabled()) { return; } @@ -124,7 +121,7 @@ public class Logging { } } - public static <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) { + public <T> void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final HttpResponse<T> response) { try { logger.debug("Received {} {} Status: {} . Body: {}", method.name(), url, response.getStatus(), response.getBody()); } @@ -133,7 +130,7 @@ public class Logging { } } - public static void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) { + public void logResponse(final EELFLogger logger, final HttpMethod method, final String url, final Response response) { logResponse(logger, method, url, response, String.class); } |