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.templates;
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;
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;
39 public class JsonTemplateManager {
40 private LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates;
41 private LinkedHashMap<String, ToscaElement> toscaElements;
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
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);
61 public LinkedHashMap<String, ToscaElement> getToscaElements() {
65 public void setToscaElements(LinkedHashMap<String, ToscaElement> toscaElements) {
66 this.toscaElements = toscaElements;
69 public LinkedHashMap<String, JsonTemplate> getJsonSchemaTemplates() {
70 return jsonSchemaTemplates;
73 public void setJsonSchemaTemplates(LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates) {
74 this.jsonSchemaTemplates = jsonSchemaTemplates;
81 * @param jsonTemplateFields fields
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);
93 * By name, find and remove a given template.
95 * @param nameTemplate name template
97 public void removeTemplate(String nameTemplate) {
98 this.jsonSchemaTemplates.remove(nameTemplate);
102 * Update Template : adding with true flag, removing with false.
104 * @param nameTemplate name template
105 * @param jsonTemplateField field name
106 * @param operation operation
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);
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);
120 * Check if the JSONTemplates have the same bodies.
122 * @param jsonTemplate template
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);
132 return duplicateTemplate;
136 * For a given policy type, get a corresponding JsonObject from the tosca model.
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
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);
151 return toscaConverterToJsonSchema.getJsonSchemaOfToscaElement(policyType);
155 * Create and complete several Templates from file.properties.
157 * @param jsonTemplates The template properties as String
160 @SuppressWarnings("unused")
161 private LinkedHashMap<String, JsonTemplate> initializeTemplates(String jsonTemplates) {
163 LinkedHashMap<String, JsonTemplate> generatedTemplates = new LinkedHashMap<>();
164 JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
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();
176 bodyJsonTemplateField = new JsonTemplateField(fieldName, fieldValue, fieldVisible, fieldStatic);
177 jsonTemplate.getJsonTemplateFields().add(bodyJsonTemplateField);
179 generatedTemplates.put(jsonTemplate.getName(), jsonTemplate);
181 return generatedTemplates;