1 package org.onap.vid.services;
3 import com.google.common.cache.CacheBuilder;
4 import com.google.common.cache.CacheLoader;
5 import com.google.common.cache.LoadingCache;
6 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
7 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
8 import org.onap.vid.asdc.AsdcCatalogException;
9 import org.onap.vid.asdc.AsdcClient;
10 import org.onap.vid.asdc.beans.Service;
11 import org.onap.vid.asdc.parser.ToscaParser;
12 import org.onap.vid.asdc.parser.ToscaParserImpl;
13 import org.onap.vid.asdc.parser.ToscaParserImpl2;
14 import org.onap.vid.exceptions.GenericUncheckedException;
15 import org.onap.vid.model.ServiceModel;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.togglz.core.manager.FeatureManager;
19 import java.nio.file.Path;
20 import java.util.UUID;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
24 import static org.onap.vid.properties.Features.FLAG_SERVICE_MODEL_CACHE;
27 * The Class VidController.
30 public class VidServiceImpl implements VidService {
34 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidServiceImpl.class);
36 * The Constant dateFormat.
38 protected final AsdcClient asdcClient;
39 private final FeatureManager featureManager;
42 private ToscaParserImpl2 toscaParser;
43 private final LoadingCache<String, ServiceModel> serviceModelCache;
46 private class NullServiceModelException extends Exception {
47 NullServiceModelException(String modelUuid) {
48 super("Could not create service model for UUID " + modelUuid);
52 public VidServiceImpl(AsdcClient asdcClient, FeatureManager featureManager) {
53 this.asdcClient = asdcClient;
54 this.featureManager = featureManager;
56 this.serviceModelCache = CacheBuilder.newBuilder()
58 .expireAfterAccess(7, TimeUnit.DAYS)
59 .build(new CacheLoader<String, ServiceModel>() {
61 public ServiceModel load(String modelUuid) throws AsdcCatalogException, NullServiceModelException {
62 ServiceModel serviceModel = getServiceFromSdc(modelUuid);
63 if (serviceModel != null) {
66 throw new NullServiceModelException(modelUuid);
75 * @see org.onap.vid.controller.VidService#getService(java.lang.String)
78 public ServiceModel getService(String uuid) throws AsdcCatalogException {
79 if (featureManager.isActive(FLAG_SERVICE_MODEL_CACHE)) {
80 return getServiceFromCache(uuid);
82 return getServiceFromSdc(uuid);
86 private ServiceModel getServiceFromCache(String uuid) throws AsdcCatalogException {
88 return serviceModelCache.get(uuid);
89 } catch (ExecutionException e) {
90 if (e.getCause() instanceof AsdcCatalogException) {
91 throw (AsdcCatalogException) e.getCause();
92 } else if (e.getCause() instanceof NullServiceModelException) {
95 throw new GenericUncheckedException(e);
100 private ServiceModel getServiceFromSdc(String uuid) throws AsdcCatalogException {
101 final Path serviceCsar = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
102 ToscaParser tosca = new ToscaParserImpl();
103 serviceCsar.toFile().getAbsolutePath();
104 ServiceModel serviceModel = null;
106 final Service asdcServiceMetadata = asdcClient.getService(UUID.fromString(uuid));
107 return getServiceModel(uuid, serviceCsar, tosca, asdcServiceMetadata);
108 } catch (Exception e) {
109 LOG.error("Failed to download and proccess service from SDC", e);
114 private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws AsdcCatalogException {
116 return toscaParser.makeServiceModel(serviceCsar, asdcServiceMetadata);
117 } catch (SdcToscaParserException e) {
118 return tosca.makeServiceModel(uuid, serviceCsar, asdcServiceMetadata);
123 public void invalidateServiceCache(){
124 serviceModelCache.invalidateAll();