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
14 package org.onap.nbi.apis.servicecatalog;
16 import java.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.LinkedHashMap;
20 import java.util.List;
22 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
23 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
24 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
25 import org.onap.sdc.toscaparser.api.NodeTemplate;
26 import org.onap.sdc.toscaparser.api.parameters.Input;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Service;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
33 import io.swagger.models.Model;
34 import io.swagger.models.ModelImpl;
35 import io.swagger.models.properties.Property;
36 import io.swagger.models.properties.PropertyBuilder;
37 import io.swagger.util.Json;
40 public class ToscaInfosProcessor {
46 private ServiceSpecificationDBManager serviceSpecificationDBManager;
48 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
50 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
52 public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
53 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,
147 serviceCatalogResponse);
149 LinkedHashMap inputSchemaRef = new LinkedHashMap();
150 // use object to match examples in Specifications
151 inputSchemaRef.put("valueType", "object");
152 inputSchemaRef.put("@schemaLocation",
153 "/serviceSpecification/" + serviceCatalogResponse.get("id") + "/specificationInputSchema");
154 inputSchemaRef.put("@type", serviceCatalogResponse.get("name") + "_ServiceCharacteristic");
156 LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
157 serviceSpecCharacteristic.put("name",
158 serviceCatalogResponse.get("name") + "_ServiceCharacteristics");
159 serviceSpecCharacteristic.put("description",
160 "This object describes all the inputs needed from the client to interact with the "
161 + serviceCatalogResponse.get("name") + " Service Topology");
162 serviceSpecCharacteristic.put("valueType", "object");
163 serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic");
164 serviceSpecCharacteristic.put("@schemaLocation", "null");
165 serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", inputSchemaRef);
167 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
169 List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
170 List<LinkedHashMap> resourceSpecifications =
171 (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
172 for (LinkedHashMap resourceSpecification : resourceSpecifications) {
173 if (resourceSpecification.get("id") != null) {
174 String id = (String) resourceSpecification.get("id");
175 LOGGER.debug("get tosca infos for service id: {}", id);
176 NodeTemplate nodeTemplate = null;
177 for (NodeTemplate node : nodeTemplates) {
178 if (node.getMetaData().getValue("UUID").equals(id)) {
183 if (nodeTemplate == null)
185 resourceSpecification.put("modelCustomizationId",
186 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));