778445c513f01365277bc4c9d643c8075f4ea63c
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / impl / GlobalSubstitutionServiceTemplate.java
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     public 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 = serviceTemplate.getNode_types();
79         for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
80             if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
81                 nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeEntry.getValue());
82             }
83         }
84
85         return Optional.of(nodeTypesToAdd);
86     }
87 }