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