aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java
diff options
context:
space:
mode:
authorSonsino, Ofir (os0695) <os0695@intl.att.com>2018-07-10 14:20:54 +0300
committerSonsino, Ofir (os0695) <os0695@intl.att.com>2018-07-10 14:20:54 +0300
commitc72d565bb58226b20625b2bce5f0019046bee649 (patch)
tree8658e49595705b02e47ddc14afa20d6bb7123547 /vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java
parentef8a6b47847012fd59ea20da21d8d3d7c4a301ed (diff)
Merge 1806 code of vid-common
Change-Id: I75d52abed4a24dfe3827d79edc4a2938726aa87a Issue-ID: VID-208 Signed-off-by: Sonsino, Ofir (os0695) <os0695@intl.att.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/services/VidServiceImpl.java78
1 files changed, 63 insertions, 15 deletions
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<String, ServiceModel> 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<Service> getServices(Map<String, String[]> 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<String, ServiceModel>() {
+ @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