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;
19 import io.swagger.util.Json;
20 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
21 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
22 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
23 import org.onap.sdc.toscaparser.api.NodeTemplate;
24 import org.onap.sdc.toscaparser.api.parameters.Input;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
31 import io.swagger.models.Model;
32 import io.swagger.models.ModelImpl;
33 import io.swagger.models.properties.Property;
34 import io.swagger.models.properties.PropertyBuilder;
37 public class ToscaInfosProcessor {
43 private ServiceSpecificationDBManager serviceSpecificationDBManager;
45 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
47 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
49 public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
50 throws SdcToscaParserException {
52 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
53 ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
54 List<Input> inputs = sdcCsarHelper.getServiceInputs();
55 if (inputs != null && inputs.size() > 0) {
56 ArrayList serviceSpecCharacteristic = new ArrayList();
57 for (Input input : inputs) {
58 LinkedHashMap mapParameter = new LinkedHashMap();
59 mapParameter.put("name", input.getName());
60 mapParameter.put("description", input.getDescription());
61 mapParameter.put("valueType", input.getType());
62 mapParameter.put("@type", "ONAPserviceCharacteristic");
63 mapParameter.put("required", input.isRequired());
64 mapParameter.put("status", null);
65 mapParameter.put("serviceSpecCharacteristicValue", null);
66 // If this Input has a default value, then put it in serviceSpecCharacteristicValue
67 if (input.getDefault() != null) {
68 List<LinkedHashMap> serviceSpecCharacteristicValues =
69 buildServiceSpecCharacteristicsValuesFromSdc(input);
70 mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
72 serviceSpecCharacteristic.add(mapParameter);
74 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
76 List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
78 List<LinkedHashMap> resourceSpecifications =
79 (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
80 for (LinkedHashMap resourceSpecification : resourceSpecifications) {
81 if (resourceSpecification.get("id") != null) {
82 String id = (String) resourceSpecification.get("id");
83 LOGGER.debug("get tosca infos for service id: {}", id);
84 NodeTemplate nodeTemplate = null;
85 for (NodeTemplate node : nodeTemplates) {
86 if (node.getMetaData().getValue("UUID").equals(id)) {
91 if (nodeTemplate == null)
93 resourceSpecification.put("modelCustomizationId",
94 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
99 private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
101 List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
102 LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
104 serviceSpecCharacteristicValue.put("isDefault", true);
105 serviceSpecCharacteristicValue.put("value", input.getDefault());
106 serviceSpecCharacteristicValue.put("valueType", input.getType());
107 serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
109 return serviceSpecCharacteristicValues;
112 public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
114 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
115 ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
116 List<Input> inputs = sdcCsarHelper.getServiceInputs();
118 Map<String, Model> definitions = new HashMap<String,Model>();
119 Model model = new ModelImpl();
121 if (inputs != null && inputs.size() > 0) {
122 for (Input input : inputs) {
123 Property property = PropertyBuilder.build(input.getType(), null, null);
124 property.setDescription(input.getDescription());
125 property.setRequired(input.isRequired());
127 if (input.getDefault() != null) {
128 property.setDefault(input.getDefault().toString());
130 ((ModelImpl) model).addProperty(input.getName(), property);
132 definitions.put("ServiceCharacteristics", model);
136 String svcCharacteristicsJson = Json.pretty(definitions);
137 serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson,serviceCatalogResponse);
139 LinkedHashMap inputSchemaRef = new LinkedHashMap();
140 inputSchemaRef.put("valueType","Object");
141 inputSchemaRef.put("@schemaLocation","/serviceSpecification/"+serviceCatalogResponse.get("id")+"/specificationInputSchema");
142 inputSchemaRef.put("@type",serviceCatalogResponse.get("name") + "_ServiceCharacteristic");
144 LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
145 serviceSpecCharacteristic.put("name",serviceCatalogResponse.get("name") + "_ServiceCharacteristics");
146 serviceSpecCharacteristic.put("description","This object describes all the inputs needed from the client to interact with the " + serviceCatalogResponse.get("name") + " Service Topology");
147 serviceSpecCharacteristic.put("valueType", "Object");
148 serviceSpecCharacteristic.put("@type","ONAPServiceCharacteristic");
149 serviceSpecCharacteristic.put("@schemaLocation", "null");
150 serviceSpecCharacteristic.put("serviceSpecCharacteristicValue",inputSchemaRef);
152 serviceCatalogResponse.put("serviceSpecCharacteristic",serviceSpecCharacteristic);
154 List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
155 List<LinkedHashMap> resourceSpecifications =
156 (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
157 for (LinkedHashMap resourceSpecification : resourceSpecifications) {
158 if (resourceSpecification.get("id") != null) {
159 String id = (String) resourceSpecification.get("id");
160 LOGGER.debug("get tosca infos for service id: {}", id);
161 NodeTemplate nodeTemplate = null;
162 for (NodeTemplate node : nodeTemplates) {
163 if (node.getMetaData().getValue("UUID").equals(id)) {
168 if (nodeTemplate == null)
170 resourceSpecification.put("modelCustomizationId", sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));