2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.translator.services.heattotosca.globaltypes;
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;
28 import java.util.EnumMap;
29 import java.util.HashMap;
32 public class GlobalTypesServiceTemplates {
34 private static final String FAILED_TO_GENERATE_GLOBAL_TYPES = "Failed to generate globalTypes";
36 private static final Map<OnboardingTypesEnum, Map<String, ServiceTemplate>>
37 onboardingGlobalTypesServiceTemplates;
39 private GlobalTypesServiceTemplates() {
43 Map<String, String> globalTypes;
45 globalTypes = ResourceWalker.readResourcesFromDirectory("globalTypes");
46 } catch (CoreException 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(),
54 onboardingGlobalTypesServiceTemplates = init(globalTypes);
57 public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates(OnboardingTypesEnum
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());
64 return onboardingGlobalTypesServiceTemplates.get(onboardingType);
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;
88 private static Map<String, ServiceTemplate> getOnboardingGlobalTypes(Map<String, String>
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
99 ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates,
100 toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class));
102 return globalTypesServiceTemplates;
105 private static boolean isTypeValidCandidateForCsarPacking(String globalTypeResourceKey,
108 if (globalTypeResourceKey.contains("openecomp-inventory")) {
109 // this global types folders should not be processed to the CSAR
112 //Global types specific to csar onboarding should not be packed for other onboarding types
113 return !globalTypeResourceKey.contains("onap") || onboardingType == OnboardingTypesEnum.CSAR;