added configurable read timeout value for A&AI 72/114672/2
authorBenjamin, Max <max.benjamin@att.com>
Fri, 6 Nov 2020 23:04:49 +0000 (18:04 -0500)
committerBenjamin, Max (mb388a) <mb388a@att.com>
Thu, 12 Nov 2020 04:13:49 +0000 (23:13 -0500)
added configurable read timeout value for A&AI

Issue-ID: SO-3370
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: I1216608a09f6a8649a57aa4b320fbea4982a7efe

adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/openstack/AaiClientPropertiesImpl.java
asdc-controller/src/main/java/org/onap/so/asdc/tenantIsolation/AaiClientPropertiesImpl.java
bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/restproperties/AAIPropertiesImpl.java
common/src/main/java/org/onap/so/client/RestClient.java
common/src/main/java/org/onap/so/client/RestClientSSL.java
common/src/main/java/org/onap/so/client/RestProperties.java
common/src/test/java/org/onap/so/client/RestClientTest.java
mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/tenantisolation/AaiClientPropertiesImpl.java
so-etsi-nfvo/so-etsi-nfvo-ns-lcm/so-etsi-nfvo-ns-lcm-bpmn-flows/src/main/java/org/onap/so/etsi/nfvo/ns/lcm/bpmn/flows/extclients/aai/AaiPropertiesImpl.java

