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.LinkedHashMap;
19 import java.util.List;
21 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
22 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
23 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
24 import org.onap.sdc.toscaparser.api.NodeTemplate;
25 import org.onap.sdc.toscaparser.api.parameters.Input;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
34 public class ToscaInfosProcessor {
39 final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
41 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
45 public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
46 throws SdcToscaParserException {
48 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
49 ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
50 List<Input> inputs = sdcCsarHelper.getServiceInputs();
51 if (inputs != null && inputs.size() > 0) {
52 ArrayList serviceSpecCharacteristic = new ArrayList();
53 for (Input input : inputs) {
54 LinkedHashMap mapParameter = new LinkedHashMap();
55 mapParameter.put("name", input.getName());
56 mapParameter.put("description", input.getDescription());
57 mapParameter.put("valueType", input.getType());
58 mapParameter.put("@type", "ONAPserviceCharacteristic");
59 mapParameter.put("required", input.isRequired());
60 mapParameter.put("status", null);
61 mapParameter.put("serviceSpecCharacteristicValue", null);
62 // If this Input has a default value, then put it in serviceSpecCharacteristicValue
63 if (input.getDefault() != null) {
64 List<LinkedHashMap> serviceSpecCharacteristicValues =
65 buildServiceSpecCharacteristicsValuesFromSdc(input);
66 mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
68 serviceSpecCharacteristic.add(mapParameter);
70 serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
72 List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
74 List<LinkedHashMap> resourceSpecifications =
75 (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
76 for (LinkedHashMap resourceSpecification : resourceSpecifications) {
77 if (resourceSpecification.get("id") != null) {
78 String id = (String) resourceSpecification.get("id");
79 LOGGER.debug("get tosca infos for service id: {}", id);
80 NodeTemplate nodeTemplate = null;
81 for (NodeTemplate node : nodeTemplates) {
82 if (node.getMetaData().getValue("UUID").equals(id)) {
87 if (nodeTemplate == null)
89 resourceSpecification.put("modelCustomizationId",
90 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
96 private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
98 List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
99 LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
101 serviceSpecCharacteristicValue.put("isDefault", true);
102 serviceSpecCharacteristicValue.put("value", input.getDefault());
103 serviceSpecCharacteristicValue.put("valueType", input.getType());
104 serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
106 return serviceSpecCharacteristicValues;