Rework tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / TemplateManagement.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update;
25
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;
34
35 public class TemplateManagement {
36
37     private LinkedHashMap<String, Template> templates;
38     private LinkedHashMap<String, Component> components;
39     private ParserToJson parserToJson;
40     private Extractor extractor;
41
42     /**
43      * Constructor.
44      *
45      * @param yamlContent        Yaml content as string
46      * @param templateProperties template properties as string
47      * @throws IOException in case of failure
48      */
49     public TemplateManagement(String yamlContent, String nativeComponent, String templateProperties)
50             throws IOException {
51         if (yamlContent != null && !yamlContent.isEmpty()) {
52             this.extractor = new Extractor(yamlContent, nativeComponent);
53             this.components = extractor.getAllItems();
54             this.templates = initializeTemplates(templateProperties);
55         }
56         else {
57             components = null;
58         }
59     }
60
61     //GETTERS & SETTERS
62     public LinkedHashMap<String, Component> getComponents() {
63         return components;
64     }
65
66     public void setComponents(LinkedHashMap<String, Component> components) {
67         this.components = components;
68     }
69
70     public ParserToJson getParseToJson() {
71         return parserToJson;
72     }
73
74     public void setParseToJson(ParserToJson parserToJson) {
75         this.parserToJson = parserToJson;
76     }
77
78     public LinkedHashMap<String, Template> getTemplates() {
79         return templates;
80     }
81
82     public void setTemplates(LinkedHashMap<String, Template> templates) {
83         this.templates = templates;
84     }
85
86     public Extractor getExtractor() {
87         return extractor;
88     }
89
90     /**
91      * Add a template.
92      *
93      * @param name   name
94      * @param fields fields
95      */
96     public void addTemplate(String name, ArrayList<String> 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);
102         }
103     }
104
105     /**
106      * By name, find and remove a given template.
107      *
108      * @param nameTemplate name template
109      */
110     public void removeTemplate(String nameTemplate) {
111         this.templates.remove(nameTemplate);
112     }
113
114     /**
115      * Update Template : adding with true flag, removing with false.
116      *
117      * @param nameTemplate name template
118      * @param fieldName    field name
119      * @param operation    operation
120      */
121     public void updateTemplate(String nameTemplate, String fieldName, Boolean operation) {
122         // Operation = true && field is not present => add Field
123         if (operation && !this.templates.get(nameTemplate).getFields().contains(fieldName)) {
124             this.templates.get(nameTemplate).addField(fieldName);
125         }
126         // Operation = false && field is present => remove Field
127         else if (!operation && this.templates.get(nameTemplate).getFields().contains(fieldName)) {
128             this.templates.get(nameTemplate).removeField(fieldName);
129         }
130     }
131
132     /**
133      * Check if the JSONTemplates have the same bodies.
134      *
135      * @param template template
136      * @return a boolean
137      */
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);
144         }
145         return duplicateTemplate;
146     }
147
148     /**
149      * For a given Component, get a corresponding JsonObject, through parseToJSON.
150      *
151      * @param componentName name
152      * @return an json object
153      */
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);
158         }
159         return parserToJson.getJsonProcess(componentName, "object");
160     }
161
162     /**
163      * Create and complete several Templates from file.properties.
164      *
165      * @param templateProperties The template properties as String
166      * @return a map
167      */
168     private LinkedHashMap<String, Template> initializeTemplates(String templateProperties) throws IOException {
169         LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>();
170         Properties templates = new Properties();
171         templates.load(new StringReader(templateProperties));
172
173         for (Object key : templates.keySet()) {
174             String fields = (String) templates.get(key);
175             String[] fieldsInArray = fields.split(",");
176             Template template = new Template((String) key, new ArrayList<>(Arrays.asList(fieldsInArray)));
177             generatedTemplates.put(template.getName(), template);
178         }
179         return generatedTemplates;
180     }
181 }