index 12e416f..b7e214f 100644 (file)
@@ -32,6 +32,7 @@ public class AaiClientPropertiesImpl implements AAIProperties {
     private String aaiEndpoint;
     private String auth;
     private String key;
+    private Long readTimeout;
     private static final String SYSTEM_NAME = "MSO";
 
     public AaiClientPropertiesImpl() {
@@ -39,6 +40,7 @@ public class AaiClientPropertiesImpl implements AAIProperties {
         aaiEndpoint = context.getEnvironment().getProperty("aai.endpoint");
         this.auth = context.getEnvironment().getProperty("aai.auth");
         this.key = context.getEnvironment().getProperty("mso.msoKey");
+        this.readTimeout = context.getEnvironment().getProperty("aai.readTimeout", Long.class, new Long(60000));
     }
 
     @Override
@@ -65,4 +67,9 @@ public class AaiClientPropertiesImpl implements AAIProperties {
     public String getKey() {
         return this.key;
     }
+
+    @Override
+    public Long getReadTimeout() {
+        return this.readTimeout;
+    }
 }
index 3ae815d..7b89af0 100644 (file)
@@ -32,6 +32,7 @@ public class AaiClientPropertiesImpl implements AAIProperties {
     private String aaiEndpoint;
     private String auth;
     private String key;
+    private Long readTimeout;
     private static final String SYSTEM_NAME = "MSO";
 
     public AaiClientPropertiesImpl() {
@@ -39,6 +40,7 @@ public class AaiClientPropertiesImpl implements AAIProperties {
         aaiEndpoint = context.getEnvironment().getProperty("mso.aai.endpoint");
         this.auth = context.getEnvironment().getProperty("aai.auth");
         this.key = context.getEnvironment().getProperty("mso.msoKey");
+        this.readTimeout = context.getEnvironment().getProperty("aai.readTimeout", Long.class, new Long(60000));
     }
 
     @Override
@@ -67,4 +69,9 @@ public class AaiClientPropertiesImpl implements AAIProperties {
     public String getKey() {
         return this.key;
     }
+
+    @Override
+    public Long getReadTimeout() {
+        return this.readTimeout;
+    }
 }
index 692d581..f67af20 100644 (file)
@@ -22,9 +22,9 @@ package org.onap.so.client.restproperties;
 
 import java.net.MalformedURLException;
 import java.net.URL;
-import org.onap.so.bpmn.core.UrnPropertiesReader;
 import org.onap.aaiclient.client.aai.AAIProperties;
 import org.onap.aaiclient.client.aai.AAIVersion;
+import org.onap.so.bpmn.core.UrnPropertiesReader;
 import org.springframework.stereotype.Component;
 
 @Component
@@ -33,6 +33,8 @@ public class AAIPropertiesImpl implements AAIProperties {
     public static final String MSO_MSO_KEY = "mso.msoKey";
     public static final String AAI_AUTH = "aai.auth";
     public static final String AAI_ENDPOINT = "aai.endpoint";
+    public static final String AAI_READ_TIMEOUT = "aai.readTimeout";
+    private UrnPropertiesReader reader;
 
     @Override
     public URL getEndpoint() throws MalformedURLException {
@@ -58,4 +60,10 @@ public class AAIPropertiesImpl implements AAIProperties {
     public String getKey() {
         return UrnPropertiesReader.getVariable(MSO_MSO_KEY);
     }
+
+    @Override
+    public Long getReadTimeout() {
+        return Long.valueOf(reader.getVariable(AAI_READ_TIMEOUT, "60000"));
+    }
+
 }
index ece1333..9fce328 100644 (file)
@@ -34,10 +34,12 @@ import java.util.List;
 import java.util.Map;
 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;
+import javax.ws.rs.client.ResponseProcessingException;
 import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.GenericType;
 import javax.ws.rs.core.MediaType;
@@ -187,7 +189,7 @@ public abstract class RestClient {
     }
 
     protected Client getClient() {
-        return ClientBuilder.newBuilder().build();
+        return ClientBuilder.newBuilder().readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build();
     }
 
     protected abstract ONAPComponentsList getTargetEntity();
@@ -201,7 +203,6 @@ public abstract class RestClient {
         metricLogClientFilter = new SOMetricLogClientFilter();
         mdcSetup.setTargetEntity(getTargetEntity());
         client.register(metricLogClientFilter);
-
         if (!path.isPresent()) {
             webTarget = client.target(host.toString());
         } else {
@@ -225,6 +226,9 @@ public abstract class RestClient {
         result.add(e -> {
             return e.getCause() instanceof ConnectException;
         });
+        result.add(e -> {
+            return e.getCause() instanceof ResponseProcessingException;
+        });
         return result;
     }
 
index 1e89538..8956e20 100644 (file)
@@ -24,6 +24,7 @@ import java.net.URI;
 import java.security.KeyStore;
 import java.security.NoSuchAlgorithmException;
 import java.util.Optional;
+import java.util.concurrent.TimeUnit;
 import javax.net.ssl.SSLContext;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
@@ -56,7 +57,8 @@ public abstract class RestClientSSL extends RestClient {
                 }
             }
             // Use default SSL context
-            client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()).build();
+            client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault())
+                    .readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build();
             logger.info("RestClientSSL using default SSL context!");
         } catch (NoSuchAlgorithmException e) {
             throw new RuntimeException(e);
index 9e4e99c..36da424 100644 (file)
@@ -40,4 +40,13 @@ public interface RestProperties {
     public default boolean mapNotFoundToEmpty() {
         return false;
     }
+
+    /**
+     * Time in milliseconds
+     * 
+     * @return
+     */
+    public default Long getReadTimeout() {
+        return Long.valueOf(60000);
+    }
 }
index cd00a9e..c6e282c 100644 (file)
@@ -21,6 +21,8 @@
 package org.onap.so.client;
 
 
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.spy;
@@ -28,7 +30,13 @@ import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import java.net.MalformedURLException;
 import java.net.SocketTimeoutException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Optional;
 import javax.ws.rs.NotFoundException;
+import javax.ws.rs.ProcessingException;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriBuilderException;
@@ -37,21 +45,24 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentMatchers;
-import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.logging.filter.base.ONAPComponents;
+import org.onap.logging.filter.base.ONAPComponentsList;
+import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+import com.github.tomakehurst.wiremock.junit.WireMockRule;
 
 @RunWith(MockitoJUnitRunner.class)
 public class RestClientTest {
 
 
     private final HttpClientFactory httpClientFactory = new HttpClientFactory();
-    @Mock
-    private RestProperties props;
 
     @Rule
     public ExpectedException thrown = ExpectedException.none();
 
+    @Rule
+    public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort());
+
     @Test
     public void retries() throws Exception {
         RestClient spy = buildSpy();
@@ -80,6 +91,50 @@ public class RestClientTest {
 
     }
 
+    @Test
+    public void timeoutTest() throws URISyntaxException {
+        wireMockRule.stubFor(get("/chunked/delayed")
+                .willReturn(aResponse().withStatus(200).withBody("Hello world!").withChunkedDribbleDelay(2, 300)));
+
+
+        RestProperties props = new RestProperties() {
+
+            @Override
+            public URL getEndpoint() throws MalformedURLException {
+                return new URL(String.format("http://localhost:%s", wireMockRule.port()));
+            }
+
+            @Override
+            public String getSystemName() {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            @Override
+            public Long getReadTimeout() {
+                return Long.valueOf(100);
+            }
+        };
+        RestClient client = new RestClient(props, Optional.of(new URI("/chunked/delayed"))) {
+
+            @Override
+            protected void initializeHeaderMap(Map<String, String> headerMap) {
+                // TODO Auto-generated method stub
+
+            }
+
+            @Override
+            protected ONAPComponentsList getTargetEntity() {
+                return ONAPComponents.EXTERNAL;
+            }
+
+        };
+
+        thrown.expect(ProcessingException.class);
+        client.get();
+
+    }
+
     private RestClient buildSpy() throws MalformedURLException, IllegalArgumentException, UriBuilderException {
         RestClient client = httpClientFactory.newJsonClient(UriBuilder.fromUri("http://localhost/test").build().toURL(),
                 ONAPComponents.BPMN);
index 8226278..6e6a9d2 100644 (file)
@@ -32,6 +32,7 @@ public class AaiClientPropertiesImpl implements AAIProperties {
     private String aaiEndpoint;
     private String auth;
     private String key;
+    private Long readTimeout;
 
     public AaiClientPropertiesImpl() {
 
@@ -39,6 +40,7 @@ public class AaiClientPropertiesImpl implements AAIProperties {
         aaiEndpoint = context.getEnvironment().getProperty("mso.aai.endpoint");
         this.auth = context.getEnvironment().getProperty("aai.auth");
         this.key = context.getEnvironment().getProperty("mso.msoKey");
+        this.readTimeout = context.getEnvironment().getProperty("aai.readTimeout", Long.class, new Long(60000));
     }
 
     @Override
@@ -65,4 +67,9 @@ public class AaiClientPropertiesImpl implements AAIProperties {
     public String getKey() {
         return this.key;
     }
+
+    @Override
+    public Long getReadTimeout() {
+        return this.readTimeout;
+    }
 }
index 3df3580..910d5fa 100644 (file)
@@ -33,14 +33,15 @@ public class AaiPropertiesImpl implements AAIProperties {
     private final String encryptedBasicAuth;
     private final String encryptionKey;
     private final String aaiVersion;
+    private final Long readTimeout;
 
     public AaiPropertiesImpl() {
-
         final ApplicationContext context = SpringContextHelper.getAppContext();
         this.endpoint = context.getEnvironment().getProperty("aai.endpoint");
         this.encryptedBasicAuth = context.getEnvironment().getProperty("aai.auth");
         this.encryptionKey = context.getEnvironment().getProperty("mso.key");
         this.aaiVersion = context.getEnvironment().getProperty("aai.version");
+        this.readTimeout = context.getEnvironment().getProperty("aai.readTimeout", Long.class, new Long(60000));
     }
 
     @Override
@@ -73,4 +74,9 @@ public class AaiPropertiesImpl implements AAIProperties {
     public String getKey() {
         return encryptionKey;
     }
+
+    @Override
+    public Long getReadTimeout() {
+        return this.readTimeout;
+    }
 }