2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.core.impl;
19 import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
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;
36 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
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";
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;
51 public GlobalSubstitutionServiceTemplate() {
54 globalServiceTemplates =
55 GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
56 globalDataTypeMap = loadGlobalDataTypes();
60 writeDefinitionSection();
61 writeMetadataSection();
62 writeImportsSection();
63 setNode_types(new HashMap<>());
64 setData_types(new HashMap<>());
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));
72 public void appendDataTypes(final Map<String, DataType> dataTypeMap) {
73 if (MapUtils.isEmpty(dataTypeMap)) {
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));
84 private void writeImportsSection() {
85 List<Map<String, Import>> imports = new ArrayList<>();
86 Map<String, Import> stringImportMap = new HashMap<>();
87 imports.add(stringImportMap);
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);
98 private void writeMetadataSection() {
99 Map<String, String> metadata = new HashMap<>();
100 metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
101 setMetadata(metadata);
104 private void writeDefinitionSection() {
105 setTosca_definitions_version(DEFINITION_VERSION);
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);
114 final Map<String, NodeType> nodeTypesToAdd = new HashMap<>();
116 for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
117 if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
118 Optional<NodeType> nodeType = parseNodeTypeToYamlObject(nodeTypeEntry);
120 .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
124 return Optional.of(nodeTypesToAdd);
127 private boolean isGlobalDataType(final String dataType) {
128 if (MapUtils.isEmpty(globalDataTypeMap)) {
132 return globalDataTypeMap.containsKey(dataType);
135 private Optional<NodeType> parseNodeTypeToYamlObject(final Entry<String, NodeType> nodeTypeEntry) {
136 return ToscaConverterUtil
137 .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
140 private Optional<DataType> parseDataTypeToYamlObject(final Entry<String, DataType> dataTypeEntry) {
141 return ToscaConverterUtil
142 .createObjectFromClass(dataTypeEntry.getKey(), dataTypeEntry.getValue(), DataType.class);
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));
153 private Map<String, NodeType> getAllGlobalNodeTypes(){
154 Map<String, NodeType> globalNodeTypes = new HashMap<>();
156 for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
157 if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
158 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
162 return globalNodeTypes;
165 private boolean isNodesServiceTemplate(String filename) {
166 return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");