diff options
author | sebdet <sebastien.determe@intl.att.com> | 2020-03-09 16:15:19 -0700 |
---|---|---|
committer | sebdet <sebastien.determe@intl.att.com> | 2020-03-10 04:22:54 -0700 |
commit | 3af9347e47302e3f6754cba8ea2b63772980a5d9 (patch) | |
tree | d23757d560ad5c20ebad4c3b2d7d2f9a4be9ac12 /src/main/java/org | |
parent | 0c4e6af85daabe730917898f466a1d45cbc16f92 (diff) |
Rework tosca converter
Fix the tosca converter template to support json instead of properties file.
Issue-ID: CLAMP-253
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Change-Id: Id4f839d0b5bfece519b0b1e615e8d6e14e464f16
Diffstat (limited to 'src/main/java/org')
8 files changed, 337 insertions, 52 deletions
diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java b/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java index b0bf8278..c6eabcd3 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java @@ -23,7 +23,6 @@ package org.onap.clamp.clds.tosca.update; -import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map.Entry; import org.yaml.snakeyaml.Yaml; @@ -33,14 +32,18 @@ public class Extractor { private String source; private String nativeComponent; - @SuppressWarnings("unchecked") - public Extractor(String toParse, String nativeComponent) throws IOException { + /** + * Constructor. + * + * @param toParse Tosca to parse + * @param nativeComponent The policy type to scan + */ + public Extractor(String toParse, String nativeComponent) { this.source = toParse; this.nativeComponent = nativeComponent; allItems = new LinkedHashMap<String, Component>(); getAllAsMaps(); - } public LinkedHashMap<String, Component> getAllItems() { diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Field.java b/src/main/java/org/onap/clamp/clds/tosca/update/Field.java new file mode 100644 index 00000000..e01f14c4 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/Field.java @@ -0,0 +1,147 @@ +/*- + * ============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; + +public class Field { + private String title; + private Object value; + private Boolean visible; + private Boolean staticValue; + + public Field(String title) { + this.title = title; + } + + /** + * Constructor. + * + * @param title The title + * @param value The value + * @param visible visible or not + * @param staticValue The static value + */ + public Field(String title, Object value, Boolean visible, Boolean staticValue) { + this.title = title; + this.value = value; + this.visible = visible; + this.staticValue = staticValue; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + public Boolean getVisible() { + return visible; + } + + public void setVisible(Boolean visible) { + this.visible = visible; + } + + public Boolean getStaticValue() { + return staticValue; + } + + public void setStaticValue(Boolean staticValue) { + this.staticValue = staticValue; + } + + public String toString() { + return title + " " + value + " " + visible + " " + staticValue; + } + + /** + * This method compares two fields. + * + * @param otherField Compare the current object with the one specified + * @return true if they are totally equals, false otherwise + */ + public boolean compareWithField(Object otherField) { + if (this == otherField) { + return true; + } + if (otherField == null || getClass() != otherField.getClass()) { + return false; + } + + Field field = (Field) otherField; + + if (title != null ? !title.equals(field.title) : field.title != null) { + return false; + } + if (value != null ? !value.equals(field.value) : field.value != null) { + return false; + } + if (visible != null ? !visible.equals(field.visible) : field.visible != null) { + return false; + } + return staticValue != null ? staticValue.equals(field.staticValue) : field.staticValue == null; + } + + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + + Field field = (Field) object; + + return title != null ? title.equals(field.title) : field.title == null; + } + + @Override + public int hashCode() { + return title != null ? title.hashCode() : 0; + } + + /** + * This method test the entire equality. + * + * @param field1 object one + * @param field2 object two + * @return true if they are totally equals (all attributes, false otherwise + */ + public static boolean fieldsEquals(Field field1, Field field2) { + return (field2.getTitle().equals(field1.getTitle()) && field2.getValue().equals(field1.getValue()) + && field2.getVisible().equals(field1.getVisible()) + && field2.getStaticValue().equals(field1.getStaticValue())); + } + +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java b/src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java index 7bf629d6..3c5cf975 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java @@ -227,7 +227,7 @@ public class ParserToJson { switch ((String) property.getItems().get("type")) { case "map": // Get it as an object - JsonObject componentAsProperty = child.getJsonProcess(nameComponent,"object"); + JsonObject componentAsProperty = child.getJsonProcess(nameComponent, "object"); propertiesContainer.add(nameComponent, componentAsProperty); if (currentPropertyTemplate.hasFields("properties")) { propertiesInJson.add("properties", propertiesContainer); @@ -247,13 +247,13 @@ public class ParserToJson { JsonObject itemContainer = new JsonObject(); String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema"); itemContainer.addProperty("type", valueInEntrySchema); - propertiesInJson.add("items", itemContainer); - } - else {//map - // propertiesInJson.add("key?", valueInEntrySchema); + propertiesInJson.add("items", itemContainer); } + // MAP Case, for now nothing + break; - default://Each classical field : type, description, default.. + default: + //Each classical field : type, description, default.. if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) { property.addFieldToJson(propertiesInJson, propertyField, property.getItems().get(propertyField)); diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Template.java b/src/main/java/org/onap/clamp/clds/tosca/update/Template.java index 34459067..4507e3d7 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Template.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/Template.java @@ -23,7 +23,9 @@ package org.onap.clamp.clds.tosca.update; +import com.google.gson.JsonObject; import java.util.ArrayList; +import java.util.List; public class Template { @@ -31,14 +33,14 @@ public class Template { * name parameter is used as "key", in the LinkedHashMap of Templates. */ private String name; - private ArrayList<String> fields; + private List<Field> fields; public Template(String name) { this.name = name; - this.fields = new ArrayList<String>(); + this.fields = new ArrayList<>(); } - public Template(String name, ArrayList<String> fields) { + public Template(String name, List<Field> fields) { this.name = name; this.fields = fields; } @@ -51,42 +53,110 @@ public class Template { this.name = name; } - public ArrayList<String> getFields() { + public List<Field> getFields() { return fields; } - public void setFields(ArrayList<String> fields) { + public void setFields(List<Field> fields) { this.fields = fields; } - public boolean hasFields(String name) { - return fields.contains(name); + /** + * Search in fields if fieldName exists. + * + * @param fieldName The field name + * @return Ture if it exists, false otherwise + */ + public boolean hasFields(String fieldName) { + for (Field field : this.getFields()) { + if (field.getTitle().equals(fieldName)) { + return true; + } + } + return false; + } + + /** + * Get a specific Field. + * + * @param fieldName The field name + * @return THe Field found + */ + public Field getSpecificField(String fieldName) { + for (Field field : this.getFields()) { + if (field.getTitle().equals(fieldName)) { + return field; + } + } + return null; } - public void addField(String field) { + public void addField(Field field) { fields.add(field); } - public void removeField(String field) { + public void removeField(Field field) { fields.remove(field); } /** + * Enable or disable the visibility. + * + * @param nameField THe field name + * @param state True or false + */ + public void setVisibility(String nameField, boolean state) { + for (Field field : this.fields) { + if (field.getTitle().equals(nameField)) { + field.setVisible(state); + } + } + } + + /** + * This method defines if a field is static or not. + * + * @param nameField The name of the field + * @param state true or false + */ + public void setStatic(String nameField, boolean state) { + for (Field field : this.fields) { + if (field.getTitle().equals(nameField)) { + field.setStaticValue(state); + } + } + } + + /** + * This method updates the value of a specfic field. + * + * @param nameField The name of the field + * @param newValue The new value as Object + */ + public void updateValueField(String nameField, Object newValue) { + for (Field field : this.fields) { + if (field.getTitle().equals(nameField)) { + field.setValue(newValue); + } + } + } + + /** * Compare two templates : size and their contents. * * @param template the template * @return a boolean */ public boolean checkFields(Template template) { - boolean duplicateFields = false; if (template.getFields().size() == this.getFields().size()) { int countMatchingFields = 0; //loop each component of first - for (String templateField : template.getFields()) { - //if component.key is present in the second - if (this.getFields().contains(templateField)) { - countMatchingFields++; + for (Field templateFieldToCheck : template.getFields()) { + for (Field templateField : this.getFields()) { + if (templateFieldToCheck.compareWithField(templateField)) { + countMatchingFields++; + } } } @@ -97,6 +167,56 @@ public class Template { return duplicateFields; } + /** + * This method gets the specific field status. + * + * @param field The field name + * @return true or false + */ + public boolean fieldStaticStatus(String field) { + if (this.hasFields(field) && this.getSpecificField(field).getStaticValue().equals(true) + && this.getSpecificField(field).getValue() != null) { + return true; + } + return false; + } + + public boolean isVisible(String field) { + return this.getSpecificField(field).getVisible(); + } + + /** + * Set the value of a property of the Field in the json. + * + * @param jsonSchema The Json schema + * @param fieldName The Field name + * @param value The value + */ + public void setValue(JsonObject jsonSchema, String fieldName, String value) { + if (isVisible(fieldName)) { + if (fieldStaticStatus(fieldName)) { + String defaultValue = (String) this.getSpecificField(fieldName).getValue(); + jsonSchema.addProperty(fieldName, defaultValue); + } + else { + jsonSchema.addProperty(fieldName, value); + } + } + } + + /** + * Inject a static value in the json. + * + * @param jsonSchema The json schema object + * @param fieldName The field name + */ + public void injectStaticValue(JsonObject jsonSchema, String fieldName) { + if (isVisible(fieldName)) { + Field toInject = this.getSpecificField(fieldName); + jsonSchema.addProperty(fieldName, (String) toInject.getValue()); + } + } + @Override public String toString() { return " fields : " + fields; diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/TemplateManagement.java b/src/main/java/org/onap/clamp/clds/tosca/update/TemplateManagement.java index ce5cdb81..74307715 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/TemplateManagement.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/TemplateManagement.java @@ -23,14 +23,14 @@ package org.onap.clamp.clds.tosca.update; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import java.io.IOException; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; -import java.util.Properties; +import java.util.List; +import java.util.Map; +import org.onap.clamp.clds.util.JsonUtils; public class TemplateManagement { @@ -93,7 +93,7 @@ public class TemplateManagement { * @param name name * @param fields fields */ - public void addTemplate(String name, ArrayList<String> fields) { + public void addTemplate(String name, List<Field> fields) { Template template = new Template(name, fields); //If it is true, the operation does not have any interest : // replace OR put two different object with the same body @@ -115,17 +115,17 @@ public class TemplateManagement { * Update Template : adding with true flag, removing with false. * * @param nameTemplate name template - * @param fieldName field name + * @param field field name * @param operation operation */ - public void updateTemplate(String nameTemplate, String fieldName, Boolean operation) { + public void updateTemplate(String nameTemplate, Field field, Boolean operation) { // Operation = true && field is not present => add Field - if (operation && !this.templates.get(nameTemplate).getFields().contains(fieldName)) { - this.templates.get(nameTemplate).addField(fieldName); + if (operation && !this.templates.get(nameTemplate).getFields().contains(field)) { + this.templates.get(nameTemplate).addField(field); } // Operation = false && field is present => remove Field - else if (!operation && this.templates.get(nameTemplate).getFields().contains(fieldName)) { - this.templates.get(nameTemplate).removeField(fieldName); + else if (!operation && this.templates.get(nameTemplate).getFields().contains(field)) { + this.templates.get(nameTemplate).removeField(field); } } @@ -162,20 +162,30 @@ public class TemplateManagement { /** * Create and complete several Templates from file.properties. * - * @param templateProperties The template properties as String + * @param jsonTemplates The template properties as String * @return a map */ - private LinkedHashMap<String, Template> initializeTemplates(String templateProperties) throws IOException { - LinkedHashMap<String, Template> generatedTemplates = new LinkedHashMap<>(); - Properties templates = new Properties(); - templates.load(new StringReader(templateProperties)); + @SuppressWarnings("unused") + private LinkedHashMap<String, Template> initializeTemplates(String jsonTemplates) { - for (Object key : templates.keySet()) { - String fields = (String) templates.get(key); - String[] fieldsInArray = fields.split(","); - Template template = new Template((String) key, new ArrayList<>(Arrays.asList(fieldsInArray))); + 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(); + Field bodyField = new Field(fieldName, fieldValue, fieldVisible, fieldStatic); + template.getFields().add(bodyField); + } generatedTemplates.put(template.getName(), template); } return generatedTemplates; } + } diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index 004c450a..d52e418e 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -294,7 +294,7 @@ public abstract class Policy extends AuditEntity { * @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.properties file + * @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, @@ -302,7 +302,7 @@ public abstract class Policy extends AuditEntity { throws IOException, UnknownComponentException { return new TemplateManagement(policyToscaModel,ResourceFileUtil.getResourceAsString( "clds/tosca_update/defaultToscaTypes.yaml"), - ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.properties")) + ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json")) .launchTranslation(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 96b3a09b..b8093ccf 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -108,7 +108,9 @@ public class MicroServicePolicy extends Policy implements Serializable { this.setPolicyModel(policyModel); this.shared = shared; try { - this.setJsonRepresentation(Policy.generateJsonRepresentationFromToscaModel(policyModel.getPolicyModelTosca(),policyModel.getPolicyModelType())); + 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()); @@ -130,7 +132,8 @@ public class MicroServicePolicy extends Policy implements Serializable { * @param pdpSubgroup The Pdp Subgrouop info */ public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, - JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) { + JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup, + String pdpSubgroup) { this.name = name; this.setPolicyModel(policyModel); this.shared = shared; @@ -265,7 +268,8 @@ public class MicroServicePolicy 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; diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java index 3ad97c59..9bc641c6 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java @@ -63,11 +63,12 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli return repository.save( repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop)) .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyModel(), - policy.getShared(), policy.getJsonRepresentation(),null, policy.getPdpGroup(), policy.getPdpSubgroup()))); + policy.getShared(), policy.getJsonRepresentation(), null, policy.getPdpGroup(), + policy.getPdpSubgroup()))); } private MicroServicePolicy updateMicroservicePolicyProperties(MicroServicePolicy oldPolicy, - MicroServicePolicy newPolicy, Loop loop) { + MicroServicePolicy newPolicy, Loop loop) { oldPolicy.setConfigurationsJson(newPolicy.getConfigurationsJson()); if (!oldPolicy.getUsedByLoops().contains(loop)) { oldPolicy.getUsedByLoops().add(loop); @@ -85,7 +86,7 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli * @param deploymentUrl The Deployment URL as returned by DCAE */ public void updateDcaeDeploymentFields(MicroServicePolicy microServicePolicy, String deploymentId, - String deploymentUrl) { + String deploymentUrl) { microServicePolicy.setDcaeDeploymentId(deploymentId); microServicePolicy.setDcaeDeploymentStatusUrl(deploymentUrl); repository.save(microServicePolicy); |