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