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.JsonElement;
27 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import java.util.Collection;
30 import java.util.LinkedHashMap;
31 import java.util.List;
33 import org.onap.clamp.clds.util.JsonUtils;
35 public class ToscaConverterManager {
37 private LinkedHashMap<String, Template> templates;
38 private LinkedHashMap<String, ToscaElement> components;
39 private ToscaConverterToJson toscaConverterToJson;
40 private ToscaItemsParser toscaItemsParser;
45 * @param toscaYamlContent Policy Tosca Yaml content as string
46 * @param nativeToscaDatatypes The tosca yaml with tosca native datatypes
47 * @param templateProperties template properties as string
48 * @throws IOException in case of failure
50 public ToscaConverterManager(String toscaYamlContent, String nativeToscaDatatypes, String templateProperties)
52 if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) {
53 this.toscaItemsParser = new ToscaItemsParser(toscaYamlContent, nativeToscaDatatypes);
54 this.components = toscaItemsParser.getAllItemsFound();
55 this.templates = initializeTemplates(templateProperties);
63 public LinkedHashMap<String, ToscaElement> getComponents() {
67 public void setComponents(LinkedHashMap<String, ToscaElement> components) {
68 this.components = components;
71 public ToscaConverterToJson getParseToJson() {
72 return toscaConverterToJson;
75 public void setParseToJson(ToscaConverterToJson toscaConverterToJson) {
76 this.toscaConverterToJson = toscaConverterToJson;
79 public LinkedHashMap<String, Template> getTemplates() {
83 public void setTemplates(LinkedHashMap<String, Template> templates) {
84 this.templates = templates;
87 public ToscaItemsParser getToscaItemsParser() {
88 return toscaItemsParser;
95 * @param templateFields fields
97 public void addTemplate(String name, List<TemplateField> templateFields) {
98 Template template = new Template(name, templateFields);
99 //If it is true, the operation does not have any interest :
100 // replace OR put two different object with the same body
101 if (!templates.containsKey(template.getName()) || !this.hasTemplate(template)) {
102 this.templates.put(template.getName(), template);
107 * By name, find and remove a given template.
109 * @param nameTemplate name template
111 public void removeTemplate(String nameTemplate) {
112 this.templates.remove(nameTemplate);
116 * Update Template : adding with true flag, removing with false.
118 * @param nameTemplate name template
119 * @param templateField field name
120 * @param operation operation
122 public void updateTemplate(String nameTemplate, TemplateField templateField, Boolean operation) {
123 // Operation = true && field is not present => add Field
124 if (operation && !this.templates.get(nameTemplate).getTemplateFields().contains(templateField)) {
125 this.templates.get(nameTemplate).addField(templateField);
127 // Operation = false && field is present => remove Field
128 else if (!operation && this.templates.get(nameTemplate).getTemplateFields().contains(templateField)) {
129 this.templates.get(nameTemplate).removeField(templateField);
134 * Check if the JSONTemplates have the same bodies.
136 * @param template template
139 public boolean hasTemplate(Template template) {
140 boolean duplicateTemplate = false;
141 Collection<String> templatesName = templates.keySet();
142 if (templatesName.contains(template.getName())) {
143 Template existingTemplate = templates.get(template.getName());
144 duplicateTemplate = existingTemplate.checkFields(template);
146 return duplicateTemplate;
150 * For a given Component, get a corresponding JsonObject, through parseToJSON.
152 * @param componentName name
153 * @return an json object
155 public JsonObject startConversionToJson(String componentName) throws UnknownComponentException {
156 this.toscaConverterToJson = new ToscaConverterToJson(components, templates);
157 if (toscaConverterToJson.matchComponent(componentName) == null) {
158 throw new UnknownComponentException(componentName);
160 return toscaConverterToJson.getJsonProcess(componentName, "object");
164 * Create and complete several Templates from file.properties.
166 * @param jsonTemplates The template properties as String
169 @SuppressWarnings("unused")
170 private LinkedHashMap<String, Template> initializeTemplates(String jsonTemplates) {
172 LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>();
173 JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
175 for (Map.Entry<String, JsonElement> templateAsJson : templates.entrySet()) {
176 Template template = new Template(templateAsJson.getKey());
177 JsonObject templateBody = (JsonObject) templateAsJson.getValue();
178 for (Map.Entry<String, JsonElement> field : templateBody.entrySet()) {
179 String fieldName = field.getKey();
180 JsonObject bodyFieldAsJson = (JsonObject) field.getValue();
181 Object fieldValue = bodyFieldAsJson.get("defaultValue").getAsString();
182 Boolean fieldVisible = bodyFieldAsJson.get("visible").getAsBoolean();
183 Boolean fieldStatic = bodyFieldAsJson.get("static").getAsBoolean();
184 TemplateField bodyTemplateField = new TemplateField(fieldName, fieldValue, fieldVisible, fieldStatic);
185 template.getTemplateFields().add(bodyTemplateField);
187 generatedTemplates.put(template.getName(), template);
189 return generatedTemplates;