use onap logging 1.6.1 with needed workarounds
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / RestMsoImplementation.java
index 31836f6..a2d71d9 100644 (file)
@@ -1,38 +1,57 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2017 - 2019 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.vid.mso;
 
+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;
+import org.glassfish.jersey.client.ClientProperties;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import org.onap.portalsdk.core.util.SystemProperties;
-import org.onap.vid.aai.ExceptionWithRequestInfo;
 import org.onap.vid.aai.util.HttpClientMode;
 import org.onap.vid.aai.util.HttpsAuthClient;
 import org.onap.vid.client.HttpBasicClient;
-import org.onap.vid.exceptions.GenericUncheckedException;
-import org.onap.vid.mso.rest.RestInterface;
+import org.onap.vid.logging.VidMetricLogClientFilter;
 import org.onap.vid.utils.Logging;
+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 abstract class RestMsoImplementation implements RestInterface {
+public class RestMsoImplementation {
 
+    
     /**
      * The logger.
      */
@@ -42,8 +61,10 @@ public abstract class RestMsoImplementation implements RestInterface {
     /** The client. */
     private Client client = null;
 
-    @Autowired
+
     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";
@@ -59,14 +80,21 @@ public abstract class RestMsoImplementation implements RestInterface {
      * Instantiates a new mso rest interface.
      */
 
+    @Autowired
+    public RestMsoImplementation(HttpsAuthClient httpsAuthClient, SystemPropertiesWrapper systemProperties, Logging loggingService){
+        this.httpsAuthClient=httpsAuthClient;
+        this.systemProperties = systemProperties;
+        this.loggingService = loggingService;
+    }
+
     @SuppressWarnings("Duplicates")
     protected MultivaluedHashMap<String, Object> initMsoClient()
     {
         final String methodname = "initRestClient()";
 
-        final String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);
-        final String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);
-        final String mso_url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
+        final String username = systemProperties.getProperty(MsoProperties.MSO_USER_NAME);
+        final String password = systemProperties.getProperty(MsoProperties.MSO_PASSWORD);
+        final String mso_url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
         final String decrypted_password = Password.deobfuscate(password);
 
         String authString = username + ":" + decrypted_password;
@@ -76,12 +104,8 @@ public abstract class RestMsoImplementation implements RestInterface {
 
         MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap();
         commonHeaders.put("Authorization",  Collections.singletonList(("Basic " + authStringEnc)));
-        commonHeaders.put("X-ONAP-PartnerName", Collections.singletonList("VID"));
 
         String requestIdValue = Logging.extractOrGenerateRequestId();
-        commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
-        commonHeaders.put(ONAP_REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
-
 
         boolean useSsl = true;
         if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {
@@ -90,11 +114,13 @@ public abstract class RestMsoImplementation implements RestInterface {
         if (client == null) {
 
             try {
-                if ( useSsl ) {
-                    client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
-                }
+                    if ( useSsl ) {
+                        client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
+                        registerClientToMetricLogClientFilter(client);
+                    }
                 else {
                     client = HttpBasicClient.getClient();
+                    registerClientToMetricLogClientFilter(client);
                 }
             } catch (Exception e) {
                 logger.info(EELFLoggerDelegate.errorLogger,methodname + " Unable to get the SSL client");
@@ -104,67 +130,26 @@ public abstract class RestMsoImplementation implements RestInterface {
         return commonHeaders;
     }
 
-    public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject, boolean warpException) {
-        String methodName = "Get";
-
-        logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_LOG);
-
-        String url = null;
-        String rawData = null;
-        Integer status = null;
-
-        try {
-            restObject.set(t);
-            url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
-
-            MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
-            Logging.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);
-
-            cres.bufferEntity();
-            status = cres.getStatus();
-            rawData = cres.readEntity(String.class);
-
-            restObject.setStatusCode(status);
-
-            if (status == 200 || status == 202) {
-                t = (T) cres.readEntity(t.getClass());
-                restObject.set(t);
-                logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
-
-            } else {
-                throw new GenericUncheckedException(new HttpException(methodName + WITH_STATUS + status + " (200 or 202 expected), url= " + url));
-            }
-
-            logger.debug(EELFLoggerDelegate.debugLogger, methodName + " received status=" + status);
-
-            return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
-        } catch (RuntimeException e) {
-            throw warpException ? new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e) : e;
-        }
+    private void registerClientToMetricLogClientFilter(Client client) {
+        VidMetricLogClientFilter metricLogClientFilter = new VidMetricLogClientFilter();
+        client.register(metricLogClientFilter);
     }
 
-    @Override
     public <T> RestObject<T> GetForObject(String path, Class<T> clazz) {
         final String methodName = getMethodName();
         logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {})", getMethodCallerName(), methodName, path, clazz);
 
-        String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
+        String url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
         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();
 
@@ -179,83 +164,15 @@ public abstract class RestMsoImplementation implements RestInterface {
         return restObject;
     }
 
-    @Override
-    public <T> void Delete(T t, Object r, String path, RestObject<T> restObject) {
-
-        String methodName = "Delete";
-        String url="";
-        Response cres;
-
-        logger.debug(EELFLoggerDelegate.debugLogger,"<== " +  methodName + START_LOG);
-
-        try {
-            MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
-
-            url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
-            Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r);
-            cres = client.target(url)
-                    .request()
-
-                    .accept(APPLICATION_JSON)
-                    .headers(commonHeaders)
-                    //.entity(r)
-                    .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
-            Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
-            int status = cres.getStatus();
-            restObject.setStatusCode (status);
-
-            if (status == 404) { // resource not found
-                String msg = "Resource does not exist...: " + cres.getStatus();
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
-            } else if (status == 200  || status == 204){
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + "Resource " + url + " deleted");
-            } else if (status == 202) {
-                String      msg = "Delete in progress: " + status;
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
-            }
-            else {
-                String msg = "Deleting Resource failed: " + status;
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
-            }
-
-            try {
-                t = (T) cres.readEntity(t.getClass());
-                restObject.set(t);
-            }
-            catch ( Exception e ) {
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
-                        + e.getMessage());
-            }
-
-        }
-        catch (Exception e)
-        {
-            logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
-            throw e;
-
-        }
-    }
-
     public <T> RestObject<T> PostForObject(Object requestDetails, String path, Class<T> clazz) {
         logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
         return restCall(HttpMethod.POST, clazz, requestDetails, path);
     }
 
-    public <T> RestObject<T> DeleteForObject(Object requestDetails, String path, Class<T> clazz) {
-        logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
-        return restCall(HttpMethod.DELETE, clazz, requestDetails, path);
-    }
-
-    @Override
-    public void Post(String t, Object r, String path, RestObject<String> restObject) {
-        logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), t.getClass(), r, path);
-        restObject.copyFrom(restCall(HttpMethod.POST, String.class, r, path));
-    }
-
     public Invocation.Builder prepareClient(String path, String methodName) {
         MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
 
-        String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
+        String url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
         logger.debug(EELFLoggerDelegate.debugLogger,"<== " +  methodName + " sending request to url= " + url);
         // Change the content length
         return client.target(url)
@@ -281,20 +198,22 @@ public abstract class RestMsoImplementation implements RestInterface {
             MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
             userId.ifPresent(id->commonHeaders.put("X-RequestorID", Collections.singletonList(id)));
 
-            url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
-            Logging.logRequest(outgoingRequestsLogger, httpMethod, url, payload);
+            url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
+            loggingService.logRequest(outgoingRequestsLogger, httpMethod, url, payload);
             // Change the content length
             final Invocation.Builder restBuilder = client.target(url)
                     .request()
                     .accept(APPLICATION_JSON)
-                    .headers(commonHeaders);
+                    .headers(commonHeaders)
+                    .property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true)
+                ;
 
             Invocation restInvocation = payload==null ?
                     restBuilder.build(httpMethod.name()) :
                     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) {
@@ -329,58 +248,6 @@ public abstract class RestMsoImplementation implements RestInterface {
         restObject.setStatusCode (status);
 
         return restObject;
-
     }
 
-    @Override
-    public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String path, RestObject<T> restObject) {
-
-        String methodName = "Put";
-        String url="";
-
-        logger.debug(EELFLoggerDelegate.debugLogger,"<== " +  methodName + START_LOG);
-
-        try {
-
-            MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
-
-            url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
-            Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, r);
-            // Change the content length
-            final Response cres = client.target(url)
-                    .request()
-                    .accept(APPLICATION_JSON)
-                    .headers(commonHeaders)
-                    //.header("content-length", 201)
-                    .put(Entity.entity(r, MediaType.APPLICATION_JSON));
-
-            Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres);
-
-            try {
-                t = (T) cres.readEntity(t.getClass());
-                restObject.set(t);
-            }
-            catch ( Exception e ) {
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
-                        + e.getMessage());
-            }
-
-            int status = cres.getStatus();
-            restObject.setStatusCode (status);
-
-            if ( status >= 200 && status <= 299 ) {
-                logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + REST_API_SUCCESSFULL_LOG);
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + REST_API_SUCCESSFULL_LOG);
-
-            } else {
-                logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
-            }
-
-        } catch (Exception e)
-        {
-            logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
-            throw e;
-
-        }
-    }
 }