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