From a16231657fe29334a589c98290ac8b6b2710a144 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max" Date: Thu, 19 Nov 2020 18:12:50 -0500 Subject: add caching to graph inventory client add caching to graph inventory client updated properties files to read cache properties Issue-ID: SO-3398 Signed-off-by: Benjamin, Max (mb388a) Change-Id: Ib3e67ae014b6668c9b004aae1e8b5d49b9ce6b06 --- common/pom.xml | 12 ++- .../java/org/onap/so/client/AddCacheHeaders.java | 28 +++++++ .../main/java/org/onap/so/client/CacheFactory.java | 25 ++++++ .../java/org/onap/so/client/CacheProperties.java | 13 ++++ .../main/java/org/onap/so/client/RestClient.java | 14 +++- .../java/org/onap/so/client/RestClientSSL.java | 4 +- .../java/org/onap/so/client/RestProperties.java | 8 ++ .../java/org/onap/so/client/RestClientTest.java | 4 +- common/src/test/resources/logback-test.xml | 91 +++++++++++----------- 9 files changed, 147 insertions(+), 52 deletions(-) create mode 100644 common/src/main/java/org/onap/so/client/AddCacheHeaders.java create mode 100644 common/src/main/java/org/onap/so/client/CacheFactory.java create mode 100644 common/src/main/java/org/onap/so/client/CacheProperties.java (limited to 'common') diff --git a/common/pom.xml b/common/pom.xml index 74e51805ad..6e265925c3 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -293,6 +293,16 @@ jaxb-impl 2.3.0 + + javax.cache + cache-api + 1.0.0 + + + org.ehcache + ehcache + 3.8.1 + @@ -366,4 +376,4 @@ - \ No newline at end of file + diff --git a/common/src/main/java/org/onap/so/client/AddCacheHeaders.java b/common/src/main/java/org/onap/so/client/AddCacheHeaders.java new file mode 100644 index 0000000000..1a41be1233 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/AddCacheHeaders.java @@ -0,0 +1,28 @@ +package org.onap.so.client; + +import java.io.IOException; +import java.util.Collections; +import javax.annotation.Priority; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.ext.Provider; + +@Provider +@Priority(1) +public class AddCacheHeaders implements ClientResponseFilter { + + private final CacheProperties props; + + public AddCacheHeaders(CacheProperties props) { + this.props = props; + } + + public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException { + if (request.getMethod().equalsIgnoreCase("GET")) { + response.getHeaders().putIfAbsent("Cache-Control", + Collections.singletonList("public, max-age=" + (props.getMaxAge() / 1000))); + } + + } +} diff --git a/common/src/main/java/org/onap/so/client/CacheFactory.java b/common/src/main/java/org/onap/so/client/CacheFactory.java new file mode 100644 index 0000000000..6bc4858463 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/CacheFactory.java @@ -0,0 +1,25 @@ +package org.onap.so.client; + + +import java.util.concurrent.TimeUnit; +import javax.cache.configuration.Factory; +import javax.cache.expiry.Duration; +import javax.cache.expiry.ExpiryPolicy; +import javax.cache.expiry.TouchedExpiryPolicy; + +public class CacheFactory implements Factory { + + private static final long serialVersionUID = 8948728679233836929L; + + private final CacheProperties props; + + public CacheFactory(CacheProperties props) { + this.props = props; + } + + @Override + public ExpiryPolicy create() { + return TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, props.getMaxAge())).create(); + } + +} diff --git a/common/src/main/java/org/onap/so/client/CacheProperties.java b/common/src/main/java/org/onap/so/client/CacheProperties.java new file mode 100644 index 0000000000..4fb2a87a5b --- /dev/null +++ b/common/src/main/java/org/onap/so/client/CacheProperties.java @@ -0,0 +1,13 @@ +package org.onap.so.client; + +public interface CacheProperties { + + + default Long getMaxAge() { + return 60000L; + } + + default String getCacheName() { + return "default-http-cache"; + } +} 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 9fce328b1d..be0a0f3f9e 100644 --- a/common/src/main/java/org/onap/so/client/RestClient.java +++ b/common/src/main/java/org/onap/so/client/RestClient.java @@ -188,8 +188,20 @@ public abstract class RestClient { return APPLICATION_MERGE_PATCH_JSON; } + protected ClientBuilder getClientBuilder() { + ClientBuilder builder = ClientBuilder.newBuilder(); + if (props.isCachingEnabled()) { + enableCaching(builder); + } + return builder.readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS); + } + + protected ClientBuilder enableCaching(ClientBuilder builder) { + return builder; + } + protected Client getClient() { - return ClientBuilder.newBuilder().readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build(); + return getClientBuilder().build(); } protected abstract ONAPComponentsList getTargetEntity(); 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 8956e20a5a..c6252e4652 100644 --- a/common/src/main/java/org/onap/so/client/RestClientSSL.java +++ b/common/src/main/java/org/onap/so/client/RestClientSSL.java @@ -24,7 +24,6 @@ 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; @@ -57,8 +56,7 @@ public abstract class RestClientSSL extends RestClient { } } // Use default SSL context - client = ClientBuilder.newBuilder().sslContext(SSLContext.getDefault()) - .readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS).build(); + client = getClientBuilder().sslContext(SSLContext.getDefault()).build(); logger.info("RestClientSSL using default SSL context!"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); diff --git a/common/src/main/java/org/onap/so/client/RestProperties.java b/common/src/main/java/org/onap/so/client/RestProperties.java index 36da424f93..a7a0ef614c 100644 --- a/common/src/main/java/org/onap/so/client/RestProperties.java +++ b/common/src/main/java/org/onap/so/client/RestProperties.java @@ -49,4 +49,12 @@ public interface RestProperties { public default Long getReadTimeout() { return Long.valueOf(60000); } + + public default boolean isCachingEnabled() { + return false; + } + + public default CacheProperties getCacheProperties() { + return new CacheProperties() {}; + } } diff --git a/common/src/test/java/org/onap/so/client/RestClientTest.java b/common/src/test/java/org/onap/so/client/RestClientTest.java index c6e282c14a..d40576b69f 100644 --- a/common/src/test/java/org/onap/so/client/RestClientTest.java +++ b/common/src/test/java/org/onap/so/client/RestClientTest.java @@ -49,6 +49,7 @@ 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.extension.responsetemplating.ResponseTemplateTransformer; import com.github.tomakehurst.wiremock.junit.WireMockRule; @RunWith(MockitoJUnitRunner.class) @@ -61,7 +62,8 @@ public class RestClientTest { public ExpectedException thrown = ExpectedException.none(); @Rule - public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.options().dynamicPort()); + public WireMockRule wireMockRule = new WireMockRule( + WireMockConfiguration.options().dynamicPort().extensions(new ResponseTemplateTransformer(false))); @Test public void retries() throws Exception { diff --git a/common/src/test/resources/logback-test.xml b/common/src/test/resources/logback-test.xml index b52e6be022..3c5f259817 100644 --- a/common/src/test/resources/logback-test.xml +++ b/common/src/test/resources/logback-test.xml @@ -19,61 +19,60 @@ --> - - - - - - - - - + + + + + + + + + - - - ${pattern} - - + + + ${pattern} + + - + - - - + + + - - - + + + - - - + + + - - - - - - - - - + + + + - - - - - - - - + + + - - - - + + + + + + + + + + + + + \ No newline at end of file -- cgit 1.2.3-korg