diff options
author | sebdet <sebastien.determe@intl.att.com> | 2020-03-12 14:38:07 -0700 |
---|---|---|
committer | sebdet <sebastien.determe@intl.att.com> | 2020-03-13 11:03:29 -0700 |
commit | 723de7f63f0951d0cfe7a23956cf9d00128809b1 (patch) | |
tree | 751562c9a0c9a885f6be143c12a6c85472519afb /src/main/java/org | |
parent | 774b4ba65f0d23ae34d3bddb63058796121c1ae3 (diff) |
Fix the new tosca converter
Fix the metadata section analysis and make the tosca parser more configurable (in applications.properties)
Issue-ID: CLAMP-580
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Change-Id: I9068bd9dc89861c640660a7f78fae2cb70bdc178
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/main/java/org')
25 files changed, 834 insertions, 514 deletions
diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterManager.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterManager.java deleted file mode 100644 index b3224b04..00000000 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterManager.java +++ /dev/null @@ -1,192 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END============================================ - * =================================================================== - * - */ - -package org.onap.clamp.clds.tosca.update; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import java.io.IOException; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import org.onap.clamp.clds.util.JsonUtils; - -public class ToscaConverterManager { - - private LinkedHashMap<String, Template> templates; - private LinkedHashMap<String, ToscaElement> components; - private ToscaConverterToJson toscaConverterToJson; - private ToscaItemsParser toscaItemsParser; - - /** - * Constructor. - * - * @param toscaYamlContent Policy Tosca Yaml content as string - * @param nativeToscaDatatypes The tosca yaml with tosca native datatypes - * @param templateProperties template properties as string - * @throws IOException in case of failure - */ - public ToscaConverterManager(String toscaYamlContent, String nativeToscaDatatypes, String templateProperties) - throws IOException { - if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) { - this.toscaItemsParser = new ToscaItemsParser(toscaYamlContent, nativeToscaDatatypes); - this.components = toscaItemsParser.getAllItemsFound(); - this.templates = initializeTemplates(templateProperties); - } - else { - components = null; - } - } - - //GETTERS & SETTERS - public LinkedHashMap<String, ToscaElement> getComponents() { - return components; - } - - public void setComponents(LinkedHashMap<String, ToscaElement> components) { - this.components = components; - } - - public ToscaConverterToJson getParseToJson() { - return toscaConverterToJson; - } - - public void setParseToJson(ToscaConverterToJson toscaConverterToJson) { - this.toscaConverterToJson = toscaConverterToJson; - } - - public LinkedHashMap<String, Template> getTemplates() { - return templates; - } - - public void setTemplates(LinkedHashMap<String, Template> templates) { - this.templates = templates; - } - - public ToscaItemsParser getToscaItemsParser() { - return toscaItemsParser; - } - - /** - * Add a template. - * - * @param name name - * @param templateFields fields - */ - public void addTemplate(String name, List<TemplateField> templateFields) { - Template template = new Template(name, templateFields); - //If it is true, the operation does not have any interest : - // replace OR put two different object with the same body - if (!templates.containsKey(template.getName()) || !this.hasTemplate(template)) { - this.templates.put(template.getName(), template); - } - } - - /** - * By name, find and remove a given template. - * - * @param nameTemplate name template - */ - public void removeTemplate(String nameTemplate) { - this.templates.remove(nameTemplate); - } - - /** - * Update Template : adding with true flag, removing with false. - * - * @param nameTemplate name template - * @param templateField field name - * @param operation operation - */ - public void updateTemplate(String nameTemplate, TemplateField templateField, Boolean operation) { - // Operation = true && field is not present => add Field - if (operation && !this.templates.get(nameTemplate).getTemplateFields().contains(templateField)) { - this.templates.get(nameTemplate).addField(templateField); - } - // Operation = false && field is present => remove Field - else if (!operation && this.templates.get(nameTemplate).getTemplateFields().contains(templateField)) { - this.templates.get(nameTemplate).removeField(templateField); - } - } - - /** - * Check if the JSONTemplates have the same bodies. - * - * @param template template - * @return a boolean - */ - public boolean hasTemplate(Template template) { - boolean duplicateTemplate = false; - Collection<String> templatesName = templates.keySet(); - if (templatesName.contains(template.getName())) { - Template existingTemplate = templates.get(template.getName()); - duplicateTemplate = existingTemplate.checkFields(template); - } - return duplicateTemplate; - } - - /** - * For a given Component, get a corresponding JsonObject, through parseToJSON. - * - * @param componentName name - * @return an json object - */ - public JsonObject startConversionToJson(String componentName) throws UnknownComponentException { - this.toscaConverterToJson = new ToscaConverterToJson(components, templates); - if (toscaConverterToJson.matchComponent(componentName) == null) { - throw new UnknownComponentException(componentName); - } - return toscaConverterToJson.getJsonProcess(componentName, "object"); - } - - /** - * Create and complete several Templates from file.properties. - * - * @param jsonTemplates The template properties as String - * @return a map - */ - @SuppressWarnings("unused") - private LinkedHashMap<String, Template> initializeTemplates(String jsonTemplates) { - - LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>(); - JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class); - - for (Map.Entry<String, JsonElement> templateAsJson : templates.entrySet()) { - Template template = new Template(templateAsJson.getKey()); - JsonObject templateBody = (JsonObject) templateAsJson.getValue(); - for (Map.Entry<String, JsonElement> field : templateBody.entrySet()) { - String fieldName = field.getKey(); - JsonObject bodyFieldAsJson = (JsonObject) field.getValue(); - Object fieldValue = bodyFieldAsJson.get("defaultValue").getAsString(); - Boolean fieldVisible = bodyFieldAsJson.get("visible").getAsBoolean(); - Boolean fieldStatic = bodyFieldAsJson.get("static").getAsBoolean(); - TemplateField bodyTemplateField = new TemplateField(fieldName, fieldValue, fieldVisible, fieldStatic); - template.getTemplateFields().add(bodyTemplateField); - } - generatedTemplates.put(template.getName(), template); - } - return generatedTemplates; - } - -} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java new file mode 100644 index 00000000..c1bf1ad8 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.tosca.update; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.JsonObject; +import java.io.IOException; +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser; +import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParserWithDictionarySupport; +import org.onap.clamp.clds.tosca.update.templates.JsonTemplateManager; +import org.onap.clamp.clds.util.JsonUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ToscaConverterWithDictionarySupport { + + private static final EELFLogger logger = + EELFManager.getInstance().getLogger(ToscaConverterWithDictionarySupport.class); + + private ClampProperties clampProperties; + private ToscaMetadataParser metadataParser; + + /** + * Constructor with Spring support. + * + * @param clampProperties Clamp Spring properties + * @param metadataParser Metadata parser + */ + @Autowired + public ToscaConverterWithDictionarySupport(ClampProperties clampProperties, + ToscaMetadataParserWithDictionarySupport metadataParser) { + this.clampProperties = clampProperties; + this.metadataParser = metadataParser; + } + + /** + * This method converts a tosca file to a json schema. + * It uses some parameters specified in the application.properties. + * + * @param toscaFile The tosca file as String + * @param policyTypeToDecode The policy type to decode + * @return A json object being a json schema + */ + public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode) { + try { + return new JsonTemplateManager(toscaFile, + clampProperties.getFileContent("tosca.converter.default.datatypes"), + clampProperties.getFileContent("tosca.converter.json.schema.templates")) + .getJsonSchemaForPolicyType(policyTypeToDecode, Boolean.parseBoolean(clampProperties.getStringValue( + "tosca.converter.dictionary.support.enabled")) ? metadataParser : null); + } catch (IOException | UnknownComponentException e) { + logger.error("Unable to convert the tosca properly, exception caught during the decoding", + e); + return new JsonObject(); + } + } + + /** + * This method converts a tosca file to a json schema. + * It uses some parameters specified in the application.properties. + * + * @param toscaFile The tosca file as String + * @param policyTypeToDecode The policy type to decode + * @return A String containing the json schema + */ + public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode) { + return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode)); + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ArrayField.java b/src/main/java/org/onap/clamp/clds/tosca/update/elements/ArrayField.java index 61e40a1e..83f792f3 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ArrayField.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/elements/ArrayField.java @@ -21,7 +21,7 @@ *
*/
-package org.onap.clamp.clds.tosca.update;
+package org.onap.clamp.clds.tosca.update.elements;
import com.google.gson.JsonArray;
import java.util.ArrayList;
diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Constraint.java b/src/main/java/org/onap/clamp/clds/tosca/update/elements/Constraint.java index 4f6b27a6..d6bd355e 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Constraint.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/elements/Constraint.java @@ -21,21 +21,22 @@ *
*/
-package org.onap.clamp.clds.tosca.update;
+package org.onap.clamp.clds.tosca.update.elements;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
+import org.onap.clamp.clds.tosca.update.templates.JsonTemplate;
public class Constraint {
private LinkedHashMap<String, Object> constraints;
- private Template template;
+ private JsonTemplate jsonTemplate;
- public Constraint(LinkedHashMap<String, Object> constraints, Template template) {
- this.template = template;
+ public Constraint(LinkedHashMap<String, Object> constraints, JsonTemplate jsonTemplate) {
+ this.jsonTemplate = jsonTemplate;
this.constraints = constraints;
}
@@ -120,7 +121,7 @@ public class Constraint { checkTemplateField("maxLength", jsonSchema, fieldValue);
break;
case "array":
- if (fieldValue.equals(1) && template.hasFields("uniqueItems")) {
+ if (fieldValue.equals(1) && jsonTemplate.hasFields("uniqueItems")) {
jsonSchema.addProperty("uniqueItems", true);
} else {
checkTemplateField("minItems", jsonSchema, fieldValue);
@@ -171,7 +172,7 @@ public class Constraint { * @param typeProperty Get as Enum the valid values for the property
*/
public void getValueArray(JsonObject jsonSchema, Object fieldValue, String typeProperty) {
- if (template.hasFields("enum")) {
+ if (jsonTemplate.hasFields("enum")) {
JsonArray enumeration = new JsonArray();
if (typeProperty.equals("string") || typeProperty.equals("String")) {
ArrayList<String> arrayValues = (ArrayList<String>) fieldValue;
@@ -197,7 +198,7 @@ public class Constraint { * @param fieldValue Simple way to avoid code duplication
*/
public void checkTemplateField(String field, JsonObject jsonSchema, Object fieldValue) {
- if (template.hasFields(field)) {
+ if (jsonTemplate.hasFields(field)) {
String typeField = fieldValue.getClass().getSimpleName();
switch (typeField) {
case "String":
diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaElement.java b/src/main/java/org/onap/clamp/clds/tosca/update/elements/ToscaElement.java index d702cda5..9035a580 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaElement.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/elements/ToscaElement.java @@ -21,7 +21,7 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.elements; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -36,7 +36,7 @@ public class ToscaElement { private String version; private String typeVersion; private String description; - private LinkedHashMap<String, Property> properties; + private LinkedHashMap<String, ToscaElementProperty> properties; public ToscaElement() { } @@ -97,16 +97,16 @@ public class ToscaElement { this.description = description; } - public LinkedHashMap<String, Property> getProperties() { + public LinkedHashMap<String, ToscaElementProperty> getProperties() { return properties; } - public void setProperties(LinkedHashMap<String, Property> properties) { + public void setProperties(LinkedHashMap<String, ToscaElementProperty> properties) { this.properties = properties; } - public void addProperties(Property property) { - this.properties.put(property.getName(), property); + public void addProperties(ToscaElementProperty toscaElementProperty) { + this.properties.put(toscaElementProperty.getName(), toscaElementProperty); } public ArrayList<String> propertiesNames() { diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Property.java b/src/main/java/org/onap/clamp/clds/tosca/update/elements/ToscaElementProperty.java index 0b6e3816..c5ab5a18 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Property.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/elements/ToscaElementProperty.java @@ -21,14 +21,15 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.elements; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.LinkedHashMap; +import org.onap.clamp.clds.tosca.update.templates.JsonTemplate; -public class Property { +public class ToscaElementProperty { /** * name parameter is used as "key", in the LinkedHashMap of Components. @@ -42,7 +43,7 @@ public class Property { * @param name the name * @param items the items */ - public Property(String name, LinkedHashMap<String, Object> items) { + public ToscaElementProperty(String name, LinkedHashMap<String, Object> items) { super(); this.name = name; this.items = items; @@ -117,14 +118,14 @@ public class Property { * * @param json a json * @param constraints constraints - * @param template template + * @param jsonTemplate template */ @SuppressWarnings("unchecked") - public void addConstraintsAsJson(JsonObject json, ArrayList<Object> constraints, Template template) { + public void addConstraintsAsJson(JsonObject json, ArrayList<Object> constraints, JsonTemplate jsonTemplate) { for (Object constraint : constraints) { if (constraint instanceof LinkedHashMap) { LinkedHashMap<String, Object> valueConstraint = (LinkedHashMap<String, Object>) constraint; - Constraint constraintParser = new Constraint(valueConstraint, template); + Constraint constraintParser = new Constraint(valueConstraint, jsonTemplate); constraintParser.deployConstraints(json, (String) getItems().get("type")); } } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterToJson.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaConverterToJsonSchema.java index 297d568b..cfc0e42a 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterToJson.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaConverterToJsonSchema.java @@ -21,7 +21,7 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.parser; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -29,41 +29,40 @@ import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map.Entry; -import org.onap.clamp.tosca.DictionaryService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; +import org.onap.clamp.clds.tosca.update.elements.ToscaElement; +import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; +import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser; +import org.onap.clamp.clds.tosca.update.templates.JsonTemplate; -@Component -public class ToscaConverterToJson { +public class ToscaConverterToJsonSchema { private LinkedHashMap<String, ToscaElement> components; - private LinkedHashMap<String, Template> templates; + private LinkedHashMap<String, JsonTemplate> templates; - // if this one is set, the dictionary mechanism is enabled - @Autowired - private DictionaryService dictionaryService; + private ToscaMetadataParser metadataParser; - public ToscaConverterToJson(LinkedHashMap<String, ToscaElement> components, LinkedHashMap<String, Template> templates) { - this.components = components; - this.templates = templates; + /** + * Constructor. + * + * @param toscaElementsMap All the tosca elements found (policy type + data types + native tosca datatypes) + * @param jsonSchemaTemplates All Json schema templates to use + * @param metadataParser The metadata parser to use for metadata section + */ + public ToscaConverterToJsonSchema(LinkedHashMap<String, ToscaElement> toscaElementsMap, + LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates, + ToscaMetadataParser metadataParser) { + this.components = toscaElementsMap; + this.templates = jsonSchemaTemplates; + this.metadataParser = metadataParser; } /** * For a given component, launch process to parse it in Json. * - * @param nameComponent name components + * @param toscaElementKey name components * @return return */ - public JsonObject getJsonProcess(String nameComponent, String typeComponent) { - JsonObject glob = new JsonObject(); - - if (typeComponent.equals("object")) { - glob = this.getFieldAsObject(matchComponent(nameComponent)); - } - else { - /*glob = this.getFieldAsArray(matchComponent(nameComponent));*/ - } - - return glob; + public JsonObject getJsonSchemaOfToscaElement(String toscaElementKey) { + return this.getFieldAsObject(getToscaElement(toscaElementKey)); } /** @@ -110,11 +109,11 @@ public class ToscaConverterToJson { requirements.addAll(getRequirements(toParse.getDerivedFrom())); } //Each property is checked, and add to the requirement array if it's required - Collection<Property> properties = toParse.getProperties().values(); - for (Property property : properties) { - if (property.getItems().containsKey("required") - && property.getItems().get("required").equals(true)) { - requirements.add(property.getName()); + Collection<ToscaElementProperty> properties = toParse.getProperties().values(); + for (ToscaElementProperty toscaElementProperty : properties) { + if (toscaElementProperty.getItems().containsKey("required") + && toscaElementProperty.getItems().get("required").equals(true)) { + requirements.add(toscaElementProperty.getName()); } } return requirements; @@ -137,10 +136,10 @@ public class ToscaConverterToJson { } //For each component property, check if its a complex properties (a component) or not. In that case, //launch the analyse of the property. - for (Entry<String, Property> property : toParse.getProperties().entrySet()) { - if (matchComponent((String) property.getValue().getItems().get("type")) != null) { + for (Entry<String, ToscaElementProperty> property : toParse.getProperties().entrySet()) { + if (getToscaElement((String) property.getValue().getItems().get("type")) != null) { jsonSchema.add(property.getValue().getName(), - this.getJsonProcess((String) property.getValue().getItems().get("type"), "object")); + this.getJsonSchemaOfToscaElement((String) property.getValue().getItems().get("type"))); } else { jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue())); @@ -162,27 +161,27 @@ public class ToscaConverterToJson { /** * to be done. * - * @param property property + * @param toscaElementProperty property * @return a json object */ @SuppressWarnings("unchecked") - public JsonObject complexParse(Property property) { + public JsonObject complexParse(ToscaElementProperty toscaElementProperty) { JsonObject propertiesInJson = new JsonObject(); - Template currentPropertyTemplate; - String typeProperty = (String) property.getItems().get("type"); + JsonTemplate currentPropertyJsonTemplate; + String typeProperty = (String) toscaElementProperty.getItems().get("type"); if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) { - currentPropertyTemplate = templates.get("object"); + currentPropertyJsonTemplate = templates.get("object"); } else { - String propertyType = (String) property.getItems().get("type"); - currentPropertyTemplate = templates.get(propertyType.toLowerCase()); + String propertyType = (String) toscaElementProperty.getItems().get("type"); + currentPropertyJsonTemplate = templates.get(propertyType.toLowerCase()); } //Each "special" field is analysed, and has a specific treatment - for (String propertyField : property.getItems().keySet()) { + for (String propertyField : toscaElementProperty.getItems().keySet()) { switch (propertyField) { case "type": - if (currentPropertyTemplate.hasFields(propertyField)) { - String fieldtype = (String) property.getItems().get(propertyField); + if (currentPropertyJsonTemplate.hasFields(propertyField)) { + String fieldtype = (String) toscaElementProperty.getItems().get(propertyField); switch (fieldtype.toLowerCase()) { case "list": propertiesInJson.addProperty("type", "array"); @@ -204,48 +203,57 @@ public class ToscaConverterToJson { break; case "range": propertiesInJson.addProperty("type", "integer"); - if (!checkConstraintPresence(property, "greater_than") - && currentPropertyTemplate.hasFields("exclusiveMinimum")) { + if (!checkConstraintPresence(toscaElementProperty, "greater_than") + && currentPropertyJsonTemplate.hasFields("exclusiveMinimum")) { propertiesInJson.addProperty("exclusiveMinimum", false); } - if (!checkConstraintPresence(property, "less_than") - && currentPropertyTemplate.hasFields("exclusiveMaximum")) { + if (!checkConstraintPresence(toscaElementProperty, "less_than") + && currentPropertyJsonTemplate.hasFields("exclusiveMaximum")) { propertiesInJson.addProperty("exclusiveMaximum", false); } break; default: - propertiesInJson.addProperty("type", currentPropertyTemplate.getName()); + propertiesInJson.addProperty("type", currentPropertyJsonTemplate.getName()); break; } } break; case "metadata": - propertiesInJson.add("enum", MetadataParser.processAllMetadataElement(property, - dictionaryService)); + if (metadataParser != null) { + metadataParser.processAllMetadataElement(toscaElementProperty).entrySet() + .forEach((jsonEntry) -> { + propertiesInJson.add(jsonEntry.getKey(), + jsonEntry.getValue()); + + }); + } break; case "constraints": - property.addConstraintsAsJson(propertiesInJson, - (ArrayList<Object>) property.getItems().get("constraints"), - currentPropertyTemplate); + toscaElementProperty.addConstraintsAsJson(propertiesInJson, + (ArrayList<Object>) toscaElementProperty.getItems().get("constraints"), + currentPropertyJsonTemplate); break; case "entry_schema": //Here, a way to check if entry is a component (datatype) or a simple string - if (matchComponent(this.extractSpecificFieldFromMap(property, "entry_schema")) != null) { - String nameComponent = this.extractSpecificFieldFromMap(property, "entry_schema"); - ToscaConverterToJson child = new ToscaConverterToJson(components, templates); + if (getToscaElement(this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema")) + != null) { + String nameComponent = this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema"); + ToscaConverterToJsonSchema + child = new ToscaConverterToJsonSchema(components, templates, + metadataParser); JsonObject propertiesContainer = new JsonObject(); - switch ((String) property.getItems().get("type")) { + switch ((String) toscaElementProperty.getItems().get("type")) { case "map": // Get it as an object - JsonObject componentAsProperty = child.getJsonProcess(nameComponent, "object"); + JsonObject componentAsProperty = child.getJsonSchemaOfToscaElement(nameComponent); propertiesContainer.add(nameComponent, componentAsProperty); - if (currentPropertyTemplate.hasFields("properties")) { + if (currentPropertyJsonTemplate.hasFields("properties")) { propertiesInJson.add("properties", propertiesContainer); } break; default://list : get it as an Array - JsonObject componentAsItem = child.getJsonProcess(nameComponent, "object"); - if (currentPropertyTemplate.hasFields("properties")) { + JsonObject componentAsItem = child.getJsonSchemaOfToscaElement(nameComponent); + if (currentPropertyJsonTemplate.hasFields("properties")) { propertiesInJson.add("items", componentAsItem); } break; @@ -253,9 +261,10 @@ public class ToscaConverterToJson { } // Native cases - else if (property.getItems().get("type").equals("list")) { + else if (toscaElementProperty.getItems().get("type").equals("list")) { JsonObject itemContainer = new JsonObject(); - String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema"); + String valueInEntrySchema = + this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema"); itemContainer.addProperty("type", valueInEntrySchema); propertiesInJson.add("items", itemContainer); } @@ -264,9 +273,9 @@ public class ToscaConverterToJson { break; default: //Each classical field : type, description, default.. - if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) { - property.addFieldToJson(propertiesInJson, propertyField, - property.getItems().get(propertyField)); + if (currentPropertyJsonTemplate.hasFields(propertyField) && !propertyField.equals("required")) { + toscaElementProperty.addFieldToJson(propertiesInJson, propertyField, + toscaElementProperty.getItems().get(propertyField)); } break; } @@ -275,12 +284,12 @@ public class ToscaConverterToJson { } /** - * Look for a matching Component for the name paramater, in the components list. + * Look for a matching Component for the name parameter, in the components list. * - * @param name the name - * @return a component + * @param name the tosca element name to search for + * @return a tosca element */ - public ToscaElement matchComponent(String name) { + public ToscaElement getToscaElement(String name) { ToscaElement correspondingToscaElement = null; if (components == null) { return null; @@ -296,28 +305,28 @@ public class ToscaConverterToJson { /** * Simple method to extract quickly a type field from particular property item. * - * @param property the property - * @param fieldName the fieldname + * @param toscaElementProperty the property + * @param fieldName the fieldname * @return a string */ @SuppressWarnings("unchecked") - public String extractSpecificFieldFromMap(Property property, String fieldName) { + public String extractSpecificFieldFromMap(ToscaElementProperty toscaElementProperty, String fieldName) { LinkedHashMap<String, String> entrySchemaFields = - (LinkedHashMap<String, String>) property.getItems().get(fieldName); + (LinkedHashMap<String, String>) toscaElementProperty.getItems().get(fieldName); return entrySchemaFields.get("type"); } /** * Check if a constraint, for a specific property, is there. * - * @param property property - * @param nameConstraint name constraint + * @param toscaElementProperty property + * @param nameConstraint name constraint * @return a flag boolean */ - public boolean checkConstraintPresence(Property property, String nameConstraint) { + public boolean checkConstraintPresence(ToscaElementProperty toscaElementProperty, String nameConstraint) { boolean presentConstraint = false; - if (property.getItems().containsKey("constraints")) { - ArrayList<Object> constraints = (ArrayList) property.getItems().get("constraints"); + if (toscaElementProperty.getItems().containsKey("constraints")) { + ArrayList<Object> constraints = (ArrayList) toscaElementProperty.getItems().get("constraints"); for (Object constraint : constraints) { if (constraint instanceof LinkedHashMap) { if (((LinkedHashMap) constraint).containsKey(nameConstraint)) { diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaElementParser.java index 443a4b0c..090fcfcf 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaElementParser.java @@ -21,38 +21,30 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.parser; -import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map.Entry; +import org.onap.clamp.clds.tosca.update.elements.ToscaElement; +import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; import org.yaml.snakeyaml.Yaml; -public class ToscaItemsParser { - private LinkedHashMap<String, ToscaElement> allItemsFound; - +public class ToscaElementParser { /** * Constructor. - * - * @param toscaYaml The tosca to parse - * @param toscaNativeDataTypeYaml THe name of the policy type to search */ - public ToscaItemsParser(String toscaYaml, String toscaNativeDataTypeYaml) { - this.allItemsFound = searchAllToscaElements(toscaYaml, toscaNativeDataTypeYaml); - } - - public LinkedHashMap<String, ToscaElement> getAllItemsFound() { - return allItemsFound; + private ToscaElementParser() { } private static LinkedHashMap<String, Object> searchAllDataTypesAndPolicyTypes(String toscaYaml) { LinkedHashMap<String, LinkedHashMap<String, Object>> file = (LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaYaml); - // Get DataTypes - LinkedHashMap<String, Object> allItemsFound = file.get("data_types"); - allItemsFound = (allItemsFound == null) ? (new LinkedHashMap<>()) : allItemsFound; + LinkedHashMap<String, Object> allDataTypesFound = file.get("data_types"); + LinkedHashMap<String, Object> allPolicyTypesFound = file.get("policy_types"); + LinkedHashMap<String, Object> allItemsFound = new LinkedHashMap<>(); // Put the policies and datatypes in the same collection - allItemsFound.putAll(file.get("policy_types")); + allItemsFound = (allDataTypesFound == null) ? (new LinkedHashMap<>()) : allDataTypesFound; + allItemsFound.putAll(allPolicyTypesFound == null ? new LinkedHashMap<>() : allPolicyTypesFound); return allItemsFound; } @@ -65,10 +57,12 @@ public class ToscaItemsParser { * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract * all entities and put them at the same level. * - * @return a map + * @param toscaYaml the tosca model content + * @param nativeToscaYaml the tosca native datatype content + * @return a map of Tosca Element containing all tosca elements found (policy types and datatypes) */ - private static LinkedHashMap<String, ToscaElement> searchAllToscaElements(String toscaYaml, - String nativeToscaYaml) { + public static LinkedHashMap<String, ToscaElement> searchAllToscaElements(String toscaYaml, + String nativeToscaYaml) { LinkedHashMap<String, Object> allItemsFound = searchAllDataTypesAndPolicyTypes(toscaYaml); allItemsFound.putAll(searchAllNativeToscaDataTypes(nativeToscaYaml)); return parseAllItemsFound(allItemsFound); @@ -97,9 +91,9 @@ public class ToscaItemsParser { LinkedHashMap<String, Object> properties = (LinkedHashMap<String, Object>) componentBody.get("properties"); for (Entry<String, Object> itemToProperty : properties.entrySet()) { - Property property = new Property(itemToProperty.getKey(), + ToscaElementProperty toscaElementProperty = new ToscaElementProperty(itemToProperty.getKey(), (LinkedHashMap<String, Object>) itemToProperty.getValue()); - toscaElement.addProperties(property); + toscaElement.addProperties(toscaElementProperty); } } allItemsFound.put(toscaElement.getName(), toscaElement); diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/MetadataParser.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParser.java index fb70231f..a51818e2 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/MetadataParser.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParser.java @@ -1,11 +1,10 @@ - /*- * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ * Copyright (C) 2020 AT&T Intellectual Property. All rights * reserved. - * ================================================================================ + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,25 +21,11 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.parser.metadata; import com.google.gson.JsonObject; -import org.onap.clamp.tosca.DictionaryService; - -public class MetadataParser { +import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; - /** - * This method is used to start the processing of the metadata field. - * - * @param property The property metadata as Json Object - * @param dictionaryService the Dictionary service, if null nothing will be done - * @return The jsonObject structure that must be added to the json schema - */ - public static JsonObject processAllMetadataElement(Property property, DictionaryService dictionaryService) { - if (dictionaryService != null) { - return null; - } else { - return null; - } - } +public interface ToscaMetadataParser { + JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty); } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java new file mode 100644 index 00000000..349ccee9 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java @@ -0,0 +1,178 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.tosca.update.parser.metadata; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Optional; +import org.json.JSONArray; +import org.onap.clamp.clds.tosca.JsonEditorSchemaConstants; +import org.onap.clamp.clds.tosca.ToscaSchemaConstants; +import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; +import org.onap.clamp.tosca.DictionaryElement; +import org.onap.clamp.tosca.DictionaryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser { + + @Autowired + private DictionaryService dictionaryService; + + /** + * This method is used to start the processing of the metadata field. + * + * @param toscaElementProperty The property metadata as Json Object + * @return The jsonObject structure that must be added to the json schema + */ + public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty) { + if (dictionaryService != null) { + return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService); + } + else { + return null; + } + } + + private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap, + DictionaryService dictionaryService) { + JsonObject childObject = new JsonObject(); + if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA) + && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) { + LinkedHashMap<String, Object> metadataMap = + (LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA); + if (metadataMap != null) { + metadataMap.entrySet().stream().forEach(constraint -> { + if (constraint.getKey() + .equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) { + JSONArray validValuesArray = new JSONArray(); + if (constraint.getValue() instanceof ArrayList<?>) { + boolean processDictionary = ((ArrayList<?>) constraint.getValue()) + .stream().anyMatch(value -> (value instanceof String + && ((String) value).contains(ToscaSchemaConstants.DICTIONARY))); + if (processDictionary) { + ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> { + if ((value instanceof String && ((String) value) + .contains(ToscaSchemaConstants.DICTIONARY))) { + processDictionaryElements((String) value, childObject, dictionaryService); + } + + }); + } + } + } + }); + } + } + return childObject; + } + + private static void processDictionaryElements(String dictionaryReference, JsonObject childObject, + DictionaryService dictionaryService) { + String[] dictionaryKeyArray = + dictionaryReference.substring(dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11, + dictionaryReference.length()).split("#"); + if (dictionaryKeyArray.length > 1) { + // We support only one # as of now. + List<DictionaryElement> dictionaryElements = null; + if (dictionaryKeyArray.length == 2) { + dictionaryElements = new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0]) + .getDictionaryElements()); + JsonArray subDictionaryNames = new JsonArray(); + new ArrayList<DictionaryElement>(dictionaryService.getDictionary(dictionaryKeyArray[1]) + .getDictionaryElements()).forEach(elem -> subDictionaryNames.add(elem.getShortName())); + + JsonArray jsonArray = new JsonArray(); + + Optional.of(dictionaryElements).get().stream().forEach(c -> { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType())); + if (c.getType() != null + && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) { + jsonObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1); + + } + jsonObject.addProperty(JsonEditorSchemaConstants.ID, c.getName()); + jsonObject.addProperty(JsonEditorSchemaConstants.LABEL, c.getShortName()); + jsonObject.add(JsonEditorSchemaConstants.OPERATORS, subDictionaryNames); + jsonArray.add(jsonObject); + }); + + JsonObject filterObject = new JsonObject(); + filterObject.add(JsonEditorSchemaConstants.FILTERS, jsonArray); + + childObject.addProperty(JsonEditorSchemaConstants.TYPE, + JsonEditorSchemaConstants.TYPE_QBLDR); + // TO invoke validation on such parameters + childObject.addProperty(JsonEditorSchemaConstants.MIN_LENGTH, 1); + childObject.add(JsonEditorSchemaConstants.QSSCHEMA, filterObject); + + } + } + else { + List<DictionaryElement> dictionaryElements = + new ArrayList<>(dictionaryService.getDictionary(dictionaryKeyArray[0]).getDictionaryElements()); + JsonArray dictionaryNames = new JsonArray(); + JsonArray dictionaryFullNames = new JsonArray(); + dictionaryElements.stream().forEach(c -> { + // Json type will be translated before Policy creation + if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) { + dictionaryFullNames.add(c.getName()); + } + dictionaryNames.add(c.getShortName()); + }); + + if (dictionaryFullNames.size() > 0) { + childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryFullNames); + // Add Enum titles for generated translated values during JSON instance + // generation + JsonObject enumTitles = new JsonObject(); + enumTitles.add(JsonEditorSchemaConstants.ENUM_TITLES, dictionaryNames); + childObject.add(JsonEditorSchemaConstants.OPTIONS, enumTitles); + } + else { + childObject.add(JsonEditorSchemaConstants.ENUM, dictionaryNames); + } + } + } + + private static String getJsonType(String toscaType) { + String jsonType = null; + if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) { + jsonType = JsonEditorSchemaConstants.TYPE_INTEGER; + } + else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) { + jsonType = JsonEditorSchemaConstants.TYPE_ARRAY; + } + else { + jsonType = JsonEditorSchemaConstants.TYPE_STRING; + } + return jsonType; + } + +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Template.java b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplate.java index 6a531aee..f64ba68c 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Template.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplate.java @@ -21,28 +21,28 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.templates; import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.List; -public class Template { +public class JsonTemplate { /** * name parameter is used as "key", in the LinkedHashMap of Templates. */ private String name; - private List<TemplateField> templateFields; + private List<JsonTemplateField> jsonTemplateFields; - public Template(String name) { + public JsonTemplate(String name) { this.name = name; - this.templateFields = new ArrayList<>(); + this.jsonTemplateFields = new ArrayList<>(); } - public Template(String name, List<TemplateField> templateFields) { + public JsonTemplate(String name, List<JsonTemplateField> jsonTemplateFields) { this.name = name; - this.templateFields = templateFields; + this.jsonTemplateFields = jsonTemplateFields; } public String getName() { @@ -53,12 +53,12 @@ public class Template { this.name = name; } - public List<TemplateField> getTemplateFields() { - return templateFields; + public List<JsonTemplateField> getJsonTemplateFields() { + return jsonTemplateFields; } - public void setTemplateFields(List<TemplateField> templateFields) { - this.templateFields = templateFields; + public void setJsonTemplateFields(List<JsonTemplateField> jsonTemplateFields) { + this.jsonTemplateFields = jsonTemplateFields; } /** @@ -68,8 +68,8 @@ public class Template { * @return Ture if it exists, false otherwise */ public boolean hasFields(String fieldName) { - for (TemplateField templateField : this.getTemplateFields()) { - if (templateField.getTitle().equals(fieldName)) { + for (JsonTemplateField jsonTemplateField : this.getJsonTemplateFields()) { + if (jsonTemplateField.getTitle().equals(fieldName)) { return true; } } @@ -82,21 +82,21 @@ public class Template { * @param fieldName The field name * @return THe Field found */ - public TemplateField getSpecificField(String fieldName) { - for (TemplateField templateField : this.getTemplateFields()) { - if (templateField.getTitle().equals(fieldName)) { - return templateField; + public JsonTemplateField getSpecificField(String fieldName) { + for (JsonTemplateField jsonTemplateField : this.getJsonTemplateFields()) { + if (jsonTemplateField.getTitle().equals(fieldName)) { + return jsonTemplateField; } } return null; } - public void addField(TemplateField templateField) { - templateFields.add(templateField); + public void addField(JsonTemplateField jsonTemplateField) { + jsonTemplateFields.add(jsonTemplateField); } - public void removeField(TemplateField templateField) { - templateFields.remove(templateField); + public void removeField(JsonTemplateField jsonTemplateField) { + jsonTemplateFields.remove(jsonTemplateField); } /** @@ -106,9 +106,9 @@ public class Template { * @param state True or false */ public void setVisibility(String nameField, boolean state) { - for (TemplateField templateField : this.templateFields) { - if (templateField.getTitle().equals(nameField)) { - templateField.setVisible(state); + for (JsonTemplateField jsonTemplateField : this.jsonTemplateFields) { + if (jsonTemplateField.getTitle().equals(nameField)) { + jsonTemplateField.setVisible(state); } } } @@ -120,9 +120,9 @@ public class Template { * @param state true or false */ public void setStatic(String nameField, boolean state) { - for (TemplateField templateField : this.templateFields) { - if (templateField.getTitle().equals(nameField)) { - templateField.setStaticValue(state); + for (JsonTemplateField jsonTemplateField : this.jsonTemplateFields) { + if (jsonTemplateField.getTitle().equals(nameField)) { + jsonTemplateField.setStaticValue(state); } } } @@ -134,9 +134,9 @@ public class Template { * @param newValue The new value as Object */ public void updateValueField(String nameField, Object newValue) { - for (TemplateField templateField : this.templateFields) { - if (templateField.getTitle().equals(nameField)) { - templateField.setValue(newValue); + for (JsonTemplateField jsonTemplateField : this.jsonTemplateFields) { + if (jsonTemplateField.getTitle().equals(nameField)) { + jsonTemplateField.setValue(newValue); } } } @@ -144,23 +144,23 @@ public class Template { /** * Compare two templates : size and their contents. * - * @param template the template + * @param jsonTemplate the template * @return a boolean */ - public boolean checkFields(Template template) { + public boolean checkFields(JsonTemplate jsonTemplate) { boolean duplicateFields = false; - if (template.getTemplateFields().size() == this.getTemplateFields().size()) { + if (jsonTemplate.getJsonTemplateFields().size() == this.getJsonTemplateFields().size()) { int countMatchingFields = 0; //loop each component of first - for (TemplateField templateFieldToCheck : template.getTemplateFields()) { - for (TemplateField templateField : this.getTemplateFields()) { - if (templateFieldToCheck.compareWithField(templateField)) { + for (JsonTemplateField jsonTemplateFieldToCheck : jsonTemplate.getJsonTemplateFields()) { + for (JsonTemplateField jsonTemplateField : this.getJsonTemplateFields()) { + if (jsonTemplateFieldToCheck.compareWithField(jsonTemplateField)) { countMatchingFields++; } } } - if (template.getTemplateFields().size() == countMatchingFields) { + if (jsonTemplate.getJsonTemplateFields().size() == countMatchingFields) { duplicateFields = true; } } @@ -212,13 +212,13 @@ public class Template { */ public void injectStaticValue(JsonObject jsonSchema, String fieldName) { if (isVisible(fieldName)) { - TemplateField toInject = this.getSpecificField(fieldName); + JsonTemplateField toInject = this.getSpecificField(fieldName); jsonSchema.addProperty(fieldName, (String) toInject.getValue()); } } @Override public String toString() { - return " templateFields : " + templateFields; + return " templateFields : " + jsonTemplateFields; } } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/TemplateField.java b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateField.java index 34446436..a1e15307 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/TemplateField.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateField.java @@ -21,15 +21,15 @@ * */ -package org.onap.clamp.clds.tosca.update; +package org.onap.clamp.clds.tosca.update.templates; -public class TemplateField { +public class JsonTemplateField { private String title; private Object value; private Boolean visible; private Boolean staticValue; - public TemplateField(String title) { + public JsonTemplateField(String title) { this.title = title; } @@ -41,7 +41,7 @@ public class TemplateField { * @param visible visible or not * @param staticValue The static value */ - public TemplateField(String title, Object value, Boolean visible, Boolean staticValue) { + public JsonTemplateField(String title, Object value, Boolean visible, Boolean staticValue) { this.title = title; this.value = value; this.visible = visible; @@ -98,18 +98,19 @@ public class TemplateField { return false; } - TemplateField templateField = (TemplateField) otherField; + JsonTemplateField jsonTemplateField = (JsonTemplateField) otherField; - if (title != null ? !title.equals(templateField.title) : templateField.title != null) { + if (title != null ? !title.equals(jsonTemplateField.title) : jsonTemplateField.title != null) { return false; } - if (value != null ? !value.equals(templateField.value) : templateField.value != null) { + if (value != null ? !value.equals(jsonTemplateField.value) : jsonTemplateField.value != null) { return false; } - if (visible != null ? !visible.equals(templateField.visible) : templateField.visible != null) { + if (visible != null ? !visible.equals(jsonTemplateField.visible) : jsonTemplateField.visible != null) { return false; } - return staticValue != null ? staticValue.equals(templateField.staticValue) : templateField.staticValue == null; + return staticValue != null ? staticValue.equals(jsonTemplateField.staticValue) : + jsonTemplateField.staticValue == null; } @Override @@ -121,9 +122,9 @@ public class TemplateField { return false; } - TemplateField templateField = (TemplateField) object; + JsonTemplateField jsonTemplateField = (JsonTemplateField) object; - return title != null ? title.equals(templateField.title) : templateField.title == null; + return title != null ? title.equals(jsonTemplateField.title) : jsonTemplateField.title == null; } @Override @@ -134,15 +135,15 @@ public class TemplateField { /** * This method test the entire equality. * - * @param templateField1 object one - * @param templateField2 object two + * @param jsonTemplateField1 object one + * @param jsonTemplateField2 object two * @return true if they are totally equals (all attributes, false otherwise */ - public static boolean fieldsEquals(TemplateField templateField1, TemplateField templateField2) { - return (templateField2.getTitle().equals(templateField1.getTitle()) - && templateField2.getValue().equals(templateField1.getValue()) - && templateField2.getVisible().equals(templateField1.getVisible()) - && templateField2.getStaticValue().equals(templateField1.getStaticValue())); + public static boolean fieldsEquals(JsonTemplateField jsonTemplateField1, JsonTemplateField jsonTemplateField2) { + return (jsonTemplateField2.getTitle().equals(jsonTemplateField1.getTitle()) + && jsonTemplateField2.getValue().equals(jsonTemplateField1.getValue()) + && jsonTemplateField2.getVisible().equals(jsonTemplateField1.getVisible()) + && jsonTemplateField2.getStaticValue().equals(jsonTemplateField1.getStaticValue())); } } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java new file mode 100644 index 00000000..5e4b6600 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java @@ -0,0 +1,184 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.tosca.update.templates; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.onap.clamp.clds.tosca.update.UnknownComponentException; +import org.onap.clamp.clds.tosca.update.elements.ToscaElement; +import org.onap.clamp.clds.tosca.update.parser.ToscaConverterToJsonSchema; +import org.onap.clamp.clds.tosca.update.parser.ToscaElementParser; +import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser; +import org.onap.clamp.clds.util.JsonUtils; + +public class JsonTemplateManager { + private LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates; + private LinkedHashMap<String, ToscaElement> toscaElements; + + /** + * Constructor. + * + * @param toscaYamlContent Policy Tosca Yaml content as string + * @param nativeToscaDatatypes The tosca yaml with tosca native datatypes + * @param jsonSchemaTemplates template properties as string + */ + public JsonTemplateManager(String toscaYamlContent, String nativeToscaDatatypes, String jsonSchemaTemplates) { + if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) { + this.toscaElements = ToscaElementParser.searchAllToscaElements(toscaYamlContent, nativeToscaDatatypes); + this.jsonSchemaTemplates = initializeTemplates(jsonSchemaTemplates); + } + else { + toscaElements = null; + } + } + + //GETTERS & SETTERS + public LinkedHashMap<String, ToscaElement> getToscaElements() { + return toscaElements; + } + + public void setToscaElements(LinkedHashMap<String, ToscaElement> toscaElements) { + this.toscaElements = toscaElements; + } + + public LinkedHashMap<String, JsonTemplate> getJsonSchemaTemplates() { + return jsonSchemaTemplates; + } + + public void setJsonSchemaTemplates(LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates) { + this.jsonSchemaTemplates = jsonSchemaTemplates; + } + + /** + * Add a template. + * + * @param name name + * @param jsonTemplateFields fields + */ + public void addTemplate(String name, List<JsonTemplateField> jsonTemplateFields) { + JsonTemplate jsonTemplate = new JsonTemplate(name, jsonTemplateFields); + //If it is true, the operation does not have any interest : + // replace OR put two different object with the same body + if (!jsonSchemaTemplates.containsKey(jsonTemplate.getName()) || !this.hasTemplate(jsonTemplate)) { + this.jsonSchemaTemplates.put(jsonTemplate.getName(), jsonTemplate); + } + } + + /** + * By name, find and remove a given template. + * + * @param nameTemplate name template + */ + public void removeTemplate(String nameTemplate) { + this.jsonSchemaTemplates.remove(nameTemplate); + } + + /** + * Update Template : adding with true flag, removing with false. + * + * @param nameTemplate name template + * @param jsonTemplateField field name + * @param operation operation + */ + public void updateTemplate(String nameTemplate, JsonTemplateField jsonTemplateField, Boolean operation) { + // Operation = true && field is not present => add Field + if (operation && !this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) { + this.jsonSchemaTemplates.get(nameTemplate).addField(jsonTemplateField); + } + // Operation = false && field is present => remove Field + else if (!operation && this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) { + this.jsonSchemaTemplates.get(nameTemplate).removeField(jsonTemplateField); + } + } + + /** + * Check if the JSONTemplates have the same bodies. + * + * @param jsonTemplate template + * @return a boolean + */ + public boolean hasTemplate(JsonTemplate jsonTemplate) { + boolean duplicateTemplate = false; + Collection<String> templatesName = jsonSchemaTemplates.keySet(); + if (templatesName.contains(jsonTemplate.getName())) { + JsonTemplate existingJsonTemplate = jsonSchemaTemplates.get(jsonTemplate.getName()); + duplicateTemplate = existingJsonTemplate.checkFields(jsonTemplate); + } + return duplicateTemplate; + } + + /** + * For a given policy type, get a corresponding JsonObject from the tosca model. + * + * @param policyType The policy type in the tosca + * @param toscaMetadataParser The MetadataParser class that must be used if metadata section are encountered, if null + * they will be skipped + * @return an json object defining the equivalent json schema from the tosca for a given policy type + */ + public JsonObject getJsonSchemaForPolicyType(String policyType, ToscaMetadataParser toscaMetadataParser) + throws UnknownComponentException { + ToscaConverterToJsonSchema + toscaConverterToJsonSchema = new ToscaConverterToJsonSchema(toscaElements, jsonSchemaTemplates, + toscaMetadataParser); + if (toscaConverterToJsonSchema.getToscaElement(policyType) == null) { + throw new UnknownComponentException(policyType); + } + return toscaConverterToJsonSchema.getJsonSchemaOfToscaElement(policyType); + } + + /** + * Create and complete several Templates from file.properties. + * + * @param jsonTemplates The template properties as String + * @return a map + */ + @SuppressWarnings("unused") + private LinkedHashMap<String, JsonTemplate> initializeTemplates(String jsonTemplates) { + + LinkedHashMap<String, JsonTemplate> generatedTemplates = new LinkedHashMap<>(); + JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class); + + for (Map.Entry<String, JsonElement> templateAsJson : templates.entrySet()) { + JsonTemplate jsonTemplate = new JsonTemplate(templateAsJson.getKey()); + JsonObject templateBody = (JsonObject) templateAsJson.getValue(); + for (Map.Entry<String, JsonElement> field : templateBody.entrySet()) { + String fieldName = field.getKey(); + JsonObject bodyFieldAsJson = (JsonObject) field.getValue(); + Object fieldValue = bodyFieldAsJson.get("defaultValue").getAsString(); + Boolean fieldVisible = bodyFieldAsJson.get("visible").getAsBoolean(); + Boolean fieldStatic = bodyFieldAsJson.get("static").getAsBoolean(); + JsonTemplateField + bodyJsonTemplateField = new JsonTemplateField(fieldName, fieldValue, fieldVisible, fieldStatic); + jsonTemplate.getJsonTemplateFields().add(bodyJsonTemplateField); + } + generatedTemplates.put(jsonTemplate.getName(), jsonTemplate); + } + return generatedTemplates; + } + +} diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java b/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java index 846b3ab2..6cf342f2 100755 --- a/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java +++ b/src/main/java/org/onap/clamp/clds/util/drawing/ClampGraphBuilder.java @@ -88,7 +88,7 @@ public class ClampGraphBuilder { public ClampGraphBuilder addLoopElementModel(LoopElementModel loopElementModel) { if (LoopElementModel.MICRO_SERVICE_TYPE.equals(loopElementModel.getLoopElementType())) { microServices.add(new MicroServicePolicy(loopElementModel.getName(), - loopElementModel.getPolicyModels().first(), false, loopElementModel)); + loopElementModel.getPolicyModels().first(), false,null,loopElementModel,null,null)); } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(loopElementModel.getLoopElementType())) { policies.add(new OperationalPolicy(loopElementModel.getName(), null, null, loopElementModel.getPolicyModels().first(), loopElementModel, null, null)); diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 2bf3decd..dd6fbf05 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -27,6 +27,7 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; +import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; @@ -48,11 +49,11 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; -import org.apache.commons.lang3.RandomStringUtils; import org.hibernate.annotations.SortNatural; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.clds.util.drawing.SvgLoopGenerator; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; @@ -63,7 +64,6 @@ import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.service.Service; import org.onap.clamp.loop.template.LoopElementModel; import org.onap.clamp.loop.template.LoopTemplate; -import org.onap.clamp.policy.Policy; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; @@ -164,24 +164,29 @@ public class Loop extends AuditEntity implements Serializable { * @param name The loop name * @param loopTemplate The loop template from which a new loop instance must be created */ - public Loop(String name, LoopTemplate loopTemplate) { - this(name,""); + public Loop(String name, LoopTemplate loopTemplate, ToscaConverterWithDictionarySupport toscaConverter) { + this(name, ""); this.setLoopTemplate(loopTemplate); this.setModelService(loopTemplate.getModelService()); loopTemplate.getLoopElementModelsUsed().forEach(element -> { if (LoopElementModel.MICRO_SERVICE_TYPE.equals(element.getLoopElementModel().getLoopElementType())) { - this.addMicroServicePolicy(new MicroServicePolicy(Policy.generatePolicyName("MICROSERVICE_", - loopTemplate.getModelService().getName(),loopTemplate.getModelService().getVersion(), - RandomStringUtils.randomAlphanumeric(3),RandomStringUtils.randomAlphanumeric(3)), - element.getLoopElementModel().getPolicyModels().first(), false, element.getLoopElementModel())); - } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE + try { + this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel() + .createPolicyInstance(this, toscaConverter)); + } catch (IOException e) { + logger.error("Exception caught when creating the microservice policy instance of the loop " + + "instance", e); + } + } + else if (LoopElementModel.OPERATIONAL_POLICY_TYPE .equals(element.getLoopElementModel().getLoopElementType())) { - this.addOperationalPolicy(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL_", - loopTemplate.getModelService().getName(),loopTemplate.getModelService().getVersion(), - RandomStringUtils.randomAlphanumeric(3),RandomStringUtils.randomAlphanumeric(3)), null, - new JsonObject(), - element.getLoopElementModel().getPolicyModels().first(), element.getLoopElementModel(), - null,null)); + try { + this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel() + .createPolicyInstance(this, toscaConverter)); + } catch (IOException e) { + logger.error("Exception caught when creating the operational policy instance of the loop instance", + e); + } } }); } @@ -379,7 +384,8 @@ public class Loop extends AuditEntity implements Serializable { if (other.name != null) { return false; } - } else if (!name.equals(other.name)) { + } + else if (!name.equals(other.name)) { return false; } return true; diff --git a/src/main/java/org/onap/clamp/loop/LoopController.java b/src/main/java/org/onap/clamp/loop/LoopController.java index 1a4ae599..d230eb97 100644 --- a/src/main/java/org/onap/clamp/loop/LoopController.java +++ b/src/main/java/org/onap/clamp/loop/LoopController.java @@ -26,6 +26,7 @@ package org.onap.clamp.loop; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; +import java.io.IOException; import java.lang.reflect.Type; import java.util.List; import org.onap.clamp.clds.util.JsonUtils; @@ -105,7 +106,7 @@ public class LoopController { * @param policyVersion The policy model version * @return The loop modified */ - public Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) { + public Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException { return loopService.addOperationalPolicy(loopName, policyType, policyVersion); } diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index 953a5947..acd125b7 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -24,14 +24,14 @@ package org.onap.clamp.loop; import com.google.gson.JsonObject; +import java.io.IOException; import java.util.List; import java.util.Set; import javax.persistence.EntityNotFoundException; -import org.apache.commons.lang3.RandomStringUtils; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.loop.template.LoopTemplatesService; import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.loop.template.PolicyModelsService; -import org.onap.clamp.policy.Policy; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; @@ -57,6 +57,9 @@ public class LoopService { @Autowired private LoopTemplatesService loopTemplateService; + @Autowired + private ToscaConverterWithDictionarySupport toscaConverter; + Loop saveOrUpdateLoop(Loop loop) { return loopsRepository.save(loop); } @@ -76,12 +79,13 @@ public class LoopService { /** * Creates a Loop Instance from Loop Template Name. * - * @param loopName Name of the Loop to be created + * @param loopName Name of the Loop to be created * @param templateName Loop Template to used for Loop * @return Loop Instance */ public Loop createLoopFromTemplate(String loopName, String templateName) { - return loopsRepository.save(new Loop(loopName,loopTemplateService.getLoopTemplate(templateName))); + return loopsRepository + .save(new Loop(loopName, loopTemplateService.getLoopTemplate(templateName), toscaConverter)); } /** @@ -104,22 +108,21 @@ public class LoopService { /** * This method add an operational policy to a loop instance. + * This creates an operational policy from the policy model info and not the loop element model * - * @param loopName The loop name - * @param policyType The policy model type + * @param loopName The loop name + * @param policyType The policy model type * @param policyVersion The policy model version * @return The loop modified */ - Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) { + Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException { Loop loop = getLoop(loopName); PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion); if (policyModel == null) { return null; } loop.addOperationalPolicy( - new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL", loop.getModelService().getName(), - loop.getModelService().getVersion(), RandomStringUtils.randomAlphanumeric(3), - RandomStringUtils.randomAlphanumeric(4)), loop, null, policyModel, null, null, null)); + new OperationalPolicy(loop,loop.getModelService(), policyModel, toscaConverter)); return loopsRepository.saveAndFlush(loop); } diff --git a/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java b/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java index b7ed1de9..6db6d920 100644 --- a/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java +++ b/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java @@ -27,9 +27,7 @@ package org.onap.clamp.loop.service; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; - import java.util.Map.Entry; - import org.onap.clamp.clds.client.CdsServices; import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse; @@ -96,7 +94,8 @@ public class CsarServiceInstaller { if (SdcTypes.PNF == type || SdcTypes.VF == type) { JsonObject controllerProperties = createCdsArtifactProperties(nodeTemplate); if (controllerProperties != null) { - resourcesPropByType.getAsJsonObject(nodeTemplate.getName()).add("controllerProperties", controllerProperties); + resourcesPropByType.getAsJsonObject(nodeTemplate.getName()) + .add("controllerProperties", controllerProperties); } } } @@ -151,7 +150,8 @@ public class CsarServiceInstaller { Object artifactName = nodeTemplate.getPropertyValue("sdnc_model_name"); Object artifactVersion = nodeTemplate.getPropertyValue("sdnc_model_version"); if (artifactName != null && artifactVersion != null) { - CdsBpWorkFlowListResponse response = queryCdsToGetWorkFlowList(artifactName.toString(), artifactVersion.toString()); + CdsBpWorkFlowListResponse response = + queryCdsToGetWorkFlowList(artifactName.toString(), artifactVersion.toString()); if (response == null) { return null; } @@ -159,7 +159,7 @@ public class CsarServiceInstaller { JsonObject workFlowProps = new JsonObject(); for (String workFlow : response.getWorkflows()) { JsonObject inputs = queryCdsToGetWorkFlowInputProperties(response.getBlueprintName(), - response.getVersion(), workFlow); + response.getVersion(), workFlow); workFlowProps.add(workFlow, inputs); } @@ -176,7 +176,8 @@ public class CsarServiceInstaller { return cdsServices.getBlueprintWorkflowList(artifactName, artifactVersion); } - private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion, String workFlow) { + private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion, + String workFlow) { return cdsServices.getWorkflowInputProperties(artifactName, artifactVersion, workFlow); } } diff --git a/src/main/java/org/onap/clamp/loop/service/Service.java b/src/main/java/org/onap/clamp/loop/service/Service.java index 89c0b2d4..338032a1 100644 --- a/src/main/java/org/onap/clamp/loop/service/Service.java +++ b/src/main/java/org/onap/clamp/loop/service/Service.java @@ -27,15 +27,12 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; - import java.io.Serializable; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; - import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; @@ -44,7 +41,7 @@ import org.onap.clamp.dao.model.jsontype.StringJsonUserType; @Entity @Table(name = "services") -@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) }) +@TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public class Service implements Serializable { /** @@ -120,6 +117,8 @@ public class Service implements Serializable { } /** + * Name getter. + * * @return the name */ public String getName() { @@ -127,6 +126,8 @@ public class Service implements Serializable { } /** + * Version getter. + * * @return the version */ public String getVersion() { @@ -157,7 +158,8 @@ public class Service implements Serializable { if (other.serviceUuid != null) { return false; } - } else if (!serviceUuid.equals(other.serviceUuid)) { + } + else if (!serviceUuid.equals(other.serviceUuid)) { return false; } return true; diff --git a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java index 47960598..dfdfc42b 100644 --- a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java +++ b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java @@ -24,6 +24,7 @@ package org.onap.clamp.loop.template; import com.google.gson.annotations.Expose; +import java.io.IOException; import java.io.Serializable; import java.util.HashSet; import java.util.Set; @@ -40,7 +41,12 @@ import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.SortNatural; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; +import org.onap.clamp.loop.Loop; import org.onap.clamp.loop.common.AuditEntity; +import org.onap.clamp.policy.Policy; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; /** * This class represents a micro service/operational/... model for a loop template. @@ -241,6 +247,24 @@ public class LoopElementModel extends AuditEntity implements Serializable { this.blueprint = blueprint; } + /** + * Create a policy instance from the current loop element model. + * + * @return A Policy object. + * @throws IOException in case of failure when creating an operational policy + */ + public Policy createPolicyInstance(Loop loop, ToscaConverterWithDictionarySupport toscaConverter) + throws IOException { + if (LoopElementModel.MICRO_SERVICE_TYPE.equals(this.getLoopElementType())) { + return new MicroServicePolicy(loop, loop.getModelService(), this, toscaConverter); + } + else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(this.getLoopElementType())) { + return new OperationalPolicy(loop, loop.getModelService(), this, toscaConverter); + } else { + return null; + } + } + @Override public int hashCode() { final int prime = 31; @@ -265,7 +289,8 @@ public class LoopElementModel extends AuditEntity implements Serializable { if (other.name != null) { return false; } - } else if (!name.equals(other.name)) { + } + else if (!name.equals(other.name)) { return false; } return true; diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java index aeea55db..ae66c54b 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -56,7 +56,7 @@ public class PolicyModelsService { * @return The Policy Model */ public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) { - return policyModelsRepository.save(policyModel); + return policyModelsRepository.saveAndFlush(policyModel); } /** diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index c9055bf9..3b220646 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -30,7 +30,6 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; -import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; import javax.persistence.Column; @@ -44,9 +43,6 @@ import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.json.JSONObject; -import org.onap.clamp.clds.tosca.update.ToscaConverterManager; -import org.onap.clamp.clds.tosca.update.UnknownComponentException; -import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.template.LoopElementModel; @@ -286,23 +282,4 @@ public abstract class Policy extends AuditEntity { .append(blueprintFilename.replaceAll(".yaml", "")); return buffer.toString().replace('.', '_').replaceAll(" ", ""); } - - /** - * This method can be used to generate the json Schema used by the UI. - * - * @param policyToscaModel The tosca model as String that must be converted - * @param policyModelType The tosca model type (the policy_type entry in the tosca) that will used to create the - * json schema - * @return THe Json Schema as JsonObject - * @throws IOException In case of failure when opening the templates.json file - * @throws UnknownComponentException If the policyModelType is not found in the tosca model - */ - public static JsonObject generateJsonRepresentationFromToscaModel(String policyToscaModel, - String policyModelType) - throws IOException, UnknownComponentException { - return new ToscaConverterManager(policyToscaModel,ResourceFileUtil.getResourceAsString( - "clds/tosca_update/default-tosca-types.yaml"), - ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json")) - .startConversionToJson(policyModelType); - } } diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index b8093ccf..321c12f6 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -27,7 +27,6 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; -import java.io.IOException; import java.io.Serializable; import java.util.HashSet; import java.util.Set; @@ -38,11 +37,13 @@ import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Transient; +import org.apache.commons.lang3.RandomStringUtils; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; -import org.onap.clamp.clds.tosca.update.UnknownComponentException; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.service.Service; import org.onap.clamp.loop.template.LoopElementModel; import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.Policy; @@ -91,31 +92,10 @@ public class MicroServicePolicy extends Policy implements Serializable { @Column(name = "dcae_blueprint_id") private String dcaeBlueprintId; - public MicroServicePolicy() { - // serialization - } - /** - * The constructor that creates the json representation from the policyTosca - * using the ToscaYamlToJsonConvertor. - * - * @param name The name of the MicroService - * @param policyModel The policy model of the MicroService - * @param shared The flag indicate whether the MicroService is shared + * Constructor for serialization. */ - public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, LoopElementModel loopElementModel) { - this.name = name; - this.setPolicyModel(policyModel); - this.shared = shared; - try { - this.setJsonRepresentation( - Policy.generateJsonRepresentationFromToscaModel(policyModel.getPolicyModelTosca(), - policyModel.getPolicyModelType())); - } catch (UnknownComponentException | NullPointerException | IOException e) { - logger.error("Unable to generate the microservice policy Schema ... ", e); - this.setJsonRepresentation(new JsonObject()); - } - this.setLoopElementModel(loopElementModel); + public MicroServicePolicy() { } /** @@ -129,7 +109,7 @@ public class MicroServicePolicy extends Policy implements Serializable { * @param jsonRepresentation The UI representation in json format * @param loopElementModel The loop element model from which this instance should be created * @param pdpGroup The Pdp Group info - * @param pdpSubgroup The Pdp Subgrouop info + * @param pdpSubgroup The Pdp Subgroup info */ public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup, @@ -143,6 +123,25 @@ public class MicroServicePolicy extends Policy implements Serializable { this.setPdpSubgroup(pdpSubgroup); } + /** + * Constructor with tosca converter. + * + * @param loop The loop instance + * @param service The service model object + * @param loopElementModel The loop element model from which this microservice instance is created + * @param toscaConverter The tosca converter that will used to convert the tosca policy model + */ + public MicroServicePolicy(Loop loop, Service service, LoopElementModel loopElementModel, + ToscaConverterWithDictionarySupport toscaConverter) { + this(Policy.generatePolicyName("MICROSERVICE", service.getName(), service.getVersion(), + RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), + loopElementModel.getPolicyModels().first(), false, + toscaConverter.convertToscaToJsonSchemaObject( + loopElementModel.getPolicyModels().first().getPolicyModelTosca(), + loopElementModel.getPolicyModels().first().getPolicyModelType()), + loopElementModel, null, null); + } + @Override public String getName() { return name; diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index 9cf51633..355a889e 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -48,11 +48,13 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; +import org.apache.commons.lang3.RandomStringUtils; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; -import org.onap.clamp.clds.tosca.update.UnknownComponentException; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.service.Service; import org.onap.clamp.loop.template.LoopElementModel; import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.Policy; @@ -80,54 +82,101 @@ public class OperationalPolicy extends Policy implements Serializable { @JoinColumn(name = "loop_id", nullable = false) private Loop loop; + /** + * Constructor for serialization. + */ public OperationalPolicy() { - // Serialization } /** * The constructor. * * @param name The name of the operational policy - * @param loop The loop that uses this operational policy * @param configurationsJson The operational policy property in the format of * json + * @param jsonRepresentation The jsonObject defining the json schema * @param policyModel The policy model associated if any, can be null * @param loopElementModel The loop element from which this instance is supposed to be created * @param pdpGroup The Pdp Group info * @param pdpSubgroup The Pdp Subgroup info */ - public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson, PolicyModel policyModel, + public OperationalPolicy(String name, JsonObject configurationsJson, + JsonObject jsonRepresentation, PolicyModel policyModel, LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) { this.name = name; - this.loop = loop; this.setPolicyModel(policyModel); this.setConfigurationsJson(configurationsJson); this.setPdpGroup(pdpGroup); this.setPdpSubgroup(pdpSubgroup); this.setLoopElementModel(loopElementModel); - this.setJsonRepresentation(this.generateJsonRepresentation(policyModel)); + this.setJsonRepresentation(jsonRepresentation); + + } + /** + * Create an operational policy from a loop element model. + * + * @param loop The parent loop + * @param service The loop service + * @param loopElementModel The loop element model + * @param toscaConverter The tosca converter that must be used to create the Json representation + * @throws IOException In case of issues with the legacy files (generated from resource files + */ + public OperationalPolicy(Loop loop, Service service, LoopElementModel loopElementModel, + ToscaConverterWithDictionarySupport toscaConverter) throws IOException { + this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(), + RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(), + new JsonObject(), loopElementModel.getPolicyModels().first(), loopElementModel, null, null); + this.setLoop(loop); + this.setJsonRepresentation(generateJsonRepresentation(this, toscaConverter)); + } + + /** + * Create an operational policy from a policy model. + * + * @param loop The parent loop + * @param service The loop service + * @param policyModel The policy model + * @param toscaConverter The tosca converter that must be used to create the Json representation + * @throws IOException In case of issues with the legacy files (generated from resource files + */ + public OperationalPolicy(Loop loop, Service service, PolicyModel policyModel, + ToscaConverterWithDictionarySupport toscaConverter) throws IOException { + this(Policy.generatePolicyName("OPERATIONAL", service.getName(), service.getVersion(), + RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(), + new JsonObject(), policyModel, null, null, null); + this.setLoop(loop); + this.setJsonRepresentation(generateJsonRepresentation(this, toscaConverter)); } - private JsonObject generateJsonRepresentation(PolicyModel policyModel) { + /** + * This method can generate a Json representation (json schema) for an operational policy. + * This is mainly to support a legacy case and a generic case. + * + * @param operationalPolicy The operational policy + * @param toscaConverter The tosca converter + * @return The Json Object with Json schema + */ + public static JsonObject generateJsonRepresentation(OperationalPolicy operationalPolicy, + ToscaConverterWithDictionarySupport toscaConverter) + throws IOException { JsonObject jsonReturned = new JsonObject(); - if (policyModel == null) { + if (operationalPolicy.getPolicyModel() == null) { return new JsonObject(); } - try { - if (isLegacy()) { - // Op policy Legacy case - LegacyOperationalPolicy.preloadConfiguration(jsonReturned, loop); - jsonReturned = - OperationalPolicyRepresentationBuilder.generateOperationalPolicySchema(loop.getModelService()); - } else { - // Generic Case - jsonReturned = Policy.generateJsonRepresentationFromToscaModel(policyModel.getPolicyModelTosca(), - policyModel.getPolicyModelType()); - } - } catch (UnknownComponentException | IOException | NullPointerException e) { - logger.error("Unable to generate the operational policy Schema ... ", e); + if (operationalPolicy.isLegacy()) { + // Op policy Legacy case + LegacyOperationalPolicy.preloadConfiguration(jsonReturned, operationalPolicy.loop); + jsonReturned = OperationalPolicyRepresentationBuilder + .generateOperationalPolicySchema(operationalPolicy.loop.getModelService()); + } + else { + // Generic Case + jsonReturned = toscaConverter.convertToscaToJsonSchemaObject( + operationalPolicy.getPolicyModel().getPolicyModelTosca(), + operationalPolicy.getPolicyModel().getPolicyModelType()); } + return jsonReturned; } @@ -178,7 +227,8 @@ public class OperationalPolicy extends Policy implements Serializable { if (other.name != null) { return false; } - } else if (!name.equals(other.name)) { + } + else if (!name.equals(other.name)) { return false; } return true; @@ -243,7 +293,8 @@ public class OperationalPolicy extends Policy implements Serializable { String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload); logger.info("Operational policy payload: " + opPayload); return opPayload; - } else { + } + else { return super.createPolicyPayload(); } } @@ -261,7 +312,7 @@ public class OperationalPolicy extends Policy implements Serializable { if (guardsList != null) { for (JsonElement guardElem : guardsList.getAsJsonArray()) { result.put(guardElem.getAsJsonObject().get("policy-id").getAsString(), - new GsonBuilder().create().toJson(guardElem)); + new GsonBuilder().create().toJson(guardElem)); } } } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java index 244f4c27..0298cfde 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java @@ -28,10 +28,8 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; - import java.io.IOException; import java.util.Map.Entry; - import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.clamp.loop.service.Service; @@ -79,7 +77,7 @@ public class OperationalPolicyRepresentationBuilder { } private static JsonObject createSchemaProperty(String title, String type, String defaultValue, String readOnlyFlag, - String[] enumArray) { + String[] enumArray) { JsonObject property = new JsonObject(); property.addProperty("title", title); property.addProperty("type", type); @@ -128,8 +126,9 @@ public class OperationalPolicyRepresentationBuilder { modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), "True", null)); properties.add("modelInvariantId", - createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", modelVfModules - .get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID").getAsString(), + createSchemaProperty("Model Invariant Id (ModelInvariantUUID)", "string", + modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelInvariantUUID") + .getAsString(), "True", null)); properties.add("modelVersionId", createSchemaProperty("Model Version Id (ModelUUID)", "string", @@ -144,8 +143,9 @@ public class OperationalPolicyRepresentationBuilder { "True", null)); properties .add("modelCustomizationId", - createSchemaProperty("Customization ID", "string", modelVfModules.get(entry.getKey()) - .getAsJsonObject().get("vfModuleModelCustomizationUUID").getAsString(), "True", + createSchemaProperty("Customization ID", "string", + modelVfModules.get(entry.getKey()).getAsJsonObject() + .get("vfModuleModelCustomizationUUID").getAsString(), "True", null)); vfModuleOneOfSchema.add("properties", properties); @@ -180,7 +180,7 @@ public class OperationalPolicyRepresentationBuilder { JsonObject obj = new JsonObject(); obj.addProperty("title", workflowsEntry.getKey()); obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(), - controllerProperties)); + controllerProperties)); schemaArray.add(obj); } @@ -207,7 +207,8 @@ public class OperationalPolicyRepresentationBuilder { StringBuilder builder = new StringBuilder("'").append("artifact_name : ").append(artifactName).append("\n") .append("artifact_version : ").append(artifactVersion).append("\n") .append("mode : async").append("\n") - .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'").append("'"); + .append("data : ").append("'").append("\\").append("'").append(data).append("\\").append("'") + .append("'"); return builder.toString(); } } |