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.policy.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.policy.clamp.clds.tosca.JsonEditorSchemaConstants;
33 import org.onap.policy.clamp.clds.tosca.ToscaSchemaConstants;
34 import org.onap.policy.clamp.clds.tosca.update.elements.ToscaElementProperty;
35 import org.onap.policy.clamp.clds.tosca.update.execution.ToscaMetadataExecutor;
36 import org.onap.policy.clamp.loop.service.Service;
37 import org.onap.policy.clamp.tosca.DictionaryElement;
38 import org.onap.policy.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);
66 private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap,
67 DictionaryService dictionaryService, Service serviceModel,
68 ToscaMetadataExecutor toscaMetadataExecutor) {
69 JsonObject childObject = new JsonObject();
70 if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA)
71 && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) {
72 ((LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA)).forEach((key,
74 if (key.equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) {
75 String[] multipleValues = ((String) value).split(",");
76 for (String multipleValue : multipleValues) {
77 if (multipleValue.contains(ToscaSchemaConstants.DICTIONARY)) {
78 processDictionaryElements(multipleValue, childObject, dictionaryService);
80 if (multipleValue.contains("ClampExecution:")) {
81 executeClampProcess(multipleValue.replaceAll("ClampExecution:", ""), childObject,
82 serviceModel, toscaMetadataExecutor);
92 private static void executeClampProcess(String processInfo, JsonObject childObject, Service serviceModel,
93 ToscaMetadataExecutor toscaMetadataExecutor) {
94 toscaMetadataExecutor.executeTheProcess(processInfo, childObject, serviceModel);
98 * For dictionary with multiple levels (defined by #).
100 * @param dictionaryKeyArray the array containing the different elements
101 * @param childObject the structure getting the new entries
102 * @param dictionaryService the dictionary service bean
104 private static void processComplexDictionaryElements(String[] dictionaryKeyArray, JsonObject childObject,
105 DictionaryService dictionaryService) {
106 // We support only one # as of now.
107 List<DictionaryElement> dictionaryElements = null;
108 if (dictionaryKeyArray.length == 2) {
109 dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0])
110 .getDictionaryElements());
111 JsonArray subDictionaryNames = new JsonArray();
112 new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1])
113 .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName()));
115 JsonArray jsonArray = new JsonArray();
117 Optional.of(dictionaryElements).get().forEach(c -> {
118 JsonObject jsonObject = new JsonObject();
119 jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType()));
120 if (c.getType() != null
121 && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) {
122 jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
125 jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
126 jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
127 jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
128 jsonArray.add(jsonObject);
131 JsonObject filterObject = new JsonObject();
132 filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);
134 childObject.addProperty(JsonEditorSchemaConstants.TYPE,
135 JsonEditorSchemaConstants.TYPE_QBLDR);
136 // TO invoke validation on such parameters
137 childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1);
138 childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject);
144 * For dictionary with single entry.
146 * @param dictionaryKeyArray the array containing the different elements
147 * @param childObject the structure getting the new entries
148 * @param dictionaryService the dictionary service bean
150 private static void processSimpleDictionaryElements(String[] dictionaryKeyArray, JsonObject childObject,
151 DictionaryService dictionaryService) {
152 JsonArray dictionaryNames = new JsonArray();
153 JsonArray dictionaryFullNames = new JsonArray();
154 dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements().forEach(c -> {
155 // Json type will be translated before Policy creation
156 if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) {
157 dictionaryFullNames.add(c.getName());
159 dictionaryNames.add(c.getShortName());
162 if (dictionaryFullNames.size() > 0) {
163 if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
164 childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryFullNames);
166 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
168 // Add Enum titles for generated translated values during JSON instance
170 JsonObject enumTitles = new JsonObject();
171 enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames);
172 if (childObject.get(JsonEditorSchemaConstants.OPTIONS) != null) {
173 childObject.get(JsonEditorSchemaConstants.OPTIONS).getAsJsonArray().add(enumTitles);
175 childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
179 if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
180 childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryNames);
182 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
187 private static void processDictionaryElements(String dictionaryReference, JsonObject childObject,
188 DictionaryService dictionaryService) {
189 String[] dictionaryKeyArray =
190 dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11,
191 dictionaryReference.length()).split("#");
192 if (dictionaryKeyArray.length > 1) {
193 processComplexDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
195 processSimpleDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
199 private static String getJsonType(String toscaType) {
200 String jsonType = null;
201 if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) {
202 jsonType = JsonEditorSchemaConstants.TYPE_INTEGER;
203 } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) {
204 jsonType = JsonEditorSchemaConstants.TYPE_ARRAY;
206 jsonType = JsonEditorSchemaConstants.TYPE_STRING;