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.elements;
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;
32 public class ToscaElementProperty {
35 * name parameter is used as "key", in the LinkedHashMap of Components.
38 private LinkedHashMap<String, Object> items;
43 * @param name the name
44 * @param items the items
46 public ToscaElementProperty(String name, LinkedHashMap<String, Object> items) {
52 public String getName() {
56 public void setName(String name) {
60 public LinkedHashMap<String, Object> getItems() {
64 public void setItems(LinkedHashMap<String, Object> items) {
69 * For each primitive value, requires to get each field Value and cast it and add it in a Json.
71 * @param fieldsContent field
72 * @param fieldName field
75 public void addFieldToJson(JsonObject fieldsContent, String fieldName, Object value) {
77 String typeValue = value.getClass().getSimpleName();
80 fieldsContent.addProperty(fieldName, (String) value);
83 fieldsContent.addProperty(fieldName, (Boolean) value);
86 fieldsContent.addProperty(fieldName, (Number) value);
89 fieldsContent.addProperty(fieldName, (Integer) value);
92 fieldsContent.add(fieldName, parseArray((ArrayList) value));
99 * If a field value is an Array, create an Instance of ArrayField to insert if in the JsonObject.
101 * @param arrayProperties array pro
102 * @return a json array
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);
110 ArrayField af = new ArrayField(arrayComponent);
111 arrayContent = af.deploy();
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).
120 * @param constraints constraints
121 * @param jsonTemplate template
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"));