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;
38 import org.onap.clamp.loop.service.Service;
40 public class JsonTemplateManager {
41 private LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates;
42 private LinkedHashMap<String, ToscaElement> toscaElements;
47 * @param toscaYamlContent Policy Tosca Yaml content as string
48 * @param nativeToscaDatatypes The tosca yaml with tosca native datatypes
49 * @param jsonSchemaTemplates template properties as string
51 public JsonTemplateManager(String toscaYamlContent, String nativeToscaDatatypes, String jsonSchemaTemplates) {
52 if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) {
53 this.toscaElements = ToscaElementParser.searchAllToscaElements(toscaYamlContent, nativeToscaDatatypes);
54 this.jsonSchemaTemplates = initializeTemplates(jsonSchemaTemplates);
62 public LinkedHashMap<String, ToscaElement> getToscaElements() {
66 public void setToscaElements(LinkedHashMap<String, ToscaElement> toscaElements) {
67 this.toscaElements = toscaElements;
70 public LinkedHashMap<String, JsonTemplate> getJsonSchemaTemplates() {
71 return jsonSchemaTemplates;
74 public void setJsonSchemaTemplates(LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates) {
75 this.jsonSchemaTemplates = jsonSchemaTemplates;
82 * @param jsonTemplateFields fields
84 public void addTemplate(String name, List<JsonTemplateField> jsonTemplateFields) {
85 JsonTemplate jsonTemplate = new JsonTemplate(name, jsonTemplateFields);
86 //If it is true, the operation does not have any interest :
87 // replace OR put two different object with the same body
88 if (!jsonSchemaTemplates.containsKey(jsonTemplate.getName()) || !this.hasTemplate(jsonTemplate)) {
89 this.jsonSchemaTemplates.put(jsonTemplate.getName(), jsonTemplate);
94 * By name, find and remove a given template.
96 * @param nameTemplate name template
98 public void removeTemplate(String nameTemplate) {
99 this.jsonSchemaTemplates.remove(nameTemplate);
103 * Update Template : adding with true flag, removing with false.
105 * @param nameTemplate name template
106 * @param jsonTemplateField field name
107 * @param operation operation
109 public void updateTemplate(String nameTemplate, JsonTemplateField jsonTemplateField, Boolean operation) {
110 // Operation = true && field is not present => add Field
112 && !this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) {
113 this.jsonSchemaTemplates.get(nameTemplate).addField(jsonTemplateField);
115 // Operation = false && field is present => remove Field
117 && this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) {
118 this.jsonSchemaTemplates.get(nameTemplate).removeField(jsonTemplateField);
123 * Check if the JSONTemplates have the same bodies.
125 * @param jsonTemplate template
128 public boolean hasTemplate(JsonTemplate jsonTemplate) {
129 boolean duplicateTemplate = false;
130 Collection<String> templatesName = jsonSchemaTemplates.keySet();
131 if (templatesName.contains(jsonTemplate.getName())) {
132 JsonTemplate existingJsonTemplate = jsonSchemaTemplates.get(jsonTemplate.getName());
133 duplicateTemplate = existingJsonTemplate.checkFields(jsonTemplate);
135 return duplicateTemplate;
139 * For a given policy type, get a corresponding JsonObject from the tosca model.
141 * @param policyType The policy type in the tosca
142 * @param toscaMetadataParser The MetadataParser class that must be used if metadata section are encountered, if null
143 * they will be skipped
144 * @return an json object defining the equivalent json schema from the tosca for a given policy type
146 public JsonObject getJsonSchemaForPolicyType(String policyType, ToscaMetadataParser toscaMetadataParser,
147 Service serviceModel)
148 throws UnknownComponentException {
149 ToscaConverterToJsonSchema
150 toscaConverterToJsonSchema = new ToscaConverterToJsonSchema(toscaElements, jsonSchemaTemplates,
151 toscaMetadataParser, serviceModel);
152 if (toscaConverterToJsonSchema.getToscaElement(policyType) == null) {
153 throw new UnknownComponentException(policyType);
155 return toscaConverterToJsonSchema.getJsonSchemaOfToscaElement(policyType);
159 * Create and complete several Templates from file.properties.
161 * @param jsonTemplates The template properties as String
164 @SuppressWarnings("unused")
165 private LinkedHashMap<String, JsonTemplate> initializeTemplates(String jsonTemplates) {
167 LinkedHashMap<String, JsonTemplate> generatedTemplates = new LinkedHashMap<>();
168 JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
170 for (Map.Entry<String, JsonElement> templateAsJson : templates.entrySet()) {
171 JsonTemplate jsonTemplate = new JsonTemplate(templateAsJson.getKey());
172 JsonObject templateBody = (JsonObject) templateAsJson.getValue();
173 for (Map.Entry<String, JsonElement> field : templateBody.entrySet()) {
174 String fieldName = field.getKey();
175 JsonObject bodyFieldAsJson = (JsonObject) field.getValue();
176 Object fieldValue = bodyFieldAsJson.get("defaultValue").getAsString();
177 Boolean fieldVisible = bodyFieldAsJson.get("visible").getAsBoolean();
178 Boolean fieldStatic = bodyFieldAsJson.get("static").getAsBoolean();
180 bodyJsonTemplateField = new JsonTemplateField(fieldName, fieldValue, fieldVisible, fieldStatic);
181 jsonTemplate.getJsonTemplateFields().add(bodyJsonTemplateField);
183 generatedTemplates.put(jsonTemplate.getName(), jsonTemplate);
185 return generatedTemplates;