4b510cb758f5f1b0e67afac950bb5f7412b5e1a4
[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 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);
54         }
55         else {
56             components = null;
57         }
58     }
59
60     //GETTERS & SETTERS
61     public LinkedHashMap<String, Component> getComponents() {
62         return components;
63     }
64
65     public void setComponents(LinkedHashMap<String, Component> components) {
66         this.components = components;
67     }
68
69     public ParserToJson getParseToJson() {
70         return parserToJson;
71     }
72
73     public void setParseToJson(ParserToJson parserToJson) {
74         this.parserToJson = parserToJson;
75     }
76
77     public LinkedHashMap<String, Template> getTemplates() {
78         return templates;
79     }
80
81     public void setTemplates(LinkedHashMap<String, Template> templates) {
82         this.templates = templates;
83     }
84
85     public Extractor getExtractor() {
86         return extractor;
87     }
88
89     /**
90      * Add a template.
91      *
92      * @param name   name
93      * @param fields fields
94      */
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);
101         }
102     }
103
104     /**
105      * By name, find and remove a given template.
106      *
107      * @param nameTemplate name template
108      */
109     public void removeTemplate(String nameTemplate) {
110         this.templates.remove(nameTemplate);
111     }
112
113     /**
114      * Update Template : adding with true flag, removing with false.
115      *
116      * @param nameTemplate name template
117      * @param fieldName    field name
118      * @param operation    operation
119      */
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);
124         }
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);
128         }
129     }
130
131     /**
132      * Check if the JSONTemplates have the same bodies.
133      *
134      * @param template template
135      * @return a boolean
136      */
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);
143         }
144         return duplicateTemplate;
145     }
146
147     /**
148      * For a given Component, get a corresponding JsonObject, through parseToJSON.
149      *
150      * @param componentName name
151      * @return an json object
152      */
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);
157         }
158         return parserToJson.getJsonProcess(componentName);
159     }
160
161     /**
162      * Create and complete several Templates from file.properties.
163      *
164      * @param templateProperties The template properties as String
165      * @return a map
166      */
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));
171
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);
177         }
178         return generatedTemplates;
179     }
180 }