2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.policy.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;
31 import java.util.Map.Entry;
32 import org.onap.policy.clamp.clds.tosca.update.elements.ToscaElement;
33 import org.onap.policy.clamp.clds.tosca.update.elements.ToscaElementProperty;
34 import org.onap.policy.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser;
35 import org.onap.policy.clamp.clds.tosca.update.templates.JsonTemplate;
36 import org.onap.policy.clamp.loop.service.Service;
39 * This class can be used to convert a tosca to a json schema.
40 * This class is not supposed to be used directly because it requires the json Schema templates
41 * (template conversion tosca type to json schema entry) but also the supported Tosca main type file.
42 * The class ToscaConverterWithDictionarySupport is more complete for the end user to be used (in the clamp context).
44 * @see org.onap.policy.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport#convertToscaToJsonSchemaObject
45 * @see org.onap.policy.clamp.clds.tosca.update.parser.ToscaConverterToJsonSchema#getJsonSchemaOfToscaElement
47 public class ToscaConverterToJsonSchema {
48 private static final String ARRAY = "array";
49 private static final String CONSTRAINTS = "constraints";
50 private static final String DESCRIPTION = "description";
51 private static final String ENTRY_SCHEMA = "entry_schema";
52 private static final String FORMAT = "format";
53 private static final String LIST = "list";
54 private static final String MAP = "map";
55 private static final String METADATA = "metadata";
56 private static final String OBJECT = "object";
57 private static final String PROPERTIES = "properties";
58 private static final String REQUIRED = "required";
59 private static final String TITLE = "title";
60 private static final String TYPE = "type";
62 private Map<String, ToscaElement> components;
63 private Map<String, JsonTemplate> templates;
65 private ToscaMetadataParser metadataParser;
67 private Service serviceModel;
72 * @param toscaElementsMap All the tosca elements found (policy type + data types + native tosca datatypes)
73 * @param jsonSchemaTemplates All Json schema templates to use
74 * @param metadataParser The metadata parser to use for metadata section
75 * @param serviceModel The service model for clamp enrichment
77 public ToscaConverterToJsonSchema(Map<String, ToscaElement> toscaElementsMap,
78 Map<String, JsonTemplate> jsonSchemaTemplates, ToscaMetadataParser metadataParser,
79 Service serviceModel) {
80 this.components = toscaElementsMap;
81 this.templates = jsonSchemaTemplates;
82 this.metadataParser = metadataParser;
83 this.serviceModel = serviceModel;
87 * For a given component, launch process to parse it in Json.
89 * @param toscaElementKey name components
92 public JsonObject getJsonSchemaOfToscaElement(String toscaElementKey) {
93 return this.getFieldAsObject(getToscaElement(toscaElementKey));
97 * Return the classical/general fields of the component, & launch the properties deployment.
99 * @param toscaElement the compo
100 * @return a json object
102 public JsonObject getFieldAsObject(ToscaElement toscaElement) {
104 var globalFields = new JsonObject();
105 if (templates.get(OBJECT).hasFields(TITLE)) {
106 globalFields.addProperty(TITLE, toscaElement.getName());
108 if (templates.get(OBJECT).hasFields(TYPE)) {
109 globalFields.addProperty(TYPE, OBJECT);
111 if (templates.get(OBJECT).hasFields(DESCRIPTION) && (toscaElement.getDescription() != null)) {
112 globalFields.addProperty(DESCRIPTION, toscaElement.getDescription());
114 if (templates.get(OBJECT).hasFields(REQUIRED)) {
115 globalFields.add(REQUIRED, this.getRequirements(toscaElement.getName()));
117 if (templates.get(OBJECT).hasFields(PROPERTIES)) {
118 globalFields.add(PROPERTIES, this.deploy(toscaElement.getName()));
124 * Get the required properties of the Component, including the parents properties requirements.
126 * @param nameComponent name component
127 * @return a json array
129 public JsonArray getRequirements(String nameComponent) {
130 var requirements = new JsonArray();
131 ToscaElement toParse = components.get(nameComponent);
132 // Check for a father component, and launch the same process
133 if (!"tosca.datatypes.Root".equals(toParse.getDerivedFrom())
134 && !"tosca.policies.Root".equals(toParse.getDerivedFrom())) {
135 requirements.addAll(getRequirements(toParse.getDerivedFrom()));
137 // Each property is checked, and add to the requirement array if it's required
138 Collection<ToscaElementProperty> properties = toParse.getProperties().values();
139 for (ToscaElementProperty toscaElementProperty : properties) {
140 if (toscaElementProperty.getItems().containsKey(REQUIRED)
141 && toscaElementProperty.getItems().get(REQUIRED).equals(true)) {
142 requirements.add(toscaElementProperty.getName());
149 * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise
150 * deploy and parse the properties.
152 * @param nameComponent name component
153 * @return a json object
155 public JsonObject deploy(String nameComponent) {
156 var jsonSchema = new JsonObject();
157 ToscaElement toParse = components.get(nameComponent);
158 // Check for a father component, and launch the same process
159 if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
160 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
161 jsonSchema = this.getParent(toParse.getDerivedFrom());
163 // For each component property, check if its a complex properties (a component) or not. In that case,
164 // launch the analyse of the property.
165 for (Entry<String, ToscaElementProperty> property : toParse.getProperties().entrySet()) {
166 if (getToscaElement((String) property.getValue().getItems().get(TYPE)) != null) {
167 jsonSchema.add(property.getValue().getName(),
168 this.getJsonSchemaOfToscaElement((String) property.getValue().getItems().get(TYPE)));
170 jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue()));
177 * If a component has a parent, it is deploy in the same way.
179 * @param nameComponent name component
180 * @return a json object
182 public JsonObject getParent(String nameComponent) {
183 return deploy(nameComponent);
189 * @param toscaElementProperty property
190 * @return a json object
192 @SuppressWarnings("unchecked")
193 public JsonObject complexParse(ToscaElementProperty toscaElementProperty) {
194 var propertiesInJson = new JsonObject();
195 JsonTemplate currentPropertyJsonTemplate;
196 String typeProperty = (String) toscaElementProperty.getItems().get(TYPE);
197 if (LIST.equalsIgnoreCase(typeProperty) || MAP.equalsIgnoreCase(typeProperty)) {
198 currentPropertyJsonTemplate = templates.get(OBJECT);
200 String propertyType = (String) toscaElementProperty.getItems().get(TYPE);
201 currentPropertyJsonTemplate = templates.get(propertyType.toLowerCase());
203 // Each "special" field is analysed, and has a specific treatment
204 for (String propertyField : toscaElementProperty.getItems().keySet()) {
205 switch (propertyField) {
207 parseType(toscaElementProperty, propertyField, propertiesInJson, currentPropertyJsonTemplate);
210 if (metadataParser != null) {
211 metadataParser.processAllMetadataElement(toscaElementProperty, serviceModel).entrySet()
212 .forEach(jsonEntry -> propertiesInJson.add(jsonEntry.getKey(), jsonEntry.getValue()));
216 toscaElementProperty.addConstraintsAsJson(propertiesInJson,
217 (ArrayList<Object>) toscaElementProperty.getItems().get(CONSTRAINTS),
218 currentPropertyJsonTemplate);
221 parseEntrySchema(toscaElementProperty, propertiesInJson, currentPropertyJsonTemplate);
224 // Each classical field : type, description, default..
225 if (currentPropertyJsonTemplate.hasFields(propertyField) && !propertyField.equals(REQUIRED)) {
226 toscaElementProperty.addFieldToJson(propertiesInJson, propertyField,
227 toscaElementProperty.getItems().get(propertyField));
232 return propertiesInJson;
235 private void parseType(ToscaElementProperty toscaElementProperty, String propertyField, JsonObject propertiesInJson,
236 JsonTemplate currentPropertyJsonTemplate) {
237 if (currentPropertyJsonTemplate.hasFields(propertyField)) {
238 String fieldtype = (String) toscaElementProperty.getItems().get(propertyField);
239 switch (fieldtype.toLowerCase()) {
241 propertiesInJson.addProperty(TYPE, ARRAY);
244 propertiesInJson.addProperty(TYPE, OBJECT);
246 case "scalar-unit.time":
247 case "scalar-unit.frequency":
248 case "scalar-unit.size":
249 propertiesInJson.addProperty(TYPE, "string");
252 propertiesInJson.addProperty(TYPE, "string");
253 propertiesInJson.addProperty(FORMAT, "date-time");
256 propertiesInJson.addProperty(TYPE, "number");
259 propertiesInJson.addProperty(TYPE, "integer");
260 if (!checkConstraintPresence(toscaElementProperty, "greater_than")
261 && currentPropertyJsonTemplate.hasFields("exclusiveMinimum")) {
262 propertiesInJson.addProperty("exclusiveMinimum", false);
264 if (!checkConstraintPresence(toscaElementProperty, "less_than")
265 && currentPropertyJsonTemplate.hasFields("exclusiveMaximum")) {
266 propertiesInJson.addProperty("exclusiveMaximum", false);
270 propertiesInJson.addProperty(TYPE, currentPropertyJsonTemplate.getName());
276 private void parseEntrySchema(ToscaElementProperty toscaElementProperty, JsonObject propertiesInJson,
277 JsonTemplate currentPropertyJsonTemplate) {
278 // Here, a way to check if entry is a component (datatype) or a simple string
279 if (getToscaElement(this.extractSpecificFieldFromMap(toscaElementProperty, ENTRY_SCHEMA)) != null) {
280 String nameComponent = this.extractSpecificFieldFromMap(toscaElementProperty, ENTRY_SCHEMA);
281 var child = new ToscaConverterToJsonSchema(components, templates, metadataParser, serviceModel);
282 var propertiesContainer = new JsonObject();
284 if (((String) toscaElementProperty.getItems().get(TYPE)).equals(MAP)) {
285 JsonObject componentAsProperty = child.getJsonSchemaOfToscaElement(nameComponent);
286 propertiesContainer.add(nameComponent, componentAsProperty);
287 if (currentPropertyJsonTemplate.hasFields(PROPERTIES)) {
288 propertiesInJson.add(PROPERTIES, propertiesContainer);
291 JsonObject componentAsItem = child.getJsonSchemaOfToscaElement(nameComponent);
292 if (currentPropertyJsonTemplate.hasFields(PROPERTIES)) {
293 propertiesInJson.add("items", componentAsItem);
294 propertiesInJson.addProperty(FORMAT, "tabs-top");
297 } else if (toscaElementProperty.getItems().get(TYPE).equals(LIST)) {
299 var itemContainer = new JsonObject();
300 String valueInEntrySchema =
301 this.extractSpecificFieldFromMap(toscaElementProperty, ENTRY_SCHEMA);
302 itemContainer.addProperty(TYPE, valueInEntrySchema);
303 propertiesInJson.add("items", itemContainer);
304 propertiesInJson.addProperty(FORMAT, "tabs-top");
307 // MAP Case, for now nothing
311 * Look for a matching Component for the name parameter, in the components list.
313 * @param name the tosca element name to search for
314 * @return a tosca element
316 public ToscaElement getToscaElement(String name) {
317 ToscaElement correspondingToscaElement = null;
318 if (components == null) {
321 for (ToscaElement toscaElement : components.values()) {
322 if (toscaElement.getName().equals(name)) {
323 correspondingToscaElement = toscaElement;
326 return correspondingToscaElement;
330 * Simple method to extract quickly a type field from particular property item.
332 * @param toscaElementProperty the property
333 * @param fieldName the fieldname
336 @SuppressWarnings("unchecked")
337 public String extractSpecificFieldFromMap(ToscaElementProperty toscaElementProperty, String fieldName) {
338 Map<String, String> entrySchemaFields =
339 (Map<String, String>) toscaElementProperty.getItems().get(fieldName);
340 return entrySchemaFields.get(TYPE);
344 * Check if a constraint, for a specific property, is there.
346 * @param toscaElementProperty property
347 * @param nameConstraint name constraint
348 * @return a flag boolean
350 public boolean checkConstraintPresence(ToscaElementProperty toscaElementProperty, String nameConstraint) {
351 var presentConstraint = false;
352 if (toscaElementProperty.getItems().containsKey(CONSTRAINTS)) {
353 @SuppressWarnings("unchecked")
354 ArrayList<Object> constraints = (ArrayList<Object>) toscaElementProperty.getItems().get(CONSTRAINTS);
355 for (Object constraint : constraints) {
356 if (constraint instanceof Map
357 && ((Map<?, ?>) constraint).containsKey(nameConstraint)) {
358 presentConstraint = true;
362 return presentConstraint;