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 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.onap.sdc.tosca.datatypes.model.Import;
26 import org.onap.sdc.tosca.datatypes.model.NodeType;
27 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Objects;
35 import java.util.Optional;
37 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
38 private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
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);
50 public GlobalSubstitutionServiceTemplate() {
56 public void appendNodes(Map<String, NodeType> nodes) {
57 Optional<Map<String, NodeType>> nodeTypesToAdd =
58 removeExistingGlobalTypes(nodes);
60 nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
64 writeDefinitionSection();
65 writeMetadataSection();
66 writeImportsSection();
67 setNode_types(new HashMap<>());
70 private void writeImportsSection() {
71 List<Map<String, Import>> imports = new ArrayList<>();
72 Map<String, Import> stringImportMap = new HashMap<>();
73 imports.add(stringImportMap);
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);
84 private void writeMetadataSection() {
85 Map<String, String> metadata = new HashMap<>();
86 metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
87 setMetadata(metadata);
90 private void writeDefinitionSection() {
91 setTosca_definitions_version(DEFININTION_VERSION);
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");
98 if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
99 return Optional.of(nodes);
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 =
107 .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
110 .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
114 return Optional.of(nodeTypesToAdd);
117 private Map<String, NodeType> getAllGlobalNodeTypes(){
118 Map<String, NodeType> globalNodeTypes = new HashMap<>();
120 for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
121 if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
122 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
126 return globalNodeTypes;
129 private boolean isNodesServiceTemplate(String filename) {
130 return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");