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;
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.tosca.DictionaryService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
37 public class ToscaConverterToJson {
38 private LinkedHashMap<String, ToscaElement> components;
39 private LinkedHashMap<String, Template> templates;
41 // if this one is set, the dictionary mechanism is enabled
43 private DictionaryService dictionaryService;
45 public ToscaConverterToJson(LinkedHashMap<String, ToscaElement> components, LinkedHashMap<String, Template> templates) {
46 this.components = components;
47 this.templates = templates;
51 * For a given component, launch process to parse it in Json.
53 * @param nameComponent name components
56 public JsonObject getJsonProcess(String nameComponent, String typeComponent) {
57 JsonObject glob = new JsonObject();
59 if (typeComponent.equals("object")) {
60 glob = this.getFieldAsObject(matchComponent(nameComponent));
63 /*glob = this.getFieldAsArray(matchComponent(nameComponent));*/
70 * Return the classical/general fields of the component, & launch the properties deployment.
72 * @param toscaElement the compo
73 * @return a json object
75 public JsonObject getFieldAsObject(ToscaElement toscaElement) {
77 JsonObject globalFields = new JsonObject();
78 if (templates.get("object").hasFields("title")) {
79 globalFields.addProperty("title", toscaElement.getName());
81 if (templates.get("object").hasFields("type")) {
82 globalFields.addProperty("type", "object");
84 if (templates.get("object").hasFields("description")) {
85 if (toscaElement.getDescription() != null) {
86 globalFields.addProperty("description", toscaElement.getDescription());
89 if (templates.get("object").hasFields("required")) {
90 globalFields.add("required", this.getRequirements(toscaElement.getName()));
92 if (templates.get("object").hasFields("properties")) {
93 globalFields.add("properties", this.deploy(toscaElement.getName()));
99 * Get the required properties of the Component, including the parents properties requirements.
101 * @param nameComponent name component
102 * @return a json array
104 public JsonArray getRequirements(String nameComponent) {
105 JsonArray requirements = new JsonArray();
106 ToscaElement toParse = components.get(nameComponent);
107 //Check for a father component, and launch the same process
108 if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
109 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
110 requirements.addAll(getRequirements(toParse.getDerivedFrom()));
112 //Each property is checked, and add to the requirement array if it's required
113 Collection<Property> properties = toParse.getProperties().values();
114 for (Property property : properties) {
115 if (property.getItems().containsKey("required")
116 && property.getItems().get("required").equals(true)) {
117 requirements.add(property.getName());
124 * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise
125 * deploy and parse the properties.
127 * @param nameComponent name component
128 * @return a json object
130 public JsonObject deploy(String nameComponent) {
131 JsonObject jsonSchema = new JsonObject();
132 ToscaElement toParse = components.get(nameComponent);
133 //Check for a father component, and launch the same process
134 if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
135 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
136 jsonSchema = this.getParent(toParse.getDerivedFrom());
138 //For each component property, check if its a complex properties (a component) or not. In that case,
139 //launch the analyse of the property.
140 for (Entry<String, Property> property : toParse.getProperties().entrySet()) {
141 if (matchComponent((String) property.getValue().getItems().get("type")) != null) {
142 jsonSchema.add(property.getValue().getName(),
143 this.getJsonProcess((String) property.getValue().getItems().get("type"), "object"));
146 jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue()));
153 * If a component has a parent, it is deploy in the same way.
155 * @param nameComponent name component
156 * @return a json object
158 public JsonObject getParent(String nameComponent) {
159 return deploy(nameComponent);
165 * @param property property
166 * @return a json object
168 @SuppressWarnings("unchecked")
169 public JsonObject complexParse(Property property) {
170 JsonObject propertiesInJson = new JsonObject();
171 Template currentPropertyTemplate;
172 String typeProperty = (String) property.getItems().get("type");
173 if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) {
174 currentPropertyTemplate = templates.get("object");
177 String propertyType = (String) property.getItems().get("type");
178 currentPropertyTemplate = templates.get(propertyType.toLowerCase());
180 //Each "special" field is analysed, and has a specific treatment
181 for (String propertyField : property.getItems().keySet()) {
182 switch (propertyField) {
184 if (currentPropertyTemplate.hasFields(propertyField)) {
185 String fieldtype = (String) property.getItems().get(propertyField);
186 switch (fieldtype.toLowerCase()) {
188 propertiesInJson.addProperty("type", "array");
191 propertiesInJson.addProperty("type", "object");
193 case "scalar-unit.time":
194 case "scalar-unit.frequency":
195 case "scalar-unit.size":
196 propertiesInJson.addProperty("type", "string");
199 propertiesInJson.addProperty("type", "string");
200 propertiesInJson.addProperty("format", "date-time");
203 propertiesInJson.addProperty("type", "number");
206 propertiesInJson.addProperty("type", "integer");
207 if (!checkConstraintPresence(property, "greater_than")
208 && currentPropertyTemplate.hasFields("exclusiveMinimum")) {
209 propertiesInJson.addProperty("exclusiveMinimum", false);
211 if (!checkConstraintPresence(property, "less_than")
212 && currentPropertyTemplate.hasFields("exclusiveMaximum")) {
213 propertiesInJson.addProperty("exclusiveMaximum", false);
217 propertiesInJson.addProperty("type", currentPropertyTemplate.getName());
223 propertiesInJson.add("enum", MetadataParser.processAllMetadataElement(property,
227 property.addConstraintsAsJson(propertiesInJson,
228 (ArrayList<Object>) property.getItems().get("constraints"),
229 currentPropertyTemplate);
232 //Here, a way to check if entry is a component (datatype) or a simple string
233 if (matchComponent(this.extractSpecificFieldFromMap(property, "entry_schema")) != null) {
234 String nameComponent = this.extractSpecificFieldFromMap(property, "entry_schema");
235 ToscaConverterToJson child = new ToscaConverterToJson(components, templates);
236 JsonObject propertiesContainer = new JsonObject();
238 switch ((String) property.getItems().get("type")) {
239 case "map": // Get it as an object
240 JsonObject componentAsProperty = child.getJsonProcess(nameComponent, "object");
241 propertiesContainer.add(nameComponent, componentAsProperty);
242 if (currentPropertyTemplate.hasFields("properties")) {
243 propertiesInJson.add("properties", propertiesContainer);
246 default://list : get it as an Array
247 JsonObject componentAsItem = child.getJsonProcess(nameComponent, "object");
248 if (currentPropertyTemplate.hasFields("properties")) {
249 propertiesInJson.add("items", componentAsItem);
256 else if (property.getItems().get("type").equals("list")) {
257 JsonObject itemContainer = new JsonObject();
258 String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema");
259 itemContainer.addProperty("type", valueInEntrySchema);
260 propertiesInJson.add("items", itemContainer);
262 // MAP Case, for now nothing
266 //Each classical field : type, description, default..
267 if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) {
268 property.addFieldToJson(propertiesInJson, propertyField,
269 property.getItems().get(propertyField));
274 return propertiesInJson;
278 * Look for a matching Component for the name paramater, in the components list.
280 * @param name the name
281 * @return a component
283 public ToscaElement matchComponent(String name) {
284 ToscaElement correspondingToscaElement = null;
285 if (components == null) {
288 for (ToscaElement toscaElement : components.values()) {
289 if (toscaElement.getName().equals(name)) {
290 correspondingToscaElement = toscaElement;
293 return correspondingToscaElement;
297 * Simple method to extract quickly a type field from particular property item.
299 * @param property the property
300 * @param fieldName the fieldname
303 @SuppressWarnings("unchecked")
304 public String extractSpecificFieldFromMap(Property property, String fieldName) {
305 LinkedHashMap<String, String> entrySchemaFields =
306 (LinkedHashMap<String, String>) property.getItems().get(fieldName);
307 return entrySchemaFields.get("type");
311 * Check if a constraint, for a specific property, is there.
313 * @param property property
314 * @param nameConstraint name constraint
315 * @return a flag boolean
317 public boolean checkConstraintPresence(Property property, String nameConstraint) {
318 boolean presentConstraint = false;
319 if (property.getItems().containsKey("constraints")) {
320 ArrayList<Object> constraints = (ArrayList) property.getItems().get("constraints");
321 for (Object constraint : constraints) {
322 if (constraint instanceof LinkedHashMap) {
323 if (((LinkedHashMap) constraint).containsKey(nameConstraint)) {
324 presentConstraint = true;
329 return presentConstraint;