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