cbc56d7773656ba855275a5c7c8dbd7dcbb156bb
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / globaltypes / GlobalTypesServiceTemplates.java
1 package org.openecomp.sdc.translator.services.heattotosca.globaltypes;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.common.errors.ErrorCategory;
5 import org.openecomp.sdc.common.errors.ErrorCode;
6 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
7 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
8 import org.openecomp.sdc.tosca.services.ToscaUtil;
9 import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil;
10 import org.openecomp.sdc.translator.utils.ResourceWalker;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 /**
16  * @author Avrahamg
17  * @since April 03, 2017
18  */
19 public class GlobalTypesServiceTemplates {
20   private static Map<String, ServiceTemplate> globalTypesServiceTemplates;
21
22
23   public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates() {
24     if (globalTypesServiceTemplates == null) {
25       synchronized (GlobalTypesServiceTemplates.class) {
26         if (globalTypesServiceTemplates == null) {
27           init();
28         }
29       }
30     }
31     return globalTypesServiceTemplates;
32   }
33
34   private static void init() {
35     globalTypesServiceTemplates = new HashMap<>();
36     Map<String, String> globalTypes = null;
37     try {
38       globalTypes = ResourceWalker.readResourcesFromDirectory("globalTypes");
39     } catch (CoreException coreException) {
40       throw coreException;
41     } catch (Exception exception) {
42       throw new CoreException((new ErrorCode.ErrorCodeBuilder())
43           .withMessage(LoggerErrorDescription.FAILED_TO_GENERATE_GLOBAL_TYPES)
44           .withId("GlobalTypes Read Error").withCategory(ErrorCategory.APPLICATION).build(),
45           exception);
46     }
47     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
48     for (Map.Entry<String, String> globalTypeContent : globalTypes.entrySet()) {
49       if (globalTypeContent.getKey().contains("openecomp-inventory")) { // this global types folder
50         // should not be
51         // processed to the CSAR
52         continue;
53       }
54       ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates,
55           toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class));
56     }
57   }
58
59   private GlobalTypesServiceTemplates() {
60   }
61 }