use logging interceptor in SDC client
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / rest / SdcRestClient.java
index decf446..9efb389 100644 (file)
@@ -28,8 +28,7 @@ import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE;
 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION;
 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.utils.Logging.REQUEST_ID_HEADER_KEY;
-import static org.onap.vid.utils.Logging.logRequest;
+import static org.onap.vid.client.UnirestPatchKt.extractRawAsString;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.google.common.collect.ImmutableMap;
@@ -43,6 +42,8 @@ import java.nio.file.StandardCopyOption;
 import java.util.Collections;
 import java.util.Map;
 import java.util.UUID;
+import javax.ws.rs.ProcessingException;
+import javax.ws.rs.client.ResponseProcessingException;
 import org.onap.portalsdk.core.util.SystemProperties;
 import org.onap.vid.aai.ExceptionWithRequestInfo;
 import org.onap.vid.aai.HttpResponseWithRequestInfo;
@@ -60,23 +61,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))
@@ -87,19 +90,41 @@ public class SdcRestClient implements AsdcClient {
 
     @Override
     public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException {
-        InputStream inputStream = Try
-                .of(() -> getServiceInputStream(uuid, false))
-                .getOrElseThrow(AsdcCatalogException::new)
-                .getResponse()
-                .getBody();
-
-        return createTmpFile(inputStream);
+        try {
+            HttpResponseWithRequestInfo<InputStream> responseWithRequestInfo = getServiceInputStream(uuid, false);
+
+            if (responseWithRequestInfo.getResponse().getStatus()>399) {
+                loggingService.logRequest(LOGGER, HttpMethod.GET,
+                    responseWithRequestInfo.getRequestUrl(), responseWithRequestInfo.getResponse());
+
+                String body = extractRawAsString(responseWithRequestInfo.getResponse());
+                throw new AsdcCatalogException(String.format("Http bad status code: %s, body: %s",
+                    responseWithRequestInfo.getResponse().getStatus(),
+                    body));
+            }
+
+            final InputStream csarInputStream = responseWithRequestInfo.getResponse().getBody();
+            Path toscaFilePath = createTmpFile(csarInputStream);
+            LOGGER.debug("Received {} {} . Tosca file was saved at: {}",
+                responseWithRequestInfo.getRequestHttpMethod().name(),
+                responseWithRequestInfo.getRequestUrl(),
+                toscaFilePath.toAbsolutePath());
+            return toscaFilePath;
+        } catch (ResponseProcessingException e) {
+            //Couldn't convert response to Java type
+            throw new AsdcCatalogException("ASDC response could not be processed", e);
+        } catch (ProcessingException e) {
+            //IO problems during request
+            throw new AsdcCatalogException("Failed to get a response from ASDC service. Cause: " + e.getMessage(), e);
+        } catch (RuntimeException e) {
+            throw new AsdcCatalogException(e);
+        }
     }
 
     @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);
@@ -127,7 +152,6 @@ public class SdcRestClient implements AsdcClient {
         return ImmutableMap.of(
                 X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME),
                 AUTHORIZATION, auth,
-                REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(),
                 CONTENT_TYPE, contentType
         );
     }