X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Ftosca%2Fupdate%2FToscaItemsParser.java;fp=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Ftosca%2Fupdate%2FExtractor.java;h=443a4b0cd168693e51029953853c25e7a9a53ad8;hb=8604d37f2f6bdd011de62ec474b6883413d30348;hp=c6eabcd34c1906867ef16c298aeacb57ff758abd;hpb=3af9347e47302e3f6754cba8ea2b63772980a5d9;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java similarity index 53% rename from src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java rename to src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java index c6eabcd3..443a4b0c 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/Extractor.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaItemsParser.java @@ -23,64 +23,55 @@ 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 Extractor { - private LinkedHashMap allItems; - private String source; - private String nativeComponent; +public class ToscaItemsParser { + private LinkedHashMap allItemsFound; /** * Constructor. * - * @param toParse Tosca to parse - * @param nativeComponent The policy type to scan + * @param toscaYaml The tosca to parse + * @param toscaNativeDataTypeYaml THe name of the policy type to search */ - public Extractor(String toParse, String nativeComponent) { + public ToscaItemsParser(String toscaYaml, String toscaNativeDataTypeYaml) { + this.allItemsFound = searchAllToscaElements(toscaYaml, toscaNativeDataTypeYaml); + } - this.source = toParse; - this.nativeComponent = nativeComponent; - allItems = new LinkedHashMap(); - getAllAsMaps(); + public LinkedHashMap getAllItemsFound() { + return allItemsFound; } - public LinkedHashMap getAllItems() { - return allItems; + 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; } - public String getSource() { - return source; + 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 an object + * @return a map */ - @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; + private static LinkedHashMap searchAllToscaElements(String toscaYaml, + String nativeToscaYaml) { + LinkedHashMap allItemsFound = searchAllDataTypesAndPolicyTypes(toscaYaml); + allItemsFound.putAll(searchAllNativeToscaDataTypes(nativeToscaYaml)); + return parseAllItemsFound(allItemsFound); } /** @@ -88,17 +79,18 @@ public class Extractor { * * @param allMaps maps */ - @SuppressWarnings("unchecked") - public void parseInComponent(LinkedHashMap allMaps) { + 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(); - Component component = new Component(itemToParse.getKey(), (String) componentBody.get("derived_from"), - (String) componentBody.get("description")); + 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) { - component.setVersion((String) componentBody.get("type_version")); - component.setTypeVersion((String) componentBody.get("type_version")); + toscaElement.setVersion((String) componentBody.get("type_version")); + toscaElement.setTypeVersion((String) componentBody.get("type_version")); } //Properties creation, from the map if (componentBody.get("properties") != null) { @@ -107,10 +99,11 @@ public class Extractor { for (Entry itemToProperty : properties.entrySet()) { Property property = new Property(itemToProperty.getKey(), (LinkedHashMap) itemToProperty.getValue()); - component.addProperties(property); + toscaElement.addProperties(property); } } - this.allItems.put(component.getName(), component); + allItemsFound.put(toscaElement.getName(), toscaElement); } + return allItemsFound; } }