b3224b045a134cbd017eb6124b43dc5a7834d03d
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / ToscaConverterManager.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.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;
32 import java.util.Map;
33 import org.onap.clamp.clds.util.JsonUtils;
34
35 public class ToscaConverterManager {
36
37     private LinkedHashMap<String, Template> templates;
38     private LinkedHashMap<String, ToscaElement> components;
39     private ToscaConverterToJson toscaConverterToJson;
40     private ToscaItemsParser toscaItemsParser;
41
42     /**
43      * Constructor.
44      *
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
49      */
50     public ToscaConverterManager(String toscaYamlContent, String nativeToscaDatatypes, String templateProperties)
51             throws IOException {
52         if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) {
53             this.toscaItemsParser = new ToscaItemsParser(toscaYamlContent, nativeToscaDatatypes);
54             this.components = toscaItemsParser.getAllItemsFound();
55             this.templates = initializeTemplates(templateProperties);
56         }
57         else {
58             components = null;
59         }
60     }
61
62     //GETTERS & SETTERS
63     public LinkedHashMap<String, ToscaElement> getComponents() {
64         return components;
65     }
66
67     public void setComponents(LinkedHashMap<String, ToscaElement> components) {
68         this.components = components;
69     }
70
71     public ToscaConverterToJson getParseToJson() {
72         return toscaConverterToJson;
73     }
74
75     public void setParseToJson(ToscaConverterToJson toscaConverterToJson) {
76         this.toscaConverterToJson = toscaConverterToJson;
77     }
78
79     public LinkedHashMap<String, Template> getTemplates() {
80         return templates;
81     }
82
83     public void setTemplates(LinkedHashMap<String, Template> templates) {
84         this.templates = templates;
85     }
86
87     public ToscaItemsParser getToscaItemsParser() {
88         return toscaItemsParser;
89     }
90
91     /**
92      * Add a template.
93      *
94      * @param name           name
95      * @param templateFields fields
96      */
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);
103         }
104     }
105
106     /**
107      * By name, find and remove a given template.
108      *
109      * @param nameTemplate name template
110      */
111     public void removeTemplate(String nameTemplate) {
112         this.templates.remove(nameTemplate);
113     }
114
115     /**
116      * Update Template : adding with true flag, removing with false.
117      *
118      * @param nameTemplate  name template
119      * @param templateField field name
120      * @param operation     operation
121      */
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);
126         }
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);
130         }
131     }
132
133     /**
134      * Check if the JSONTemplates have the same bodies.
135      *
136      * @param template template
137      * @return a boolean
138      */
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);
145         }
146         return duplicateTemplate;
147     }
148
149     /**
150      * For a given Component, get a corresponding JsonObject, through parseToJSON.
151      *
152      * @param componentName name
153      * @return an json object
154      */
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);
159         }
160         return toscaConverterToJson.getJsonProcess(componentName, "object");
161     }
162
163     /**
164      * Create and complete several Templates from file.properties.
165      *
166      * @param jsonTemplates The template properties as String
167      * @return a map
168      */
169     @SuppressWarnings("unused")
170     private LinkedHashMap<String, Template> initializeTemplates(String jsonTemplates) {
171
172         LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>();
173         JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
174
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);
186             }
187             generatedTemplates.put(template.getName(), template);
188         }
189         return generatedTemplates;
190     }
191
192 }