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.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;
29 import java.util.EnumMap;
30 import java.util.HashMap;
33 public class GlobalTypesServiceTemplates {
35 private static final Map<OnboardingTypesEnum, Map<String, ServiceTemplate>>
36 onboardingGlobalTypesServiceTemplates;
38 private GlobalTypesServiceTemplates() {
42 Map<String, String> globalTypes;
44 globalTypes = ResourceWalker.readResourcesFromDirectory("globalTypes");
45 } catch (CoreException 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(),
53 onboardingGlobalTypesServiceTemplates = init(globalTypes);
56 public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates(OnboardingTypesEnum
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());
63 return onboardingGlobalTypesServiceTemplates.get(onboardingType);
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;
87 private static Map<String, ServiceTemplate> getOnboardingGlobalTypes(Map<String, String>
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
98 ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates,
99 toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class));
101 return globalTypesServiceTemplates;
104 private static boolean isTypeValidCandidateForCsarPacking(String globalTypeResourceKey,
107 if (globalTypeResourceKey.contains("openecomp-inventory")) {
108 // this global types folders should not be processed to the CSAR
111 //Global types specific to csar onboarding should not be packed for other onboarding types
112 return !globalTypeResourceKey.contains("onap") || onboardingType == OnboardingTypesEnum.CSAR;