2e025ba7dfdcf3d2b67f22d264d258aa88a4ea6b
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / ToscaYamlToJsonConvertor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.yaml.snakeyaml.Yaml;
36
37 /**
38  * Tosca Model Yaml parser and convertor to JSON Schema consumable for JSON
39  * Editor.
40  *
41  */
42 public class ToscaYamlToJsonConvertor {
43
44     private int simpleTypeOrder = 1000;
45     private int complexTypeOrder = 10000;
46     private int complexSimpleTypeOrder = 1;
47
48     private int incrementSimpleTypeOrder() {
49         return simpleTypeOrder++;
50     }
51
52     private int incrementComplexTypeOrder() {
53         return complexTypeOrder = complexTypeOrder + 10000;
54     }
55
56     private int incrementComplexSimpleTypeOrder() {
57         complexSimpleTypeOrder++;
58         return complexTypeOrder + complexSimpleTypeOrder;
59     }
60
61     /**
62      * Parses Tosca YAML string.
63      *
64      * @param yamlString     YAML string
65      * @param modelTypeToUse The model type that must be used to obtain the Json
66      *                       Schema
67      * @return JSON string
68      */
69     public String parseToscaYaml(String yamlString, String modelTypeToUse) {
70
71         Yaml yaml = new Yaml();
72         LinkedHashMap<String, Object> loadedYaml = yaml.load(yamlString);
73         if (loadedYaml == null) {
74             return "";
75         }
76         LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<>();
77         LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<>();
78         JSONObject jsonParentObject = new JSONObject();
79         JSONObject jsonTempObject = new JSONObject();
80         parseNodeAndDataType(loadedYaml, nodeTypes, dataNodes);
81         populateJsonEditorObject(loadedYaml, nodeTypes, dataNodes, jsonParentObject, jsonTempObject, modelTypeToUse);
82         if (jsonTempObject.length() > 0) {
83             jsonParentObject = jsonTempObject;
84         }
85         JSONObject jsonEditorObject = new JSONObject();
86         jsonEditorObject.put(JsonEditorSchemaConstants.SCHEMA, jsonParentObject);
87         return jsonEditorObject.toString();
88     }
89
90     // Parse node_type and data_type
91     @SuppressWarnings("unchecked")
92     private void parseNodeAndDataType(LinkedHashMap<String, Object> map, LinkedHashMap<String, Object> nodeTypes,
93             LinkedHashMap<String, Object> dataNodes) {
94         map.entrySet().stream().forEach(n -> {
95             if (n.getKey().contains(ToscaSchemaConstants.NODE_TYPES) && n.getValue() instanceof Map) {
96                 parseNodeAndDataType((LinkedHashMap<String, Object>) n.getValue(), nodeTypes, dataNodes);
97             } else if (n.getKey().contains(ToscaSchemaConstants.DATA_TYPES) && n.getValue() instanceof Map) {
98                 parseNodeAndDataType((LinkedHashMap<String, Object>) n.getValue(), nodeTypes, dataNodes);
99             } else if (n.getKey().contains(ToscaSchemaConstants.POLICY_NODE)) {
100                 nodeTypes.put(n.getKey(), n.getValue());
101             } else if (n.getKey().contains(ToscaSchemaConstants.POLICY_DATA)) {
102                 dataNodes.put(n.getKey(), n.getValue());
103             }
104         });
105     }
106
107     @SuppressWarnings("unchecked")
108     private void populateJsonEditorObject(LinkedHashMap<String, Object> map, LinkedHashMap<String, Object> nodeTypes,
109             LinkedHashMap<String, Object> dataNodes, JSONObject jsonParentObject, JSONObject jsonTempObject,
110             String modelTypeToUse) {
111
112         Map<String, JSONObject> jsonEntrySchema = new HashMap<>();
113         jsonParentObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_OBJECT);
114         if (nodeTypes.get(modelTypeToUse) instanceof Map) {
115             ((LinkedHashMap<String, Object>) nodeTypes.get(modelTypeToUse)).entrySet().forEach(ntElement -> {
116                 if (ntElement.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
117                     JSONArray rootNodeArray = new JSONArray();
118                     if (ntElement.getValue() instanceof Map) {
119                         ((LinkedHashMap<String, Object>) ntElement.getValue()).entrySet()
120                                 .forEach((ntPropertiesElement) -> {
121                                     boolean isListNode = false;
122                                     parseDescription((LinkedHashMap<String, Object>) ntPropertiesElement.getValue(),
123                                             jsonParentObject);
124                                     LinkedHashMap<String, Object> parentPropertiesMap = (LinkedHashMap<String, Object>) ntPropertiesElement
125                                             .getValue();
126                                     if (parentPropertiesMap.containsKey(ToscaSchemaConstants.TYPE)
127                                             && ((String) parentPropertiesMap.get(ToscaSchemaConstants.TYPE))
128                                                     .contains(ToscaSchemaConstants.TYPE_MAP)
129                                             && parentPropertiesMap.containsKey(ToscaSchemaConstants.ENTRY_SCHEMA)) {
130                                         parentPropertiesMap = (LinkedHashMap<String, Object>) parentPropertiesMap
131                                                 .get(ToscaSchemaConstants.ENTRY_SCHEMA);
132                                         isListNode = true;
133                                     }
134                                     if (parentPropertiesMap.containsKey(ToscaSchemaConstants.TYPE)
135                                             && ((String) parentPropertiesMap.get(ToscaSchemaConstants.TYPE))
136                                                     .contains(ToscaSchemaConstants.POLICY_DATA)) {
137                                         ((LinkedHashMap<String, Object>) dataNodes
138                                                 .get(parentPropertiesMap.get(ToscaSchemaConstants.TYPE))).entrySet()
139                                                         .stream().forEach(pmap -> {
140                                                             if (pmap.getKey().equalsIgnoreCase(
141                                                                     ToscaSchemaConstants.PROPERTIES)) {
142                                                                 parseToscaProperties(ToscaSchemaConstants.POLICY_NODE,
143                                                                         (LinkedHashMap<String, Object>) pmap.getValue(),
144                                                                         jsonParentObject, rootNodeArray,
145                                                                         jsonEntrySchema, dataNodes,
146                                                                         incrementSimpleTypeOrder());
147                                                             }
148                                                         });
149                                     }
150                                     if (isListNode) {
151                                         jsonTempObject.put(JsonEditorSchemaConstants.TYPE,
152                                                 JsonEditorSchemaConstants.TYPE_ARRAY);
153                                         parseDescription((LinkedHashMap<String, Object>) ntPropertiesElement.getValue(),
154                                                 jsonTempObject);
155                                         jsonTempObject.put(JsonEditorSchemaConstants.ITEMS, jsonParentObject);
156                                         jsonTempObject.put(JsonEditorSchemaConstants.FORMAT,
157                                                 JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP);
158                                         jsonTempObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS,
159                                                 JsonEditorSchemaConstants.TRUE);
160                                     }
161                                 });
162                     }
163                 }
164             });
165         }
166     }
167
168     @SuppressWarnings("unchecked")
169     private void parseToscaProperties(String parentKey, LinkedHashMap<String, Object> propertiesMap,
170             JSONObject jsonDataNode, JSONArray array, Map<String, JSONObject> jsonEntrySchema,
171             LinkedHashMap<String, Object> dataNodes, final int order) {
172         JSONObject jsonPropertyNode = new JSONObject();
173         propertiesMap.entrySet().stream().forEach(p -> {
174             // Populate JSON Array for "required" key
175
176             if (p.getValue() instanceof Map) {
177                 LinkedHashMap<String, Object> nodeMap = (LinkedHashMap<String, Object>) p.getValue();
178                 if (nodeMap.containsKey(ToscaSchemaConstants.REQUIRED)
179                         && ((boolean) nodeMap.get(ToscaSchemaConstants.REQUIRED))) {
180                     array.put(p.getKey());
181                 }
182                 // if(nodeMap.containsKey(ToscaSchemaConstants.CONSTRAINTS))
183                 parseToscaChildNodeMap(p.getKey(), nodeMap, jsonPropertyNode, jsonEntrySchema, dataNodes, array,
184                         incrementSimpleTypeOrder());
185             }
186         });
187         jsonDataNode.put(JsonEditorSchemaConstants.REQUIRED, array);
188         jsonDataNode.put(JsonEditorSchemaConstants.PROPERTIES, jsonPropertyNode);
189     }
190
191     @SuppressWarnings("unchecked")
192     private void parseToscaPropertiesForType(String parentKey, LinkedHashMap<String, Object> propertiesMap,
193             JSONObject jsonDataNode, JSONArray array, Map<String, JSONObject> jsonEntrySchema,
194             LinkedHashMap<String, Object> dataNodes, boolean isType, int order) {
195         JSONObject jsonPropertyNode = new JSONObject();
196
197         propertiesMap.entrySet().stream().forEach(p -> {
198             // array.put(p.getKey());
199             boolean overWriteArray = false;
200             if (p.getValue() instanceof Map) {
201                 LinkedHashMap<String, Object> nodeMap = (LinkedHashMap<String, Object>) p.getValue();
202                 if (!(parentKey.contains(ToscaSchemaConstants.ENTRY_SCHEMA)
203                         || parentKey.contains(ToscaSchemaConstants.POLICY_NODE))
204                         && nodeMap.containsKey(ToscaSchemaConstants.TYPE)
205                         && (((String) nodeMap.get(ToscaSchemaConstants.TYPE))
206                                 .contains(ToscaSchemaConstants.POLICY_DATA))) {
207                     overWriteArray = true;
208                 }
209                 if (nodeMap.containsKey(ToscaSchemaConstants.REQUIRED)
210                         && ((boolean) nodeMap.get(ToscaSchemaConstants.REQUIRED))) {
211                     array.put(p.getKey());
212                 }
213                 parseToscaChildNodeMap(p.getKey(), nodeMap, jsonPropertyNode, jsonEntrySchema, dataNodes, array, order);
214             }
215         });
216         jsonDataNode.put(JsonEditorSchemaConstants.REQUIRED, array);
217         jsonDataNode.put(JsonEditorSchemaConstants.PROPERTIES, jsonPropertyNode);
218     }
219
220     private void parseToscaChildNodeMap(String childObjectKey, LinkedHashMap<String, Object> childNodeMap,
221             JSONObject jsonPropertyNode, Map<String, JSONObject> jsonEntrySchema,
222             LinkedHashMap<String, Object> dataNodes, JSONArray array, int order) {
223         JSONObject childObject = new JSONObject();
224         // JSONArray childArray = new JSONArray();
225         parseDescription(childNodeMap, childObject);
226         parseTypes(childObjectKey, childNodeMap, childObject, jsonEntrySchema, dataNodes, array, order);
227         parseConstraints(childNodeMap, childObject);
228         parseEntrySchema(childNodeMap, childObject, jsonPropertyNode, jsonEntrySchema, dataNodes);
229
230         jsonPropertyNode.put(childObjectKey, childObject);
231         order++;
232
233     }
234
235     private void parseEntrySchema(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject,
236             JSONObject jsonPropertyNode, Map<String, JSONObject> jsonEntrySchema,
237             LinkedHashMap<String, Object> dataNodes) {
238         if (childNodeMap.get(ToscaSchemaConstants.ENTRY_SCHEMA) != null) {
239             if (childNodeMap.get(ToscaSchemaConstants.ENTRY_SCHEMA) instanceof Map) {
240                 LinkedHashMap<String, Object> entrySchemaMap = (LinkedHashMap<String, Object>) childNodeMap
241                         .get(ToscaSchemaConstants.ENTRY_SCHEMA);
242                 entrySchemaMap.entrySet().stream().forEach(entry -> {
243                     if (entry.getKey().equalsIgnoreCase(ToscaSchemaConstants.TYPE) && entry.getValue() != null) {
244                         String entrySchemaType = (String) entry.getValue();
245                         if (entrySchemaType.contains(ToscaSchemaConstants.POLICY_DATA)) {
246                             JSONArray array = new JSONArray();
247                             if (jsonEntrySchema.get(entrySchemaType) != null) {
248                                 // Already traversed
249                                 JSONObject entrySchemaObject = jsonEntrySchema.get(entrySchemaType);
250                                 attachEntrySchemaJsonObject(childObject, entrySchemaObject,
251                                         JsonEditorSchemaConstants.TYPE_OBJECT);
252                             } else if (dataNodes.containsKey(entrySchemaType)) {
253
254                                 JSONObject entrySchemaObject = new JSONObject();
255                                 // Need to traverse
256                                 ((LinkedHashMap<String, Object>) dataNodes.get(entrySchemaType)).entrySet().stream()
257                                         .forEach(pmap -> {
258                                             if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
259                                                 parseToscaProperties(ToscaSchemaConstants.ENTRY_SCHEMA,
260                                                         (LinkedHashMap<String, Object>) pmap.getValue(),
261                                                         entrySchemaObject, array, jsonEntrySchema, dataNodes,
262                                                         incrementComplexTypeOrder());
263                                                 jsonEntrySchema.put(entrySchemaType, entrySchemaObject);
264                                                 dataNodes.remove(entrySchemaType);
265                                                 attachEntrySchemaJsonObject(childObject, entrySchemaObject,
266                                                         JsonEditorSchemaConstants.TYPE_OBJECT);
267                                             }
268
269                                         });
270                             }
271                         } else if (entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)
272                                 || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)
273                                 || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
274                             JSONObject entrySchemaObject = new JSONObject();
275                             parseConstraints(entrySchemaMap, entrySchemaObject);
276                             String jsontype = JsonEditorSchemaConstants.TYPE_STRING;
277                             if (entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)
278                                     || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
279                                 jsontype = JsonEditorSchemaConstants.TYPE_INTEGER;
280                             }
281                             if (childNodeMap.get(ToscaSchemaConstants.TYPE) != null) {
282                                 // Only known value of type is String for now
283                                 if (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String) {
284                                     String typeValue = (String) childNodeMap.get(ToscaSchemaConstants.TYPE);
285                                     if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
286                                         // Custom key for JSON Editor and UI rendering
287                                         childObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT,
288                                                 JsonEditorSchemaConstants.FORMAT_SELECT);
289                                         // childObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS,
290                                         // JsonEditorSchemaConstants.TRUE);
291                                     }
292                                 }
293                             }
294                             attachEntrySchemaJsonObject(childObject, entrySchemaObject, jsontype);
295                         }
296                     }
297                 });
298             }
299         }
300     }
301
302     private void attachEntrySchemaJsonObject(JSONObject childObject, JSONObject entrySchemaObject, String dataType) {
303
304         entrySchemaObject.put(JsonEditorSchemaConstants.TYPE, dataType);
305         childObject.put(JsonEditorSchemaConstants.ITEMS, entrySchemaObject);
306     }
307
308     @SuppressWarnings("unchecked")
309     private void attachTypeJsonObject(JSONObject childObject, JSONObject typeObject) {
310         Iterator<String> keys = typeObject.keys();
311         while (keys.hasNext()) {
312             String key = keys.next();
313             childObject.put(key, typeObject.get(key));
314         }
315     }
316
317     /*
318      * private String parseKey(String toscaKey, String lookupString) { return
319      * toscaKey.substring(toscaKey.indexOf(lookupString) + lookupString.length(),
320      * toscaKey.length()); }
321      */
322
323     private void parseDescription(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject) {
324         if (childNodeMap.get(ToscaSchemaConstants.DESCRIPTION) != null) {
325             childObject.put(JsonEditorSchemaConstants.TITLE, childNodeMap.get(ToscaSchemaConstants.DESCRIPTION));
326         }
327     }
328
329     private void parseTypes(String childObjectKey, LinkedHashMap<String, Object> childNodeMap, JSONObject childObject,
330             Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes, JSONArray array,
331             int order) {
332         if (childNodeMap.get(ToscaSchemaConstants.TYPE) != null) {
333             // Only known value of type is String for now
334             if (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String) {
335                 childObject.put(JsonEditorSchemaConstants.PROPERTY_ORDER, order);
336                 String typeValue = (String) childNodeMap.get(ToscaSchemaConstants.TYPE);
337                 if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
338                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_INTEGER);
339
340                 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
341                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_INTEGER);
342                 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
343                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_ARRAY);
344                     // Custom key for JSON Editor and UI rendering
345                     childObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT,
346                             JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP);
347                     childObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS, JsonEditorSchemaConstants.TRUE);
348                 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_MAP)) {
349                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_OBJECT);
350                 } else if (typeValue.contains(ToscaSchemaConstants.POLICY_DATA)) {
351                     JSONArray childArray = new JSONArray();
352
353                     if (jsonEntrySchema.get(typeValue) != null) {
354                         // Already traversed
355                         JSONObject entrySchemaObject = jsonEntrySchema.get(typeValue);
356                         attachTypeJsonObject(childObject, entrySchemaObject);
357                     } else if (dataNodes.containsKey(typeValue)) {
358                         JSONObject entrySchemaObject = new JSONObject();
359                         // Need to traverse
360                         JSONArray jsonArray = new JSONArray();
361                         ((LinkedHashMap<String, Object>) dataNodes.get(typeValue)).entrySet().stream().forEach(pmap -> {
362                             if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
363
364                                 ((LinkedHashMap<String, Object>) pmap.getValue()).entrySet().stream().forEach(p -> {
365                                     if (p.getValue() instanceof Map) {
366                                         LinkedHashMap<String, Object> childNodeMap2 = (LinkedHashMap<String, Object>) p
367                                                 .getValue();
368                                         if (childNodeMap2.containsKey(ToscaSchemaConstants.TYPE)
369                                                 && (((String) childNodeMap2.get(ToscaSchemaConstants.TYPE))
370                                                         .contains(ToscaSchemaConstants.POLICY_DATA))) {
371                                         }
372                                     }
373                                 });
374                             }
375                         });
376                         ((LinkedHashMap<String, Object>) dataNodes.get(typeValue)).entrySet().stream().forEach(pmap -> {
377                             if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
378                                 parseToscaPropertiesForType(childObjectKey,
379                                         (LinkedHashMap<String, Object>) pmap.getValue(), entrySchemaObject, childArray,
380                                         jsonEntrySchema, dataNodes, true, incrementComplexSimpleTypeOrder());
381                                 jsonEntrySchema.put(typeValue, entrySchemaObject);
382                                 dataNodes.remove(typeValue);
383                                 attachTypeJsonObject(childObject, entrySchemaObject);
384                             }
385                         });
386                     }
387                 } else {
388                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_STRING);
389                 }
390             }
391             if (childNodeMap.get(ToscaSchemaConstants.DEFAULT) != null) {
392                 childObject.put(JsonEditorSchemaConstants.DEFAULT, childNodeMap.get(ToscaSchemaConstants.DEFAULT));
393             }
394         }
395     }
396
397     private void parseConstraints(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject) {
398         if (childNodeMap.containsKey(ToscaSchemaConstants.CONSTRAINTS)
399                 && childNodeMap.get(ToscaSchemaConstants.CONSTRAINTS) != null) {
400             List<LinkedHashMap<String, Object>> constraintsList = (List<LinkedHashMap<String, Object>>) childNodeMap
401                     .get(ToscaSchemaConstants.CONSTRAINTS);
402             constraintsList.stream().forEach(c -> {
403                 if (c instanceof Map) {
404                     c.entrySet().stream().forEach(constraint -> {
405                         if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.MIN_LENGTH)
406                                 || constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.GREATER_OR_EQUAL)) {
407                             // For String min_lenghth is minimum length whereas for number, it will be
408                             // minimum or greater than to the defined value
409                             if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
410                                     && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
411                                     && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
412                                             .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
413                                 childObject.put(JsonEditorSchemaConstants.MIN_LENGTH, constraint.getValue());
414                             } else {
415                                 childObject.put(JsonEditorSchemaConstants.MINIMUM, constraint.getValue());
416                             }
417                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.MAX_LENGTH)
418                                 || constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.LESS_OR_EQUAL)) {
419                             // For String max_lenghth is maximum length whereas for number, it will be
420                             // maximum or less than the defined value
421                             if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
422                                     && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
423                                     && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
424                                             .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
425                                 childObject.put(JsonEditorSchemaConstants.MAX_LENGTH, constraint.getValue());
426                             } else {
427                                 childObject.put(JsonEditorSchemaConstants.MAXIMUM, constraint.getValue());
428                             }
429                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.LESS_THAN)) {
430                             childObject.put(JsonEditorSchemaConstants.EXCLUSIVE_MAXIMUM, constraint.getValue());
431                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.GREATER_THAN)) {
432                             childObject.put(JsonEditorSchemaConstants.EXCLUSIVE_MINIMUM, constraint.getValue());
433                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.IN_RANGE)) {
434                             if (constraint.getValue() instanceof ArrayList<?>) {
435                                 if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
436                                         && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
437                                         && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
438                                                 .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
439                                     childObject.put(JsonEditorSchemaConstants.MIN_LENGTH,
440                                             ((ArrayList) constraint.getValue()).get(0));
441                                     childObject.put(JsonEditorSchemaConstants.MAX_LENGTH,
442                                             ((ArrayList) constraint.getValue()).get(1));
443                                 } else {
444                                     childObject.put(JsonEditorSchemaConstants.MINIMUM,
445                                             ((ArrayList) constraint.getValue()).get(0));
446                                     childObject.put(JsonEditorSchemaConstants.MAXIMUM,
447                                             ((ArrayList) constraint.getValue()).get(1));
448                                 }
449
450                             }
451                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.VALID_VALUES)) {
452                             JSONArray validValuesArray = new JSONArray();
453
454                             if (constraint.getValue() instanceof ArrayList<?>) {
455                                 boolean processDictionary = ((ArrayList<?>) constraint.getValue()).stream()
456                                         .anyMatch(value -> (value instanceof String
457                                                 && ((String) value).contains(ToscaSchemaConstants.DICTIONARY)));
458                                 if (!processDictionary) {
459                                     ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
460                                         validValuesArray.put(value);
461                                     });
462                                     childObject.put(JsonEditorSchemaConstants.ENUM, validValuesArray);
463                                 } else {
464                                     ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
465                                         if ((value instanceof String
466                                                 && ((String) value).contains(ToscaSchemaConstants.DICTIONARY))) {
467                                             processDictionaryElements(childObject, (String) value);
468                                         }
469
470                                     });
471
472                                 }
473                             }
474
475                         }
476                     });
477                 }
478             });
479         }
480     }
481
482     private void processDictionaryElements(JSONObject childObject, String dictionaryReference) {
483
484     }
485
486     private String getJsonType(String toscaType) {
487         String jsonType = null;
488         if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
489             jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
490         } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
491             jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
492         } else {
493             jsonType = JsonEditorSchemaConstants.TYPE_STRING;
494         }
495         return jsonType;
496     }
497
498 }