Fix the CDS calls
[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 import org.onap.clamp.loop.service.Service;
39
40 public class JsonTemplateManager {
41     private LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates;
42     private LinkedHashMap<String, ToscaElement> toscaElements;
43
44     /**
45      * Constructor.
46      *
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
50      */
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);
55         }
56         else {
57             toscaElements = null;
58         }
59     }
60
61     //GETTERS & SETTERS
62     public LinkedHashMap<String, ToscaElement> getToscaElements() {
63         return toscaElements;
64     }
65
66     public void setToscaElements(LinkedHashMap<String, ToscaElement> toscaElements) {
67         this.toscaElements = toscaElements;
68     }
69
70     public LinkedHashMap<String, JsonTemplate> getJsonSchemaTemplates() {
71         return jsonSchemaTemplates;
72     }
73
74     public void setJsonSchemaTemplates(LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates) {
75         this.jsonSchemaTemplates = jsonSchemaTemplates;
76     }
77
78     /**
79      * Add a template.
80      *
81      * @param name               name
82      * @param jsonTemplateFields fields
83      */
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);
90         }
91     }
92
93     /**
94      * By name, find and remove a given template.
95      *
96      * @param nameTemplate name template
97      */
98     public void removeTemplate(String nameTemplate) {
99         this.jsonSchemaTemplates.remove(nameTemplate);
100     }
101
102     /**
103      * Update Template : adding with true flag, removing with false.
104      *
105      * @param nameTemplate      name template
106      * @param jsonTemplateField field name
107      * @param operation         operation
108      */
109     public void updateTemplate(String nameTemplate, JsonTemplateField jsonTemplateField, Boolean operation) {
110         // Operation = true && field is not present => add Field
111         if (operation
112                 && !this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) {
113             this.jsonSchemaTemplates.get(nameTemplate).addField(jsonTemplateField);
114         }
115         // Operation = false && field is present => remove Field
116         else if (!operation
117                 && this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) {
118             this.jsonSchemaTemplates.get(nameTemplate).removeField(jsonTemplateField);
119         }
120     }
121
122     /**
123      * Check if the JSONTemplates have the same bodies.
124      *
125      * @param jsonTemplate template
126      * @return a boolean
127      */
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);
134         }
135         return duplicateTemplate;
136     }
137
138     /**
139      * For a given policy type, get a corresponding JsonObject from the tosca model.
140      *
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
145      */
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);
154         }
155         return toscaConverterToJsonSchema.getJsonSchemaOfToscaElement(policyType);
156     }
157
158     /**
159      * Create and complete several Templates from file.properties.
160      *
161      * @param jsonTemplates The template properties as String
162      * @return a map
163      */
164     @SuppressWarnings("unused")
165     private LinkedHashMap<String, JsonTemplate> initializeTemplates(String jsonTemplates) {
166
167         LinkedHashMap<String, JsonTemplate> generatedTemplates = new LinkedHashMap<>();
168         JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class);
169
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();
179                 JsonTemplateField
180                         bodyJsonTemplateField = new JsonTemplateField(fieldName, fieldValue, fieldVisible, fieldStatic);
181                 jsonTemplate.getJsonTemplateFields().add(bodyJsonTemplateField);
182             }
183             generatedTemplates.put(jsonTemplate.getName(), jsonTemplate);
184         }
185         return generatedTemplates;
186     }
187 }