2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
23 import static org.onap.vid.model.probes.ExternalComponentStatus.Component.SDC;
24 import static org.onap.vid.properties.Features.FLAG_SERVICE_MODEL_CACHE;
26 import com.google.common.cache.CacheBuilder;
27 import com.google.common.cache.CacheLoader;
28 import com.google.common.cache.LoadingCache;
29 import io.joshworks.restclient.http.HttpResponse;
30 import java.io.InputStream;
31 import java.nio.file.Path;
32 import java.util.UUID;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.TimeUnit;
35 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
37 import org.onap.vid.aai.ExceptionWithRequestInfo;
38 import org.onap.vid.aai.HttpResponseWithRequestInfo;
39 import org.onap.vid.asdc.AsdcCatalogException;
40 import org.onap.vid.asdc.AsdcClient;
41 import org.onap.vid.asdc.beans.Service;
42 import org.onap.vid.asdc.parser.ToscaParser;
43 import org.onap.vid.asdc.parser.ToscaParserImpl;
44 import org.onap.vid.asdc.parser.ToscaParserImpl2;
45 import org.onap.vid.exceptions.GenericUncheckedException;
46 import org.onap.vid.model.ServiceModel;
47 import org.onap.vid.model.probes.ErrorMetadata;
48 import org.onap.vid.model.probes.ExternalComponentStatus;
49 import org.onap.vid.model.probes.HttpRequestMetadata;
50 import org.onap.vid.model.probes.StatusMetadata;
51 import org.onap.vid.properties.VidProperties;
52 import org.onap.vid.utils.Logging;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.http.HttpMethod;
55 import org.springframework.lang.NonNull;
56 import org.togglz.core.manager.FeatureManager;
59 * The Class VidController.
62 @org.springframework.stereotype.Service
63 public class VidServiceImpl implements VidService {
67 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidServiceImpl.class);
69 protected final AsdcClient asdcClient;
70 private final FeatureManager featureManager;
72 private ToscaParserImpl2 toscaParser;
73 private final LoadingCache<String, ServiceModel> serviceModelCache;
76 private class NullServiceModelException extends Exception {
77 NullServiceModelException(String modelUuid) {
78 super("Could not create service model for UUID " + modelUuid);
83 public VidServiceImpl(AsdcClient asdcClient, ToscaParserImpl2 toscaParser, FeatureManager featureManager) {
84 this.asdcClient = asdcClient;
85 this.featureManager = featureManager;
86 this.toscaParser=toscaParser;
87 this.serviceModelCache = CacheBuilder.newBuilder()
89 .expireAfterAccess(7, TimeUnit.DAYS)
90 .build(new CacheLoader<String, ServiceModel>() {
92 public ServiceModel load(String modelUuid) throws AsdcCatalogException, NullServiceModelException {
93 ServiceModel serviceModel = getServiceFromSdc(modelUuid);
94 if (serviceModel != null) {
97 throw new NullServiceModelException(modelUuid);
104 public ServiceModel getService(String uuid) throws AsdcCatalogException {
105 if (featureManager.isActive(FLAG_SERVICE_MODEL_CACHE)) {
106 return getServiceFromCache(uuid);
108 return getServiceFromSdc(uuid);
114 public ServiceModel getServiceModelOrThrow(String modelVersionId) {
116 final ServiceModel serviceModel = getService(modelVersionId);
117 if (serviceModel == null) {
118 throw new GenericUncheckedException("Model version '" + modelVersionId + "' not found");
121 } catch (AsdcCatalogException e) {
122 throw new GenericUncheckedException("Exception while loading model version '" + modelVersionId + "'", e);
126 private ServiceModel getServiceFromCache(String uuid) throws AsdcCatalogException {
128 return serviceModelCache.get(uuid);
129 } catch (ExecutionException e) {
130 LOG.error("Failed to get service info from cache ", e.getLocalizedMessage());
131 if (e.getCause() instanceof AsdcCatalogException) {
132 throw (AsdcCatalogException) e.getCause();
133 } else if (e.getCause() instanceof NullServiceModelException) {
136 throw new GenericUncheckedException(e);
141 private ServiceModel getServiceFromSdc(String uuid) throws AsdcCatalogException {
142 final Path serviceCsar = asdcClient.getServiceToscaModel(UUID.fromString(uuid));
143 ToscaParser tosca = new ToscaParserImpl();
144 serviceCsar.toFile().getAbsolutePath();
145 ServiceModel serviceModel = null;
147 final Service asdcServiceMetadata = asdcClient.getService(UUID.fromString(uuid));
148 return getServiceModel(uuid, serviceCsar, tosca, asdcServiceMetadata);
149 } catch (Exception e) {
150 LOG.error("Failed to download and process service from SDC", e);
155 private ServiceModel getServiceModel(String uuid, Path serviceCsar, ToscaParser tosca, Service asdcServiceMetadata) throws AsdcCatalogException {
157 return toscaParser.makeServiceModel(serviceCsar, asdcServiceMetadata);
158 } catch (SdcToscaParserException e) {
159 LOG.error("Failed to create service model using sdc tosca library", e);
160 return tosca.makeServiceModel(uuid, serviceCsar, asdcServiceMetadata);
165 public void invalidateServiceCache() {
166 serviceModelCache.invalidateAll();
170 public ExternalComponentStatus probeComponent() {
173 modelUUID = UUID.fromString(VidProperties.getPropertyWithDefault(
174 VidProperties.PROBE_SDC_MODEL_UUID,""));
176 //in case of no such PROBE_SDC_MODEL_UUID property or non uuid there we check only sdc connectivity
177 catch (Exception e) {
178 return probeComponentBySdcConnectivity();
181 return probeSdcByGettingModel(modelUUID);
184 public ExternalComponentStatus probeComponentBySdcConnectivity() {
185 long startTime = System.currentTimeMillis();
186 ExternalComponentStatus externalComponentStatus;
188 HttpResponse<String> stringHttpResponse = asdcClient.checkSDCConnectivity();
189 HttpRequestMetadata httpRequestMetadata = new HttpRequestMetadata(HttpMethod.GET, stringHttpResponse.getStatus(), asdcClient.getBaseUrl() + AsdcClient.URIS.HEALTH_CHECK_ENDPOINT, stringHttpResponse.getBody(), "SDC healthCheck",
190 System.currentTimeMillis() - startTime);
191 externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.SDC, stringHttpResponse.isSuccessful(), httpRequestMetadata);
192 } catch (Exception e) {
193 ErrorMetadata errorMetadata = new ErrorMetadata(Logging.exceptionToDescription(e), System.currentTimeMillis() - startTime);
194 externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.SDC, false, errorMetadata);
196 return externalComponentStatus;
199 protected ExternalComponentStatus probeSdcByGettingModel(UUID modelUUID) {
200 final long startTime = System.currentTimeMillis();
202 HttpResponseWithRequestInfo<InputStream> response = null;
204 response = asdcClient.getServiceInputStream(modelUUID, true);
205 int responseStatusCode = response.getResponse().getStatus();
207 if (responseStatusCode == 404) {
208 return errorStatus(response, startTime, "model " + modelUUID + " not found in SDC" +
209 " (consider updating vid probe configuration '" + VidProperties.PROBE_SDC_MODEL_UUID + "')");
210 } else if (responseStatusCode >= 400) {
211 return errorStatus(response, startTime, "error while retrieving model " + modelUUID + " from SDC");
213 InputStream inputStreamEntity = response.getResponse().getRawBody();//validate we can read the input steam from the response
214 if (inputStreamEntity.read() <= 0) {
215 return errorStatus(response, startTime, "error reading model " + modelUUID + " from SDC");
217 // RESPONSE IS SUCCESS
218 return new ExternalComponentStatus(SDC, true,
219 new HttpRequestMetadata(response, "OK", startTime, false));
222 } catch (ExceptionWithRequestInfo e) {
223 return new ExternalComponentStatus(SDC, false,
224 new HttpRequestMetadata(e, System.currentTimeMillis() - startTime));
225 } catch (Exception e) {
226 return errorStatus(response, startTime, Logging.exceptionToDescription(e));
230 private ExternalComponentStatus errorStatus(HttpResponseWithRequestInfo<InputStream> response, long startTime, String description) {
231 final long duration = System.currentTimeMillis() - startTime;
232 StatusMetadata statusMetadata = (response == null) ?
233 new ErrorMetadata(description, duration) :
234 new HttpRequestMetadata(response, description, duration, true);
236 return new ExternalComponentStatus(SDC, false, statusMetadata);