2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.tosca.update;
26 import java.util.LinkedHashMap;
27 import java.util.Map.Entry;
28 import org.yaml.snakeyaml.Yaml;
30 public class Extractor {
31 private LinkedHashMap<String, Component> allItems;
32 private String source;
33 private String nativeComponent;
38 * @param toParse Tosca to parse
39 * @param nativeComponent The policy type to scan
41 public Extractor(String toParse, String nativeComponent) {
43 this.source = toParse;
44 this.nativeComponent = nativeComponent;
45 allItems = new LinkedHashMap<String, Component>();
49 public LinkedHashMap<String, Component> getAllItems() {
53 public String getSource() {
58 * Yaml Parse gets raw policies and datatypes, in different sections : necessary to extract
59 * all entities and put them at the same level.
63 @SuppressWarnings("unchecked")
64 public LinkedHashMap<String, Object> getAllAsMaps() {
65 Yaml yaml = new Yaml();
66 Object contentFile = yaml.load(source);
67 LinkedHashMap<String, LinkedHashMap<String, Object>> file =
68 (LinkedHashMap<String, LinkedHashMap<String, Object>>) contentFile;
70 LinkedHashMap<String, Object> dataTypes = file.get("data_types");
71 dataTypes = (dataTypes == null) ? (new LinkedHashMap<>()) : dataTypes;
72 // Get Policies : first, get topology and after extract policies from it
73 LinkedHashMap<String, Object> policyTypes = file.get("policy_types");
74 // Put the policies and datatypes in the same collection
75 dataTypes.putAll(policyTypes);
77 Object contentNativeFile = yaml.load(nativeComponent);
78 LinkedHashMap<String, Object> dataTypesEmbedded =
79 ((LinkedHashMap<String, LinkedHashMap<String, Object>>) contentNativeFile).get("data_types");
80 dataTypes.putAll(dataTypesEmbedded);
82 parseInComponent(dataTypes);
87 * With all the component, get as Map, Components and Components properties are created.
91 @SuppressWarnings("unchecked")
92 public void parseInComponent(LinkedHashMap<String, Object> allMaps) {
93 //Component creations, from the file maps
94 for (Entry<String, Object> itemToParse : allMaps.entrySet()) {
95 LinkedHashMap<String, Object> componentBody = (LinkedHashMap<String, Object>) itemToParse.getValue();
96 Component component = new Component(itemToParse.getKey(), (String) componentBody.get("derived_from"),
97 (String) componentBody.get("description"));
98 //If policy, version and type_version :
99 if (componentBody.get("type_version") != null) {
100 component.setVersion((String) componentBody.get("type_version"));
101 component.setTypeVersion((String) componentBody.get("type_version"));
103 //Properties creation, from the map
104 if (componentBody.get("properties") != null) {
105 LinkedHashMap<String, Object> properties =
106 (LinkedHashMap<String, Object>) componentBody.get("properties");
107 for (Entry<String, Object> itemToProperty : properties.entrySet()) {
108 Property property = new Property(itemToProperty.getKey(),
109 (LinkedHashMap<String, Object>) itemToProperty.getValue());
110 component.addProperties(property);
113 this.allItems.put(component.getName(), component);