Fix the new tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / templates / JsonTemplateManager.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.templates;
25
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import java.util.Collection;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import org.onap.clamp.clds.tosca.update.UnknownComponentException;
33 import org.onap.clamp.clds.tosca.update.elements.ToscaElement;
34 import org.onap.clamp.clds.tosca.update.parser.ToscaConverterToJsonSchema;
35 import org.onap.clamp.clds.tosca.update.parser.ToscaElementParser;
36 import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser;
37 import org.onap.clamp.clds.util.JsonUtils;
38
39 public class JsonTemplateManager {
40     private LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates;
41     private LinkedHashMap<String, ToscaElement> toscaElements;
42
43     /**
44      * Constructor.
45      *
46      * @param toscaYamlContent     Policy Tosca Yaml content as string
47      * @param nativeToscaDatatypes The tosca yaml with tosca native datatypes
48      * @param jsonSchemaTemplates   template properties as string
49      */
50     public JsonTemplateManager(String toscaYamlContent, String nativeToscaDatatypes, String jsonSchemaTemplates) {
51         if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) {
52             this.toscaElements = ToscaElementParser.searchAllToscaElements(toscaYamlContent, nativeToscaDatatypes);
53             this.jsonSchemaTemplates = initializeTemplates(jsonSchemaTemplates);
54         }
55         else {
56             toscaElements = null;
57         }
58     }
59
60     //GETTERS & SETTERS
61     public LinkedHashMap<String, ToscaElement> getToscaElements() {
62         return toscaElements;
63     }
64
65     public void setToscaElements(LinkedHashMap<String, ToscaElement> toscaElements) {
66         this.toscaElements = toscaElements;
67     }
68
69     public LinkedHashMap<String, JsonTemplate> getJsonSchemaTemplates() {
70         return jsonSchemaTemplates;
71     }
72
73     public void setJsonSchemaTemplates(LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates) {
74         this.jsonSchemaTemplates = jsonSchemaTemplates;
75     }
76
77     /**
78      * Add a template.
79      *
80      * @param name               name
81      * @param jsonTemplateFields fields
82      */
83     public void addTemplate(String name, List<JsonTemplateField> jsonTemplateFields) {
84         JsonTemplate jsonTemplate = new JsonTemplate(name, jsonTemplateFields);
85         //If it is true, the operation does not have any interest :
86         // replace OR put two different object with the same body
87         if (!jsonSchemaTemplates.containsKey(jsonTemplate.getName()) || !this.hasTemplate(jsonTemplate)) {
88             this.jsonSchemaTemplates.put(jsonTemplate.getName(), jsonTemplate);
89         }
90     }
91
92     /**
93      * By name, find and remove a given template.
94      *
95      * @param nameTemplate name template
96      */
97     public void removeTemplate(String nameTemplate) {
98         this.jsonSchemaTemplates.remove(nameTemplate);
99     }
100
101     /**
102      * Update Template : adding with true flag, removing with false.
103      *
104      * @param nameTemplate      name template
105      * @param jsonTemplateField field name
106      * @param operation         operation
107      */
108     public void updateTemplate(String nameTemplate, JsonTemplateField jsonTemplateField, Boolean operation) {
109         // Operation = true && field is not present => add Field
110         if (operation && !this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) {
111             this.jsonSchemaTemplates.get(nameTemplate).addField(jsonTemplateField);
112         }
113         // Operation = false && field is present => remove Field
114         else if (!operation && this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) {
115             this.jsonSchemaTemplates.get(nameTemplate).removeField(jsonTemplateField);
116         }
117     }
118
119     /**
120      * Check if the JSONTemplates have the same bodies.
121      *
122      * @param jsonTemplate template
123      * @return a boolean
124      */
125     public boolean hasTemplate(JsonTemplate jsonTemplate) {
126         boolean duplicateTemplate = false;
127         Collection<String> templatesName = jsonSchemaTemplates.keySet();
128         if (templatesName.contains(jsonTemplate.getName())) {
129             JsonTemplate existingJsonTemplate = jsonSchemaTemplates.get(jsonTemplate.getName());
130             duplicateTemplate = existingJsonTemplate.checkFields(jsonTemplate);
131         }
132         return duplicateTemplate;
133     }
134
135     /**
136      * For a given policy type, get a corresponding JsonObject from the tosca model.
137      *
138      * @param policyType     The policy type in the tosca
139      * @param toscaMetadataParser The MetadataParser class that must be used if metadata section are encountered, if null
140      *                       they will be skipped
141      * @return an json object defining the equivalent json schema from the tosca for a given policy type
142      */
143     public JsonObject getJsonSchemaForPolicyType(String policyType, ToscaMetadataParser toscaMetadataParser)
144             throws UnknownComponentException {
145         ToscaConverterToJsonSchema
146                 toscaConverterToJsonSchema = new ToscaConverterToJsonSchema(toscaElements, jsonSchemaTemplates,
147                 toscaMetadataParser);
148         if (toscaConverterToJsonSchema.getToscaElement(policyType) == null) {
149             throw new UnknownComponentException(policyType);
150         }
151         return toscaConverterToJsonSchema.getJsonSchemaOfToscaElement(policyType);
152     }
153
154     /**
155      * Create and complete several Templates from file.properties.
156      *
157      * @param jsonTemplates The template properties as String
158      * @return a map
159      */
160     @SuppressWarnings("unused")
161     private LinkedHashMap<String, JsonTemplate> initializeTemplates(String jsonTemplates) {
162
163         LinkedHashMap<String, JsonTemplate> generatedTemplates = new LinkedHashMap<>();
164         JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
165
166         for (Map.Entry<String, JsonElement> templateAsJson : templates.entrySet()) {
167             JsonTemplate jsonTemplate = new JsonTemplate(templateAsJson.getKey());
168             JsonObject templateBody = (JsonObject) templateAsJson.getValue();
169             for (Map.Entry<String, JsonElement> field : templateBody.entrySet()) {
170                 String fieldName = field.getKey();
171                 JsonObject bodyFieldAsJson = (JsonObject) field.getValue();
172                 Object fieldValue = bodyFieldAsJson.get("defaultValue").getAsString();
173                 Boolean fieldVisible = bodyFieldAsJson.get("visible").getAsBoolean();
174                 Boolean fieldStatic = bodyFieldAsJson.get("static").getAsBoolean();
175                 JsonTemplateField
176                         bodyJsonTemplateField = new JsonTemplateField(fieldName, fieldValue, fieldVisible, fieldStatic);
177                 jsonTemplate.getJsonTemplateFields().add(bodyJsonTemplateField);
178             }
179             generatedTemplates.put(jsonTemplate.getName(), jsonTemplate);
180         }
181         return generatedTemplates;
182     }
183
184 }