Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessor.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.nio.file.Path;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
24 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
25 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
26 import org.onap.sdc.toscaparser.api.NodeTemplate;
27 import org.onap.sdc.toscaparser.api.parameters.Input;
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 com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
34 import io.swagger.models.Model;
35 import io.swagger.models.ModelImpl;
36 import io.swagger.models.properties.Property;
37 import io.swagger.models.properties.PropertyBuilder;
38 import io.swagger.util.Json;
39
40 @Service
41 public class ToscaInfosProcessor {
42
43     @Autowired
44     SdcClient sdcClient;
45
46     @Autowired
47     private ServiceSpecificationDBManager serviceSpecificationDBManager;
48
49     final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
52
53     public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
54
55         SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
56         ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
57         List<Input> inputs = sdcCsarHelper.getServiceInputs();
58         if (inputs != null && inputs.size() > 0) {
59             ArrayList serviceSpecCharacteristic = new ArrayList();
60             for (Input input : inputs) {
61                 LinkedHashMap mapParameter = new LinkedHashMap();
62                 mapParameter.put("name", input.getName());
63                 mapParameter.put("description", input.getDescription());
64                 mapParameter.put("valueType", input.getType());
65                 mapParameter.put("@type", "ONAPserviceCharacteristic");
66                 mapParameter.put("required", input.isRequired());
67                 mapParameter.put("status", null);
68                 mapParameter.put("serviceSpecCharacteristicValue", null);
69                 // If this Input has a default value, then put it in serviceSpecCharacteristicValue
70                 if (input.getDefault() != null) {
71                     List<LinkedHashMap> serviceSpecCharacteristicValues =
72                             buildServiceSpecCharacteristicsValuesFromSdc(input);
73                     mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
74                 }
75                 serviceSpecCharacteristic.add(mapParameter);
76             }
77             serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
78         }
79         List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
80
81         List<LinkedHashMap> resourceSpecifications =
82                 (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
83         for (LinkedHashMap resourceSpecification : resourceSpecifications) {
84             if (resourceSpecification.get("id") != null) {
85                 String id = (String) resourceSpecification.get("id");
86                 LOGGER.debug("get tosca infos for service id: {}", id);
87                 NodeTemplate nodeTemplate = null;
88                 for (NodeTemplate node : nodeTemplates) {
89                     if (node.getMetaData().getValue("UUID").equals(id)) {
90                         nodeTemplate = node;
91                         break;
92                     }
93                 }
94                 if (nodeTemplate == null)
95                     continue;
96                 resourceSpecification.put("modelCustomizationId",
97                         sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
98             }
99         }
100     }
101
102     private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
103
104         List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
105         LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
106
107         serviceSpecCharacteristicValue.put("isDefault", true);
108         serviceSpecCharacteristicValue.put("value", input.getDefault());
109         serviceSpecCharacteristicValue.put("valueType", input.getType());
110         serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
111
112         return serviceSpecCharacteristicValues;
113     }
114
115     public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
116             throws SdcToscaParserException {
117
118         SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
119         ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
120         List<Input> inputs = sdcCsarHelper.getServiceInputs();
121
122         Map<String, Model> definitions = new HashMap<String, Model>();
123         Model model = new ModelImpl();
124
125         if (inputs != null && inputs.size() > 0) {
126             for (Input input : inputs) {
127                 Property property = null;
128                 if (input.getType().equals("list") || input.getType().equals("map"))
129                     property = PropertyBuilder.build("array", null, null);
130                 else
131                     property = PropertyBuilder.build(input.getType(), null, null);
132
133                 property.setDescription(input.getDescription());
134                 property.setRequired(input.isRequired());
135
136                 if (input.getDefault() != null) {
137                     property.setDefault(input.getDefault().toString());
138                 }
139                 ((ModelImpl) model).addProperty(input.getName(), property);
140             }
141             definitions.put("ServiceCharacteristics", model);
142
143         }
144
145         String svcCharacteristicsJson = Json.pretty(definitions);
146         serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson, serviceCatalogResponse);
147
148         LinkedHashMap inputSchemaRef = new LinkedHashMap();
149         // use object to match examples in Specifications
150         inputSchemaRef.put("valueType", "object");
151         inputSchemaRef.put("@schemaLocation",
152                 "/serviceSpecification/" + serviceCatalogResponse.get("id") + "/specificationInputSchema");
153         inputSchemaRef.put("@type", serviceCatalogResponse.get("name") + "_ServiceCharacteristic");
154
155         LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
156         serviceSpecCharacteristic.put("name", serviceCatalogResponse.get("name") + "_ServiceCharacteristics");
157         serviceSpecCharacteristic.put("description",
158                 "This object describes all the inputs needed from the client to interact with the "
159                         + serviceCatalogResponse.get("name") + " Service Topology");
160         serviceSpecCharacteristic.put("valueType", "object");
161         serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic");
162         serviceSpecCharacteristic.put("@schemaLocation", "null");
163         serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", inputSchemaRef);
164
165         serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
166
167         List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
168         List<LinkedHashMap> resourceSpecifications =
169                 (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
170         for (LinkedHashMap resourceSpecification : resourceSpecifications) {
171             if (resourceSpecification.get("id") != null) {
172                 String id = (String) resourceSpecification.get("id");
173                 LOGGER.debug("get tosca infos for service id: {}", id);
174                 NodeTemplate nodeTemplate = null;
175                 for (NodeTemplate node : nodeTemplates) {
176                     if (node.getMetaData().getValue("UUID").equals(id)) {
177                         nodeTemplate = node;
178                         break;
179                     }
180                 }
181                 if (nodeTemplate == null)
182                     continue;
183                 resourceSpecification.put("modelCustomizationId",
184                         sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
185             }
186         }
187     }
188
189 }