2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.tosca.services;
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
26 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
28 import java.util.Collections;
29 import java.util.HashMap;
31 import java.util.Objects;
32 import java.util.Optional;
33 import java.util.UUID;
36 * The type Tosca util.
38 public class ToscaUtil {
41 * Gets service template file name.
43 * @param serviceTemplate the service template
44 * @return the service template file name
46 public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) {
47 if (serviceTemplate == null) {
50 if (serviceTemplate.getMetadata() == null) {
51 return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
53 return getServiceTemplateFileName(serviceTemplate.getMetadata());
57 * Gets service template file name.
59 * @param metadata the file name
60 * @return the service template file name
62 public static String getServiceTemplateFileName(Map<String, String> metadata) {
63 if (metadata.get(ToscaConstants.ST_METADATA_FILE_NAME) != null) {
64 return metadata.get(ToscaConstants.ST_METADATA_FILE_NAME);
65 } else if (metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) != null) {
66 return metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) + "ServiceTemplate.yaml";
68 return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
72 public static Optional<String> getSubstitutableGroupMemberId(String heatFileName,
73 ServiceTemplate serviceTemplate){
75 Map<String, NodeTemplate> nodeTemplates =
76 DataModelUtil.getNodeTemplates(serviceTemplate);
78 if(MapUtils.isEmpty(nodeTemplates)){
79 return Optional.empty();
82 String heatFileNameWithoutExt = FileUtils.getFileWithoutExtention(heatFileName);
84 for(Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplates.entrySet()){
85 String subServiceTemplateName =
86 getSubstitutionServiceTemplateNameFromProperties(nodeTemplateEntry);
88 if(Objects.nonNull(subServiceTemplateName)
89 && isGroupMemberIdSubstitutable(heatFileNameWithoutExt, subServiceTemplateName)){
90 return Optional.of(nodeTemplateEntry.getKey());
94 return Optional.empty();
97 private static boolean isGroupMemberIdSubstitutable(String heatFileNameWithoutExt,
98 String subServiceTemplateName) {
99 return subServiceTemplateName.startsWith(heatFileNameWithoutExt);
102 private static String getSubstitutionServiceTemplateNameFromProperties(
103 Map.Entry<String, NodeTemplate> nodeTemplateEntry) {
104 Map<String, Object> properties =
105 nodeTemplateEntry.getValue().getProperties() == null ? Collections.emptyMap() :
106 nodeTemplateEntry.getValue().getProperties();
108 Map<String, Object> serviceTemplateFilter =
109 properties.containsKey(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME)?
110 (Map<String, Object>) properties.get(ToscaConstants
111 .SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) : Collections.emptyMap();
113 return (String) serviceTemplateFilter.get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME);
118 * Add service template to map with key file name.
120 * @param serviceTemplateMap the service template map
121 * @param serviceTemplate the service template
123 public static void addServiceTemplateToMapWithKeyFileName(
124 Map<String, ServiceTemplate> serviceTemplateMap, ServiceTemplate serviceTemplate) {
125 serviceTemplateMap.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate);
128 public static String getServiceTemplateFileName(String templateName) {
129 Map<String, String> metadata = new HashMap<>();
130 metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
131 return getServiceTemplateFileName(metadata);