1890a5b3711f7f7bedd196dbfd80c2180eb7e55a
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / VidServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.vid.services;
22
23 import com.google.common.cache.CacheBuilder;
24 import com.google.common.cache.CacheLoader;
25 import com.google.common.cache.LoadingCache;
26 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
27 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
28 import org.onap.vid.asdc.AsdcCatalogException;
29 import org.onap.vid.asdc.AsdcClient;
30 import org.onap.vid.asdc.beans.Service;
31 import org.onap.vid.asdc.parser.ToscaParser;
32 import org.onap.vid.asdc.parser.ToscaParserImpl;
33 import org.onap.vid.asdc.parser.ToscaParserImpl2;
34 import org.onap.vid.exceptions.GenericUncheckedException;
35 import org.onap.vid.model.ServiceModel;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.togglz.core.manager.FeatureManager;
38
39 import java.nio.file.Path;
40 import java.util.UUID;
41 import java.util.concurrent.ExecutionException;
42 import java.util.concurrent.TimeUnit;
43
44 import static org.onap.vid.properties.Features.FLAG_SERVICE_MODEL_CACHE;
45
46 /**
47  * The Class VidController.
48  */
49
50 public class VidServiceImpl implements VidService {
51     /**
52      * The Constant LOG.
53      */
54     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidServiceImpl.class);
55     /**
56      * The Constant dateFormat.
57      */
58     protected final AsdcClient asdcClient;
59     private final FeatureManager featureManager;
60
61     @Autowired
62     private ToscaParserImpl2 toscaParser;
63     private final LoadingCache<String, ServiceModel> serviceModelCache;
64
65
66     private class NullServiceModelException extends Exception {
67         NullServiceModelException(String modelUuid) {
68             super("Could not create service model for UUID " + modelUuid);
69         }
70     }
71
72     public VidServiceImpl(AsdcClient asdcClient, FeatureManager featureManager) {
73         this.asdcClient = asdcClient;
74         this.featureManager = featureManager;
75
76         this.serviceModelCache = CacheBuilder.newBuilder()
77                 .maximumSize(1000)
78                 .expireAfterAccess(7, TimeUnit.DAYS)
79                 .build(new CacheLoader<String, ServiceModel>() {
80                     @Override
81                     public ServiceModel load(String modelUuid) throws AsdcCatalogException, NullServiceModelException {
82                         ServiceModel serviceModel = getServiceFromSdc(modelUuid);
83                         if (serviceModel != null) {
84                             return serviceModel;
85                         } else {
86                             throw new NullServiceModelException(modelUuid);
87                         }
88                     }
89                 });
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.onap.vid.controller.VidService#getService(java.lang.String)
96      */
97     @Override
98     public ServiceModel getService(String uuid) throws AsdcCatalogException {
99         if (featureManager.isActive(FLAG_SERVICE_MODEL_CACHE)) {
100             return getServiceFromCache(uuid);
101         }else {
102             return getServiceFromSdc(uuid);
103         }
104     }
105
106     private ServiceModel getServiceFromCache(String uuid) throws AsdcCatalogException {
107         try {
108             return serviceModelCache.get(uuid);
109         } catch (ExecutionException e) {
110             if (e.getCause() instanceof AsdcCatalogException) {
111                 throw (AsdcCatalogException) e.getCause();
112             } else if (e.getCause() instanceof NullServiceModelException) {
113                 return null;
114             } else {
115                 throw new GenericUncheckedException(e);
116             }
117         }
118     }
119
120     private ServiceModel getServiceFromSdc(String uuid) throws AsdcCatalogException {
121         final Path serviceCsar = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
122         ToscaParser tosca = new ToscaParserImpl();
123         serviceCsar.toFile().getAbsolutePath();
124         ServiceModel serviceModel = null;
125         try {
126             final Service asdcServiceMetadata = asdcClient.getService(UUID.fromString(uuid));
127             return getServiceModel(uuid, serviceCsar, tosca, asdcServiceMetadata);
128         } catch (Exception e) {
129             LOG.error("Failed to download and proccess service from SDC", e);
130         }
131         return serviceModel;
132     }
133
134     private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws AsdcCatalogException {
135         try {
136             return toscaParser.makeServiceModel(serviceCsar, asdcServiceMetadata);
137         } catch (SdcToscaParserException e) {
138             return tosca.makeServiceModel(uuid, serviceCsar, asdcServiceMetadata);
139         }
140     }
141     
142     @Override
143     public void invalidateServiceCache(){
144         serviceModelCache.invalidateAll();
145     }
146
147 }