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 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceSpecificationService.class);
57 public Map get(String serviceSpecId) {
58 Map sdcResponse = sdcClient.callGet(serviceSpecId);
59 LinkedHashMap serviceCatalogResponse =
60 (LinkedHashMap) getServiceSpecJsonTransformer.transform(sdcResponse);
61 String toscaModelUrl = (String) sdcResponse.get("toscaModelURL");
62 String serviceId = (String) sdcResponse.get("id");
63 File toscaFile = sdcClient.callGetWithAttachment(toscaModelUrl);
64 Path pathToToscaCsar = toscaFile.toPath().toAbsolutePath();
66 toscaInfosProcessor.buildResponseWithSdcToscaParser(pathToToscaCsar, serviceCatalogResponse);
67 } catch (SdcToscaParserException e) {
68 LOGGER.debug("unable to build response from tosca csar using sdc-parser, partial response : "
69 + pathToToscaCsar.toString() + " " + e.getMessage());
72 if (toscaFile != null) {
73 LOGGER.debug("deleting tosca archive : " + toscaFile.getName());
74 FileUtils.forceDelete(toscaFile);
76 } catch (IOException e) {
77 LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId, e);
79 return serviceCatalogResponse;
82 public List<LinkedHashMap> find(MultiValueMap<String, String> parametersMap) {
83 List<LinkedHashMap> sdcResponse = sdcClient.callFind(parametersMap);
84 List<LinkedHashMap> serviceCatalogResponse = new ArrayList<>();
85 if (!CollectionUtils.isEmpty(sdcResponse)) {
86 serviceCatalogResponse = findServiceSpecJsonTransformer.transform(sdcResponse);
88 return serviceCatalogResponse;