2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.update.parser.metadata;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Optional;
32 import org.json.JSONArray;
33 import org.onap.clamp.clds.tosca.JsonEditorSchemaConstants;
34 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
35 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
36 import org.onap.clamp.tosca.DictionaryElement;
37 import org.onap.clamp.tosca.DictionaryService;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
42 public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser {
45 private DictionaryService dictionaryService;
48 * This method is used to start the processing of the metadata field.
50 * @param toscaElementProperty The property metadata as Json Object
51 * @return The jsonObject structure that must be added to the json schema
53 public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty) {
54 if (dictionaryService != null) {
55 return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService);
62 private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap,
63 DictionaryService dictionaryService) {
64 JsonObject childObject = new JsonObject();
65 if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA)
66 && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) {
67 LinkedHashMap<String, Object> metadataMap =
68 (LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA);
69 if (metadataMap != null) {
70 metadataMap.entrySet().stream().forEach(constraint -> {
71 if (constraint.getKey()
72 .equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) {
73 JSONArray validValuesArray = new JSONArray();
74 if (constraint.getValue() instanceof ArrayList<?>) {
75 boolean processDictionary = ((ArrayList<?>) constraint.getValue())
76 .stream().anyMatch(value -> (value instanceof String
77 && ((String) value).contains(ToscaSchemaConstants.DICTIONARY)));
78 if (processDictionary) {
79 ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> {
80 if ((value instanceof String && ((String) value)
81 .contains(ToscaSchemaConstants.DICTIONARY))) {
82 processDictionaryElements((String) value, childObject, dictionaryService);
95 private static void processDictionaryElements(String dictionaryReference, JsonObject childObject,
96 DictionaryService dictionaryService) {
97 String[] dictionaryKeyArray =
98 dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
99 dictionaryReference.length()).split("#");
100 if (dictionaryKeyArray.length > 1) {
101 // We support only one # as of now.
102 List<DictionaryElement> dictionaryElements = null;
103 if (dictionaryKeyArray.length == 2) {
104 dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0])
105 .getDictionaryElements());
106 JsonArray subDictionaryNames = new JsonArray();
107 new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1])
108 .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName()));
110 JsonArray jsonArray = new JsonArray();
112 Optional.of(dictionaryElements).get().stream().forEach(c -> {
113 JsonObject jsonObject = new JsonObject();
114 jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType()));
115 if (c.getType() != null
116 && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
117 jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
120 jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
121 jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
122 jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
123 jsonArray.add(jsonObject);
126 JsonObject filterObject = new JsonObject();
127 filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);
129 childObject.addProperty(JsonEditorSchemaConstants.TYPE,
130 JsonEditorSchemaConstants.TYPE_QBLDR);
131 // TO invoke validation on such parameters
132 childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
133 childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
138 List<DictionaryElement> dictionaryElements =
139 new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements());
140 JsonArray dictionaryNames = new JsonArray();
141 JsonArray dictionaryFullNames = new JsonArray();
142 dictionaryElements.stream().forEach(c -> {
143 // Json type will be translated before Policy creation
144 if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) {
145 dictionaryFullNames.add(c.getName());
147 dictionaryNames.add(c.getShortName());
150 if (dictionaryFullNames.size() > 0) {
151 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
152 // Add Enum titles for generated translated values during JSON instance
154 JsonObject enumTitles = new JsonObject();
155 enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames);
156 childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
159 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
164 private static String getJsonType(String toscaType) {
165 String jsonType = null;
166 if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
167 jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
169 else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
170 jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
173 jsonType = JsonEditorSchemaConstants.TYPE_STRING;