Merge "Add support for object characteristic type in Service Order"
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessor.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14 package org.onap.nbi.apis.servicecatalog;
15
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;
21 import java.util.Map;
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;
38
39 @Service
40 public class ToscaInfosProcessor {
41
42   @Autowired
43   SdcClient sdcClient;
44
45   @Autowired
46   private ServiceSpecificationDBManager serviceSpecificationDBManager;
47
48   final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
49
50   private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
51
52   public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
53       throws SdcToscaParserException {
54
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);
74         }
75         serviceSpecCharacteristic.add(mapParameter);
76       }
77       serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
78     }
79     List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
80
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)) {
90             nodeTemplate = node;
91             break;
92           }
93         }
94         if (nodeTemplate == null)
95           continue;
96         resourceSpecification.put("modelCustomizationId",
97             sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
98       }
99     }
100   }
101
102   private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
103
104     List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
105     LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
106
107     serviceSpecCharacteristicValue.put("isDefault", true);
108     serviceSpecCharacteristicValue.put("value", input.getDefault());
109     serviceSpecCharacteristicValue.put("valueType", input.getType());
110     serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
111
112     return serviceSpecCharacteristicValues;
113   }
114
115   public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
116       throws SdcToscaParserException {
117
118     SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
119     ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
120     List<Input> inputs = sdcCsarHelper.getServiceInputs();
121
122     Map<String, Model> definitions = new HashMap<String, Model>();
123     Model model = new ModelImpl();
124
125     if (inputs != null && inputs.size() > 0) {
126       for (Input input : inputs) {
127         Property property = PropertyBuilder.build(input.getType(), null, null);
128         property.setDescription(input.getDescription());
129         property.setRequired(input.isRequired());
130
131         if (input.getDefault() != null) {
132           property.setDefault(input.getDefault().toString());
133         }
134         ((ModelImpl) model).addProperty(input.getName(), property);
135       }
136       definitions.put("ServiceCharacteristics", model);
137
138     }
139
140     String svcCharacteristicsJson = Json.pretty(definitions);
141     serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson,
142         serviceCatalogResponse);
143
144     LinkedHashMap inputSchemaRef = new LinkedHashMap();
145     // use object to match examples in Specifications
146     inputSchemaRef.put("valueType", "object");
147     inputSchemaRef.put("@schemaLocation",
148         "/serviceSpecification/" + serviceCatalogResponse.get("id") + "/specificationInputSchema");
149     inputSchemaRef.put("@type", serviceCatalogResponse.get("name") + "_ServiceCharacteristic");
150
151     LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
152     serviceSpecCharacteristic.put("name",
153         serviceCatalogResponse.get("name") + "_ServiceCharacteristics");
154     serviceSpecCharacteristic.put("description",
155         "This object describes all the inputs needed from the client to interact with the "
156             + serviceCatalogResponse.get("name") + " Service Topology");
157     serviceSpecCharacteristic.put("valueType", "object");
158     serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic");
159     serviceSpecCharacteristic.put("@schemaLocation", "null");
160     serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", inputSchemaRef);
161
162     serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
163
164     List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
165     List<LinkedHashMap> resourceSpecifications =
166         (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
167     for (LinkedHashMap resourceSpecification : resourceSpecifications) {
168       if (resourceSpecification.get("id") != null) {
169         String id = (String) resourceSpecification.get("id");
170         LOGGER.debug("get tosca infos for service id: {}", id);
171         NodeTemplate nodeTemplate = null;
172         for (NodeTemplate node : nodeTemplates) {
173           if (node.getMetaData().getValue("UUID").equals(id)) {
174             nodeTemplate = node;
175             break;
176           }
177         }
178         if (nodeTemplate == null)
179           continue;
180         resourceSpecification.put("modelCustomizationId",
181             sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
182       }
183     }
184   }
185
186 }