4ff8af495c4a6093651ba817716028954bf27161
[sdc.git] /
1 package org.openecomp.core.impl;
2
3 import org.apache.commons.collections4.MapUtils;
4 import org.openecomp.sdc.logging.api.Logger;
5 import org.openecomp.sdc.logging.api.LoggerFactory;
6 import org.openecomp.sdc.tosca.datatypes.model.Import;
7 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
8 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
9 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Objects;
16 import java.util.Optional;
17
18 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
19     private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
20
21     public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME =
22         "GlobalSubstitutionTypesServiceTemplate.yaml";
23     public static final String TEMPLATE_NAME_PROPERTY = "template_name";
24     public static final String DEFININTION_VERSION = "tosca_simple_yaml_1_0_0";
25     public static final String HEAT_INDEX = "openecomp_heat_index";
26     private static final Map<String, ServiceTemplate> globalServiceTemplates =
27         GlobalTypesGenerator.getGlobalTypesServiceTemplate();
28
29     public GlobalSubstitutionServiceTemplate() {
30         super();
31         init();
32     }
33
34
35     public void appendNodes(Map<String, NodeType> nodes) {
36         Optional<Map<String, NodeType>> nodeTypesToAdd =
37             removeExistingGlobalTypes(nodes);
38
39         nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
40     }
41
42     public void init()   {
43         writeDefinitionSection();
44         writeMetadataSection();
45         writeImportsSection();
46         setNode_types(new HashMap<>());
47     }
48
49     private void writeImportsSection() {
50         List<Map<String, Import>> imports = new ArrayList<>();
51         Map<String, Import> stringImportMap = new HashMap<>();
52         imports.add(stringImportMap);
53         setImports(imports);
54         Import imprtObj = new Import();
55         imprtObj.setFile("openecomp-heat/_index.yml");
56         stringImportMap.put("openecomp_heat_index", imprtObj);
57     }
58
59
60     private void writeMetadataSection() {
61         Map<String, String> metadata = new HashMap<>();
62         metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
63         setMetadata(metadata);
64     }
65
66     private void writeDefinitionSection() {
67         setTosca_definitions_version(DEFININTION_VERSION);
68     }
69
70     private Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){
71         Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
72         ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml");
73
74         if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
75             return Optional.of(nodes);
76         }
77
78         Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
79         for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
80             if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
81                 Optional<NodeType> nodeType =
82                     ToscaConverterUtil
83                         .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
84
85                 nodeType
86                     .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
87             }
88         }
89
90         return Optional.of(nodeTypesToAdd);
91     }
92
93     private Map<String, NodeType> getAllGlobalNodeTypes(){
94         Map<String, NodeType> globalNodeTypes = new HashMap<>();
95
96         for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
97             if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
98                 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
99             }
100         }
101
102         return globalNodeTypes;
103     }
104
105     private boolean isNodesServiceTemplate(String filename) {
106         return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");
107     }
108 }