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 TemplateManagement {
37 private LinkedHashMap<String, Template> templates;
38 private LinkedHashMap<String, Component> components;
39 private ParserToJson parserToJson;
40 private Extractor extractor;
45 * @param yamlContent Yaml content as string
46 * @param templateProperties template properties as string
47 * @throws IOException in case of failure
49 public TemplateManagement(String yamlContent, String nativeComponent, String templateProperties)
51 if (yamlContent != null && !yamlContent.isEmpty()) {
52 this.extractor = new Extractor(yamlContent, nativeComponent);
53 this.components = extractor.getAllItems();
54 this.templates = initializeTemplates(templateProperties);
62 public LinkedHashMap<String, Component> getComponents() {
66 public void setComponents(LinkedHashMap<String, Component> components) {
67 this.components = components;
70 public ParserToJson getParseToJson() {
74 public void setParseToJson(ParserToJson parserToJson) {
75 this.parserToJson = parserToJson;
78 public LinkedHashMap<String, Template> getTemplates() {
82 public void setTemplates(LinkedHashMap<String, Template> templates) {
83 this.templates = templates;
86 public Extractor getExtractor() {
94 * @param fields fields
96 public void addTemplate(String name, List<Field> fields) {
97 Template template = new Template(name, fields);
98 //If it is true, the operation does not have any interest :
99 // replace OR put two different object with the same body
100 if (!templates.containsKey(template.getName()) || !this.hasTemplate(template)) {
101 this.templates.put(template.getName(), template);
106 * By name, find and remove a given template.
108 * @param nameTemplate name template
110 public void removeTemplate(String nameTemplate) {
111 this.templates.remove(nameTemplate);
115 * Update Template : adding with true flag, removing with false.
117 * @param nameTemplate name template
118 * @param field field name
119 * @param operation operation
121 public void updateTemplate(String nameTemplate, Field field, Boolean operation) {
122 // Operation = true && field is not present => add Field
123 if (operation && !this.templates.get(nameTemplate).getFields().contains(field)) {
124 this.templates.get(nameTemplate).addField(field);
126 // Operation = false && field is present => remove Field
127 else if (!operation && this.templates.get(nameTemplate).getFields().contains(field)) {
128 this.templates.get(nameTemplate).removeField(field);
133 * Check if the JSONTemplates have the same bodies.
135 * @param template template
138 public boolean hasTemplate(Template template) {
139 boolean duplicateTemplate = false;
140 Collection<String> templatesName = templates.keySet();
141 if (templatesName.contains(template.getName())) {
142 Template existingTemplate = templates.get(template.getName());
143 duplicateTemplate = existingTemplate.checkFields(template);
145 return duplicateTemplate;
149 * For a given Component, get a corresponding JsonObject, through parseToJSON.
151 * @param componentName name
152 * @return an json object
154 public JsonObject launchTranslation(String componentName) throws UnknownComponentException {
155 this.parserToJson = new ParserToJson(components, templates);
156 if (parserToJson.matchComponent(componentName) == null) {
157 throw new UnknownComponentException(componentName);
159 return parserToJson.getJsonProcess(componentName, "object");
163 * Create and complete several Templates from file.properties.
165 * @param jsonTemplates The template properties as String
168 @SuppressWarnings("unused")
169 private LinkedHashMap<String, Template> initializeTemplates(String jsonTemplates) {
171 LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>();
172 JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
174 for (Map.Entry<String, JsonElement> templateAsJson : templates.entrySet()) {
175 Template template = new Template(templateAsJson.getKey());
176 JsonObject templateBody = (JsonObject) templateAsJson.getValue();
177 for (Map.Entry<String, JsonElement> field : templateBody.entrySet()) {
178 String fieldName = field.getKey();
179 JsonObject bodyFieldAsJson = (JsonObject) field.getValue();
180 Object fieldValue = bodyFieldAsJson.get("defaultValue").getAsString();
181 Boolean fieldVisible = bodyFieldAsJson.get("visible").getAsBoolean();
182 Boolean fieldStatic = bodyFieldAsJson.get("static").getAsBoolean();
183 Field bodyField = new Field(fieldName, fieldValue, fieldVisible, fieldStatic);
184 template.getFields().add(bodyField);
186 generatedTemplates.put(template.getName(), template);
188 return generatedTemplates;