3fdd192d6ec6896a84368f2f000cdaf97b273c73
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 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.update.parser.metadata;
25
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;
41
42 @Component
43 public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser {
44
45     @Autowired
46     private ToscaMetadataExecutor toscaMetadataExecutor;
47
48     @Autowired
49     private DictionaryService dictionaryService;
50
51     /**
52      * This method is used to start the processing of the metadata field.
53      *
54      * @param toscaElementProperty The property metadata as Json Object
55      * @return The jsonObject structure that must be added to the json schema
56      */
57     public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty, Service serviceModel) {
58         if (dictionaryService != null) {
59             return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService, serviceModel,
60                     toscaMetadataExecutor);
61         } else {
62             return null;
63         }
64     }
65
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,
73                                                                                                        value) -> {
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);
79                         }
80                         if (multipleValue.contains("ClampExecution:")) {
81                             executeClampProcess(multipleValue.replaceAll("ClampExecution:", ""), childObject,
82                                     serviceModel, toscaMetadataExecutor);
83                         }
84                     }
85
86                 }
87             });
88         }
89         return childObject;
90     }
91
92     private static void executeClampProcess(String processInfo, JsonObject childObject, Service serviceModel,
93                                             ToscaMetadataExecutor toscaMetadataExecutor) {
94         toscaMetadataExecutor.executeTheProcess(processInfo, childObject, serviceModel);
95     }
96
97     /**
98      * For dictionary with multiple levels (defined by #).
99      *
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
103      */
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()));
114
115             JsonArray jsonArray = new JsonArray();
116
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);
123
124                 }
125                 jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName());
126                 jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName());
127                 jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames);
128                 jsonArray.add(jsonObject);
129             });
130
131             JsonObject filterObject = new JsonObject();
132             filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray);
133
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);
139
140         }
141     }
142
143     /**
144      * For dictionary with single entry.
145      *
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
149      */
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());
158             }
159             dictionaryNames.add(c.getShortName());
160         });
161
162         if (dictionaryFullNames.size() > 0) {
163             if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
164                 childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryFullNames);
165             } else {
166                 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames);
167             }
168             // Add Enum titles for generated translated values during JSON instance
169             // generation
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);
174             } else {
175                 childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles);
176             }
177
178         } else {
179             if (childObject.get(JsonEditorSchemaConstants.ENUM) != null) {
180                 childObject.get(JsonEditorSchemaConstants.ENUM).getAsJsonArray().add(dictionaryNames);
181             } else {
182                 childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames);
183             }
184         }
185     }
186
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);
194         } else {
195             processSimpleDictionaryElements(dictionaryKeyArray, childObject, dictionaryService);
196         }
197     }
198
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;
205         } else {
206             jsonType = JsonEditorSchemaConstants.TYPE_STRING;
207         }
208         return jsonType;
209     }
210
211 }