Junit Test Failure
[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-2018 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 java.util.EnumMap;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
24 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.ErrorCategory;
28 import org.openecomp.sdc.common.errors.ErrorCode;
29 import org.openecomp.sdc.tosca.services.ToscaUtil;
30 import org.openecomp.sdc.translator.services.heattotosca.Constants;
31 import org.openecomp.sdc.translator.utils.ResourceWalker;
32
33 public class GlobalTypesServiceTemplates {
34
35     private static final String ONAP_FILEPATH_REGEX = ".*" + Constants.GLOBAL_TYPES + "(/onap/|\\\\onap\\\\).*";
36     private static final Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> onboardingGlobalTypesServiceTemplates;
37
38     private GlobalTypesServiceTemplates() {
39     }
40
41     static {
42         Map<String, String> globalTypes;
43         try {
44             globalTypes = ResourceWalker.readResourcesFromDirectory(Constants.GLOBAL_TYPES);
45         } catch (CoreException coreException) {
46             throw coreException;
47         } catch (Exception exception) {
48             throw new CoreException((new ErrorCode.ErrorCodeBuilder())
49                                             .withMessage(Constants.FAILED_TO_GENERATE_GLOBAL_TYPES)
50                                             .withId(Constants.GLOBAL_TYPES_READ_ERROR)
51                                             .withCategory(ErrorCategory.APPLICATION).build(), exception);
52         }
53         onboardingGlobalTypesServiceTemplates = init(globalTypes);
54     }
55
56     public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates(OnboardingTypesEnum onboardingType) {
57         if (onboardingType == null) {
58             throw new CoreException((new ErrorCode.ErrorCodeBuilder())
59                                             .withMessage(Constants.FAILED_TO_GENERATE_GLOBAL_TYPES)
60                                             .withId(Constants.INVALID_ONBOARDING_TYPE)
61                                             .withCategory(ErrorCategory.APPLICATION).build());
62         }
63         return onboardingGlobalTypesServiceTemplates.get(onboardingType);
64     }
65
66     private static Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> init(Map<String, String> globalTypes) {
67         Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> onboardingGlobalTypesServiceTemplates =
68                 new EnumMap<>(OnboardingTypesEnum.class);
69         Map<String, ServiceTemplate> zipOnboardingGlobalTypes =
70                 getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.ZIP);
71         Map<String, ServiceTemplate> csarOnboardingGlobalTypes =
72                 getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.CSAR);
73         Map<String, ServiceTemplate> manualOnboardingGlobalTypes =
74                 getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.MANUAL);
75         Map<String, ServiceTemplate> defaultOnboardingGlobalTypes =
76                 getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.NONE);
77         onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.ZIP, zipOnboardingGlobalTypes);
78         onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.CSAR, csarOnboardingGlobalTypes);
79         onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.MANUAL, manualOnboardingGlobalTypes);
80         onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.NONE, defaultOnboardingGlobalTypes);
81         return onboardingGlobalTypesServiceTemplates;
82     }
83
84     private static Map<String, ServiceTemplate> getOnboardingGlobalTypes(Map<String, String> globalTypes,
85                                                                                 OnboardingTypesEnum onboardingType) {
86         Map<String, ServiceTemplate> globalTypesServiceTemplates = new HashMap<>();
87         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
88         for (Map.Entry<String, String> globalTypeContent : globalTypes.entrySet()) {
89             if (!isTypeValidCandidateForCsarPacking(globalTypeContent.getKey(), onboardingType)) {
90                 // this global types folders should not be processed to the CSAR
91                 continue;
92             }
93             ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates,
94                     toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class));
95         }
96         return globalTypesServiceTemplates;
97     }
98
99     private static boolean isTypeValidCandidateForCsarPacking(String globalTypeResourceKey,
100                                                                      OnboardingTypesEnum onboardingType) {
101         if (globalTypeResourceKey.contains(Constants.OPENECOMP_INVENTORY)) {
102             // this global types folders should not be processed to the CSAR
103             return false;
104         }
105         //Global types specific to csar onboarding should not be packed for other onboarding types
106         return !globalTypeResourceKey.matches(ONAP_FILEPATH_REGEX) || onboardingType == OnboardingTypesEnum.CSAR;
107     }
108
109 }