aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java
blob: 7bf629d61b6b2f190032ddc4bf686ea8c8f86d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.clamp.clds.tosca.update;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class ParserToJson {
    private LinkedHashMap<String, Component> components;
    private LinkedHashMap<String, Template> templates;

    public ParserToJson(LinkedHashMap<String, Component> components, LinkedHashMap<String, Template> templates) {
        this.components = components;
        this.templates = templates;
    }

    /**
     * For a given component, launch process to parse it in Json.
     *
     * @param nameComponent name components
     * @return return
     */
    public JsonObject getJsonProcess(String nameComponent, String typeComponent) {
        JsonObject glob = new JsonObject();

        if (typeComponent.equals("object")) {
            glob = this.getFieldAsObject(matchComponent(nameComponent));
        }
        else {
            /*glob = this.getFieldAsArray(matchComponent(nameComponent));*/
        }

        return glob;
    }

    /**
     * Return the classical/general fields of the component, & launch the properties deployment.
     *
     * @param component the compo
     * @return a json object
     */
    public JsonObject getFieldAsObject(Component component) {

        JsonObject globalFields = new JsonObject();
        if (templates.get("object").hasFields("title")) {
            globalFields.addProperty("title", component.getName());
        }
        if (templates.get("object").hasFields("type")) {
            globalFields.addProperty("type", "object");
        }
        if (templates.get("object").hasFields("description")) {
            if (component.getDescription() != null) {
                globalFields.addProperty("description", component.getDescription());
            }
        }
        if (templates.get("object").hasFields("required")) {
            globalFields.add("required", this.getRequirements(component.getName()));
        }
        if (templates.get("object").hasFields("properties")) {
            globalFields.add("properties", this.deploy(component.getName()));
        }
        return globalFields;
    }

    /**
     * Get the required properties of the Component, including the parents properties requirements.
     *
     * @param nameComponent name component
     * @return a json array
     */
    public JsonArray getRequirements(String nameComponent) {
        JsonArray requirements = new JsonArray();
        Component toParse = components.get(nameComponent);
        //Check for a father component, and launch the same process
        if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
                && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
            requirements.addAll(getRequirements(toParse.getDerivedFrom()));
        }
        //Each property is checked, and add to the requirement array if it's required
        Collection<Property> properties = toParse.getProperties().values();
        for (Property property : properties) {
            if (property.getItems().containsKey("required")
                    && property.getItems().get("required").equals(true)) {
                requirements.add(property.getName());
            }
        }
        return requirements;
    }

    /**
     * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise
     * deploy and parse the properties.
     *
     * @param nameComponent name component
     * @return a json object
     */
    public JsonObject deploy(String nameComponent) {
        JsonObject jsonSchema = new JsonObject();
        Component toParse = components.get(nameComponent);
        //Check for a father component, and launch the same process
        if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root")
                && !toParse.getDerivedFrom().equals("tosca.policies.Root")) {
            jsonSchema = this.getParent(toParse.getDerivedFrom());
        }
        //For each component property, check if its a complex properties (a component) or not. In that case,
        //launch the analyse of the property.
        for (Entry<String, Property> property : toParse.getProperties().entrySet()) {
            if (matchComponent((String) property.getValue().getItems().get("type")) != null) {
                jsonSchema.add(property.getValue().getName(),
                        this.getJsonProcess((String) property.getValue().getItems().get("type"), "object"));
            }
            else {
                jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue()));
            }
        }
        return jsonSchema;
    }

    /**
     * If a component has a parent, it is deploy in the same way.
     *
     * @param nameComponent name component
     * @return a json object
     */
    public JsonObject getParent(String nameComponent) {
        return deploy(nameComponent);
    }

    /**
     * to be done.
     *
     * @param property property
     * @return a json object
     */
    @SuppressWarnings("unchecked")
    public JsonObject complexParse(Property property) {
        JsonObject propertiesInJson = new JsonObject();
        Template currentPropertyTemplate;
        String typeProperty = (String) property.getItems().get("type");
        if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) {
            currentPropertyTemplate = templates.get("object");
        }
        else {
            String propertyType = (String) property.getItems().get("type");
            currentPropertyTemplate = templates.get(propertyType.toLowerCase());
        }
        //Each "special" field is analysed, and has a specific treatment
        for (String propertyField : property.getItems().keySet()) {
            switch (propertyField) {
                case "type":
                    if (currentPropertyTemplate.hasFields(propertyField)) {
                        String fieldtype = (String) property.getItems().get(propertyField);
                        switch (fieldtype.toLowerCase()) {
                            case "list":
                                propertiesInJson.addProperty("type", "array");
                                break;
                            case "map":
                                propertiesInJson.addProperty("type", "object");
                                break;
                            case "scalar-unit.time":
                            case "scalar-unit.frequency":
                            case "scalar-unit.size":
                                propertiesInJson.addProperty("type", "string");
                                break;
                            case "timestamp":
                                propertiesInJson.addProperty("type", "string");
                                propertiesInJson.addProperty("format", "date-time");
                                break;
                            case "float":
                                propertiesInJson.addProperty("type", "number");
                                break;
                            case "range":
                                propertiesInJson.addProperty("type", "integer");
                                if (!checkConstraintPresence(property, "greater_than")
                                        && currentPropertyTemplate.hasFields("exclusiveMinimum")) {
                                    propertiesInJson.addProperty("exclusiveMinimum", false);
                                }
                                if (!checkConstraintPresence(property, "less_than")
                                        && currentPropertyTemplate.hasFields("exclusiveMaximum")) {
                                    propertiesInJson.addProperty("exclusiveMaximum", false);
                                }
                                break;
                            default:
                                propertiesInJson.addProperty("type", currentPropertyTemplate.getName());
                                break;
                        }
                    }
                    break;
                case "metadata":
                    break;
                case "constraints":
                    property.addConstraintsAsJson(propertiesInJson,
                            (ArrayList<Object>) property.getItems().get("constraints"),
                            currentPropertyTemplate);
                    break;
                case "entry_schema":
                    //Here, a way to check if entry is a component (datatype) or a simple string
                    if (matchComponent(this.extractSpecificFieldFromMap(property, "entry_schema")) != null) {
                        String nameComponent = this.extractSpecificFieldFromMap(property, "entry_schema");
                        ParserToJson child = new ParserToJson(components, templates);
                        JsonObject propertiesContainer = new JsonObject();

                        switch ((String) property.getItems().get("type")) {
                            case "map": // Get it as an object
                                JsonObject componentAsProperty = child.getJsonProcess(nameComponent,"object");
                                propertiesContainer.add(nameComponent, componentAsProperty);
                                if (currentPropertyTemplate.hasFields("properties")) {
                                    propertiesInJson.add("properties", propertiesContainer);
                                }
                                break;
                            default://list : get it as an Array
                                JsonObject componentAsItem = child.getJsonProcess(nameComponent, "object");
                                if (currentPropertyTemplate.hasFields("properties")) {
                                    propertiesInJson.add("items", componentAsItem);
                                }
                                break;
                        }

                    }
                    // Native cases
                    else if (property.getItems().get("type").equals("list")) {
                        JsonObject itemContainer = new JsonObject();
                        String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema");
                        itemContainer.addProperty("type", valueInEntrySchema);
                          propertiesInJson.add("items", itemContainer);
                    }
                    else {//map
                        // propertiesInJson.add("key?", valueInEntrySchema);
                    }
                    break;
                default://Each classical field : type, description, default..
                    if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) {
                        property.addFieldToJson(propertiesInJson, propertyField,
                                property.getItems().get(propertyField));
                    }
                    break;
            }
        }
        return propertiesInJson;
    }

    /**
     * Look for a matching Component for the name paramater, in the components list.
     *
     * @param name the name
     * @return a component
     */
    public Component matchComponent(String name) {
        Component correspondingComponent = null;
        if (components == null) {
            return null;
        }
        for (Component component : components.values()) {
            if (component.getName().equals(name)) {
                correspondingComponent = component;
            }
        }
        return correspondingComponent;
    }

    /**
     * Simple method to extract quickly a type field from particular property item.
     *
     * @param property  the property
     * @param fieldName the fieldname
     * @return a string
     */
    @SuppressWarnings("unchecked")
    public String extractSpecificFieldFromMap(Property property, String fieldName) {
        LinkedHashMap<String, String> entrySchemaFields =
                (LinkedHashMap<String, String>) property.getItems().get(fieldName);
        return entrySchemaFields.get("type");
    }

    /**
     * Check if a constraint, for a specific property, is there.
     *
     * @param property       property
     * @param nameConstraint name constraint
     * @return a flag boolean
     */
    public boolean checkConstraintPresence(Property property, String nameConstraint) {
        boolean presentConstraint = false;
        if (property.getItems().containsKey("constraints")) {
            ArrayList<Object> constraints = (ArrayList) property.getItems().get("constraints");
            for (Object constraint : constraints) {
                if (constraint instanceof LinkedHashMap) {
                    if (((LinkedHashMap) constraint).containsKey(nameConstraint)) {
                        presentConstraint = true;
                    }
                }
            }
        }
        return presentConstraint;
    }
}