98d8d23751b593b23c8eb0a9fbc007b32c001bd3
[sdc.git] /
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 java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Optional;
27 import java.util.stream.Collectors;
28 import org.apache.commons.collections4.MapUtils;
29 import org.onap.sdc.tosca.datatypes.model.DataType;
30 import org.onap.sdc.tosca.datatypes.model.Import;
31 import org.onap.sdc.tosca.datatypes.model.NodeType;
32 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
33 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
34 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
35
36 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
37
38     public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME =
39         "GlobalSubstitutionTypesServiceTemplate.yaml";
40     public static final String TEMPLATE_NAME_PROPERTY = "template_name";
41     public static final String DEFINITION_VERSION = "tosca_simple_yaml_1_0_0";
42     public static final String HEAT_INDEX = "openecomp_heat_index";
43     public static final String HEAT_INDEX_IMPORT_FILE = "openecomp-heat/_index.yml";
44     public static final String ONAP_INDEX_IMPORT_FILE = "onap/_index.yml";
45
46     // transient needed to avoid being parsed as a YAML String. Used parser is reading fields instead of getters,
47     // although it ignores static or transient fields.
48     private final transient Map<String, ServiceTemplate> globalServiceTemplates;
49     private final transient Map<String, DataType> globalDataTypeMap;
50
51     public GlobalSubstitutionServiceTemplate() {
52         super();
53         init();
54         globalServiceTemplates =
55             GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
56         globalDataTypeMap = loadGlobalDataTypes();
57     }
58
59     public void init()   {
60         writeDefinitionSection();
61         writeMetadataSection();
62         writeImportsSection();
63         setNode_types(new HashMap<>());
64         setData_types(new HashMap<>());
65     }
66
67     public void appendNodes(final Map<String, NodeType> nodes) {
68         final Optional<Map<String, NodeType>> nodeTypesToAdd = findNonGlobalTypesNodes(nodes);
69         nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
70     }
71
72     public void appendDataTypes(final Map<String, DataType> dataTypeMap) {
73         if (MapUtils.isEmpty(dataTypeMap)) {
74             return;
75         }
76         dataTypeMap.entrySet().stream()
77             .filter(dataTypeEntry -> !isGlobalDataType(dataTypeEntry.getKey()))
78             .forEach(dataTypeEntry -> {
79                 final Optional<DataType> dataType = parseDataTypeToYamlObject(dataTypeEntry);
80                 dataType.ifPresent(dataType1 -> getData_types().put(dataTypeEntry.getKey(), dataType1));
81             });
82     }
83
84     private void writeImportsSection() {
85         List<Map<String, Import>> imports = new ArrayList<>();
86         Map<String, Import> stringImportMap = new HashMap<>();
87         imports.add(stringImportMap);
88         setImports(imports);
89         Import imprtObj = new Import();
90         imprtObj.setFile(HEAT_INDEX_IMPORT_FILE);
91         stringImportMap.put(HEAT_INDEX, imprtObj);
92         Import onapDefinitionsImport = new Import();
93         onapDefinitionsImport.setFile(ONAP_INDEX_IMPORT_FILE);
94         stringImportMap.put(ONAP_INDEX, onapDefinitionsImport);
95     }
96
97
98     private void writeMetadataSection() {
99         Map<String, String> metadata = new HashMap<>();
100         metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
101         setMetadata(metadata);
102     }
103
104     private void writeDefinitionSection() {
105         setTosca_definitions_version(DEFINITION_VERSION);
106     }
107
108     private Optional<Map<String, NodeType>> findNonGlobalTypesNodes(final Map<String, NodeType> nodes){
109         final Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
110         if (MapUtils.isEmpty(globalNodeTypes)) {
111             return Optional.of(nodes);
112         }
113
114         final Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
115
116         for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
117             if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
118                 Optional<NodeType> nodeType = parseNodeTypeToYamlObject(nodeTypeEntry);
119                 nodeType
120                     .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
121             }
122         }
123
124         return Optional.of(nodeTypesToAdd);
125     }
126
127     private boolean isGlobalDataType(final String dataType) {
128         if (MapUtils.isEmpty(globalDataTypeMap)) {
129             return false;
130         }
131
132         return globalDataTypeMap.containsKey(dataType);
133     }
134
135     private Optional<NodeType> parseNodeTypeToYamlObject(final Entry<String, NodeType> nodeTypeEntry) {
136         return ToscaConverterUtil
137             .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
138     }
139
140     private Optional<DataType> parseDataTypeToYamlObject(final Entry<String, DataType> dataTypeEntry) {
141         return ToscaConverterUtil
142             .createObjectFromClass(dataTypeEntry.getKey(), dataTypeEntry.getValue(), DataType.class);
143     }
144
145     private Map<String, DataType> loadGlobalDataTypes() {
146         return globalServiceTemplates.values().stream()
147             .map(ServiceTemplate::getData_types)
148             .filter(MapUtils::isNotEmpty)
149             .flatMap(stringDataTypeMap -> stringDataTypeMap.entrySet().stream())
150             .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (dataType, dataType2) -> dataType));
151     }
152
153     private Map<String, NodeType> getAllGlobalNodeTypes(){
154         Map<String, NodeType> globalNodeTypes = new HashMap<>();
155
156         for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
157             if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
158                 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
159             }
160         }
161
162         return globalNodeTypes;
163     }
164
165     private boolean isNodesServiceTemplate(String filename) {
166         return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");
167     }
168 }