2 * Copyright © 2016-2018 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 java.util.EnumMap;
20 import java.util.HashMap;
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;
33 public class GlobalTypesServiceTemplates {
35 private static final String ONAP_FILEPATH_REGEX = ".*" + Constants.GLOBAL_TYPES + "(/onap/|\\\\onap\\\\).*";
36 private static final Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> onboardingGlobalTypesServiceTemplates;
38 private GlobalTypesServiceTemplates() {
42 Map<String, String> globalTypes;
44 globalTypes = ResourceWalker.readResourcesFromDirectory(Constants.GLOBAL_TYPES);
45 } catch (CoreException 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);
53 onboardingGlobalTypesServiceTemplates = init(globalTypes);
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());
63 return onboardingGlobalTypesServiceTemplates.get(onboardingType);
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;
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
93 ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates,
94 toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class));
96 return globalTypesServiceTemplates;
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
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;