[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.UUID;
28
29 /**
30  * The type Tosca util.
31  */
32 public class ToscaUtil {
33
34   /**
35    * Gets service template file name.
36    *
37    * @param serviceTemplate the service template
38    * @return the service template file name
39    */
40   public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) {
41     if (serviceTemplate == null) {
42       return null;
43     }
44     if (serviceTemplate.getMetadata() == null) {
45       return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
46     }
47     return getServiceTemplateFileName(serviceTemplate.getMetadata());
48   }
49
50   /**
51    * Gets service template file name.
52    *
53    * @param metaData the file name
54    * @return the service template file name
55    */
56   public static String getServiceTemplateFileName(Map<String, String> metadata) {
57     if (metadata.get(ToscaConstants.ST_METADATA_FILE_NAME) != null) {
58       return metadata.get(ToscaConstants.ST_METADATA_FILE_NAME);
59     } else if (metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) != null) {
60       return metadata.get(ToscaConstants.ST_METADATA_TEMPLATE_NAME) + "ServiceTemplate.yaml";
61     }
62     return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
63
64   }
65
66
67   /**
68    * Add service template to map with key file name.
69    *
70    * @param serviceTemplateMap the service template map
71    * @param serviceTemplate    the service template
72    */
73   public static void addServiceTemplateToMapWithKeyFileName(
74       Map<String, ServiceTemplate> serviceTemplateMap, ServiceTemplate serviceTemplate) {
75     serviceTemplateMap.put(ToscaUtil.getServiceTemplateFileName(serviceTemplate), serviceTemplate);
76   }
77
78   public static String getServiceTemplateFileName(String templateName) {
79     Map<String, String> metadata = new HashMap<>();
80     metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
81     return getServiceTemplateFileName(metadata);
82   }
83 }