f53cf59c954438d3245a77019017725e3ea3a6cc
[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 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.globaltypes;
18
19 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
20 import org.openecomp.sdc.common.errors.CoreException;
21 import org.openecomp.sdc.common.errors.ErrorCategory;
22 import org.openecomp.sdc.common.errors.ErrorCode;
23 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
24 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
25 import org.openecomp.sdc.tosca.services.ToscaUtil;
26 import org.openecomp.sdc.translator.utils.ResourceWalker;
27
28 import java.util.EnumMap;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 public class GlobalTypesServiceTemplates {
33
34   private static final String FAILED_TO_GENERATE_GLOBAL_TYPES = "Failed to generate globalTypes";
35
36   private static final Map<OnboardingTypesEnum, Map<String, ServiceTemplate>>
37       onboardingGlobalTypesServiceTemplates;
38
39   private GlobalTypesServiceTemplates() {
40   }
41
42   static {
43     Map<String, String> globalTypes;
44     try {
45       globalTypes = ResourceWalker.readResourcesFromDirectory("globalTypes");
46     } catch (CoreException coreException) {
47       throw coreException;
48     } catch (Exception exception) {
49       throw new CoreException((new ErrorCode.ErrorCodeBuilder())
50           .withMessage(FAILED_TO_GENERATE_GLOBAL_TYPES)
51           .withId("GlobalTypes Read Error").withCategory(ErrorCategory.APPLICATION).build(),
52           exception);
53     }
54     onboardingGlobalTypesServiceTemplates = init(globalTypes);
55   }
56
57   public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates(OnboardingTypesEnum
58                                                                                 onboardingType) {
59     if (onboardingType == null) {
60       throw new CoreException((new ErrorCode.ErrorCodeBuilder())
61           .withMessage(FAILED_TO_GENERATE_GLOBAL_TYPES)
62           .withId("Invalid Onboarding Type").withCategory(ErrorCategory.APPLICATION).build());
63     }
64     return onboardingGlobalTypesServiceTemplates.get(onboardingType);
65   }
66
67   private static Map<OnboardingTypesEnum, Map<String, ServiceTemplate>>
68                                       init(Map<String, String> globalTypes) {
69     Map<OnboardingTypesEnum, Map<String, ServiceTemplate>>
70         onboardingGlobalTypesServiceTemplates = new EnumMap<>(OnboardingTypesEnum.class);
71     Map<String, ServiceTemplate> zipOnboardingGlobalTypes =
72         getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.ZIP);
73     Map<String, ServiceTemplate> csarOnboardingGlobalTypes =
74         getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.CSAR);
75     Map<String, ServiceTemplate> manualOnboardingGlobalTypes =
76         getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.MANUAL);
77     Map<String, ServiceTemplate> defaultOnboardingGlobalTypes =
78         getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.NONE);
79     onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.ZIP, zipOnboardingGlobalTypes);
80     onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.CSAR, csarOnboardingGlobalTypes);
81     onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.MANUAL,
82         manualOnboardingGlobalTypes);
83     onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.NONE,
84         defaultOnboardingGlobalTypes);
85     return onboardingGlobalTypesServiceTemplates;
86   }
87
88   private static Map<String, ServiceTemplate> getOnboardingGlobalTypes(Map<String, String>
89                                                                          globalTypes,
90                                                                        OnboardingTypesEnum
91                                                                            onboardingType) {
92     Map<String, ServiceTemplate> globalTypesServiceTemplates = new HashMap<>();
93     ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
94     for (Map.Entry<String, String> globalTypeContent : globalTypes.entrySet()) {
95       if (!isTypeValidCandidateForCsarPacking(globalTypeContent.getKey(), onboardingType)) {
96         // this global types folders should not be processed to the CSAR
97         continue;
98       }
99       ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates,
100           toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class));
101     }
102     return globalTypesServiceTemplates;
103   }
104
105   private static boolean isTypeValidCandidateForCsarPacking(String globalTypeResourceKey,
106                                                             OnboardingTypesEnum
107                                                                 onboardingType) {
108     if (globalTypeResourceKey.contains("openecomp-inventory")) {
109       // this global types folders should not be processed to the CSAR
110       return false;
111     }
112     //Global types specific to csar onboarding should not be packed for other onboarding types
113     return !globalTypeResourceKey.contains("onap") || onboardingType == OnboardingTypesEnum.CSAR;
114   }
115
116 }