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 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;
30 import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;
32 public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {
33 private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class);
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);
45 public GlobalSubstitutionServiceTemplate() {
51 public void appendNodes(Map<String, NodeType> nodes) {
52 Optional<Map<String, NodeType>> nodeTypesToAdd =
53 removeExistingGlobalTypes(nodes);
55 nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
59 writeDefinitionSection();
60 writeMetadataSection();
61 writeImportsSection();
62 setNode_types(new HashMap<>());
65 private void writeImportsSection() {
66 List<Map<String, Import>> imports = new ArrayList<>();
67 Map<String, Import> stringImportMap = new HashMap<>();
68 imports.add(stringImportMap);
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);
79 private void writeMetadataSection() {
80 Map<String, String> metadata = new HashMap<>();
81 metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
82 setMetadata(metadata);
85 private void writeDefinitionSection() {
86 setTosca_definitions_version(DEFININTION_VERSION);
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");
93 if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){
94 return Optional.of(nodes);
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 =
102 .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
105 .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
109 return Optional.of(nodeTypesToAdd);
112 private Map<String, NodeType> getAllGlobalNodeTypes(){
113 Map<String, NodeType> globalNodeTypes = new HashMap<>();
115 for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
116 if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
117 globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
121 return globalNodeTypes;
124 private boolean isNodesServiceTemplate(String filename) {
125 return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");