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.onap.clamp.clds.tosca.JsonEditorSchemaConstants;
33 import org.onap.clamp.clds.tosca.ToscaSchemaConstants;
34 import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty;
35 import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataExecutor;
36 import org.onap.clamp.loop.service.Service;
37 import org.onap.clamp.tosca.DictionaryElement;
38 import org.onap.clamp.tosca.DictionaryService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
43 public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser {
46 private ToscaMetadataExecutor toscaMetadataExecutor;
49 private DictionaryService dictionaryService;
52 * This method is used to start the processing of the metadata field.
54 * @param toscaElementProperty The property metadata as Json Object
55 * @return The jsonObject structure that must be added to the json schema
57 public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty, Service serviceModel) {
58 if (dictionaryService != null) {
59 return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService, serviceModel,
60 toscaMetadataExecutor);
67 private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap,
68 DictionaryService dictionaryService, Service serviceModel,
69 ToscaMetadataExecutor toscaMetadataExecutor) {
70 JsonObject childObject = new JsonObject();
71 if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA)
72 && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) {
73 ((LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA)).forEach((key,
75 if (key.equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) {
76 String[] multipleValues = ((String) value).split(",");
77 for (String multipleValue : multipleValues) {
78 if (multipleValue.contains(ToscaSchemaConstants.DICTIONARY)) {
79 processDictionaryElements(multipleValue, childObject, dictionaryService);
81 if (multipleValue.contains("ClampExecution:")) {
82 executeClampProcess(multipleValue.replaceAll("ClampExecution:", ""), childObject,
83 serviceModel, toscaMetadataExecutor);
93 private static void executeClampProcess(String processInfo, JsonObject childObject, Service serviceModel,
94 ToscaMetadataExecutor toscaMetadataExecutor) {
95 toscaMetadataExecutor.executeTheProcess(processInfo, childObject, serviceModel);
99 * For dictionary with multiple levels (defined by #).
101 * @param dictionaryKeyArray the array containing the different elements
102 * @param childObject the structure getting the new entries
103 * @param dictionaryService the dictionary service bean
105 private static void processComplexDictionaryElements(String[] dictionaryKeyArray, JsonObject childObject,
106 DictionaryService dictionaryService) {
107 // We support only one # as of now.
108 List<DictionaryElement> dictionaryElements = null;
109 if (dictionaryKeyArray.length == 2) {
110 dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0])
111 .getDictionaryElements());
112 JsonArray subDictionaryNames = new JsonArray();
113 new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1])
114 .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName()));
116 JsonArray jsonArray = new JsonArray();
118 Optional.of(dictionaryElements).get().forEach(c -> {
119 JsonObject jsonObject = new JsonObject();
120 jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType()));
121 if (c.getType() != null
122 && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
123 jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
126 jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
127 jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
128 jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
129 jsonArray.add(jsonObject);
132 JsonObject filterObject = new JsonObject();
133 filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);
135 childObject.addProperty(JsonEditorSchemaConstants.TYPE,
136 JsonEditorSchemaConstants.TYPE_QBLDR);
137 // TO invoke validation on such parameters
138 childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
139 childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
145 * For dictionary with single entry.
147 * @param dictionaryKeyArray the array containing the different elements
148 * @param childObject the structure getting the new entries
149 * @param dictionaryService the dictionary service bean
151 private static void processSimpleDictionaryElements(String[] dictionaryKeyArray, JsonObject childObject,
152 DictionaryService dictionaryService) {
153 JsonArray dictionaryNames = new JsonArray();
154 JsonArray dictionaryFullNames = new JsonArray();
155 dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements().forEach(c -> {
156 // Json type will be translated before Policy creation
157 if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) {
158 dictionaryFullNames.add(c.getName());
160 dictionaryNames.add(c.getShortName());
163 if (dictionaryFullNames.size() > 0) {
164 if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
165 childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryFullNames);
168 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
170 // Add Enum titles for generated translated values during JSON instance
172 JsonObject enumTitles = new JsonObject();
173 enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames);
174 if (childObject.get(JsonEditorSchemaConstants.OPTIONS) != null) {
175 childObject.get(JsonEditorSchemaConstants.OPTIONS).getAsJsonArray().add(enumTitles);
178 childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
183 if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
184 childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryNames);
187 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
192 private static void processDictionaryElements(String dictionaryReference, JsonObject childObject,
193 DictionaryService dictionaryService) {
194 String[] dictionaryKeyArray =
195 dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
196 dictionaryReference.length()).split("#");
197 if (dictionaryKeyArray.length > 1) {
198 processComplexDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
201 processSimpleDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
205 private static String getJsonType(String toscaType) {
206 String jsonType = null;
207 if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
208 jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
210 else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
211 jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
214 jsonType = JsonEditorSchemaConstants.TYPE_STRING;