2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.nbi.apis.servicecatalog;
17 import java.io.IOException;
18 import java.nio.file.Path;
19 import java.util.ArrayList;
20 import java.util.LinkedHashMap;
21 import java.util.List;
23 import org.apache.commons.io.FileUtils;
24 import org.onap.nbi.apis.servicecatalog.jolt.FindServiceSpecJsonTransformer;
25 import org.onap.nbi.apis.servicecatalog.jolt.GetServiceSpecJsonTransformer;
26 import org.onap.nbi.apis.serviceorder.ServiceCatalogUrl;
27 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service;
32 import org.springframework.util.CollectionUtils;
33 import org.springframework.util.MultiValueMap;
36 public class ServiceSpecificationService {
42 GetServiceSpecJsonTransformer getServiceSpecJsonTransformer;
45 FindServiceSpecJsonTransformer findServiceSpecJsonTransformer;
48 ToscaInfosProcessor toscaInfosProcessor;
51 private ServiceCatalogUrl serviceCatalogUrl;
54 ServiceSpecificationDBManager serviceSpecificationDBManager;
56 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceSpecificationService.class);
58 public Map get(String serviceSpecId) {
59 if(serviceSpecificationDBManager.checkServiceSpecExistence(serviceSpecId)) {
60 return serviceSpecificationDBManager.getServiceSpecification(serviceSpecId);
62 Map sdcResponse = sdcClient.callGet(serviceSpecId);
63 LinkedHashMap serviceCatalogResponse =
64 (LinkedHashMap) getServiceSpecJsonTransformer.transform(sdcResponse);
65 String toscaModelUrl = (String) sdcResponse.get("toscaModelURL");
66 String serviceId = (String) sdcResponse.get("id");
67 File toscaFile = sdcClient.callGetWithAttachment(toscaModelUrl);
68 Path pathToToscaCsar = toscaFile.toPath().toAbsolutePath();
70 toscaInfosProcessor.buildAndSaveResponseWithSdcToscaParser(pathToToscaCsar, serviceCatalogResponse);
71 serviceSpecificationDBManager.saveCatalogResponse(serviceCatalogResponse);
72 } catch (SdcToscaParserException e) {
73 LOGGER.debug("unable to build response from tosca csar using sdc-parser, partial response : "
74 + pathToToscaCsar.toString() + " " + e.getMessage());
77 if (toscaFile != null) {
78 LOGGER.debug("deleting tosca archive : " + toscaFile.getName());
79 FileUtils.forceDelete(toscaFile);
81 } catch (IOException e) {
82 LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId, e);
84 return serviceCatalogResponse;
88 public List<LinkedHashMap> find(MultiValueMap<String, String> parametersMap) {
89 List<LinkedHashMap> sdcResponse = sdcClient.callFind(parametersMap);
90 List<LinkedHashMap> serviceCatalogResponse = new ArrayList<>();
91 if (!CollectionUtils.isEmpty(sdcResponse)) {
92 serviceCatalogResponse = findServiceSpecJsonTransformer.transform(sdcResponse);
94 return serviceCatalogResponse;
96 public String getInputSchema(String serviceSpecId) {
97 if(serviceSpecificationDBManager.checkInputSchemaExistence(serviceSpecId)) {
98 return serviceSpecificationDBManager.getInputSchema(serviceSpecId);