Merge "Fix the tosca converter"
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / ToscaConverterToJson.java
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;
25
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.LinkedHashMap;
31 import java.util.Map.Entry;
32 import org.onap.clamp.tosca.DictionaryService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class ToscaConverterToJson {
38     private LinkedHashMap<String, ToscaElement> components;
39     private LinkedHashMap<String, Template> templates;
40
41     // if this one is set, the dictionary mechanism is enabled
42     @Autowired
43     private DictionaryService dictionaryService;
44
45     public ToscaConverterToJson(LinkedHashMap<String, ToscaElement> components, LinkedHashMap<String, Template> templates) {
46         this.components = components;
47         this.templates = templates;
48     }
49
50     /**
51      * For a given component, launch process to parse it in Json.
52      *
53      * @param nameComponent name components
54      * @return return
55      */
56     public JsonObject getJsonProcess(String nameComponent, String typeComponent) {
57         JsonObject glob = new JsonObject();
58
59         if (typeComponent.equals("object")) {
60             glob = this.getFieldAsObject(matchComponent(nameComponent));
61         }
62         else {
63             /*glob = this.getFieldAsArray(matchComponent(nameComponent));*/
64         }
65
66         return glob;
67     }
68
69     /**
70      * Return the classical/general fields of the component, & launch the properties deployment.
71      *
72      * @param toscaElement the compo
73      * @return a json object
74      */
75     public JsonObject getFieldAsObject(ToscaElement toscaElement) {
76
77         JsonObject globalFields = new JsonObject();
78         if (templates.get("object").hasFields("title")) {
79             globalFields.addProperty("title", toscaElement.getName());
80         }
81         if (templates.get("object").hasFields("type")) {
82             globalFields.addProperty("type", "object");
83         }
84         if (templates.get("object").hasFields("description")) {
85             if (toscaElement.getDescription() != null) {
86                 globalFields.addProperty("description", toscaElement.getDescription());
87             }
88         }
89         if (templates.get("object").hasFields("required")) {
90             globalFields.add("required", this.getRequirements(toscaElement.getName()));
91         }
92         if (templates.get("object").hasFields("properties")) {
93             globalFields.add("properties", this.deploy(toscaElement.getName()));
94         }
95         return globalFields;
96     }
97
98     /**
99      * Get the required properties of the Component, including the parents properties requirements.
100      *
101      * @param nameComponent name component
102      * @return a json array
103      */
104     public JsonArray getRequirements(String nameComponent) {
105         JsonArray requirements = new JsonArray();
106         ToscaElement toParse = components.get(nameComponent);
107         //Check for a father component, and launch the same process
108         if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
109                 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
110             requirements.addAll(getRequirements(toParse.getDerivedFrom()));
111         }
112         //Each property is checked, and add to the requirement array if it's required
113         Collection<Property> properties = toParse.getProperties().values();
114         for (Property property : properties) {
115             if (property.getItems().containsKey("required")
116                     && property.getItems().get("required").equals(true)) {
117                 requirements.add(property.getName());
118             }
119         }
120         return requirements;
121     }
122
123     /**
124      * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise
125      * deploy and parse the properties.
126      *
127      * @param nameComponent name component
128      * @return a json object
129      */
130     public JsonObject deploy(String nameComponent) {
131         JsonObject jsonSchema = new JsonObject();
132         ToscaElement toParse = components.get(nameComponent);
133         //Check for a father component, and launch the same process
134         if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
135                 && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
136             jsonSchema = this.getParent(toParse.getDerivedFrom());
137         }
138         //For each component property, check if its a complex properties (a component) or not. In that case,
139         //launch the analyse of the property.
140         for (Entry<String, Property> property : toParse.getProperties().entrySet()) {
141             if (matchComponent((String) property.getValue().getItems().get("type")) != null) {
142                 jsonSchema.add(property.getValue().getName(),
143                         this.getJsonProcess((String) property.getValue().getItems().get("type"), "object"));
144             }
145             else {
146                 jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue()));
147             }
148         }
149         return jsonSchema;
150     }
151
152     /**
153      * If a component has a parent, it is deploy in the same way.
154      *
155      * @param nameComponent name component
156      * @return a json object
157      */
158     public JsonObject getParent(String nameComponent) {
159         return deploy(nameComponent);
160     }
161
162     /**
163      * to be done.
164      *
165      * @param property property
166      * @return a json object
167      */
168     @SuppressWarnings("unchecked")
169     public JsonObject complexParse(Property property) {
170         JsonObject propertiesInJson = new JsonObject();
171         Template currentPropertyTemplate;
172         String typeProperty = (String) property.getItems().get("type");
173         if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) {
174             currentPropertyTemplate = templates.get("object");
175         }
176         else {
177             String propertyType = (String) property.getItems().get("type");
178             currentPropertyTemplate = templates.get(propertyType.toLowerCase());
179         }
180         //Each "special" field is analysed, and has a specific treatment
181         for (String propertyField : property.getItems().keySet()) {
182             switch (propertyField) {
183                 case "type":
184                     if (currentPropertyTemplate.hasFields(propertyField)) {
185                         String fieldtype = (String) property.getItems().get(propertyField);
186                         switch (fieldtype.toLowerCase()) {
187                             case "list":
188                                 propertiesInJson.addProperty("type", "array");
189                                 break;
190                             case "map":
191                                 propertiesInJson.addProperty("type", "object");
192                                 break;
193                             case "scalar-unit.time":
194                             case "scalar-unit.frequency":
195                             case "scalar-unit.size":
196                                 propertiesInJson.addProperty("type", "string");
197                                 break;
198                             case "timestamp":
199                                 propertiesInJson.addProperty("type", "string");
200                                 propertiesInJson.addProperty("format", "date-time");
201                                 break;
202                             case "float":
203                                 propertiesInJson.addProperty("type", "number");
204                                 break;
205                             case "range":
206                                 propertiesInJson.addProperty("type", "integer");
207                                 if (!checkConstraintPresence(property, "greater_than")
208                                         && currentPropertyTemplate.hasFields("exclusiveMinimum")) {
209                                     propertiesInJson.addProperty("exclusiveMinimum", false);
210                                 }
211                                 if (!checkConstraintPresence(property, "less_than")
212                                         && currentPropertyTemplate.hasFields("exclusiveMaximum")) {
213                                     propertiesInJson.addProperty("exclusiveMaximum", false);
214                                 }
215                                 break;
216                             default:
217                                 propertiesInJson.addProperty("type", currentPropertyTemplate.getName());
218                                 break;
219                         }
220                     }
221                     break;
222                 case "metadata":
223                     propertiesInJson.add("enum", MetadataParser.processAllMetadataElement(property,
224                             dictionaryService));
225                     break;
226                 case "constraints":
227                     property.addConstraintsAsJson(propertiesInJson,
228                             (ArrayList<Object>) property.getItems().get("constraints"),
229                             currentPropertyTemplate);
230                     break;
231                 case "entry_schema":
232                     //Here, a way to check if entry is a component (datatype) or a simple string
233                     if (matchComponent(this.extractSpecificFieldFromMap(property, "entry_schema")) != null) {
234                         String nameComponent = this.extractSpecificFieldFromMap(property, "entry_schema");
235                         ToscaConverterToJson child = new ToscaConverterToJson(components, templates);
236                         JsonObject propertiesContainer = new JsonObject();
237
238                         switch ((String) property.getItems().get("type")) {
239                             case "map": // Get it as an object
240                                 JsonObject componentAsProperty = child.getJsonProcess(nameComponent, "object");
241                                 propertiesContainer.add(nameComponent, componentAsProperty);
242                                 if (currentPropertyTemplate.hasFields("properties")) {
243                                     propertiesInJson.add("properties", propertiesContainer);
244                                 }
245                                 break;
246                             default://list : get it as an Array
247                                 JsonObject componentAsItem = child.getJsonProcess(nameComponent, "object");
248                                 if (currentPropertyTemplate.hasFields("properties")) {
249                                     propertiesInJson.add("items", componentAsItem);
250                                 }
251                                 break;
252                         }
253
254                     }
255                     // Native cases
256                     else if (property.getItems().get("type").equals("list")) {
257                         JsonObject itemContainer = new JsonObject();
258                         String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema");
259                         itemContainer.addProperty("type", valueInEntrySchema);
260                         propertiesInJson.add("items", itemContainer);
261                     }
262                     // MAP Case, for now nothing
263
264                     break;
265                 default:
266                     //Each classical field : type, description, default..
267                     if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) {
268                         property.addFieldToJson(propertiesInJson, propertyField,
269                                 property.getItems().get(propertyField));
270                     }
271                     break;
272             }
273         }
274         return propertiesInJson;
275     }
276
277     /**
278      * Look for a matching Component for the name paramater, in the components list.
279      *
280      * @param name the name
281      * @return a component
282      */
283     public ToscaElement matchComponent(String name) {
284         ToscaElement correspondingToscaElement = null;
285         if (components == null) {
286             return null;
287         }
288         for (ToscaElement toscaElement : components.values()) {
289             if (toscaElement.getName().equals(name)) {
290                 correspondingToscaElement = toscaElement;
291             }
292         }
293         return correspondingToscaElement;
294     }
295
296     /**
297      * Simple method to extract quickly a type field from particular property item.
298      *
299      * @param property  the property
300      * @param fieldName the fieldname
301      * @return a string
302      */
303     @SuppressWarnings("unchecked")
304     public String extractSpecificFieldFromMap(Property property, String fieldName) {
305         LinkedHashMap<String, String> entrySchemaFields =
306                 (LinkedHashMap<String, String>) property.getItems().get(fieldName);
307         return entrySchemaFields.get("type");
308     }
309
310     /**
311      * Check if a constraint, for a specific property, is there.
312      *
313      * @param property       property
314      * @param nameConstraint name constraint
315      * @return a flag boolean
316      */
317     public boolean checkConstraintPresence(Property property, String nameConstraint) {
318         boolean presentConstraint = false;
319         if (property.getItems().containsKey("constraints")) {
320             ArrayList<Object> constraints = (ArrayList) property.getItems().get("constraints");
321             for (Object constraint : constraints) {
322                 if (constraint instanceof LinkedHashMap) {
323                     if (((LinkedHashMap) constraint).containsKey(nameConstraint)) {
324                         presentConstraint = true;
325                     }
326                 }
327             }
328         }
329         return presentConstraint;
330     }
331 }