2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.tosca.update.parser;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.LinkedHashMap;
31 import java.util.Map.Entry;
32 import org.onap.clamp.clds.tosca.update.elements.ToscaElement;
33 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
34 import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser;
35 import org.onap.clamp.clds.tosca.update.templates.JsonTemplate;
36 import org.onap.clamp.loop.service.Service;
38 public class ToscaConverterToJsonSchema {
39 private LinkedHashMap<String, ToscaElement> components;
40 private LinkedHashMap<String, JsonTemplate> templates;
42 private ToscaMetadataParser metadataParser;
44 private Service serviceModel;
49 * @param toscaElementsMap All the tosca elements found (policy type + data types + native tosca datatypes)
50 * @param jsonSchemaTemplates All Json schema templates to use
51 * @param metadataParser The metadata parser to use for metadata section
52 * @param serviceModel The service model for clamp enrichment
54 public ToscaConverterToJsonSchema(LinkedHashMap<String, ToscaElement> toscaElementsMap,
55 LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates,
56 ToscaMetadataParser metadataParser, Service serviceModel) {
57 this.components = toscaElementsMap;
58 this.templates = jsonSchemaTemplates;
59 this.metadataParser = metadataParser;
60 this.serviceModel = serviceModel;
64 * For a given component, launch process to parse it in Json.
66 * @param toscaElementKey name components
69 public JsonObject getJsonSchemaOfToscaElement(String toscaElementKey) {
70 return this.getFieldAsObject(getToscaElement(toscaElementKey));
74 * Return the classical/general fields of the component, & launch the properties deployment.
76 * @param toscaElement the compo
77 * @return a json object
79 public JsonObject getFieldAsObject(ToscaElement toscaElement) {
81 JsonObject globalFields = new JsonObject();
82 if (templates.get("object").hasFields("title")) {
83 globalFields.addProperty("title", toscaElement.getName());
85 if (templates.get("object").hasFields("type")) {
86 globalFields.addProperty("type", "object");
88 if (templates.get("object").hasFields("description")) {
89 if (toscaElement.getDescription() != null) {
90 globalFields.addProperty("description", toscaElement.getDescription());
93 if (templates.get("object").hasFields("required")) {
94 globalFields.add("required", this.getRequirements(toscaElement.getName()));
96 if (templates.get("object").hasFields("properties")) {
97 globalFields.add("properties", this.deploy(toscaElement.getName()));
103 * Get the required properties of the Component, including the parents properties requirements.
105 * @param nameComponent name component
106 * @return a json array
108 public JsonArray getRequirements(String nameComponent) {
109 JsonArray requirements = new JsonArray();
110 ToscaElement toParse = components.get(nameComponent);
111 //Check for a father component, and launch the same process
112 if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
113 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
114 requirements.addAll(getRequirements(toParse.getDerivedFrom()));
116 //Each property is checked, and add to the requirement array if it's required
117 Collection<ToscaElementProperty> properties = toParse.getProperties().values();
118 for (ToscaElementProperty toscaElementProperty : properties) {
119 if (toscaElementProperty.getItems().containsKey("required")
120 && toscaElementProperty.getItems().get("required").equals(true)) {
121 requirements.add(toscaElementProperty.getName());
128 * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise
129 * deploy and parse the properties.
131 * @param nameComponent name component
132 * @return a json object
134 public JsonObject deploy(String nameComponent) {
135 JsonObject jsonSchema = new JsonObject();
136 ToscaElement toParse = components.get(nameComponent);
137 //Check for a father component, and launch the same process
138 if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
139 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
140 jsonSchema = this.getParent(toParse.getDerivedFrom());
142 //For each component property, check if its a complex properties (a component) or not. In that case,
143 //launch the analyse of the property.
144 for (Entry<String, ToscaElementProperty> property : toParse.getProperties().entrySet()) {
145 if (getToscaElement((String) property.getValue().getItems().get("type")) != null) {
146 jsonSchema.add(property.getValue().getName(),
147 this.getJsonSchemaOfToscaElement((String) property.getValue().getItems().get("type")));
150 jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue()));
157 * If a component has a parent, it is deploy in the same way.
159 * @param nameComponent name component
160 * @return a json object
162 public JsonObject getParent(String nameComponent) {
163 return deploy(nameComponent);
169 * @param toscaElementProperty property
170 * @return a json object
172 @SuppressWarnings("unchecked")
173 public JsonObject complexParse(ToscaElementProperty toscaElementProperty) {
174 JsonObject propertiesInJson = new JsonObject();
175 JsonTemplate currentPropertyJsonTemplate;
176 String typeProperty = (String) toscaElementProperty.getItems().get("type");
177 if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) {
178 currentPropertyJsonTemplate = templates.get("object");
181 String propertyType = (String) toscaElementProperty.getItems().get("type");
182 currentPropertyJsonTemplate = templates.get(propertyType.toLowerCase());
184 //Each "special" field is analysed, and has a specific treatment
185 for (String propertyField : toscaElementProperty.getItems().keySet()) {
186 switch (propertyField) {
188 if (currentPropertyJsonTemplate.hasFields(propertyField)) {
189 String fieldtype = (String) toscaElementProperty.getItems().get(propertyField);
190 switch (fieldtype.toLowerCase()) {
192 propertiesInJson.addProperty("type", "array");
195 propertiesInJson.addProperty("type", "object");
197 case "scalar-unit.time":
198 case "scalar-unit.frequency":
199 case "scalar-unit.size":
200 propertiesInJson.addProperty("type", "string");
203 propertiesInJson.addProperty("type", "string");
204 propertiesInJson.addProperty("format", "date-time");
207 propertiesInJson.addProperty("type", "number");
210 propertiesInJson.addProperty("type", "integer");
211 if (!checkConstraintPresence(toscaElementProperty, "greater_than")
212 && currentPropertyJsonTemplate.hasFields("exclusiveMinimum")) {
213 propertiesInJson.addProperty("exclusiveMinimum", false);
215 if (!checkConstraintPresence(toscaElementProperty, "less_than")
216 && currentPropertyJsonTemplate.hasFields("exclusiveMaximum")) {
217 propertiesInJson.addProperty("exclusiveMaximum", false);
221 propertiesInJson.addProperty("type", currentPropertyJsonTemplate.getName());
227 if (metadataParser != null) {
228 metadataParser.processAllMetadataElement(toscaElementProperty, serviceModel).entrySet()
229 .forEach((jsonEntry) -> {
230 propertiesInJson.add(jsonEntry.getKey(),
231 jsonEntry.getValue());
237 toscaElementProperty.addConstraintsAsJson(propertiesInJson,
238 (ArrayList<Object>) toscaElementProperty.getItems().get("constraints"),
239 currentPropertyJsonTemplate);
242 //Here, a way to check if entry is a component (datatype) or a simple string
243 if (getToscaElement(this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema"))
245 String nameComponent = this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema");
246 ToscaConverterToJsonSchema child = new ToscaConverterToJsonSchema(components, templates,
247 metadataParser, serviceModel);
248 JsonObject propertiesContainer = new JsonObject();
250 switch ((String) toscaElementProperty.getItems().get("type")) {
251 case "map": // Get it as an object
252 JsonObject componentAsProperty = child.getJsonSchemaOfToscaElement(nameComponent);
253 propertiesContainer.add(nameComponent, componentAsProperty);
254 if (currentPropertyJsonTemplate.hasFields("properties")) {
255 propertiesInJson.add("properties", propertiesContainer);
258 default://list : get it as an Array
259 JsonObject componentAsItem = child.getJsonSchemaOfToscaElement(nameComponent);
260 if (currentPropertyJsonTemplate.hasFields("properties")) {
261 propertiesInJson.add("items", componentAsItem);
262 propertiesInJson.addProperty("format", "tabs-top");
269 else if (toscaElementProperty.getItems().get("type").equals("list")) {
270 JsonObject itemContainer = new JsonObject();
271 String valueInEntrySchema =
272 this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema");
273 itemContainer.addProperty("type", valueInEntrySchema);
274 propertiesInJson.add("items", itemContainer);
275 propertiesInJson.addProperty("format", "tabs-top");
277 // MAP Case, for now nothing
281 //Each classical field : type, description, default..
282 if (currentPropertyJsonTemplate.hasFields(propertyField) && !propertyField.equals("required")) {
283 toscaElementProperty.addFieldToJson(propertiesInJson, propertyField,
284 toscaElementProperty.getItems().get(propertyField));
289 return propertiesInJson;
293 * Look for a matching Component for the name parameter, in the components list.
295 * @param name the tosca element name to search for
296 * @return a tosca element
298 public ToscaElement getToscaElement(String name) {
299 ToscaElement correspondingToscaElement = null;
300 if (components == null) {
303 for (ToscaElement toscaElement : components.values()) {
304 if (toscaElement.getName().equals(name)) {
305 correspondingToscaElement = toscaElement;
308 return correspondingToscaElement;
312 * Simple method to extract quickly a type field from particular property item.
314 * @param toscaElementProperty the property
315 * @param fieldName the fieldname
318 @SuppressWarnings("unchecked")
319 public String extractSpecificFieldFromMap(ToscaElementProperty toscaElementProperty, String fieldName) {
320 LinkedHashMap<String, String> entrySchemaFields =
321 (LinkedHashMap<String, String>) toscaElementProperty.getItems().get(fieldName);
322 return entrySchemaFields.get("type");
326 * Check if a constraint, for a specific property, is there.
328 * @param toscaElementProperty property
329 * @param nameConstraint name constraint
330 * @return a flag boolean
332 public boolean checkConstraintPresence(ToscaElementProperty toscaElementProperty, String nameConstraint) {
333 boolean presentConstraint = false;
334 if (toscaElementProperty.getItems().containsKey("constraints")) {
335 ArrayList<Object> constraints = (ArrayList) toscaElementProperty.getItems().get("constraints");
336 for (Object constraint : constraints) {
337 if (constraint instanceof LinkedHashMap) {
338 if (((LinkedHashMap) constraint).containsKey(nameConstraint)) {
339 presentConstraint = true;
344 return presentConstraint;