2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.tosca.update;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
31 public class Property {
34 * name parameter is used as "key", in the LinkedHashMap of Components.
37 private LinkedHashMap<String, Object> items;
42 * @param name the name
43 * @param items the items
45 public Property(String name, LinkedHashMap<String, Object> items) {
51 public String getName() {
55 public void setName(String name) {
59 public LinkedHashMap<String, Object> getItems() {
63 public void setItems(LinkedHashMap<String, Object> items) {
68 * For each primitive value, requires to get each field Value and cast it and add it in a Json.
70 * @param fieldsContent field
71 * @param fieldName field
74 public void addFieldToJson(JsonObject fieldsContent, String fieldName, Object value) {
76 String typeValue = value.getClass().getSimpleName();
79 fieldsContent.addProperty(fieldName, (String) value);
82 fieldsContent.addProperty(fieldName, (Boolean) value);
85 fieldsContent.addProperty(fieldName, (Number) value);
88 fieldsContent.addProperty(fieldName, (Integer) value);
91 fieldsContent.add(fieldName, parseArray((ArrayList) value));
98 * If a field value is an Array, create an Instance of ArrayField to insert if in the JsonObject.
100 * @param arrayProperties array pro
101 * @return a json array
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);
109 ArrayField af = new ArrayField(arrayComponent);
110 arrayContent = af.deploy();
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).
119 * @param constraints constraints
120 * @param template template
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"));