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.JsonObject;
27 import java.io.IOException;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.LinkedHashMap;
33 import java.util.Properties;
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 templateProperties) throws IOException {
50 if (yamlContent != null && !yamlContent.isEmpty()) {
51 this.extractor = new Extractor(yamlContent);
52 this.components = extractor.getAllItems();
53 this.templates = initializeTemplates(templateProperties);
61 public LinkedHashMap<String, Component> getComponents() {
65 public void setComponents(LinkedHashMap<String, Component> components) {
66 this.components = components;
69 public ParserToJson getParseToJson() {
73 public void setParseToJson(ParserToJson parserToJson) {
74 this.parserToJson = parserToJson;
77 public LinkedHashMap<String, Template> getTemplates() {
81 public void setTemplates(LinkedHashMap<String, Template> templates) {
82 this.templates = templates;
85 public Extractor getExtractor() {
93 * @param fields fields
95 public void addTemplate(String name, ArrayList<String> fields) {
96 Template template = new Template(name, fields);
97 //If it is true, the operation does not have any interest :
98 // replace OR put two different object with the same body
99 if (!templates.containsKey(template.getName()) || !this.hasTemplate(template)) {
100 this.templates.put(template.getName(), template);
105 * By name, find and remove a given template.
107 * @param nameTemplate name template
109 public void removeTemplate(String nameTemplate) {
110 this.templates.remove(nameTemplate);
114 * Update Template : adding with true flag, removing with false.
116 * @param nameTemplate name template
117 * @param fieldName field name
118 * @param operation operation
120 public void updateTemplate(String nameTemplate, String fieldName, Boolean operation) {
121 // Operation = true && field is not present => add Field
122 if (operation && !this.templates.get(nameTemplate).getFields().contains(fieldName)) {
123 this.templates.get(nameTemplate).addField(fieldName);
125 // Operation = false && field is present => remove Field
126 else if (!operation && this.templates.get(nameTemplate).getFields().contains(fieldName)) {
127 this.templates.get(nameTemplate).removeField(fieldName);
132 * Check if the JSONTemplates have the same bodies.
134 * @param template template
137 public boolean hasTemplate(Template template) {
138 boolean duplicateTemplate = false;
139 Collection<String> templatesName = templates.keySet();
140 if (templatesName.contains(template.getName())) {
141 Template existingTemplate = templates.get(template.getName());
142 duplicateTemplate = existingTemplate.checkFields(template);
144 return duplicateTemplate;
148 * For a given Component, get a corresponding JsonObject, through parseToJSON.
150 * @param componentName name
151 * @return an json object
153 public JsonObject launchTranslation(String componentName) throws UnknownComponentException {
154 this.parserToJson = new ParserToJson(components, templates);
155 if (parserToJson.matchComponent(componentName) == null) {
156 throw new UnknownComponentException(componentName);
158 return parserToJson.getJsonProcess(componentName);
162 * Create and complete several Templates from file.properties.
164 * @param templateProperties The template properties as String
167 private LinkedHashMap<String, Template> initializeTemplates(String templateProperties) throws IOException {
168 LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>();
169 Properties templates = new Properties();
170 templates.load(new StringReader(templateProperties));
172 for (Object key : templates.keySet()) {
173 String fields = (String) templates.get(key);
174 String[] fieldsInArray = fields.split(",");
175 Template template = new Template((String) key, new ArrayList<>(Arrays.asList(fieldsInArray)));
176 generatedTemplates.put(template.getName(), template);
178 return generatedTemplates;