Extend probe mechanism
[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 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 - 2019 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 io.joshworks.restclient.http.HttpResponse;
27 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
28 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
29 import org.onap.vid.asdc.AsdcCatalogException;
30 import org.onap.vid.asdc.AsdcClient;
31 import org.onap.vid.asdc.beans.Service;
32 import org.onap.vid.asdc.parser.ToscaParser;
33 import org.onap.vid.asdc.parser.ToscaParserImpl;
34 import org.onap.vid.asdc.parser.ToscaParserImpl2;
35 import org.onap.vid.exceptions.GenericUncheckedException;
36 import org.onap.vid.model.ServiceModel;
37 import org.onap.vid.model.probes.ExternalComponentStatus;
38 import org.onap.vid.model.probes.HttpRequestMetadata;
39 import org.onap.vid.utils.Logging;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpMethod;
42 import org.togglz.core.manager.FeatureManager;
43
44 import java.nio.file.Path;
45 import java.util.UUID;
46 import java.util.concurrent.ExecutionException;
47 import java.util.concurrent.TimeUnit;
48
49 import static org.onap.vid.properties.Features.FLAG_SERVICE_MODEL_CACHE;
50
51 /**
52  * The Class VidController.
53  */
54
55 public class VidServiceImpl implements VidService {
56     /**
57      * The Constant LOG.
58      */
59     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidServiceImpl.class);
60
61     protected final AsdcClient asdcClient;
62     private final FeatureManager featureManager;
63
64     @Autowired
65     private ToscaParserImpl2 toscaParser;
66     private final LoadingCache<String, ServiceModel> serviceModelCache;
67
68
69     private class NullServiceModelException extends Exception {
70         NullServiceModelException(String modelUuid) {
71             super("Could not create service model for UUID " + modelUuid);
72         }
73     }
74
75     public VidServiceImpl(AsdcClient asdcClient, FeatureManager featureManager) {
76         this.asdcClient = asdcClient;
77         this.featureManager = featureManager;
78
79         this.serviceModelCache = CacheBuilder.newBuilder()
80                 .maximumSize(1000)
81                 .expireAfterAccess(7, TimeUnit.DAYS)
82                 .build(new CacheLoader<String, ServiceModel>() {
83                     @Override
84                     public ServiceModel load(String modelUuid) throws AsdcCatalogException, NullServiceModelException {
85                         ServiceModel serviceModel = getServiceFromSdc(modelUuid);
86                         if (serviceModel != null) {
87                             return serviceModel;
88                         } else {
89                             throw new NullServiceModelException(modelUuid);
90                         }
91                     }
92                 });
93     }
94
95     /*
96      * (non-Javadoc)
97      *
98      * @see org.onap.vid.controller.VidService#getService(java.lang.String)
99      */
100     @Override
101     public ServiceModel getService(String uuid) throws AsdcCatalogException {
102         if (featureManager.isActive(FLAG_SERVICE_MODEL_CACHE)) {
103             return getServiceFromCache(uuid);
104         } else {
105             return getServiceFromSdc(uuid);
106         }
107     }
108
109     private ServiceModel getServiceFromCache(String uuid) throws AsdcCatalogException {
110         try {
111             return serviceModelCache.get(uuid);
112         } catch (ExecutionException e) {
113             if (e.getCause() instanceof AsdcCatalogException) {
114                 throw (AsdcCatalogException) e.getCause();
115             } else if (e.getCause() instanceof NullServiceModelException) {
116                 return null;
117             } else {
118                 throw new GenericUncheckedException(e);
119             }
120         }
121     }
122
123     private ServiceModel getServiceFromSdc(String uuid) throws AsdcCatalogException {
124         final Path serviceCsar = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
125         ToscaParser tosca = new ToscaParserImpl();
126         serviceCsar.toFile().getAbsolutePath();
127         ServiceModel serviceModel = null;
128         try {
129             final Service asdcServiceMetadata = asdcClient.getService(UUID.fromString(uuid));
130             return getServiceModel(uuid, serviceCsar, tosca, asdcServiceMetadata);
131         } catch (Exception e) {
132             LOG.error("Failed to download and process service from SDC", e);
133         }
134         return serviceModel;
135     }
136
137     private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws AsdcCatalogException {
138         try {
139             return toscaParser.makeServiceModel(serviceCsar, asdcServiceMetadata);
140         } catch (SdcToscaParserException e) {
141             return tosca.makeServiceModel(uuid, serviceCsar, asdcServiceMetadata);
142         }
143     }
144
145     @Override
146     public void invalidateServiceCache() {
147         serviceModelCache.invalidateAll();
148     }
149
150     @Override
151     public ExternalComponentStatus probeSDCConnection() {
152         long startTime = System.currentTimeMillis();
153         ExternalComponentStatus externalComponentStatus;
154         try {
155             HttpResponse<String> stringHttpResponse = asdcClient.checkSDCConnectivity();
156             HttpRequestMetadata httpRequestMetadata = new HttpRequestMetadata(stringHttpResponse, HttpMethod.GET, "SDC healthCheck",
157                     System.currentTimeMillis() - startTime, AsdcClient.URIS.HEALTH_CHECK_ENDPOINT);
158             externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.SDC, stringHttpResponse.isSuccessful(), httpRequestMetadata);
159         } catch (Exception e) {
160             HttpRequestMetadata httpRequestMetadata = new HttpRequestMetadata(HttpMethod.GET, 0,
161                     AsdcClient.URIS.HEALTH_CHECK_ENDPOINT, "", Logging.exceptionToDescription(e), System.currentTimeMillis() - startTime);
162             externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.SDC, false, httpRequestMetadata);
163         }
164         return externalComponentStatus;
165     }
166 }