Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / VidServiceImpl.java
1 package org.onap.vid.services;
2
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;
18
19 import java.nio.file.Path;
20 import java.util.UUID;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23
24 import static org.onap.vid.properties.Features.FLAG_SERVICE_MODEL_CACHE;
25
26 /**
27  * The Class VidController.
28  */
29
30 public class VidServiceImpl implements VidService {
31     /**
32      * The Constant LOG.
33      */
34     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidServiceImpl.class);
35     /**
36      * The Constant dateFormat.
37      */
38     protected final AsdcClient asdcClient;
39     private final FeatureManager featureManager;
40
41     @Autowired
42     private ToscaParserImpl2 toscaParser;
43     private final LoadingCache<String, ServiceModel> serviceModelCache;
44
45
46     private class NullServiceModelException extends Exception {
47         NullServiceModelException(String modelUuid) {
48             super("Could not create service model for UUID " + modelUuid);
49         }
50     }
51
52     public VidServiceImpl(AsdcClient asdcClient, FeatureManager featureManager) {
53         this.asdcClient = asdcClient;
54         this.featureManager = featureManager;
55
56         this.serviceModelCache = CacheBuilder.newBuilder()
57                 .maximumSize(1000)
58                 .expireAfterAccess(7, TimeUnit.DAYS)
59                 .build(new CacheLoader<String, ServiceModel>() {
60                     @Override
61                     public ServiceModel load(String modelUuid) throws AsdcCatalogException, NullServiceModelException {
62                         ServiceModel serviceModel = getServiceFromSdc(modelUuid);
63                         if (serviceModel != null) {
64                             return serviceModel;
65                         } else {
66                             throw new NullServiceModelException(modelUuid);
67                         }
68                     }
69                 });
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.onap.vid.controller.VidService#getService(java.lang.String)
76      */
77     @Override
78     public ServiceModel getService(String uuid) throws AsdcCatalogException {
79         if (featureManager.isActive(FLAG_SERVICE_MODEL_CACHE)) {
80             return getServiceFromCache(uuid);
81         }else {
82             return getServiceFromSdc(uuid);
83         }
84     }
85
86     private ServiceModel getServiceFromCache(String uuid) throws AsdcCatalogException {
87         try {
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) {
93                 return null;
94             } else {
95                 throw new GenericUncheckedException(e);
96             }
97         }
98     }
99
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;
105         try {
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);
110         }
111         return serviceModel;
112     }
113
114     private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws AsdcCatalogException {
115         try {
116             return toscaParser.makeServiceModel(serviceCsar, asdcServiceMetadata);
117         } catch (SdcToscaParserException e) {
118             return tosca.makeServiceModel(uuid, serviceCsar, asdcServiceMetadata);
119         }
120     }
121     
122     @Override
123     public void invalidateServiceCache(){
124         serviceModelCache.invalidateAll();
125     }
126
127 }