1 package org.openecomp.core.impl;
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;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Objects;
16 import java.util.Optional;
18 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
19 private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
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();
29 public GlobalSubstitutionServiceTemplate() {
35 public void appendNodes(Map<String, NodeType> nodes) {
36 Optional<Map<String, NodeType>> nodeTypesToAdd =
37 removeExistingGlobalTypes(nodes);
39 nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
43 writeDefinitionSection();
44 writeMetadataSection();
45 writeImportsSection();
46 setNode_types(new HashMap<>());
49 private void writeImportsSection() {
50 List<Map<String, Import>> imports = new ArrayList<>();
51 Map<String, Import> stringImportMap = new HashMap<>();
52 imports.add(stringImportMap);
54 Import imprtObj = new Import();
55 imprtObj.setFile("openecomp-heat/_index.yml");
56 stringImportMap.put("openecomp_heat_index", imprtObj);
60 private void writeMetadataSection() {
61 Map<String, String> metadata = new HashMap<>();
62 metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
63 setMetadata(metadata);
66 private void writeDefinitionSection() {
67 setTosca_definitions_version(DEFININTION_VERSION);
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");
74 if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
75 return Optional.of(nodes);
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());
85 return Optional.of(nodeTypesToAdd);