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<String, Component> allItems;
- private String source;
- private String nativeComponent;
+public class ToscaItemsParser {
+ private LinkedHashMap<String, ToscaElement> 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<String, Component>();
- getAllAsMaps();
+ public LinkedHashMap<String, ToscaElement> getAllItemsFound() {
+ return allItemsFound;
}
- public LinkedHashMap<String, Component> getAllItems() {
- return allItems;
+ private static LinkedHashMap<String, Object> searchAllDataTypesAndPolicyTypes(String toscaYaml) {
+ LinkedHashMap<String, LinkedHashMap<String, Object>> file =
+ (LinkedHashMap<String, LinkedHashMap<String, Object>>) new Yaml().load(toscaYaml);
+ // Get DataTypes
+ LinkedHashMap<String, Object> allItemsFound = file.get("data_types");
+ allItemsFound = (allItemsFound == null) ? (new LinkedHashMap<>()) : allItemsFound;
+ // 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<String, Object> searchAllNativeToscaDataTypes(String toscaNativeYaml) {
+ return ((LinkedHashMap<String, LinkedHashMap<String, Object>>) 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<String, Object> getAllAsMaps() {
- Yaml yaml = new Yaml();
- Object contentFile = yaml.load(source);
- LinkedHashMap<String, LinkedHashMap<String, Object>> file =
- (LinkedHashMap<String, LinkedHashMap<String, Object>>) contentFile;
- // Get DataTypes
- LinkedHashMap<String, Object> dataTypes = file.get("data_types");
- dataTypes = (dataTypes == null) ? (new LinkedHashMap<>()) : dataTypes;
- // Get Policies : first, get topology and after extract policies from it
- LinkedHashMap<String, Object> policyTypes = file.get("policy_types");
- // Put the policies and datatypes in the same collection
- dataTypes.putAll(policyTypes);
-
- Object contentNativeFile = yaml.load(nativeComponent);
- LinkedHashMap<String, Object> dataTypesEmbedded =
- ((LinkedHashMap<String, LinkedHashMap<String, Object>>) contentNativeFile).get("data_types");
- dataTypes.putAll(dataTypesEmbedded);
-
- parseInComponent(dataTypes);
- return dataTypes;
+ private static LinkedHashMap<String, ToscaElement> searchAllToscaElements(String toscaYaml,
+ String nativeToscaYaml) {
+ LinkedHashMap<String, Object> allItemsFound = searchAllDataTypesAndPolicyTypes(toscaYaml);
+ allItemsFound.putAll(searchAllNativeToscaDataTypes(nativeToscaYaml));
+ return parseAllItemsFound(allItemsFound);
}
/**
*
* @param allMaps maps
*/
- @SuppressWarnings("unchecked")
- public void parseInComponent(LinkedHashMap<String, Object> allMaps) {
+ private static LinkedHashMap<String, ToscaElement> parseAllItemsFound(LinkedHashMap<String, Object> allMaps) {
+ LinkedHashMap<String, ToscaElement> allItemsFound = new LinkedHashMap<String, ToscaElement>();
//Component creations, from the file maps
for (Entry<String, Object> itemToParse : allMaps.entrySet()) {
LinkedHashMap<String, Object> componentBody = (LinkedHashMap<String, Object>) 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) {
for (Entry<String, Object> itemToProperty : properties.entrySet()) {
Property property = new Property(itemToProperty.getKey(),
(LinkedHashMap<String, Object>) itemToProperty.getValue());
- component.addProperties(property);
+ toscaElement.addProperties(property);
}
}
- this.allItems.put(component.getName(), component);
+ allItemsFound.put(toscaElement.getName(), toscaElement);
}
+ return allItemsFound;
}
}