8f0829bc336ab9abbad08343578f59258d9445ce
[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   @Autowired
54   ServiceSpecificationDBManager serviceSpecificationDBManager;
55
56   private static final Logger LOGGER = LoggerFactory.getLogger(ServiceSpecificationService.class);
57
58   public Map get(String serviceSpecId) {
59     if(serviceSpecificationDBManager.checkServiceSpecExistence(serviceSpecId)) {
60       return serviceSpecificationDBManager.getServiceSpecification(serviceSpecId);
61     }else {
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();
69       try {
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());
75       }
76       try {
77         if (toscaFile != null) {
78           LOGGER.debug("deleting tosca archive : " + toscaFile.getName());
79           FileUtils.forceDelete(toscaFile);
80         }
81       } catch (IOException e) {
82         LOGGER.error("unable to delete temp directory tosca file for id : " + serviceId, e);
83       }
84       return serviceCatalogResponse;
85     }
86   }
87
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);
93     }
94     return serviceCatalogResponse;
95   }
96   public String getInputSchema(String serviceSpecId) {
97     if(serviceSpecificationDBManager.checkInputSchemaExistence(serviceSpecId)) {
98       return serviceSpecificationDBManager.getInputSchema(serviceSpecId);
99     } else {
100       return null;
101     }
102   }
103 }