From 8604d37f2f6bdd011de62ec474b6883413d30348 Mon Sep 17 00:00:00 2001 From: sebdet Date: Tue, 10 Mar 2020 02:44:53 -0700 Subject: Fix the tosca converter Fix the new tosca converter to support metadata section Issue-ID: CLAMP-580 Signed-off-by: sebdet Change-Id: I9068bd9dc89851c630660a7f78fae1cb70bdc178 --- .../onap/clamp/clds/tosca/update/Component.java | 121 -------- .../onap/clamp/clds/tosca/update/Extractor.java | 116 -------- .../org/onap/clamp/clds/tosca/update/Field.java | 147 --------- .../clamp/clds/tosca/update/MetadataParser.java | 46 +++ .../onap/clamp/clds/tosca/update/ParserToJson.java | 321 -------------------- .../org/onap/clamp/clds/tosca/update/Template.java | 66 ++-- .../clamp/clds/tosca/update/TemplateField.java | 148 +++++++++ .../clds/tosca/update/TemplateManagement.java | 191 ------------ .../clds/tosca/update/ToscaConverterManager.java | 192 ++++++++++++ .../clds/tosca/update/ToscaConverterToJson.java | 331 +++++++++++++++++++++ .../onap/clamp/clds/tosca/update/ToscaElement.java | 121 ++++++++ .../clamp/clds/tosca/update/ToscaItemsParser.java | 109 +++++++ .../clamp/loop/service/CsarServiceInstaller.java | 2 +- .../onap/clamp/loop/service/ServiceRepository.java | 32 -- src/main/java/org/onap/clamp/policy/Policy.java | 8 +- .../operational/OperationalPolicyService.java | 21 +- 16 files changed, 999 insertions(+), 973 deletions(-) delete mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/Component.java delete mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java delete mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/Field.java create mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/MetadataParser.java delete mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java create mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/TemplateField.java delete mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/TemplateManagement.java create mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterManager.java create mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterToJson.java create mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/ToscaElement.java create mode 100644 src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java delete mode 100644 src/main/java/org/onap/clamp/loop/service/ServiceRepository.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Component.java b/src/main/java/org/onap/clamp/clds/tosca/update/Component.java deleted file mode 100644 index 6db129d1..00000000 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Component.java +++ /dev/null @@ -1,121 +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 java.util.ArrayList; -import java.util.LinkedHashMap; - -public class Component { - - /** - * name parameter is used as "key", in the LinkedHashMap of Components. - */ - private String name; - private String derivedFrom; - private String version; - private String typeVersion; - private String description; - private LinkedHashMap properties; - - public Component() { - } - - /** - * Constructor. - * - * @param name name - * @param derivedFrom derivedFrom - * @param description description - */ - @SuppressWarnings({"unchecked", "rawtypes"}) - public Component(String name, String derivedFrom, String description) { - super(); - this.name = name; - this.derivedFrom = derivedFrom; - this.description = description; - this.properties = new LinkedHashMap(); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDerivedFrom() { - return derivedFrom; - } - - public void setDerivedFrom(String derivedFrom) { - this.derivedFrom = derivedFrom; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getTypeVersion() { - return typeVersion; - } - - public void setTypeVersion(String typeVersion) { - this.typeVersion = typeVersion; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public LinkedHashMap getProperties() { - return properties; - } - - public void setProperties(LinkedHashMap properties) { - this.properties = properties; - } - - public void addProperties(Property property) { - this.properties.put(property.getName(), property); - } - - public ArrayList propertiesNames() { - return new ArrayList<>(properties.keySet()); - } - - @Override - public String toString() { - return name + ": " + description + ", version: " + version + ", nb de properties: " + properties.size() - + System.getProperty("line.separator") + propertiesNames(); - } -} 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 deleted file mode 100644 index c6eabcd3..00000000 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java +++ /dev/null @@ -1,116 +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 java.util.LinkedHashMap; -import java.util.Map.Entry; -import org.yaml.snakeyaml.Yaml; - -public class Extractor { - private LinkedHashMap allItems; - private String source; - private String nativeComponent; - - /** - * 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(); - getAllAsMaps(); - } - - public LinkedHashMap getAllItems() { - return allItems; - } - - public String getSource() { - return source; - } - - /** - * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract - * all entities and put them at the same level. - * - * @return an object - */ - @SuppressWarnings("unchecked") - public LinkedHashMap getAllAsMaps() { - Yaml yaml = new Yaml(); - Object contentFile = yaml.load(source); - LinkedHashMap> file = - (LinkedHashMap>) contentFile; - // Get DataTypes - LinkedHashMap dataTypes = file.get("data_types"); - dataTypes = (dataTypes == null) ? (new LinkedHashMap<>()) : dataTypes; - // Get Policies : first, get topology and after extract policies from it - LinkedHashMap policyTypes = file.get("policy_types"); - // Put the policies and datatypes in the same collection - dataTypes.putAll(policyTypes); - - Object contentNativeFile = yaml.load(nativeComponent); - LinkedHashMap dataTypesEmbedded = - ((LinkedHashMap>) contentNativeFile).get("data_types"); - dataTypes.putAll(dataTypesEmbedded); - - parseInComponent(dataTypes); - return dataTypes; - } - - /** - * With all the component, get as Map, Components and Components properties are created. - * - * @param allMaps maps - */ - @SuppressWarnings("unchecked") - public void parseInComponent(LinkedHashMap allMaps) { - //Component creations, from the file maps - for (Entry itemToParse : allMaps.entrySet()) { - LinkedHashMap componentBody = (LinkedHashMap) itemToParse.getValue(); - Component component = new Component(itemToParse.getKey(), (String) componentBody.get("derived_from"), - (String) componentBody.get("description")); - //If policy, version and type_version : - if (componentBody.get("type_version") != null) { - component.setVersion((String) componentBody.get("type_version")); - component.setTypeVersion((String) componentBody.get("type_version")); - } - //Properties creation, from the map - if (componentBody.get("properties") != null) { - LinkedHashMap properties = - (LinkedHashMap) componentBody.get("properties"); - for (Entry itemToProperty : properties.entrySet()) { - Property property = new Property(itemToProperty.getKey(), - (LinkedHashMap) itemToProperty.getValue()); - component.addProperties(property); - } - } - this.allItems.put(component.getName(), component); - } - } -} 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 deleted file mode 100644 index e01f14c4..00000000 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Field.java +++ /dev/null @@ -1,147 +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; - -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/MetadataParser.java b/src/main/java/org/onap/clamp/clds/tosca/update/MetadataParser.java new file mode 100644 index 00000000..fb70231f --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/MetadataParser.java @@ -0,0 +1,46 @@ + +/*- + * ============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.JsonObject; +import org.onap.clamp.tosca.DictionaryService; + +public class MetadataParser { + + /** + * 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; + } + } +} 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 deleted file mode 100644 index 3c5cf975..00000000 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ParserToJson.java +++ /dev/null @@ -1,321 +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.JsonArray; -import com.google.gson.JsonObject; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map.Entry; - -public class ParserToJson { - private LinkedHashMap components; - private LinkedHashMap templates; - - public ParserToJson(LinkedHashMap components, LinkedHashMap templates) { - this.components = components; - this.templates = templates; - } - - /** - * For a given component, launch process to parse it in Json. - * - * @param nameComponent 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; - } - - /** - * Return the classical/general fields of the component, & launch the properties deployment. - * - * @param component the compo - * @return a json object - */ - public JsonObject getFieldAsObject(Component component) { - - JsonObject globalFields = new JsonObject(); - if (templates.get("object").hasFields("title")) { - globalFields.addProperty("title", component.getName()); - } - if (templates.get("object").hasFields("type")) { - globalFields.addProperty("type", "object"); - } - if (templates.get("object").hasFields("description")) { - if (component.getDescription() != null) { - globalFields.addProperty("description", component.getDescription()); - } - } - if (templates.get("object").hasFields("required")) { - globalFields.add("required", this.getRequirements(component.getName())); - } - if (templates.get("object").hasFields("properties")) { - globalFields.add("properties", this.deploy(component.getName())); - } - return globalFields; - } - - /** - * Get the required properties of the Component, including the parents properties requirements. - * - * @param nameComponent name component - * @return a json array - */ - public JsonArray getRequirements(String nameComponent) { - JsonArray requirements = new JsonArray(); - Component toParse = components.get(nameComponent); - //Check for a father component, and launch the same process - if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root") - && !toParse.getDerivedFrom().equals("tosca.policies.Root")) { - requirements.addAll(getRequirements(toParse.getDerivedFrom())); - } - //Each property is checked, and add to the requirement array if it's required - Collection properties = toParse.getProperties().values(); - for (Property property : properties) { - if (property.getItems().containsKey("required") - && property.getItems().get("required").equals(true)) { - requirements.add(property.getName()); - } - } - return requirements; - } - - /** - * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise - * deploy and parse the properties. - * - * @param nameComponent name component - * @return a json object - */ - public JsonObject deploy(String nameComponent) { - JsonObject jsonSchema = new JsonObject(); - Component toParse = components.get(nameComponent); - //Check for a father component, and launch the same process - if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root") - && !toParse.getDerivedFrom().equals("tosca.policies.Root")) { - jsonSchema = this.getParent(toParse.getDerivedFrom()); - } - //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 property : toParse.getProperties().entrySet()) { - if (matchComponent((String) property.getValue().getItems().get("type")) != null) { - jsonSchema.add(property.getValue().getName(), - this.getJsonProcess((String) property.getValue().getItems().get("type"), "object")); - } - else { - jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue())); - } - } - return jsonSchema; - } - - /** - * If a component has a parent, it is deploy in the same way. - * - * @param nameComponent name component - * @return a json object - */ - public JsonObject getParent(String nameComponent) { - return deploy(nameComponent); - } - - /** - * to be done. - * - * @param property property - * @return a json object - */ - @SuppressWarnings("unchecked") - public JsonObject complexParse(Property property) { - JsonObject propertiesInJson = new JsonObject(); - Template currentPropertyTemplate; - String typeProperty = (String) property.getItems().get("type"); - if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) { - currentPropertyTemplate = templates.get("object"); - } - else { - String propertyType = (String) property.getItems().get("type"); - currentPropertyTemplate = templates.get(propertyType.toLowerCase()); - } - //Each "special" field is analysed, and has a specific treatment - for (String propertyField : property.getItems().keySet()) { - switch (propertyField) { - case "type": - if (currentPropertyTemplate.hasFields(propertyField)) { - String fieldtype = (String) property.getItems().get(propertyField); - switch (fieldtype.toLowerCase()) { - case "list": - propertiesInJson.addProperty("type", "array"); - break; - case "map": - propertiesInJson.addProperty("type", "object"); - break; - case "scalar-unit.time": - case "scalar-unit.frequency": - case "scalar-unit.size": - propertiesInJson.addProperty("type", "string"); - break; - case "timestamp": - propertiesInJson.addProperty("type", "string"); - propertiesInJson.addProperty("format", "date-time"); - break; - case "float": - propertiesInJson.addProperty("type", "number"); - break; - case "range": - propertiesInJson.addProperty("type", "integer"); - if (!checkConstraintPresence(property, "greater_than") - && currentPropertyTemplate.hasFields("exclusiveMinimum")) { - propertiesInJson.addProperty("exclusiveMinimum", false); - } - if (!checkConstraintPresence(property, "less_than") - && currentPropertyTemplate.hasFields("exclusiveMaximum")) { - propertiesInJson.addProperty("exclusiveMaximum", false); - } - break; - default: - propertiesInJson.addProperty("type", currentPropertyTemplate.getName()); - break; - } - } - break; - case "metadata": - break; - case "constraints": - property.addConstraintsAsJson(propertiesInJson, - (ArrayList) property.getItems().get("constraints"), - currentPropertyTemplate); - 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"); - ParserToJson child = new ParserToJson(components, templates); - JsonObject propertiesContainer = new JsonObject(); - - switch ((String) property.getItems().get("type")) { - case "map": // Get it as an object - JsonObject componentAsProperty = child.getJsonProcess(nameComponent, "object"); - propertiesContainer.add(nameComponent, componentAsProperty); - if (currentPropertyTemplate.hasFields("properties")) { - propertiesInJson.add("properties", propertiesContainer); - } - break; - default://list : get it as an Array - JsonObject componentAsItem = child.getJsonProcess(nameComponent, "object"); - if (currentPropertyTemplate.hasFields("properties")) { - propertiesInJson.add("items", componentAsItem); - } - break; - } - - } - // Native cases - else if (property.getItems().get("type").equals("list")) { - JsonObject itemContainer = new JsonObject(); - String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema"); - itemContainer.addProperty("type", valueInEntrySchema); - propertiesInJson.add("items", itemContainer); - } - // MAP Case, for now nothing - - break; - default: - //Each classical field : type, description, default.. - if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) { - property.addFieldToJson(propertiesInJson, propertyField, - property.getItems().get(propertyField)); - } - break; - } - } - return propertiesInJson; - } - - /** - * Look for a matching Component for the name paramater, in the components list. - * - * @param name the name - * @return a component - */ - public Component matchComponent(String name) { - Component correspondingComponent = null; - if (components == null) { - return null; - } - for (Component component : components.values()) { - if (component.getName().equals(name)) { - correspondingComponent = component; - } - } - return correspondingComponent; - } - - /** - * Simple method to extract quickly a type field from particular property item. - * - * @param property the property - * @param fieldName the fieldname - * @return a string - */ - @SuppressWarnings("unchecked") - public String extractSpecificFieldFromMap(Property property, String fieldName) { - LinkedHashMap entrySchemaFields = - (LinkedHashMap) property.getItems().get(fieldName); - return entrySchemaFields.get("type"); - } - - /** - * Check if a constraint, for a specific property, is there. - * - * @param property property - * @param nameConstraint name constraint - * @return a flag boolean - */ - public boolean checkConstraintPresence(Property property, String nameConstraint) { - boolean presentConstraint = false; - if (property.getItems().containsKey("constraints")) { - ArrayList constraints = (ArrayList) property.getItems().get("constraints"); - for (Object constraint : constraints) { - if (constraint instanceof LinkedHashMap) { - if (((LinkedHashMap) constraint).containsKey(nameConstraint)) { - presentConstraint = true; - } - } - } - } - return presentConstraint; - } -} 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 4507e3d7..6a531aee 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 @@ -33,16 +33,16 @@ public class Template { * name parameter is used as "key", in the LinkedHashMap of Templates. */ private String name; - private List fields; + private List templateFields; public Template(String name) { this.name = name; - this.fields = new ArrayList<>(); + this.templateFields = new ArrayList<>(); } - public Template(String name, List fields) { + public Template(String name, List templateFields) { this.name = name; - this.fields = fields; + this.templateFields = templateFields; } public String getName() { @@ -53,12 +53,12 @@ public class Template { this.name = name; } - public List getFields() { - return fields; + public List getTemplateFields() { + return templateFields; } - public void setFields(List fields) { - this.fields = fields; + public void setTemplateFields(List templateFields) { + this.templateFields = templateFields; } /** @@ -68,8 +68,8 @@ public class Template { * @return Ture if it exists, false otherwise */ public boolean hasFields(String fieldName) { - for (Field field : this.getFields()) { - if (field.getTitle().equals(fieldName)) { + for (TemplateField templateField : this.getTemplateFields()) { + if (templateField.getTitle().equals(fieldName)) { return true; } } @@ -82,21 +82,21 @@ public class Template { * @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; + public TemplateField getSpecificField(String fieldName) { + for (TemplateField templateField : this.getTemplateFields()) { + if (templateField.getTitle().equals(fieldName)) { + return templateField; } } return null; } - public void addField(Field field) { - fields.add(field); + public void addField(TemplateField templateField) { + templateFields.add(templateField); } - public void removeField(Field field) { - fields.remove(field); + public void removeField(TemplateField templateField) { + templateFields.remove(templateField); } /** @@ -106,9 +106,9 @@ public class Template { * @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); + for (TemplateField templateField : this.templateFields) { + if (templateField.getTitle().equals(nameField)) { + templateField.setVisible(state); } } } @@ -120,9 +120,9 @@ public class Template { * @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); + for (TemplateField templateField : this.templateFields) { + if (templateField.getTitle().equals(nameField)) { + templateField.setStaticValue(state); } } } @@ -134,9 +134,9 @@ public class Template { * @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); + for (TemplateField templateField : this.templateFields) { + if (templateField.getTitle().equals(nameField)) { + templateField.setValue(newValue); } } } @@ -149,18 +149,18 @@ public class Template { */ public boolean checkFields(Template template) { boolean duplicateFields = false; - if (template.getFields().size() == this.getFields().size()) { + if (template.getTemplateFields().size() == this.getTemplateFields().size()) { int countMatchingFields = 0; //loop each component of first - for (Field templateFieldToCheck : template.getFields()) { - for (Field templateField : this.getFields()) { + for (TemplateField templateFieldToCheck : template.getTemplateFields()) { + for (TemplateField templateField : this.getTemplateFields()) { if (templateFieldToCheck.compareWithField(templateField)) { countMatchingFields++; } } } - if (template.getFields().size() == countMatchingFields) { + if (template.getTemplateFields().size() == countMatchingFields) { duplicateFields = true; } } @@ -212,13 +212,13 @@ public class Template { */ public void injectStaticValue(JsonObject jsonSchema, String fieldName) { if (isVisible(fieldName)) { - Field toInject = this.getSpecificField(fieldName); + TemplateField toInject = this.getSpecificField(fieldName); jsonSchema.addProperty(fieldName, (String) toInject.getValue()); } } @Override public String toString() { - return " fields : " + fields; + return " templateFields : " + templateFields; } } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/TemplateField.java b/src/main/java/org/onap/clamp/clds/tosca/update/TemplateField.java new file mode 100644 index 00000000..34446436 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/TemplateField.java @@ -0,0 +1,148 @@ +/*- + * ============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 TemplateField { + private String title; + private Object value; + private Boolean visible; + private Boolean staticValue; + + public TemplateField(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 TemplateField(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; + } + + TemplateField templateField = (TemplateField) otherField; + + if (title != null ? !title.equals(templateField.title) : templateField.title != null) { + return false; + } + if (value != null ? !value.equals(templateField.value) : templateField.value != null) { + return false; + } + if (visible != null ? !visible.equals(templateField.visible) : templateField.visible != null) { + return false; + } + return staticValue != null ? staticValue.equals(templateField.staticValue) : templateField.staticValue == null; + } + + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + + TemplateField templateField = (TemplateField) object; + + return title != null ? title.equals(templateField.title) : templateField.title == null; + } + + @Override + public int hashCode() { + return title != null ? title.hashCode() : 0; + } + + /** + * This method test the entire equality. + * + * @param templateField1 object one + * @param templateField2 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())); + } + +} 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 deleted file mode 100644 index 74307715..00000000 --- a/src/main/java/org/onap/clamp/clds/tosca/update/TemplateManagement.java +++ /dev/null @@ -1,191 +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 TemplateManagement { - - private LinkedHashMap templates; - private LinkedHashMap components; - private ParserToJson parserToJson; - private Extractor extractor; - - /** - * Constructor. - * - * @param yamlContent Yaml content as string - * @param templateProperties template properties as string - * @throws IOException in case of failure - */ - public TemplateManagement(String yamlContent, String nativeComponent, String templateProperties) - throws IOException { - if (yamlContent != null && !yamlContent.isEmpty()) { - this.extractor = new Extractor(yamlContent, nativeComponent); - this.components = extractor.getAllItems(); - this.templates = initializeTemplates(templateProperties); - } - else { - components = null; - } - } - - //GETTERS & SETTERS - public LinkedHashMap getComponents() { - return components; - } - - public void setComponents(LinkedHashMap components) { - this.components = components; - } - - public ParserToJson getParseToJson() { - return parserToJson; - } - - public void setParseToJson(ParserToJson parserToJson) { - this.parserToJson = parserToJson; - } - - public LinkedHashMap getTemplates() { - return templates; - } - - public void setTemplates(LinkedHashMap templates) { - this.templates = templates; - } - - public Extractor getExtractor() { - return extractor; - } - - /** - * Add a template. - * - * @param name name - * @param fields fields - */ - public void addTemplate(String name, List 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 - 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 field field name - * @param operation 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(field)) { - this.templates.get(nameTemplate).addField(field); - } - // Operation = false && field is present => remove Field - else if (!operation && this.templates.get(nameTemplate).getFields().contains(field)) { - this.templates.get(nameTemplate).removeField(field); - } - } - - /** - * Check if the JSONTemplates have the same bodies. - * - * @param template template - * @return a boolean - */ - public boolean hasTemplate(Template template) { - boolean duplicateTemplate = false; - Collection 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 launchTranslation(String componentName) throws UnknownComponentException { - this.parserToJson = new ParserToJson(components, templates); - if (parserToJson.matchComponent(componentName) == null) { - throw new UnknownComponentException(componentName); - } - return parserToJson.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 initializeTemplates(String jsonTemplates) { - - LinkedHashMap generatedTemplates = new LinkedHashMap<>(); - JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class); - - for (Map.Entry templateAsJson : templates.entrySet()) { - Template template = new Template(templateAsJson.getKey()); - JsonObject templateBody = (JsonObject) templateAsJson.getValue(); - for (Map.Entry 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/clds/tosca/update/ToscaConverterManager.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterManager.java new file mode 100644 index 00000000..b3224b04 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterManager.java @@ -0,0 +1,192 @@ +/*- + * ============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 templates; + private LinkedHashMap 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 getComponents() { + return components; + } + + public void setComponents(LinkedHashMap components) { + this.components = components; + } + + public ToscaConverterToJson getParseToJson() { + return toscaConverterToJson; + } + + public void setParseToJson(ToscaConverterToJson toscaConverterToJson) { + this.toscaConverterToJson = toscaConverterToJson; + } + + public LinkedHashMap getTemplates() { + return templates; + } + + public void setTemplates(LinkedHashMap templates) { + this.templates = templates; + } + + public ToscaItemsParser getToscaItemsParser() { + return toscaItemsParser; + } + + /** + * Add a template. + * + * @param name name + * @param templateFields fields + */ + public void addTemplate(String name, List 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 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 initializeTemplates(String jsonTemplates) { + + LinkedHashMap generatedTemplates = new LinkedHashMap<>(); + JsonObject templates = JsonUtils.GSON.fromJson(jsonTemplates, JsonObject.class); + + for (Map.Entry templateAsJson : templates.entrySet()) { + Template template = new Template(templateAsJson.getKey()); + JsonObject templateBody = (JsonObject) templateAsJson.getValue(); + for (Map.Entry 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/ToscaConverterToJson.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterToJson.java new file mode 100644 index 00000000..297d568b --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterToJson.java @@ -0,0 +1,331 @@ +/*- + * ============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.JsonArray; +import com.google.gson.JsonObject; +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; + +@Component +public class ToscaConverterToJson { + private LinkedHashMap components; + private LinkedHashMap templates; + + // if this one is set, the dictionary mechanism is enabled + @Autowired + private DictionaryService dictionaryService; + + public ToscaConverterToJson(LinkedHashMap components, LinkedHashMap templates) { + this.components = components; + this.templates = templates; + } + + /** + * For a given component, launch process to parse it in Json. + * + * @param nameComponent 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; + } + + /** + * Return the classical/general fields of the component, & launch the properties deployment. + * + * @param toscaElement the compo + * @return a json object + */ + public JsonObject getFieldAsObject(ToscaElement toscaElement) { + + JsonObject globalFields = new JsonObject(); + if (templates.get("object").hasFields("title")) { + globalFields.addProperty("title", toscaElement.getName()); + } + if (templates.get("object").hasFields("type")) { + globalFields.addProperty("type", "object"); + } + if (templates.get("object").hasFields("description")) { + if (toscaElement.getDescription() != null) { + globalFields.addProperty("description", toscaElement.getDescription()); + } + } + if (templates.get("object").hasFields("required")) { + globalFields.add("required", this.getRequirements(toscaElement.getName())); + } + if (templates.get("object").hasFields("properties")) { + globalFields.add("properties", this.deploy(toscaElement.getName())); + } + return globalFields; + } + + /** + * Get the required properties of the Component, including the parents properties requirements. + * + * @param nameComponent name component + * @return a json array + */ + public JsonArray getRequirements(String nameComponent) { + JsonArray requirements = new JsonArray(); + ToscaElement toParse = components.get(nameComponent); + //Check for a father component, and launch the same process + if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root") + && !toParse.getDerivedFrom().equals("tosca.policies.Root")) { + requirements.addAll(getRequirements(toParse.getDerivedFrom())); + } + //Each property is checked, and add to the requirement array if it's required + Collection properties = toParse.getProperties().values(); + for (Property property : properties) { + if (property.getItems().containsKey("required") + && property.getItems().get("required").equals(true)) { + requirements.add(property.getName()); + } + } + return requirements; + } + + /** + * The beginning of the recursive process. Get the parents (or not) to launch the same process, and otherwise + * deploy and parse the properties. + * + * @param nameComponent name component + * @return a json object + */ + public JsonObject deploy(String nameComponent) { + JsonObject jsonSchema = new JsonObject(); + ToscaElement toParse = components.get(nameComponent); + //Check for a father component, and launch the same process + if (!toParse.getDerivedFrom().equals("tosca.datatypes.Root") + && !toParse.getDerivedFrom().equals("tosca.policies.Root")) { + jsonSchema = this.getParent(toParse.getDerivedFrom()); + } + //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 property : toParse.getProperties().entrySet()) { + if (matchComponent((String) property.getValue().getItems().get("type")) != null) { + jsonSchema.add(property.getValue().getName(), + this.getJsonProcess((String) property.getValue().getItems().get("type"), "object")); + } + else { + jsonSchema.add(property.getValue().getName(), this.complexParse(property.getValue())); + } + } + return jsonSchema; + } + + /** + * If a component has a parent, it is deploy in the same way. + * + * @param nameComponent name component + * @return a json object + */ + public JsonObject getParent(String nameComponent) { + return deploy(nameComponent); + } + + /** + * to be done. + * + * @param property property + * @return a json object + */ + @SuppressWarnings("unchecked") + public JsonObject complexParse(Property property) { + JsonObject propertiesInJson = new JsonObject(); + Template currentPropertyTemplate; + String typeProperty = (String) property.getItems().get("type"); + if (typeProperty.toLowerCase().equals("list") || typeProperty.toLowerCase().equals("map")) { + currentPropertyTemplate = templates.get("object"); + } + else { + String propertyType = (String) property.getItems().get("type"); + currentPropertyTemplate = templates.get(propertyType.toLowerCase()); + } + //Each "special" field is analysed, and has a specific treatment + for (String propertyField : property.getItems().keySet()) { + switch (propertyField) { + case "type": + if (currentPropertyTemplate.hasFields(propertyField)) { + String fieldtype = (String) property.getItems().get(propertyField); + switch (fieldtype.toLowerCase()) { + case "list": + propertiesInJson.addProperty("type", "array"); + break; + case "map": + propertiesInJson.addProperty("type", "object"); + break; + case "scalar-unit.time": + case "scalar-unit.frequency": + case "scalar-unit.size": + propertiesInJson.addProperty("type", "string"); + break; + case "timestamp": + propertiesInJson.addProperty("type", "string"); + propertiesInJson.addProperty("format", "date-time"); + break; + case "float": + propertiesInJson.addProperty("type", "number"); + break; + case "range": + propertiesInJson.addProperty("type", "integer"); + if (!checkConstraintPresence(property, "greater_than") + && currentPropertyTemplate.hasFields("exclusiveMinimum")) { + propertiesInJson.addProperty("exclusiveMinimum", false); + } + if (!checkConstraintPresence(property, "less_than") + && currentPropertyTemplate.hasFields("exclusiveMaximum")) { + propertiesInJson.addProperty("exclusiveMaximum", false); + } + break; + default: + propertiesInJson.addProperty("type", currentPropertyTemplate.getName()); + break; + } + } + break; + case "metadata": + propertiesInJson.add("enum", MetadataParser.processAllMetadataElement(property, + dictionaryService)); + break; + case "constraints": + property.addConstraintsAsJson(propertiesInJson, + (ArrayList) property.getItems().get("constraints"), + currentPropertyTemplate); + 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); + JsonObject propertiesContainer = new JsonObject(); + + switch ((String) property.getItems().get("type")) { + case "map": // Get it as an object + JsonObject componentAsProperty = child.getJsonProcess(nameComponent, "object"); + propertiesContainer.add(nameComponent, componentAsProperty); + if (currentPropertyTemplate.hasFields("properties")) { + propertiesInJson.add("properties", propertiesContainer); + } + break; + default://list : get it as an Array + JsonObject componentAsItem = child.getJsonProcess(nameComponent, "object"); + if (currentPropertyTemplate.hasFields("properties")) { + propertiesInJson.add("items", componentAsItem); + } + break; + } + + } + // Native cases + else if (property.getItems().get("type").equals("list")) { + JsonObject itemContainer = new JsonObject(); + String valueInEntrySchema = this.extractSpecificFieldFromMap(property, "entry_schema"); + itemContainer.addProperty("type", valueInEntrySchema); + propertiesInJson.add("items", itemContainer); + } + // MAP Case, for now nothing + + break; + default: + //Each classical field : type, description, default.. + if (currentPropertyTemplate.hasFields(propertyField) && !propertyField.equals("required")) { + property.addFieldToJson(propertiesInJson, propertyField, + property.getItems().get(propertyField)); + } + break; + } + } + return propertiesInJson; + } + + /** + * Look for a matching Component for the name paramater, in the components list. + * + * @param name the name + * @return a component + */ + public ToscaElement matchComponent(String name) { + ToscaElement correspondingToscaElement = null; + if (components == null) { + return null; + } + for (ToscaElement toscaElement : components.values()) { + if (toscaElement.getName().equals(name)) { + correspondingToscaElement = toscaElement; + } + } + return correspondingToscaElement; + } + + /** + * Simple method to extract quickly a type field from particular property item. + * + * @param property the property + * @param fieldName the fieldname + * @return a string + */ + @SuppressWarnings("unchecked") + public String extractSpecificFieldFromMap(Property property, String fieldName) { + LinkedHashMap entrySchemaFields = + (LinkedHashMap) property.getItems().get(fieldName); + return entrySchemaFields.get("type"); + } + + /** + * Check if a constraint, for a specific property, is there. + * + * @param property property + * @param nameConstraint name constraint + * @return a flag boolean + */ + public boolean checkConstraintPresence(Property property, String nameConstraint) { + boolean presentConstraint = false; + if (property.getItems().containsKey("constraints")) { + ArrayList constraints = (ArrayList) property.getItems().get("constraints"); + for (Object constraint : constraints) { + if (constraint instanceof LinkedHashMap) { + if (((LinkedHashMap) constraint).containsKey(nameConstraint)) { + presentConstraint = true; + } + } + } + } + return presentConstraint; + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaElement.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaElement.java new file mode 100644 index 00000000..d702cda5 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaElement.java @@ -0,0 +1,121 @@ +/*- + * ============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 java.util.ArrayList; +import java.util.LinkedHashMap; + +public class ToscaElement { + + /** + * name parameter is used as "key", in the LinkedHashMap of Components. + */ + private String name; + private String derivedFrom; + private String version; + private String typeVersion; + private String description; + private LinkedHashMap properties; + + public ToscaElement() { + } + + /** + * Constructor. + * + * @param name name + * @param derivedFrom derivedFrom + * @param description description + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public ToscaElement(String name, String derivedFrom, String description) { + super(); + this.name = name; + this.derivedFrom = derivedFrom; + this.description = description; + this.properties = new LinkedHashMap(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDerivedFrom() { + return derivedFrom; + } + + public void setDerivedFrom(String derivedFrom) { + this.derivedFrom = derivedFrom; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getTypeVersion() { + return typeVersion; + } + + public void setTypeVersion(String typeVersion) { + this.typeVersion = typeVersion; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LinkedHashMap getProperties() { + return properties; + } + + public void setProperties(LinkedHashMap properties) { + this.properties = properties; + } + + public void addProperties(Property property) { + this.properties.put(property.getName(), property); + } + + public ArrayList propertiesNames() { + return new ArrayList<>(properties.keySet()); + } + + @Override + public String toString() { + return name + ": " + description + ", version: " + version + ", nb de properties: " + properties.size() + + System.getProperty("line.separator") + propertiesNames(); + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java new file mode 100644 index 00000000..443a4b0c --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java @@ -0,0 +1,109 @@ +/*- + * ============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 java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map.Entry; +import org.yaml.snakeyaml.Yaml; + +public class ToscaItemsParser { + private LinkedHashMap allItemsFound; + + /** + * 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 getAllItemsFound() { + return allItemsFound; + } + + private static LinkedHashMap searchAllDataTypesAndPolicyTypes(String toscaYaml) { + LinkedHashMap> file = + (LinkedHashMap>) new Yaml().load(toscaYaml); + // Get DataTypes + LinkedHashMap allItemsFound = file.get("data_types"); + allItemsFound = (allItemsFound == null) ? (new LinkedHashMap<>()) : allItemsFound; + // Put the policies and datatypes in the same collection + allItemsFound.putAll(file.get("policy_types")); + return allItemsFound; + } + + private static LinkedHashMap searchAllNativeToscaDataTypes(String toscaNativeYaml) { + return ((LinkedHashMap>) new Yaml().load(toscaNativeYaml)) + .get("data_types"); + } + + /** + * 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 + */ + private static LinkedHashMap searchAllToscaElements(String toscaYaml, + String nativeToscaYaml) { + LinkedHashMap allItemsFound = searchAllDataTypesAndPolicyTypes(toscaYaml); + allItemsFound.putAll(searchAllNativeToscaDataTypes(nativeToscaYaml)); + return parseAllItemsFound(allItemsFound); + } + + /** + * With all the component, get as Map, Components and Components properties are created. + * + * @param allMaps maps + */ + private static LinkedHashMap parseAllItemsFound(LinkedHashMap allMaps) { + LinkedHashMap allItemsFound = new LinkedHashMap(); + //Component creations, from the file maps + for (Entry itemToParse : allMaps.entrySet()) { + LinkedHashMap componentBody = (LinkedHashMap) itemToParse.getValue(); + ToscaElement toscaElement = + new ToscaElement(itemToParse.getKey(), (String) componentBody.get("derived_from"), + (String) componentBody.get("description")); + //If policy, version and type_version : + if (componentBody.get("type_version") != null) { + toscaElement.setVersion((String) componentBody.get("type_version")); + toscaElement.setTypeVersion((String) componentBody.get("type_version")); + } + //Properties creation, from the map + if (componentBody.get("properties") != null) { + LinkedHashMap properties = + (LinkedHashMap) componentBody.get("properties"); + for (Entry itemToProperty : properties.entrySet()) { + Property property = new Property(itemToProperty.getKey(), + (LinkedHashMap) itemToProperty.getValue()); + toscaElement.addProperties(property); + } + } + allItemsFound.put(toscaElement.getName(), toscaElement); + } + return allItemsFound; + } +} 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 889125fe..b7ed1de9 100644 --- a/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java +++ b/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java @@ -54,7 +54,7 @@ public class CsarServiceInstaller { private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarServiceInstaller.class); @Autowired - ServiceRepository serviceRepository; + ServicesRepository serviceRepository; @Autowired CdsServices cdsServices; diff --git a/src/main/java/org/onap/clamp/loop/service/ServiceRepository.java b/src/main/java/org/onap/clamp/loop/service/ServiceRepository.java deleted file mode 100644 index a6c5ff18..00000000 --- a/src/main/java/org/onap/clamp/loop/service/ServiceRepository.java +++ /dev/null @@ -1,32 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2019 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.loop.service; - -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface ServiceRepository extends CrudRepository { - -} diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index d52e418e..c9055bf9 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -44,7 +44,7 @@ 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.TemplateManagement; +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; @@ -300,9 +300,9 @@ public abstract class Policy extends AuditEntity { public static JsonObject generateJsonRepresentationFromToscaModel(String policyToscaModel, String policyModelType) throws IOException, UnknownComponentException { - return new TemplateManagement(policyToscaModel,ResourceFileUtil.getResourceAsString( - "clds/tosca_update/defaultToscaTypes.yaml"), + return new ToscaConverterManager(policyToscaModel,ResourceFileUtil.getResourceAsString( + "clds/tosca_update/default-tosca-types.yaml"), ResourceFileUtil.getResourceAsString("clds/tosca_update/templates.json")) - .launchTranslation(policyModelType); + .startConversionToJson(policyModelType); } } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java index 357a96d2..ad6cbd94 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java @@ -23,11 +23,11 @@ package org.onap.clamp.policy.operational; -import com.google.gson.JsonObject; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.onap.clamp.loop.Loop; +import org.onap.clamp.loop.template.PolicyModelsRepository; import org.onap.clamp.policy.PolicyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -37,9 +37,13 @@ public class OperationalPolicyService implements PolicyService operationalPolicyRepository .findById(policy.getName()) - .map(p -> setConfigurationJson(p, policy)) - .orElse(new OperationalPolicy(policy.getName(), loop, - policy.getConfigurationsJson(), - policy.getPolicyModel(), null, policy.getPdpGroup(), policy.getPdpSubgroup()))) + .map(p -> setConfiguration(p, policy)) + .orElse(initializeMissingFields(loop,policy))) .collect(Collectors.toSet()); } @@ -61,7 +63,12 @@ public class OperationalPolicyService implements PolicyService