From c72d565bb58226b20625b2bce5f0019046bee649 Mon Sep 17 00:00:00 2001 From: "Sonsino, Ofir (os0695)" Date: Tue, 10 Jul 2018 14:20:54 +0300 Subject: Merge 1806 code of vid-common Change-Id: I75d52abed4a24dfe3827d79edc4a2938726aa87a Issue-ID: VID-208 Signed-off-by: Sonsino, Ofir (os0695) --- .../java/org/onap/vid/services/VidServiceImpl.java | 78 +++++++++++++++++----- 1 file changed, 63 insertions(+), 15 deletions(-) (limited to 'vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java') diff --git a/vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java b/vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java index 552430195..dc2541b3f 100644 --- a/vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java +++ b/vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java @@ -1,5 +1,8 @@ package org.onap.vid.services; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; import org.onap.vid.asdc.AsdcCatalogException; @@ -8,13 +11,17 @@ import org.onap.vid.asdc.beans.Service; import org.onap.vid.asdc.parser.ToscaParser; import org.onap.vid.asdc.parser.ToscaParserImpl; import org.onap.vid.asdc.parser.ToscaParserImpl2; +import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.vid.model.ServiceModel; import org.springframework.beans.factory.annotation.Autowired; +import org.togglz.core.manager.FeatureManager; import java.nio.file.Path; -import java.util.Collection; -import java.util.Map; import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static org.onap.vid.properties.Features.FLAG_SERVICE_MODEL_CACHE; /** * The Class VidController. @@ -29,22 +36,37 @@ public class VidServiceImpl implements VidService { * The Constant dateFormat. */ protected final AsdcClient asdcClient; + private final FeatureManager featureManager; + @Autowired private ToscaParserImpl2 toscaParser; + private final LoadingCache serviceModelCache; - public VidServiceImpl(AsdcClient asdcClient) { - this.asdcClient = asdcClient; + + private class NullServiceModelException extends Exception { + NullServiceModelException(String modelUuid) { + super("Could not create service model for UUID " + modelUuid); + } } - /* - * (non-Javadoc) - * - * @see org.onap.vid.controller.VidService#getServices(java.util.Map) - */ - @Override - public Collection getServices(Map requestParams) - throws AsdcCatalogException { - return asdcClient.getServices(requestParams); + public VidServiceImpl(AsdcClient asdcClient, FeatureManager featureManager) { + this.asdcClient = asdcClient; + this.featureManager = featureManager; + + this.serviceModelCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterAccess(7, TimeUnit.DAYS) + .build(new CacheLoader() { + @Override + public ServiceModel load(String modelUuid) throws AsdcCatalogException, NullServiceModelException { + ServiceModel serviceModel = getServiceFromSdc(modelUuid); + if (serviceModel != null) { + return serviceModel; + } else { + throw new NullServiceModelException(modelUuid); + } + } + }); } /* @@ -54,6 +76,28 @@ public class VidServiceImpl implements VidService { */ @Override public ServiceModel getService(String uuid) throws AsdcCatalogException { + if (featureManager.isActive(FLAG_SERVICE_MODEL_CACHE)) { + return getServiceFromCache(uuid); + }else { + return getServiceFromSdc(uuid); + } + } + + private ServiceModel getServiceFromCache(String uuid) throws AsdcCatalogException { + try { + return serviceModelCache.get(uuid); + } catch (ExecutionException e) { + if (e.getCause() instanceof AsdcCatalogException) { + throw (AsdcCatalogException) e.getCause(); + } else if (e.getCause() instanceof NullServiceModelException) { + return null; + } else { + throw new GenericUncheckedException(e); + } + } + } + + private ServiceModel getServiceFromSdc(String uuid) throws AsdcCatalogException { final Path serviceCsar = asdcClient.getServiceToscaModel(UUID.fromString(uuid)); ToscaParser tosca = new ToscaParserImpl(); serviceCsar.toFile().getAbsolutePath(); @@ -67,13 +111,17 @@ public class VidServiceImpl implements VidService { return serviceModel; } - private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws Exception { + private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws AsdcCatalogException { try { return toscaParser.makeServiceModel(serviceCsar, asdcServiceMetadata); } catch (SdcToscaParserException e) { return tosca.makeServiceModel(uuid, serviceCsar, asdcServiceMetadata); } } - + + @Override + public void invalidateServiceCache(){ + serviceModelCache.invalidateAll(); + } } \ No newline at end of file -- cgit 1.2.3-korg