Format Java code with respect to ONAP Code Style
[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
15 package org.onap.nbi.apis.servicecatalog;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.file.Path;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.apache.commons.io.FileUtils;
25 import org.onap.nbi.apis.servicecatalog.jolt.FindServiceSpecJsonTransformer;
26 import org.onap.nbi.apis.servicecatalog.jolt.GetServiceSpecJsonTransformer;
27 import org.onap.nbi.apis.serviceorder.ServiceCatalogUrl;
28 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33 import org.springframework.util.CollectionUtils;
34 import org.springframework.util.MultiValueMap;
35
36 @Service
37 public class ServiceSpecificationService {
38
39     @Autowired
40     SdcClient sdcClient;
41
42     @Autowired
43     GetServiceSpecJsonTransformer getServiceSpecJsonTransformer;
44
45     @Autowired
46     FindServiceSpecJsonTransformer findServiceSpecJsonTransformer;
47
48     @Autowired
49     ToscaInfosProcessor toscaInfosProcessor;
50
51     @Autowired
52     private ServiceCatalogUrl serviceCatalogUrl;
53
54     @Autowired
55     ServiceSpecificationDBManager serviceSpecificationDBManager;
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceSpecificationService.class);
58
59     public Map get(String serviceSpecId) {
60         if (serviceSpecificationDBManager.checkServiceSpecExistence(serviceSpecId)) {
61             return serviceSpecificationDBManager.getServiceSpecification(serviceSpecId);
62         } else {
63             Map sdcResponse = sdcClient.callGet(serviceSpecId);
64             LinkedHashMap serviceCatalogResponse = (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
97     public String getInputSchema(String serviceSpecId) {
98         if (serviceSpecificationDBManager.checkInputSchemaExistence(serviceSpecId)) {
99             return serviceSpecificationDBManager.getInputSchema(serviceSpecId);
100         } else {
101             return null;
102         }
103     }
104 }