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