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