Rename packages from openecomp to onap.
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / ToscaUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.services;
22
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;
27
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Objects;
32 import java.util.Optional;
33 import java.util.UUID;
34
35 /**
36  * The type Tosca util.
37  */
38 public class ToscaUtil {
39
40   /**
41    * Gets service template file name.
42    *
43    * @param serviceTemplate the service template
44    * @return the service template file name
45    */
46   public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) {
47     if (serviceTemplate == null) {
48       return null;
49     }
50     if (serviceTemplate.getMetadata() == null) {
51       return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
52     }
53     return getServiceTemplateFileName(serviceTemplate.getMetadata());
54   }
55
56   /**
57    * Gets service template file name.
58    *
59    * @param metadata the file name
60    * @return the service template file name
61    */
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";
67     }
68     return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
69
70   }
71
72   public static Optional<String> getSubstitutableGroupMemberId(String heatFileName,
73                                                          ServiceTemplate serviceTemplate){
74
75     Map<String, NodeTemplate> nodeTemplates =
76         DataModelUtil.getNodeTemplates(serviceTemplate);
77
78     if(MapUtils.isEmpty(nodeTemplates)){
79       return Optional.empty();
80     }
81
82     String heatFileNameWithoutExt = FileUtils.getFileWithoutExtention(heatFileName);
83
84     for(Map.Entry<String, NodeTemplate> nodeTemplateEntry : nodeTemplates.entrySet()){
85       String subServiceTemplateName =
86           getSubstitutionServiceTemplateNameFromProperties(nodeTemplateEntry);
87
88       if(Objects.nonNull(subServiceTemplateName)
89           && isGroupMemberIdSubstitutable(heatFileNameWithoutExt, subServiceTemplateName)){
90         return Optional.of(nodeTemplateEntry.getKey());
91       }
92     }
93
94     return Optional.empty();
95   }
96
97   private static boolean isGroupMemberIdSubstitutable(String heatFileNameWithoutExt,
98                                                       String subServiceTemplateName) {
99     return subServiceTemplateName.startsWith(heatFileNameWithoutExt);
100   }
101
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();
107
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();
112
113     return (String) serviceTemplateFilter.get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME);
114   }
115
116
117   /**
118    * Add service template to map with key file name.
119    *
120    * @param serviceTemplateMap the service template map
121    * @param serviceTemplate    the service template
122    */
123   public static void addServiceTemplateToMapWithKeyFileName(
124       Map<String, ServiceTemplate> serviceTemplateMap, ServiceTemplate serviceTemplate) {
125     serviceTemplateMap.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate);
126   }
127
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);
132   }
133 }