16a817ea43daea9257a658eb5122dc8c3c14a946
[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 import java.util.Optional;
33 import java.util.stream.Collectors;
34
35 import org.json.JSONArray;
36 import org.json.JSONObject;
37 import org.onap.clamp.clds.dao.CldsDao;
38 import org.onap.clamp.clds.model.CldsDictionaryItem;
39 import org.yaml.snakeyaml.Yaml;
40
41 /**
42  * Tosca Model Yaml parser and convertor to JSON Schema consumable for JSON
43  * Editor
44  *
45  */
46 public class ToscaYamlToJsonConvertor {
47
48     private CldsDao cldsDao;
49     private int simpleTypeOrder = 1000;
50     private int complexTypeOrder = 10000;
51     private int complexSimpleTypeOrder = 1;
52
53     public ToscaYamlToJsonConvertor(CldsDao cldsDao) {
54         this.cldsDao = cldsDao;
55     }
56
57     private int incrementSimpleTypeOrder() {
58         return simpleTypeOrder++;
59     }
60
61     private int incrementComplexTypeOrder() {
62         return complexTypeOrder = complexTypeOrder + 10000;
63     }
64
65     private int incrementComplexSimpleTypeOrder() {
66         complexSimpleTypeOrder++;
67         return complexTypeOrder + complexSimpleTypeOrder;
68     }
69
70     /**
71      * @return the cldsDao
72      */
73     public CldsDao getCldsDao() {
74         return cldsDao;
75     }
76
77     /**
78      * @param cldsDao
79      *        the cldsDao to set
80      */
81     public void setCldsDao(CldsDao cldsDao) {
82         this.cldsDao = cldsDao;
83     }
84
85     public String parseToscaYaml(String yamlString) {
86
87         Yaml yaml = new Yaml();
88         LinkedHashMap<String, Object> loadedYaml = yaml.load(yamlString);
89         if (loadedYaml == null) {
90             return "";
91         }
92         LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<>();
93         LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<>();
94         JSONObject jsonEditorObject = new JSONObject();
95         JSONObject jsonParentObject = new JSONObject();
96         JSONObject jsonTempObject = new JSONObject();
97         parseNodeAndDataType(loadedYaml, nodeTypes, dataNodes);
98         populateJsonEditorObject(loadedYaml, nodeTypes, dataNodes, jsonParentObject, jsonTempObject);
99         if (jsonTempObject.length() > 0) {
100             jsonParentObject = jsonTempObject;
101         }
102         jsonEditorObject.put(JsonEditorSchemaConstants.SCHEMA, jsonParentObject);
103         return jsonEditorObject.toString();
104     }
105
106     // Parse node_type and data_type
107     @SuppressWarnings("unchecked")
108     private void parseNodeAndDataType(LinkedHashMap<String, Object> map, LinkedHashMap<String, Object> nodeTypes,
109         LinkedHashMap<String, Object> dataNodes) {
110         map.entrySet().stream().forEach(n -> {
111             if (n.getKey().contains(ToscaSchemaConstants.NODE_TYPES) && n.getValue() instanceof Map) {
112                 parseNodeAndDataType((LinkedHashMap<String, Object>) n.getValue(), nodeTypes, dataNodes);
113             } else if (n.getKey().contains(ToscaSchemaConstants.DATA_TYPES) && n.getValue() instanceof Map) {
114                 parseNodeAndDataType((LinkedHashMap<String, Object>) n.getValue(), nodeTypes, dataNodes);
115             } else if (n.getKey().contains(ToscaSchemaConstants.POLICY_NODE)) {
116                 nodeTypes.put(n.getKey(), n.getValue());
117             } else if (n.getKey().contains(ToscaSchemaConstants.POLICY_DATA)) {
118                 dataNodes.put(n.getKey(), n.getValue());
119             }
120
121         });
122     }
123
124     @SuppressWarnings("unchecked")
125     private void populateJsonEditorObject(LinkedHashMap<String, Object> map, LinkedHashMap<String, Object> nodeTypes,
126         LinkedHashMap<String, Object> dataNodes, JSONObject jsonParentObject, JSONObject jsonTempObject) {
127
128         Map<String, JSONObject> jsonEntrySchema = new HashMap();
129         jsonParentObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_OBJECT);
130         nodeTypes.entrySet().stream().forEach(nt -> {
131             if (nt.getValue() instanceof Map) {
132                 ((LinkedHashMap<String, Object>) nt.getValue()).entrySet().forEach(ntElement -> {
133                     if (ntElement.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
134                         JSONArray rootNodeArray = new JSONArray();
135                         if (ntElement.getValue() instanceof Map) {
136                             ((LinkedHashMap<String, Object>) ntElement.getValue()).entrySet()
137                                 .forEach((ntPropertiesElement) -> {
138                                     boolean isListNode = false;
139                                     parseDescription((LinkedHashMap<String, Object>) ntPropertiesElement.getValue(),
140                                         jsonParentObject);
141                                     LinkedHashMap<String, Object> parentPropertiesMap = (LinkedHashMap<String, Object>) ntPropertiesElement
142                                         .getValue();
143                                     if (parentPropertiesMap.containsKey(ToscaSchemaConstants.TYPE)
144                                         && ((String) parentPropertiesMap.get(ToscaSchemaConstants.TYPE))
145                                             .contains(ToscaSchemaConstants.TYPE_MAP)
146                                         && parentPropertiesMap.containsKey(ToscaSchemaConstants.ENTRY_SCHEMA)) {
147                                         parentPropertiesMap = (LinkedHashMap<String, Object>) parentPropertiesMap
148                                             .get(ToscaSchemaConstants.ENTRY_SCHEMA);
149                                         isListNode = true;
150                                     }
151                                     if (parentPropertiesMap.containsKey(ToscaSchemaConstants.TYPE)
152                                         && ((String) parentPropertiesMap.get(ToscaSchemaConstants.TYPE))
153                                             .contains(ToscaSchemaConstants.POLICY_DATA)) {
154                                         ((LinkedHashMap<String, Object>) dataNodes
155                                             .get(parentPropertiesMap.get(ToscaSchemaConstants.TYPE))).entrySet()
156                                                 .stream().forEach(pmap -> {
157                                                     if (pmap.getKey()
158                                                         .equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
159                                                         parseToscaProperties(ToscaSchemaConstants.POLICY_NODE,
160                                                             (LinkedHashMap<String, Object>) pmap.getValue(),
161                                                             jsonParentObject, rootNodeArray, jsonEntrySchema, dataNodes,
162                                                             incrementSimpleTypeOrder());
163                                                     }
164
165                                                 });
166
167                                     }
168                                     if (isListNode) {
169                                         jsonTempObject.put(JsonEditorSchemaConstants.TYPE,
170                                             JsonEditorSchemaConstants.TYPE_ARRAY);
171                                         parseDescription((LinkedHashMap<String, Object>) ntPropertiesElement.getValue(),
172                                             jsonTempObject);
173                                         jsonTempObject.put(JsonEditorSchemaConstants.ITEMS, jsonParentObject);
174                                         jsonTempObject.put(JsonEditorSchemaConstants.FORMAT,
175                                             JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP);
176                                         jsonTempObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS,
177                                             JsonEditorSchemaConstants.TRUE);
178                                     }
179                                 });
180                         }
181                     }
182                 });
183             }
184         });
185     }
186
187     @SuppressWarnings("unchecked")
188     private void parseToscaProperties(String parentKey, LinkedHashMap<String, Object> propertiesMap,
189         JSONObject jsonDataNode, JSONArray array, Map<String, JSONObject> jsonEntrySchema,
190         LinkedHashMap<String, Object> dataNodes, final int order) {
191         JSONObject jsonPropertyNode = new JSONObject();
192         propertiesMap.entrySet().stream().forEach(p -> {
193             // Populate JSON Array for "required" key
194
195             if (p.getValue() instanceof Map) {
196                 LinkedHashMap<String, Object> nodeMap = (LinkedHashMap<String, Object>) p.getValue();
197                 if (nodeMap.containsKey(ToscaSchemaConstants.REQUIRED)
198                     && ((boolean) nodeMap.get(ToscaSchemaConstants.REQUIRED))) {
199                     array.put(p.getKey());
200                 }
201                 // if(nodeMap.containsKey(ToscaSchemaConstants.CONSTRAINTS))
202                 parseToscaChildNodeMap(p.getKey(), nodeMap, jsonPropertyNode, jsonEntrySchema, dataNodes, array,
203                     incrementSimpleTypeOrder());
204             }
205         });
206         jsonDataNode.put(JsonEditorSchemaConstants.REQUIRED, array);
207         jsonDataNode.put(JsonEditorSchemaConstants.PROPERTIES, jsonPropertyNode);
208     }
209
210     @SuppressWarnings("unchecked")
211     private void parseToscaPropertiesForType(String parentKey, LinkedHashMap<String, Object> propertiesMap,
212         JSONObject jsonDataNode, JSONArray array, Map<String, JSONObject> jsonEntrySchema,
213         LinkedHashMap<String, Object> dataNodes, boolean isType, int order) {
214         JSONObject jsonPropertyNode = new JSONObject();
215
216         propertiesMap.entrySet().stream().forEach(p -> {
217             // array.put(p.getKey());
218             boolean overWriteArray = false;
219             if (p.getValue() instanceof Map) {
220                 LinkedHashMap<String, Object> nodeMap = (LinkedHashMap<String, Object>) p.getValue();
221                 if (!(parentKey.contains(ToscaSchemaConstants.ENTRY_SCHEMA)
222                     || parentKey.contains(ToscaSchemaConstants.POLICY_NODE))
223                     && nodeMap.containsKey(ToscaSchemaConstants.TYPE)
224                     && (((String) nodeMap.get(ToscaSchemaConstants.TYPE)).contains(ToscaSchemaConstants.POLICY_DATA))) {
225                     overWriteArray = true;
226                 }
227                 if (nodeMap.containsKey(ToscaSchemaConstants.REQUIRED)
228                     && ((boolean) nodeMap.get(ToscaSchemaConstants.REQUIRED))) {
229                     array.put(p.getKey());
230                 }
231                 parseToscaChildNodeMap(p.getKey(), nodeMap, jsonPropertyNode, jsonEntrySchema, dataNodes, array, order);
232             }
233         });
234         jsonDataNode.put(JsonEditorSchemaConstants.REQUIRED, array);
235         jsonDataNode.put(JsonEditorSchemaConstants.PROPERTIES, jsonPropertyNode);
236     }
237
238     private void parseToscaChildNodeMap(String childObjectKey, LinkedHashMap<String, Object> childNodeMap,
239         JSONObject jsonPropertyNode, Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes,
240         JSONArray array, int order) {
241         JSONObject childObject = new JSONObject();
242         // JSONArray childArray = new JSONArray();
243         parseDescription(childNodeMap, childObject);
244         parseTypes(childObjectKey, childNodeMap, childObject, jsonEntrySchema, dataNodes, array, order);
245         parseConstraints(childNodeMap, childObject);
246         parseEntrySchema(childNodeMap, childObject, jsonPropertyNode, jsonEntrySchema, dataNodes);
247
248         jsonPropertyNode.put(childObjectKey, childObject);
249         order++;
250
251     }
252
253     private void parseEntrySchema(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject,
254         JSONObject jsonPropertyNode, Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes) {
255         if (childNodeMap.get(ToscaSchemaConstants.ENTRY_SCHEMA) != null) {
256             if (childNodeMap.get(ToscaSchemaConstants.ENTRY_SCHEMA) instanceof Map) {
257                 LinkedHashMap<String, Object> entrySchemaMap = (LinkedHashMap<String, Object>) childNodeMap
258                     .get(ToscaSchemaConstants.ENTRY_SCHEMA);
259                 entrySchemaMap.entrySet().stream().forEach(entry -> {
260                     if (entry.getKey().equalsIgnoreCase(ToscaSchemaConstants.TYPE) && entry.getValue() != null) {
261                         String entrySchemaType = (String) entry.getValue();
262                         if (entrySchemaType.contains(ToscaSchemaConstants.POLICY_DATA)) {
263                             JSONArray array = new JSONArray();
264                             if (jsonEntrySchema.get(entrySchemaType) != null) {
265                                 // Already traversed
266                                 JSONObject entrySchemaObject = jsonEntrySchema.get(entrySchemaType);
267                                 attachEntrySchemaJsonObject(childObject, entrySchemaObject,
268                                     JsonEditorSchemaConstants.TYPE_OBJECT);
269                             } else if (dataNodes.containsKey(entrySchemaType)) {
270
271                                 JSONObject entrySchemaObject = new JSONObject();
272                                 // Need to traverse
273                                 ((LinkedHashMap<String, Object>) dataNodes.get(entrySchemaType)).entrySet().stream()
274                                     .forEach(pmap -> {
275                                         if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
276                                             parseToscaProperties(ToscaSchemaConstants.ENTRY_SCHEMA,
277                                                 (LinkedHashMap<String, Object>) pmap.getValue(), entrySchemaObject,
278                                                 array, jsonEntrySchema, dataNodes, incrementComplexTypeOrder());
279                                             jsonEntrySchema.put(entrySchemaType, entrySchemaObject);
280                                             dataNodes.remove(entrySchemaType);
281                                             attachEntrySchemaJsonObject(childObject, entrySchemaObject,
282                                                 JsonEditorSchemaConstants.TYPE_OBJECT);
283                                         }
284
285                                     });
286                             }
287                         } else if (entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)
288                             || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)
289                             || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
290                             JSONObject entrySchemaObject = new JSONObject();
291                             parseConstraints(entrySchemaMap, entrySchemaObject);
292                             String jsontype = JsonEditorSchemaConstants.TYPE_STRING;
293                             if (entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)
294                                 || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
295                                 jsontype = JsonEditorSchemaConstants.TYPE_INTEGER;
296                             }
297                             if (childNodeMap.get(ToscaSchemaConstants.TYPE) != null) {
298                                 // Only known value of type is String for now
299                                 if (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String) {
300                                     String typeValue = (String) childNodeMap.get(ToscaSchemaConstants.TYPE);
301                                     if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
302                                         // Custom key for JSON Editor and UI rendering
303                                         childObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT,
304                                             JsonEditorSchemaConstants.FORMAT_SELECT);
305                                         // childObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS,
306                                         // JsonEditorSchemaConstants.TRUE);
307                                     }
308                                 }
309                             }
310                             attachEntrySchemaJsonObject(childObject, entrySchemaObject, jsontype);
311                         }
312                     }
313                 });
314             }
315         }
316     }
317
318     private void attachEntrySchemaJsonObject(JSONObject childObject, JSONObject entrySchemaObject, String dataType) {
319
320         entrySchemaObject.put(JsonEditorSchemaConstants.TYPE, dataType);
321         childObject.put(JsonEditorSchemaConstants.ITEMS, entrySchemaObject);
322     }
323
324     @SuppressWarnings("unchecked")
325     private void attachTypeJsonObject(JSONObject childObject, JSONObject typeObject) {
326         Iterator<String> keys = typeObject.keys();
327         while (keys.hasNext()) {
328             String key = keys.next();
329             childObject.put(key, typeObject.get(key));
330         }
331     }
332
333     /*
334      * private String parseKey(String toscaKey, String lookupString) { return
335      * toscaKey.substring(toscaKey.indexOf(lookupString) + lookupString.length(),
336      * toscaKey.length()); }
337      */
338
339     private void parseDescription(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject) {
340         if (childNodeMap.get(ToscaSchemaConstants.DESCRIPTION) != null) {
341             childObject.put(JsonEditorSchemaConstants.TITLE, childNodeMap.get(ToscaSchemaConstants.DESCRIPTION));
342         }
343     }
344
345     private void parseTypes(String childObjectKey, LinkedHashMap<String, Object> childNodeMap, JSONObject childObject,
346         Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes, JSONArray array, int order) {
347         if (childNodeMap.get(ToscaSchemaConstants.TYPE) != null) {
348             // Only known value of type is String for now
349             if (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String) {
350                 childObject.put(JsonEditorSchemaConstants.PROPERTY_ORDER, order);
351                 String typeValue = (String) childNodeMap.get(ToscaSchemaConstants.TYPE);
352                 if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
353                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_INTEGER);
354
355                 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
356                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_INTEGER);
357                 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
358                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_ARRAY);
359                     // Custom key for JSON Editor and UI rendering
360                     childObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT,
361                         JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP);
362                     childObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS, JsonEditorSchemaConstants.TRUE);
363                 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_MAP)) {
364                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_OBJECT);
365                 } else if (typeValue.contains(ToscaSchemaConstants.POLICY_DATA)) {
366                     JSONArray childArray = new JSONArray();
367
368                     if (jsonEntrySchema.get(typeValue) != null) {
369                         // Already traversed
370                         JSONObject entrySchemaObject = jsonEntrySchema.get(typeValue);
371                         attachTypeJsonObject(childObject, entrySchemaObject);
372                     } else if (dataNodes.containsKey(typeValue)) {
373                         JSONObject entrySchemaObject = new JSONObject();
374                         // Need to traverse
375                         JSONArray jsonArray = new JSONArray();
376                         ((LinkedHashMap<String, Object>) dataNodes.get(typeValue)).entrySet().stream().forEach(pmap -> {
377                             if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
378
379                                 ((LinkedHashMap<String, Object>) pmap.getValue()).entrySet().stream().forEach(p -> {
380                                     if (p.getValue() instanceof Map) {
381                                         LinkedHashMap<String, Object> childNodeMap2 = (LinkedHashMap<String, Object>) p
382                                             .getValue();
383                                         if (childNodeMap2.containsKey(ToscaSchemaConstants.TYPE)
384                                             && (((String) childNodeMap2.get(ToscaSchemaConstants.TYPE))
385                                                 .contains(ToscaSchemaConstants.POLICY_DATA))) {
386                                         }
387                                     }
388                                 });
389                             }
390                         });
391                         ((LinkedHashMap<String, Object>) dataNodes.get(typeValue)).entrySet().stream().forEach(pmap -> {
392                             if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
393                                 parseToscaPropertiesForType(childObjectKey,
394                                     (LinkedHashMap<String, Object>) pmap.getValue(), entrySchemaObject, childArray,
395                                     jsonEntrySchema, dataNodes, true, incrementComplexSimpleTypeOrder());
396                                 jsonEntrySchema.put(typeValue, entrySchemaObject);
397                                 dataNodes.remove(typeValue);
398                                 attachTypeJsonObject(childObject, entrySchemaObject);
399                             }
400                         });
401                     }
402                 } else {
403                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_STRING);
404                 }
405             }
406             if (childNodeMap.get(ToscaSchemaConstants.DEFAULT) != null) {
407                 childObject.put(JsonEditorSchemaConstants.DEFAULT, childNodeMap.get(ToscaSchemaConstants.DEFAULT));
408             }
409         }
410     }
411
412     private void parseConstraints(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject) {
413         if (childNodeMap.containsKey(ToscaSchemaConstants.CONSTRAINTS)
414             && childNodeMap.get(ToscaSchemaConstants.CONSTRAINTS) != null) {
415             List<LinkedHashMap<String, Object>> constraintsList = (List<LinkedHashMap<String, Object>>) childNodeMap
416                 .get(ToscaSchemaConstants.CONSTRAINTS);
417             constraintsList.stream().forEach(c -> {
418                 if (c instanceof Map) {
419                     c.entrySet().stream().forEach(constraint -> {
420                         if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.MIN_LENGTH)
421                             || constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.GREATER_OR_EQUAL)) {
422                             // For String min_lenghth is minimum length whereas for number, it will be
423                             // minimum or greater than to the defined value
424                             if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
425                                 && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
426                                 && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
427                                     .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
428                                 childObject.put(JsonEditorSchemaConstants.MIN_LENGTH, constraint.getValue());
429                             } else {
430                                 childObject.put(JsonEditorSchemaConstants.MINIMUM, constraint.getValue());
431                             }
432                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.MAX_LENGTH)
433                             || constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.LESS_OR_EQUAL)) {
434                             // For String max_lenghth is maximum length whereas for number, it will be
435                             // maximum or less than the defined value
436                             if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
437                                 && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
438                                 && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
439                                     .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
440                                 childObject.put(JsonEditorSchemaConstants.MAX_LENGTH, constraint.getValue());
441                             } else {
442                                 childObject.put(JsonEditorSchemaConstants.MAXIMUM, constraint.getValue());
443                             }
444                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.LESS_THAN)) {
445                             childObject.put(JsonEditorSchemaConstants.EXCLUSIVE_MAXIMUM, constraint.getValue());
446                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.GREATER_THAN)) {
447                             childObject.put(JsonEditorSchemaConstants.EXCLUSIVE_MINIMUM, constraint.getValue());
448                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.IN_RANGE)) {
449                             if (constraint.getValue() instanceof ArrayList<?>) {
450                                 if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
451                                     && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
452                                     && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
453                                         .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
454                                     childObject.put(JsonEditorSchemaConstants.MIN_LENGTH,
455                                         ((ArrayList) constraint.getValue()).get(0));
456                                     childObject.put(JsonEditorSchemaConstants.MAX_LENGTH,
457                                         ((ArrayList) constraint.getValue()).get(1));
458                                 } else {
459                                     childObject.put(JsonEditorSchemaConstants.MINIMUM,
460                                         ((ArrayList) constraint.getValue()).get(0));
461                                     childObject.put(JsonEditorSchemaConstants.MAXIMUM,
462                                         ((ArrayList) constraint.getValue()).get(1));
463                                 }
464
465                             }
466                         } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.VALID_VALUES)) {
467                             JSONArray validValuesArray = new JSONArray();
468
469                             if (constraint.getValue() instanceof ArrayList<?>) {
470                                 boolean processDictionary = ((ArrayList<?>) constraint.getValue()).stream()
471                                     .anyMatch(value -> (value instanceof String
472                                         && ((String) value).contains(ToscaSchemaConstants.DICTIONARY)));
473                                 if (!processDictionary) {
474                                     ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
475                                         validValuesArray.put(value);
476                                     });
477                                     childObject.put(JsonEditorSchemaConstants.ENUM, validValuesArray);
478                                 } else {
479                                     ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
480                                         if ((value instanceof String
481                                             && ((String) value).contains(ToscaSchemaConstants.DICTIONARY))) {
482                                             processDictionaryElements(childObject, (String) value);
483                                         }
484
485                                     });
486
487                                 }
488                             }
489
490                         }
491                     });
492                 }
493             });
494         }
495     }
496
497     private void processDictionaryElements(JSONObject childObject, String dictionaryReference) {
498
499         if (dictionaryReference.contains("#")) {
500             String[] dictionaryKeyArray = dictionaryReference
501                 .substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
502                     dictionaryReference.length())
503                 .split("#");
504             // We support only one # as of now.
505             List<CldsDictionaryItem> cldsDictionaryElements = null;
506             List<CldsDictionaryItem> subDictionaryElements = null;
507             if (dictionaryKeyArray != null && dictionaryKeyArray.length == 2) {
508                 cldsDictionaryElements = getCldsDao().getDictionaryElements(dictionaryKeyArray[0], null, null);
509                 subDictionaryElements = getCldsDao().getDictionaryElements(dictionaryKeyArray[1], null, null);
510
511                 if (cldsDictionaryElements != null) {
512                     List<String> subCldsDictionaryNames = subDictionaryElements.stream()
513                         .map(CldsDictionaryItem::getDictElementShortName).collect(Collectors.toList());
514                     JSONArray jsonArray = new JSONArray();
515
516                     Optional.ofNullable(cldsDictionaryElements).get().stream().forEach(c -> {
517                         JSONObject jsonObject = new JSONObject();
518                         jsonObject.put(JsonEditorSchemaConstants.TYPE, getJsonType(c.getDictElementType()));
519                         if (c.getDictElementType() != null
520                             && c.getDictElementType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
521                             jsonObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1);
522                         }
523                         jsonObject.put(JsonEditorSchemaConstants.ID, c.getDictElementName());
524                         jsonObject.put(JsonEditorSchemaConstants.LABEL, c.getDictElementShortName());
525                         jsonObject.put(JsonEditorSchemaConstants.OPERATORS, subCldsDictionaryNames);
526                         jsonArray.put(jsonObject);
527                     });
528                     ;
529                     JSONObject filterObject = new JSONObject();
530                     filterObject.put(JsonEditorSchemaConstants.FILTERS, jsonArray);
531
532                     childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_QBLDR);
533                     // TO invoke validation on such parameters
534                     childObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1);
535                     childObject.put(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
536
537                 }
538             }
539         } else {
540             String dictionaryKey = dictionaryReference.substring(
541                 dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11, dictionaryReference.length());
542             if (dictionaryKey != null) {
543                 List<CldsDictionaryItem> cldsDictionaryElements = getCldsDao().getDictionaryElements(dictionaryKey,
544                     null, null);
545                 if (cldsDictionaryElements != null) {
546                     List<String> cldsDictionaryNames = new ArrayList<>();
547                     List<String> cldsDictionaryFullNames = new ArrayList<>();
548                     cldsDictionaryElements.stream().forEach(c -> {
549                         // Json type will be translated before Policy creation
550                         if (c.getDictElementType() != null && !c.getDictElementType().equalsIgnoreCase("json")) {
551                             cldsDictionaryFullNames.add(c.getDictElementName());
552                         }
553                         cldsDictionaryNames.add(c.getDictElementShortName());
554                     });
555
556                     if (cldsDictionaryFullNames.size() > 0) {
557                         childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryFullNames);
558                         // Add Enum titles for generated translated values during JSON instance
559                         // generation
560                         JSONObject enumTitles = new JSONObject();
561                         enumTitles.put(JsonEditorSchemaConstants.ENUM_TITLES, cldsDictionaryNames);
562                         childObject.put(JsonEditorSchemaConstants.OPTIONS, enumTitles);
563                     } else {
564                         childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryNames);
565                     }
566
567                 }
568             }
569         }
570     }
571
572     private String getJsonType(String toscaType) {
573         String jsonType = null;
574         if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
575             jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
576         } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
577             jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
578         } else {
579             jsonType = JsonEditorSchemaConstants.TYPE_STRING;
580         }
581         return jsonType;
582     }
583
584 }