Fix the new tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / elements / ToscaElementProperty.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.elements;
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 org.onap.clamp.clds.tosca.update.templates.JsonTemplate;
31
32 public class ToscaElementProperty {
33
34     /**
35      * name parameter is used as "key", in the LinkedHashMap of Components.
36      */
37     private String name;
38     private LinkedHashMap<String, Object> items;
39
40     /**
41      * Constructor.
42      *
43      * @param name  the name
44      * @param items the items
45      */
46     public ToscaElementProperty(String name, LinkedHashMap<String, Object> items) {
47         super();
48         this.name = name;
49         this.items = items;
50     }
51
52     public String getName() {
53         return name;
54     }
55
56     public void setName(String name) {
57         this.name = name;
58     }
59
60     public LinkedHashMap<String, Object> getItems() {
61         return items;
62     }
63
64     public void setItems(LinkedHashMap<String, Object> items) {
65         this.items = items;
66     }
67
68     /**
69      * For each primitive value, requires to get each field Value and cast it and add it in a Json.
70      *
71      * @param fieldsContent field
72      * @param fieldName     field
73      * @param value         value
74      */
75     public void addFieldToJson(JsonObject fieldsContent, String fieldName, Object value) {
76         if (value != null) {
77             String typeValue = value.getClass().getSimpleName();
78             switch (typeValue) {
79                 case "String":
80                     fieldsContent.addProperty(fieldName, (String) value);
81                     break;
82                 case "Boolean":
83                     fieldsContent.addProperty(fieldName, (Boolean) value);
84                     break;
85                 case "Double":
86                     fieldsContent.addProperty(fieldName, (Number) value);
87                     break;
88                 case "Integer":
89                     fieldsContent.addProperty(fieldName, (Integer) value);
90                     break;
91                 default:
92                     fieldsContent.add(fieldName, parseArray((ArrayList) value));
93                     break;
94             }
95         }
96     }
97
98     /**
99      * If a field value is an Array, create an Instance of ArrayField to insert if in the JsonObject.
100      *
101      * @param arrayProperties array pro
102      * @return a json array
103      */
104     public JsonArray parseArray(ArrayList<Object> arrayProperties) {
105         JsonArray arrayContent = new JsonArray();
106         ArrayList<Object> arrayComponent = new ArrayList<>();
107         for (Object itemArray : arrayProperties) {
108             arrayComponent.add(itemArray);
109         }
110         ArrayField af = new ArrayField(arrayComponent);
111         arrayContent = af.deploy();
112         return arrayContent;
113     }
114
115     /**
116      * Create an instance of Constraint, to extract the values and add it to the Json (according to the type
117      * *                    of the current property).
118      *
119      * @param json a json
120      * @param constraints constraints
121      * @param jsonTemplate template
122      */
123     @SuppressWarnings("unchecked")
124     public void addConstraintsAsJson(JsonObject json, ArrayList<Object> constraints, JsonTemplate jsonTemplate) {
125         for (Object constraint : constraints) {
126             if (constraint instanceof LinkedHashMap) {
127                 LinkedHashMap<String, Object> valueConstraint = (LinkedHashMap<String, Object>) constraint;
128                 Constraint constraintParser = new Constraint(valueConstraint, jsonTemplate);
129                 constraintParser.deployConstraints(json, (String) getItems().get("type"));
130             }
131         }
132
133     }
134
135 }