re base code
[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 org.apache.commons.collections4.MapUtils;
20 import org.onap.sdc.tosca.datatypes.model.Import;
21 import org.onap.sdc.tosca.datatypes.model.NodeType;
22 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
23 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
27
28 import java.util.*;
29
30 import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;
31
32 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
33     private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
34
35     public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME =
36         "GlobalSubstitutionTypesServiceTemplate.yaml";
37     public static final String TEMPLATE_NAME_PROPERTY = "template_name";
38     public static final String DEFININTION_VERSION = "tosca_simple_yaml_1_0_0";
39     public static final String HEAT_INDEX = "openecomp_heat_index";
40     public static final String HEAT_INDEX_IMPORT_FILE = "openecomp-heat/_index.yml";
41     public static final String ONAP_INDEX_IMPORT_FILE = "onap/_index.yml";
42     private static final Map<String, ServiceTemplate> globalServiceTemplates =
43         GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
44
45     public GlobalSubstitutionServiceTemplate() {
46         super();
47         init();
48     }
49
50
51     public void appendNodes(Map<String, NodeType> nodes) {
52         Optional<Map<String, NodeType>> nodeTypesToAdd =
53             removeExistingGlobalTypes(nodes);
54
55         nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
56     }
57
58     public void init()   {
59         writeDefinitionSection();
60         writeMetadataSection();
61         writeImportsSection();
62         setNode_types(new HashMap<>());
63     }
64
65     private void writeImportsSection() {
66         List<Map<String, Import>> imports = new ArrayList<>();
67         Map<String, Import> stringImportMap = new HashMap<>();
68         imports.add(stringImportMap);
69         setImports(imports);
70         Import imprtObj = new Import();
71         imprtObj.setFile(HEAT_INDEX_IMPORT_FILE);
72         stringImportMap.put(HEAT_INDEX, imprtObj);
73         Import onapDefinitionsImport = new Import();
74         onapDefinitionsImport.setFile(ONAP_INDEX_IMPORT_FILE);
75         stringImportMap.put(ONAP_INDEX, onapDefinitionsImport);
76     }
77
78
79     private void writeMetadataSection() {
80         Map<String, String> metadata = new HashMap<>();
81         metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
82         setMetadata(metadata);
83     }
84
85     private void writeDefinitionSection() {
86         setTosca_definitions_version(DEFININTION_VERSION);
87     }
88
89     private Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){
90         Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
91         ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml");
92
93         if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
94             return Optional.of(nodes);
95         }
96
97         Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
98         for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
99             if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
100                 Optional<NodeType> nodeType =
101                     ToscaConverterUtil
102                         .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
103
104                 nodeType
105                     .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
106             }
107         }
108
109         return Optional.of(nodeTypesToAdd);
110     }
111
112     private Map<String, NodeType> getAllGlobalNodeTypes(){
113         Map<String, NodeType> globalNodeTypes = new HashMap<>();
114
115         for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
116             if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
117                 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
118             }
119         }
120
121         return globalNodeTypes;
122     }
123
124     private boolean isNodesServiceTemplate(String filename) {
125         return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");
126     }
127 }