2 * Copyright (c) 2018 Orange
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
15 package org.onap.nbi.apis.servicecatalog;
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;
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;
42 public class ToscaInfosProcessor {
48 private ServiceSpecificationDBManager serviceSpecificationDBManager;
50 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
52 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
54 public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
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);
76 serviceSpecCharacteristic.add(mapParameter);
78 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
80 List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
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)) {
95 if (nodeTemplate == null)
97 resourceSpecification.put("modelCustomizationId",
98 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
103 private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
105 List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
106 LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
108 serviceSpecCharacteristicValue.put("isDefault", true);
109 serviceSpecCharacteristicValue.put("value", input.getDefault());
110 serviceSpecCharacteristicValue.put("valueType", input.getType());
111 serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
113 return serviceSpecCharacteristicValues;
116 public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
117 throws SdcToscaParserException {
119 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
120 ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
121 List<Input> inputs = sdcCsarHelper.getServiceInputs();
123 Map<String, Model> definitions = new HashMap<String, Model>();
124 Model model = new ModelImpl();
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);
132 property = PropertyBuilder.build(input.getType(), null, null);
134 property.setDescription(input.getDescription());
135 property.setRequired(input.isRequired());
137 if (input.getDefault() != null) {
138 property.setDefault(input.getDefault().toString());
140 ((ModelImpl) model).addProperty(input.getName(), property);
142 definitions.put("ServiceCharacteristics", model);
146 String svcCharacteristicsJson = Json.pretty(definitions);
147 serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson, serviceCatalogResponse);
149 Metadata serviceMetadata = sdcCsarHelper.getServiceMetadata();
150 String instantationType = serviceMetadata.getValue("instantiationType");
151 serviceCatalogResponse.put("instantiationType", instantationType);
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");
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);
170 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
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)) {
186 if (nodeTemplate == null)
188 resourceSpecification.put("modelCustomizationId",
189 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));