5e3e4cf1b2f53f27e6b25253a0747cb964a07283
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ServiceSpecificationService.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14 package org.onap.nbi.apis.servicecatalog;
15
16 import java.io.File;
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;
22 import java.util.Map;
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;
34
35 @Service
36 public class ServiceSpecificationService {
37
38   @Autowired
39   SdcClient sdcClient;
40
41   @Autowired
42   GetServiceSpecJsonTransformer getServiceSpecJsonTransformer;
43
44   @Autowired
45   FindServiceSpecJsonTransformer findServiceSpecJsonTransformer;
46
47   @Autowired
48   ToscaInfosProcessor toscaInfosProcessor;
49
50   @Autowired
51   private ServiceCatalogUrl serviceCatalogUrl;
52
53
54   private static final Logger LOGGER = LoggerFactory.getLogger(ServiceSpecificationService.class);
55
56
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();
65     try {
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());
70     }
71     try {
72       if (toscaFile != null) {
73         LOGGER.debug("deleting tosca archive : " + toscaFile.getName());
74         FileUtils.forceDelete(toscaFile);
75       }
76     } catch (IOException e) {
77       LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId, e);
78     }
79     return serviceCatalogResponse;
80   }
81
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);
87     }
88     return serviceCatalogResponse;
89   }
90 }