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.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;
41 public class ToscaInfosProcessor {
47 private ServiceSpecificationDBManager serviceSpecificationDBManager;
49 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
51 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
53 public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
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);
75 serviceSpecCharacteristic.add(mapParameter);
77 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
79 List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
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)) {
94 if (nodeTemplate == null)
96 resourceSpecification.put("modelCustomizationId",
97 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
102 private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
104 List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
105 LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
107 serviceSpecCharacteristicValue.put("isDefault", true);
108 serviceSpecCharacteristicValue.put("value", input.getDefault());
109 serviceSpecCharacteristicValue.put("valueType", input.getType());
110 serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
112 return serviceSpecCharacteristicValues;
115 public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
116 throws SdcToscaParserException {
118 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
119 ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
120 List<Input> inputs = sdcCsarHelper.getServiceInputs();
122 Map<String, Model> definitions = new HashMap<String, Model>();
123 Model model = new ModelImpl();
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);
131 property = PropertyBuilder.build(input.getType(), null, null);
133 property.setDescription(input.getDescription());
134 property.setRequired(input.isRequired());
136 if (input.getDefault() != null) {
137 property.setDefault(input.getDefault().toString());
139 ((ModelImpl) model).addProperty(input.getName(), property);
141 definitions.put("ServiceCharacteristics", model);
145 String svcCharacteristicsJson = Json.pretty(definitions);
146 serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson, serviceCatalogResponse);
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");
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);
165 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
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)) {
181 if (nodeTemplate == null)
183 resourceSpecification.put("modelCustomizationId",
184 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));