2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.tosca;
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;
32 import java.util.Optional;
33 import java.util.stream.Collectors;
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;
42 * Tosca Model Yaml parser and convertor to JSON Schema consumable for JSON
46 public class ToscaYamlToJsonConvertor {
48 private CldsDao cldsDao;
49 private int simpleTypeOrder = 1000;
50 private int complexTypeOrder = 10000;
51 private int complexSimpleTypeOrder = 1;
53 public ToscaYamlToJsonConvertor(CldsDao cldsDao) {
54 this.cldsDao = cldsDao;
57 private int incrementSimpleTypeOrder() {
58 return simpleTypeOrder++;
61 private int incrementComplexTypeOrder() {
62 return complexTypeOrder = complexTypeOrder + 10000;
65 private int incrementComplexSimpleTypeOrder() {
66 complexSimpleTypeOrder++;
67 return complexTypeOrder + complexSimpleTypeOrder;
73 public CldsDao getCldsDao() {
81 public void setCldsDao(CldsDao cldsDao) {
82 this.cldsDao = cldsDao;
85 public String parseToscaYaml(String yamlString) {
87 Yaml yaml = new Yaml();
88 LinkedHashMap<String, Object> loadedYaml = yaml.load(yamlString);
89 if (loadedYaml == null) {
92 LinkedHashMap<String, Object> nodeTypes = new LinkedHashMap<>();
93 LinkedHashMap<String, Object> dataNodes = new LinkedHashMap<>();
94 JSONObject jsonEditorObject = new JSONObject();
95 JSONObject jsonParentObject = new JSONObject();
96 JSONObject jsonTempObject = new JSONObject();
97 parseNodeAndDataType(loadedYaml, nodeTypes, dataNodes);
98 populateJsonEditorObject(loadedYaml, nodeTypes, dataNodes, jsonParentObject, jsonTempObject);
99 if (jsonTempObject.length() > 0) {
100 jsonParentObject = jsonTempObject;
102 jsonEditorObject.put(JsonEditorSchemaConstants.SCHEMA, jsonParentObject);
103 return jsonEditorObject.toString();
106 // Parse node_type and data_type
107 @SuppressWarnings("unchecked")
108 private void parseNodeAndDataType(LinkedHashMap<String, Object> map, LinkedHashMap<String, Object> nodeTypes,
109 LinkedHashMap<String, Object> dataNodes) {
110 map.entrySet().stream().forEach(n -> {
111 if (n.getKey().contains(ToscaSchemaConstants.NODE_TYPES) && n.getValue() instanceof Map) {
113 parseNodeAndDataType((LinkedHashMap<String, Object>) n.getValue(), nodeTypes, dataNodes);
115 } else if (n.getKey().contains(ToscaSchemaConstants.DATA_TYPES) && n.getValue() instanceof Map) {
117 parseNodeAndDataType((LinkedHashMap<String, Object>) n.getValue(), nodeTypes, dataNodes);
119 } else if (n.getKey().contains(ToscaSchemaConstants.POLICY_NODE)) {
121 nodeTypes.put(n.getKey(), n.getValue());
123 } else if (n.getKey().contains(ToscaSchemaConstants.POLICY_DATA)) {
125 dataNodes.put(n.getKey(), n.getValue());
131 @SuppressWarnings("unchecked")
132 private void populateJsonEditorObject(LinkedHashMap<String, Object> map, LinkedHashMap<String, Object> nodeTypes,
133 LinkedHashMap<String, Object> dataNodes, JSONObject jsonParentObject, JSONObject jsonTempObject) {
135 Map<String, JSONObject> jsonEntrySchema = new HashMap();
136 jsonParentObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_OBJECT);
137 nodeTypes.entrySet().stream().forEach(nt -> {
138 if (nt.getValue() instanceof Map) {
139 ((LinkedHashMap<String, Object>) nt.getValue()).entrySet().forEach(ntElement -> {
140 if (ntElement.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
141 JSONArray rootNodeArray = new JSONArray();
142 if (ntElement.getValue() instanceof Map) {
143 ((LinkedHashMap<String, Object>) ntElement.getValue()).entrySet()
144 .forEach((ntPropertiesElement) -> {
145 boolean isListNode = false;
146 parseDescription((LinkedHashMap<String, Object>) ntPropertiesElement.getValue(),
148 LinkedHashMap<String, Object> parentPropertiesMap = (LinkedHashMap<String, Object>) ntPropertiesElement
150 if (parentPropertiesMap.containsKey(ToscaSchemaConstants.TYPE)
151 && ((String) parentPropertiesMap.get(ToscaSchemaConstants.TYPE))
152 .contains(ToscaSchemaConstants.TYPE_LIST)
153 && parentPropertiesMap.containsKey(ToscaSchemaConstants.ENTRY_SCHEMA)) {
154 parentPropertiesMap = (LinkedHashMap<String, Object>) parentPropertiesMap
155 .get(ToscaSchemaConstants.ENTRY_SCHEMA);
158 if (parentPropertiesMap.containsKey(ToscaSchemaConstants.TYPE)
159 && ((String) parentPropertiesMap.get(ToscaSchemaConstants.TYPE))
160 .contains(ToscaSchemaConstants.POLICY_DATA)) {
161 ((LinkedHashMap<String, Object>) dataNodes
162 .get(parentPropertiesMap.get(ToscaSchemaConstants.TYPE))).entrySet()
163 .stream().forEach(pmap -> {
165 .equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
166 parseToscaProperties(ToscaSchemaConstants.POLICY_NODE,
167 (LinkedHashMap<String, Object>) pmap.getValue(),
168 jsonParentObject, rootNodeArray, jsonEntrySchema, dataNodes,
169 incrementSimpleTypeOrder());
176 jsonTempObject.put(JsonEditorSchemaConstants.TYPE,
177 JsonEditorSchemaConstants.TYPE_ARRAY);
178 parseDescription((LinkedHashMap<String, Object>) ntPropertiesElement.getValue(),
180 jsonTempObject.put(JsonEditorSchemaConstants.ITEMS, jsonParentObject);
181 jsonTempObject.put(JsonEditorSchemaConstants.FORMAT,
182 JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP);
183 jsonTempObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS,
184 JsonEditorSchemaConstants.TRUE);
194 @SuppressWarnings("unchecked")
195 private void parseToscaProperties(String parentKey, LinkedHashMap<String, Object> propertiesMap,
196 JSONObject jsonDataNode, JSONArray array, Map<String, JSONObject> jsonEntrySchema,
197 LinkedHashMap<String, Object> dataNodes, final int order) {
198 JSONObject jsonPropertyNode = new JSONObject();
199 propertiesMap.entrySet().stream().forEach(p -> {
200 // Populate JSON Array for "required" key
202 if (p.getValue() instanceof Map) {
203 LinkedHashMap<String, Object> nodeMap = (LinkedHashMap<String, Object>) p.getValue();
204 if (nodeMap.containsKey(ToscaSchemaConstants.REQUIRED)
205 && ((boolean) nodeMap.get(ToscaSchemaConstants.REQUIRED))) {
206 array.put(p.getKey());
208 // if(nodeMap.containsKey(ToscaSchemaConstants.CONSTRAINTS))
209 parseToscaChildNodeMap(p.getKey(), nodeMap, jsonPropertyNode, jsonEntrySchema, dataNodes, array,
210 incrementSimpleTypeOrder());
213 jsonDataNode.put(JsonEditorSchemaConstants.REQUIRED, array);
214 jsonDataNode.put(JsonEditorSchemaConstants.PROPERTIES, jsonPropertyNode);
217 @SuppressWarnings("unchecked")
218 private void parseToscaPropertiesForType(String parentKey, LinkedHashMap<String, Object> propertiesMap,
219 JSONObject jsonDataNode, JSONArray array, Map<String, JSONObject> jsonEntrySchema,
220 LinkedHashMap<String, Object> dataNodes, boolean isType, int order) {
221 JSONObject jsonPropertyNode = new JSONObject();
223 propertiesMap.entrySet().stream().forEach(p -> {
224 // array.put(p.getKey());
225 boolean overWriteArray = false;
226 if (p.getValue() instanceof Map) {
227 LinkedHashMap<String, Object> nodeMap = (LinkedHashMap<String, Object>) p.getValue();
228 if (!(parentKey.contains(ToscaSchemaConstants.ENTRY_SCHEMA)
229 || parentKey.contains(ToscaSchemaConstants.POLICY_NODE))
230 && nodeMap.containsKey(ToscaSchemaConstants.TYPE)
231 && (((String) nodeMap.get(ToscaSchemaConstants.TYPE)).contains(ToscaSchemaConstants.POLICY_DATA))) {
232 overWriteArray = true;
234 if (nodeMap.containsKey(ToscaSchemaConstants.REQUIRED)
235 && ((boolean) nodeMap.get(ToscaSchemaConstants.REQUIRED))) {
236 array.put(p.getKey());
238 parseToscaChildNodeMap(p.getKey(), nodeMap, jsonPropertyNode, jsonEntrySchema, dataNodes, array, order);
241 jsonDataNode.put(JsonEditorSchemaConstants.REQUIRED, array);
242 jsonDataNode.put(JsonEditorSchemaConstants.PROPERTIES, jsonPropertyNode);
245 private void parseToscaChildNodeMap(String childObjectKey, LinkedHashMap<String, Object> childNodeMap,
246 JSONObject jsonPropertyNode, Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes,
247 JSONArray array, int order) {
248 JSONObject childObject = new JSONObject();
249 // JSONArray childArray = new JSONArray();
250 parseDescription(childNodeMap, childObject);
251 parseTypes(childObjectKey, childNodeMap, childObject, jsonEntrySchema, dataNodes, array, order);
252 parseConstraints(childNodeMap, childObject);
253 parseEntrySchema(childNodeMap, childObject, jsonPropertyNode, jsonEntrySchema, dataNodes);
255 jsonPropertyNode.put(childObjectKey, childObject);
260 private void parseEntrySchema(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject,
261 JSONObject jsonPropertyNode, Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes) {
262 if (childNodeMap.get(ToscaSchemaConstants.ENTRY_SCHEMA) != null) {
263 if (childNodeMap.get(ToscaSchemaConstants.ENTRY_SCHEMA) instanceof Map) {
264 LinkedHashMap<String, Object> entrySchemaMap = (LinkedHashMap<String, Object>) childNodeMap
265 .get(ToscaSchemaConstants.ENTRY_SCHEMA);
266 entrySchemaMap.entrySet().stream().forEach(entry -> {
267 if (entry.getKey().equalsIgnoreCase(ToscaSchemaConstants.TYPE) && entry.getValue() != null) {
268 String entrySchemaType = (String) entry.getValue();
269 if (entrySchemaType.contains(ToscaSchemaConstants.POLICY_DATA)) {
270 JSONArray array = new JSONArray();
271 if (jsonEntrySchema.get(entrySchemaType) != null) {
273 JSONObject entrySchemaObject = jsonEntrySchema.get(entrySchemaType);
274 attachEntrySchemaJsonObject(childObject, entrySchemaObject,
275 JsonEditorSchemaConstants.TYPE_OBJECT);
276 } else if (dataNodes.containsKey(entrySchemaType)) {
278 JSONObject entrySchemaObject = new JSONObject();
280 ((LinkedHashMap<String, Object>) dataNodes.get(entrySchemaType)).entrySet().stream()
282 if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
283 parseToscaProperties(ToscaSchemaConstants.ENTRY_SCHEMA,
284 (LinkedHashMap<String, Object>) pmap.getValue(), entrySchemaObject,
285 array, jsonEntrySchema, dataNodes, incrementComplexTypeOrder());
286 jsonEntrySchema.put(entrySchemaType, entrySchemaObject);
287 dataNodes.remove(entrySchemaType);
288 attachEntrySchemaJsonObject(childObject, entrySchemaObject,
289 JsonEditorSchemaConstants.TYPE_OBJECT);
294 } else if (entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)
295 || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)
296 || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
297 JSONObject entrySchemaObject = new JSONObject();
298 parseConstraints(entrySchemaMap, entrySchemaObject);
299 String jsontype = JsonEditorSchemaConstants.TYPE_STRING;
300 if (entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)
301 || entrySchemaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
302 jsontype = JsonEditorSchemaConstants.TYPE_INTEGER;
304 if (childNodeMap.get(ToscaSchemaConstants.TYPE) != null) {
305 // Only known value of type is String for now
306 if (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String) {
307 String typeValue = (String) childNodeMap.get(ToscaSchemaConstants.TYPE);
308 if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
309 // Custom key for JSON Editor and UI rendering
310 childObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT,
311 JsonEditorSchemaConstants.FORMAT_SELECT);
312 // childObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS,
313 // JsonEditorSchemaConstants.TRUE);
317 attachEntrySchemaJsonObject(childObject, entrySchemaObject, jsontype);
325 private void attachEntrySchemaJsonObject(JSONObject childObject, JSONObject entrySchemaObject, String dataType) {
327 entrySchemaObject.put(JsonEditorSchemaConstants.TYPE, dataType);
328 childObject.put(JsonEditorSchemaConstants.ITEMS, entrySchemaObject);
331 @SuppressWarnings("unchecked")
332 private void attachTypeJsonObject(JSONObject childObject, JSONObject typeObject) {
333 Iterator<String> keys = typeObject.keys();
334 while (keys.hasNext()) {
335 String key = keys.next();
336 childObject.put(key, typeObject.get(key));
341 * private String parseKey(String toscaKey, String lookupString) { return
342 * toscaKey.substring(toscaKey.indexOf(lookupString) + lookupString.length(),
343 * toscaKey.length()); }
346 private void parseDescription(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject) {
347 if (childNodeMap.get(ToscaSchemaConstants.DESCRIPTION) != null) {
348 childObject.put(JsonEditorSchemaConstants.TITLE, childNodeMap.get(ToscaSchemaConstants.DESCRIPTION));
352 private void parseTypes(String childObjectKey, LinkedHashMap<String, Object> childNodeMap, JSONObject childObject,
353 Map<String, JSONObject> jsonEntrySchema, LinkedHashMap<String, Object> dataNodes, JSONArray array, int order) {
354 if (childNodeMap.get(ToscaSchemaConstants.TYPE) != null) {
355 // Only known value of type is String for now
356 if (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String) {
357 childObject.put(JsonEditorSchemaConstants.PROPERTY_ORDER, order);
358 String typeValue = (String) childNodeMap.get(ToscaSchemaConstants.TYPE);
359 if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
360 childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_INTEGER);
362 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_FLOAT)) {
363 childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_INTEGER);
364 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
365 childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_ARRAY);
366 // Custom key for JSON Editor and UI rendering
367 childObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT,
368 JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP);
369 childObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS, JsonEditorSchemaConstants.TRUE);
370 } else if (typeValue.equalsIgnoreCase(ToscaSchemaConstants.TYPE_MAP)) {
371 childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_OBJECT);
372 } else if (typeValue.contains(ToscaSchemaConstants.POLICY_DATA)) {
373 JSONArray childArray = new JSONArray();
375 if (jsonEntrySchema.get(typeValue) != null) {
377 JSONObject entrySchemaObject = jsonEntrySchema.get(typeValue);
378 attachTypeJsonObject(childObject, entrySchemaObject);
379 } else if (dataNodes.containsKey(typeValue)) {
380 JSONObject entrySchemaObject = new JSONObject();
382 JSONArray jsonArray = new JSONArray();
383 ((LinkedHashMap<String, Object>) dataNodes.get(typeValue)).entrySet().stream().forEach(pmap -> {
384 if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
386 ((LinkedHashMap<String, Object>) pmap.getValue()).entrySet().stream().forEach(p -> {
387 if (p.getValue() instanceof Map) {
388 LinkedHashMap<String, Object> childNodeMap2 = (LinkedHashMap<String, Object>) p
390 if (childNodeMap2.containsKey(ToscaSchemaConstants.TYPE)
391 && (((String) childNodeMap2.get(ToscaSchemaConstants.TYPE))
392 .contains(ToscaSchemaConstants.POLICY_DATA))) {
398 ((LinkedHashMap<String, Object>) dataNodes.get(typeValue)).entrySet().stream().forEach(pmap -> {
399 if (pmap.getKey().equalsIgnoreCase(ToscaSchemaConstants.PROPERTIES)) {
400 parseToscaPropertiesForType(childObjectKey,
401 (LinkedHashMap<String, Object>) pmap.getValue(), entrySchemaObject, childArray,
402 jsonEntrySchema, dataNodes, true, incrementComplexSimpleTypeOrder());
403 jsonEntrySchema.put(typeValue, entrySchemaObject);
404 dataNodes.remove(typeValue);
405 attachTypeJsonObject(childObject, entrySchemaObject);
410 childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_STRING);
413 if (childNodeMap.get(ToscaSchemaConstants.DEFAULT) != null) {
414 childObject.put(JsonEditorSchemaConstants.DEFAULT, childNodeMap.get(ToscaSchemaConstants.DEFAULT));
419 private void parseConstraints(LinkedHashMap<String, Object> childNodeMap, JSONObject childObject) {
420 if (childNodeMap.containsKey(ToscaSchemaConstants.CONSTRAINTS)
421 && childNodeMap.get(ToscaSchemaConstants.CONSTRAINTS) != null) {
422 List<LinkedHashMap<String, Object>> constraintsList = (List<LinkedHashMap<String, Object>>) childNodeMap
423 .get(ToscaSchemaConstants.CONSTRAINTS);
424 constraintsList.stream().forEach(c -> {
425 if (c instanceof Map) {
426 c.entrySet().stream().forEach(constraint -> {
427 if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.MIN_LENGTH)
428 || constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.GREATER_OR_EQUAL)) {
429 // For String min_lenghth is minimum length whereas for number, it will be
430 // minimum or greater than to the defined value
431 if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
432 && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
433 && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
434 .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
435 childObject.put(JsonEditorSchemaConstants.MIN_LENGTH, constraint.getValue());
437 childObject.put(JsonEditorSchemaConstants.MINIMUM, constraint.getValue());
439 } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.MAX_LENGTH)
440 || constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.LESS_OR_EQUAL)) {
441 // For String max_lenghth is maximum length whereas for number, it will be
442 // maximum or less than the defined value
443 if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
444 && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
445 && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
446 .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
447 childObject.put(JsonEditorSchemaConstants.MAX_LENGTH, constraint.getValue());
449 childObject.put(JsonEditorSchemaConstants.MAXIMUM, constraint.getValue());
451 } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.LESS_THAN)) {
452 childObject.put(JsonEditorSchemaConstants.EXCLUSIVE_MAXIMUM, constraint.getValue());
453 } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.GREATER_THAN)) {
454 childObject.put(JsonEditorSchemaConstants.EXCLUSIVE_MINIMUM, constraint.getValue());
455 } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.IN_RANGE)) {
456 if (constraint.getValue() instanceof ArrayList<?>) {
457 if (childNodeMap.containsKey(ToscaSchemaConstants.TYPE)
458 && (childNodeMap.get(ToscaSchemaConstants.TYPE) instanceof String)
459 && ((String) childNodeMap.get(ToscaSchemaConstants.TYPE))
460 .equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
461 childObject.put(JsonEditorSchemaConstants.MIN_LENGTH,
462 ((ArrayList) constraint.getValue()).get(0));
463 childObject.put(JsonEditorSchemaConstants.MAX_LENGTH,
464 ((ArrayList) constraint.getValue()).get(1));
466 childObject.put(JsonEditorSchemaConstants.MINIMUM,
467 ((ArrayList) constraint.getValue()).get(0));
468 childObject.put(JsonEditorSchemaConstants.MAXIMUM,
469 ((ArrayList) constraint.getValue()).get(1));
473 } else if (constraint.getKey().equalsIgnoreCase(ToscaSchemaConstants.VALID_VALUES)) {
474 JSONArray validValuesArray = new JSONArray();
476 if (constraint.getValue() instanceof ArrayList<?>) {
477 boolean processDictionary = ((ArrayList<?>) constraint.getValue()).stream()
478 .anyMatch(value -> (value instanceof String
479 && ((String) value).contains(ToscaSchemaConstants.DICTIONARY)));
480 if (!processDictionary) {
481 ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
482 validValuesArray.put(value);
484 childObject.put(JsonEditorSchemaConstants.ENUM, validValuesArray);
486 ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
487 if ((value instanceof String
488 && ((String) value).contains(ToscaSchemaConstants.DICTIONARY))) {
489 processDictionaryElements(childObject, (String) value);
504 private void processDictionaryElements(JSONObject childObject, String dictionaryReference) {
506 if (dictionaryReference.contains("#")) {
507 String[] dictionaryKeyArray = dictionaryReference
508 .substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
509 dictionaryReference.length())
511 // We support only one # as of now.
512 List<CldsDictionaryItem> cldsDictionaryElements = null;
513 List<CldsDictionaryItem> subDictionaryElements = null;
514 if (dictionaryKeyArray != null && dictionaryKeyArray.length == 2) {
515 cldsDictionaryElements = getCldsDao().getDictionaryElements(dictionaryKeyArray[0], null, null);
516 subDictionaryElements = getCldsDao().getDictionaryElements(dictionaryKeyArray[1], null, null);
518 if (cldsDictionaryElements != null) {
519 List<String> subCldsDictionaryNames = subDictionaryElements.stream()
520 .map(CldsDictionaryItem::getDictElementShortName).collect(Collectors.toList());
521 JSONArray jsonArray = new JSONArray();
523 Optional.ofNullable(cldsDictionaryElements).get().stream().forEach(c -> {
524 JSONObject jsonObject = new JSONObject();
525 jsonObject.put(JsonEditorSchemaConstants.TYPE, getJsonType(c.getDictElementType()));
526 if (c.getDictElementType() != null
527 && c.getDictElementType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
528 jsonObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1);
530 jsonObject.put(JsonEditorSchemaConstants.ID, c.getDictElementName());
531 jsonObject.put(JsonEditorSchemaConstants.LABEL, c.getDictElementShortName());
532 jsonObject.put(JsonEditorSchemaConstants.OPERATORS, subCldsDictionaryNames);
533 jsonArray.put(jsonObject);
536 JSONObject filterObject = new JSONObject();
537 filterObject.put(JsonEditorSchemaConstants.FILTERS, jsonArray);
539 childObject.put(JsonEditorSchemaConstants.TYPE, JsonEditorSchemaConstants.TYPE_QBLDR);
540 // TO invoke validation on such parameters
541 childObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1);
542 childObject.put(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
547 String dictionaryKey = dictionaryReference.substring(
548 dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11, dictionaryReference.length());
549 if (dictionaryKey != null) {
550 List<CldsDictionaryItem> cldsDictionaryElements = getCldsDao().getDictionaryElements(dictionaryKey,
552 if (cldsDictionaryElements != null) {
553 List<String> cldsDictionaryNames = new ArrayList<>();
554 List<String> cldsDictionaryFullNames = new ArrayList<>();
555 cldsDictionaryElements.stream().forEach(c -> {
556 // Json type will be translated before Policy creation
557 if (c.getDictElementType() != null && !c.getDictElementType().equalsIgnoreCase("json")) {
558 cldsDictionaryFullNames.add(c.getDictElementName());
560 cldsDictionaryNames.add(c.getDictElementShortName());
563 if (cldsDictionaryFullNames.size() > 0) {
564 childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryFullNames);
565 // Add Enum titles for generated translated values during JSON instance
567 JSONObject enumTitles = new JSONObject();
568 enumTitles.put(JsonEditorSchemaConstants.ENUM_TITLES, cldsDictionaryNames);
569 childObject.put(JsonEditorSchemaConstants.OPTIONS, enumTitles);
571 childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryNames);
579 private String getJsonType(String toscaType) {
580 String jsonType = null;
581 if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
582 jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
583 } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
584 jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
586 jsonType = JsonEditorSchemaConstants.TYPE_STRING;