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