4a1f7c53cce4f710ee7909d8452da83c3755107c
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / impl / GlobalSubstitutionServiceTemplate.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.impl;
18
19 import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;
20
21 import org.apache.commons.collections4.MapUtils;
22 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.tosca.datatypes.model.Import;
26 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35 import java.util.Optional;
36
37 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
38     private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
39
40     public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME =
41         "GlobalSubstitutionTypesServiceTemplate.yaml";
42     public static final String TEMPLATE_NAME_PROPERTY = "template_name";
43     public static final String DEFININTION_VERSION = "tosca_simple_yaml_1_0_0";
44     public static final String HEAT_INDEX = "openecomp_heat_index";
45     public static final String HEAT_INDEX_IMPORT_FILE = "openecomp-heat/_index.yml";
46     public static final String ONAP_INDEX_IMPORT_FILE = "onap/_index.yml";
47     private static final Map<String, ServiceTemplate> globalServiceTemplates =
48         GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
49
50     public GlobalSubstitutionServiceTemplate() {
51         super();
52         init();
53     }
54
55
56     public void appendNodes(Map<String, NodeType> nodes) {
57         Optional<Map<String, NodeType>> nodeTypesToAdd =
58             removeExistingGlobalTypes(nodes);
59
60         nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
61     }
62
63     public void init()   {
64         writeDefinitionSection();
65         writeMetadataSection();
66         writeImportsSection();
67         setNode_types(new HashMap<>());
68     }
69
70     private void writeImportsSection() {
71         List<Map<String, Import>> imports = new ArrayList<>();
72         Map<String, Import> stringImportMap = new HashMap<>();
73         imports.add(stringImportMap);
74         setImports(imports);
75         Import imprtObj = new Import();
76         imprtObj.setFile(HEAT_INDEX_IMPORT_FILE);
77         stringImportMap.put(HEAT_INDEX, imprtObj);
78         Import onapDefinitionsImport = new Import();
79         onapDefinitionsImport.setFile(ONAP_INDEX_IMPORT_FILE);
80         stringImportMap.put(ONAP_INDEX, onapDefinitionsImport);
81     }
82
83
84     private void writeMetadataSection() {
85         Map<String, String> metadata = new HashMap<>();
86         metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
87         setMetadata(metadata);
88     }
89
90     private void writeDefinitionSection() {
91         setTosca_definitions_version(DEFININTION_VERSION);
92     }
93
94     private Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){
95         Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
96         ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml");
97
98         if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
99             return Optional.of(nodes);
100         }
101
102         Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
103         for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
104             if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
105                 Optional<NodeType> nodeType =
106                     ToscaConverterUtil
107                         .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
108
109                 nodeType
110                     .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
111             }
112         }
113
114         return Optional.of(nodeTypesToAdd);
115     }
116
117     private Map<String, NodeType> getAllGlobalNodeTypes(){
118         Map<String, NodeType> globalNodeTypes = new HashMap<>();
119
120         for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
121             if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
122                 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
123             }
124         }
125
126         return globalNodeTypes;
127     }
128
129     private boolean isNodesServiceTemplate(String filename) {
130         return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");
131     }
132 }