diff options
author | ojasdubey <ojas.dubey@amdocs.com> | 2017-12-13 16:23:43 +0530 |
---|---|---|
committer | Vitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com> | 2017-12-27 09:37:31 +0000 |
commit | 6395962b153816a24eeada88a01449ce0c59dcbb (patch) | |
tree | 771af07152bfa586df9f9750af8cfe4e02169596 /openecomp-be/lib/openecomp-sdc-translator-lib | |
parent | 231e28d57fd0e9b4246027c852b20a5627069d36 (diff) |
Simplified the return condition
Note: Sonar violations will be handled as a separate
task
Change-Id: I862247f5c693f4d64dff4ce18f2f71e42c85355a
Issue-ID: SDC-749
Signed-off-by: ojasdubey <ojas.dubey@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib')
5 files changed, 195 insertions, 54 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/TranslationContext.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/TranslationContext.java index 675a2c5e96..d2ad6daba1 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/TranslationContext.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/TranslationContext.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2017 European Support Limited + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.translator.datatypes.heattotosca; @@ -25,6 +21,7 @@ import org.openecomp.config.api.Configuration; import org.openecomp.config.api.ConfigurationManager; import org.openecomp.core.utilities.CommonMethods; import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.utils.SdcCommon; import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; @@ -70,7 +67,8 @@ public class TranslationContext { translationMapping = config.generateMap(ConfigConstants.MAPPING_NAMESPACE, ConfigConstants.RESOURCE_MAPPING_KEY); try { - globalServiceTemplates = GlobalTypesGenerator.getGlobalTypesServiceTemplate(); + globalServiceTemplates = + GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP); } catch (Exception exc) { throw new RuntimeException("Failed to load GlobalTypes", exc); } diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java index 9d875d20f4..a0a7a51c92 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java @@ -1,21 +1,17 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2017 European Support Limited + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.translator.services.heattotosca; @@ -27,6 +23,7 @@ import org.openecomp.core.translator.datatypes.TranslatorOutput; import org.openecomp.core.translator.factory.HeatToToscaTranslatorFactory; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.core.validation.util.MessageContainerUtil; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.togglz.ToggleableFeature; @@ -1457,7 +1454,8 @@ public class HeatToToscaUtil { mdcDataDebugMessage.debugEntryMessage(null, null); List<ServiceTemplate> serviceTemplates = new ArrayList<>(); - serviceTemplates.addAll(GlobalTypesGenerator.getGlobalTypesServiceTemplate().values()); + serviceTemplates.addAll(GlobalTypesGenerator + .getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP).values()); serviceTemplates.addAll(translationContext.getTranslatedServiceTemplates().values()); Map<String, ServiceTemplate> serviceTemplatesMap = new HashMap<>(); diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesGenerator.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesGenerator.java index 9eadfc2907..ecc0a5f06c 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesGenerator.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesGenerator.java @@ -1,25 +1,22 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2017 European Support Limited + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * ============LICENSE_END========================================================= */ package org.openecomp.sdc.translator.services.heattotosca.globaltypes; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.tosca.datatypes.model.Import; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; @@ -40,8 +37,9 @@ public class GlobalTypesGenerator { * * @return the global types service template */ - public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplate() { - return GlobalTypesServiceTemplates.getGlobalTypesServiceTemplates(); + public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplate(OnboardingTypesEnum + onboardingType) { + return GlobalTypesServiceTemplates.getGlobalTypesServiceTemplates(onboardingType); } /** @@ -53,7 +51,7 @@ public class GlobalTypesGenerator { List<Map<String, Import>> globalImports = new ArrayList<>(); Map<String, Import> globalImportMap = new HashMap<>(); Map<String, ServiceTemplate> globalTypesServiceTemplate = - GlobalTypesGenerator.getGlobalTypesServiceTemplate(); + GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP); globalImportMap.put("openecomp_heat_index", HeatToToscaUtil.createServiceTemplateImport(globalTypesServiceTemplate.get ("openecomp-heat/_index.yml"))); @@ -70,7 +68,7 @@ public class GlobalTypesGenerator { List<Map<String, Import>> globalImports = new ArrayList<>(); Map<String, Import> globalImportMap = new HashMap<>(); Map<String, ServiceTemplate> globalTypesServiceTemplate = - GlobalTypesGenerator.getGlobalTypesServiceTemplate(); + GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.MANUAL); globalImportMap.put("openecomp_index", HeatToToscaUtil.createServiceTemplateImport(globalTypesServiceTemplate.get ("openecomp/_index.yml"))); diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplates.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplates.java index dff208f101..5490452155 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplates.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplates.java @@ -1,5 +1,22 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.openecomp.sdc.translator.services.heattotosca.globaltypes; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; @@ -9,31 +26,20 @@ import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil; import org.openecomp.sdc.tosca.services.ToscaUtil; import org.openecomp.sdc.translator.utils.ResourceWalker; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; -/** - * @author Avrahamg - * @since April 03, 2017 - */ public class GlobalTypesServiceTemplates { - private static Map<String, ServiceTemplate> globalTypesServiceTemplates; + private static final Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> + onboardingGlobalTypesServiceTemplates; - public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates() { - if (globalTypesServiceTemplates == null) { - synchronized (GlobalTypesServiceTemplates.class) { - if (globalTypesServiceTemplates == null) { - init(); - } - } - } - return globalTypesServiceTemplates; + private GlobalTypesServiceTemplates() { } - private static void init() { - globalTypesServiceTemplates = new HashMap<>(); - Map<String, String> globalTypes = null; + static { + Map<String, String> globalTypes; try { globalTypes = ResourceWalker.readResourcesFromDirectory("globalTypes"); } catch (CoreException coreException) { @@ -44,18 +50,66 @@ public class GlobalTypesServiceTemplates { .withId("GlobalTypes Read Error").withCategory(ErrorCategory.APPLICATION).build(), exception); } + onboardingGlobalTypesServiceTemplates = init(globalTypes); + } + + public static Map<String, ServiceTemplate> getGlobalTypesServiceTemplates(OnboardingTypesEnum + onboardingType) { + if (onboardingType == null) { + throw new CoreException((new ErrorCode.ErrorCodeBuilder()) + .withMessage(LoggerErrorDescription.FAILED_TO_GENERATE_GLOBAL_TYPES) + .withId("Invalid Onboarding Type").withCategory(ErrorCategory.APPLICATION).build()); + } + return onboardingGlobalTypesServiceTemplates.get(onboardingType); + } + + private static Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> + init(Map<String, String> globalTypes) { + Map<OnboardingTypesEnum, Map<String, ServiceTemplate>> + onboardingGlobalTypesServiceTemplates = new EnumMap<>(OnboardingTypesEnum.class); + Map<String, ServiceTemplate> zipOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.ZIP); + Map<String, ServiceTemplate> csarOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.CSAR); + Map<String, ServiceTemplate> manualOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.MANUAL); + Map<String, ServiceTemplate> defaultOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.NONE); + onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.ZIP, zipOnboardingGlobalTypes); + onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.CSAR, csarOnboardingGlobalTypes); + onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.MANUAL, + manualOnboardingGlobalTypes); + onboardingGlobalTypesServiceTemplates.put(OnboardingTypesEnum.NONE, + defaultOnboardingGlobalTypes); + return onboardingGlobalTypesServiceTemplates; + } + + private static Map<String, ServiceTemplate> getOnboardingGlobalTypes(Map<String, String> + globalTypes, + OnboardingTypesEnum + onboardingType) { + Map<String, ServiceTemplate> globalTypesServiceTemplates = new HashMap<>(); ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); for (Map.Entry<String, String> globalTypeContent : globalTypes.entrySet()) { - if (globalTypeContent.getKey().contains("openecomp-inventory")) { // this global types folder - // should not be - // processed to the CSAR + if (!isTypeValidCandidateForCsarPacking(globalTypeContent.getKey(), onboardingType)) { + // this global types folders should not be processed to the CSAR continue; } ToscaUtil.addServiceTemplateToMapWithKeyFileName(globalTypesServiceTemplates, toscaExtensionYamlUtil.yamlToObject(globalTypeContent.getValue(), ServiceTemplate.class)); } + return globalTypesServiceTemplates; } - private GlobalTypesServiceTemplates() { + private static boolean isTypeValidCandidateForCsarPacking(String globalTypeResourceKey, + OnboardingTypesEnum + onboardingType) { + if (globalTypeResourceKey.contains("openecomp-inventory")) { + // this global types folders should not be processed to the CSAR + return false; + } + //Global types specific to csar onboarding should not be packed for other onboarding types + return !globalTypeResourceKey.contains("onap") || onboardingType == OnboardingTypesEnum.CSAR; } + } diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplatesTest.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplatesTest.java new file mode 100644 index 0000000000..d97128eea3 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplatesTest.java @@ -0,0 +1,93 @@ +/*
+ * Copyright © 2016-2017 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openecomp.sdc.translator.services.heattotosca.globaltypes;
+
+import static org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesServiceTemplates.getGlobalTypesServiceTemplates;
+
+import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
+import org.openecomp.sdc.common.errors.CoreException;
+import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class GlobalTypesServiceTemplatesTest {
+
+ @Test(expectedExceptions = CoreException.class,
+ expectedExceptionsMessageRegExp = "Failed to generate globalTypes")
+ public void testGetGlobalTypesServiceTemplatesOnboardingMethodNull() {
+ Map<String, ServiceTemplate> globalTypesServiceTemplates = getGlobalTypesServiceTemplates(null);
+ }
+
+ @Test
+ public void testGetGlobalTypesServiceTemplatesOnboardingMethodToscaCsar() {
+ Map<String, ServiceTemplate> globalTypesServiceTemplates =
+ getGlobalTypesServiceTemplates(OnboardingTypesEnum.CSAR);
+ Assert.assertNotNull(globalTypesServiceTemplates);
+ Assert.assertEquals(globalTypesServiceTemplates.size(), 24);
+ Set<String> globalTypeFolders = globalTypesServiceTemplates.keySet();
+ List<String> onapGlobalTypes = globalTypeFolders.stream()
+ .filter(resourceKey -> resourceKey.contains("onap"))
+ .collect(Collectors.toList());
+ Assert.assertNotNull(onapGlobalTypes);
+ Assert.assertEquals(onapGlobalTypes.size(), 5);
+ }
+
+ @Test
+ public void testGetGlobalTypesServiceTemplatesOnboardingMethodHeat() {
+ Map<String, ServiceTemplate> globalTypesServiceTemplates =
+ getGlobalTypesServiceTemplates(OnboardingTypesEnum.ZIP);
+ Assert.assertNotNull(globalTypesServiceTemplates);
+ Assert.assertEquals(globalTypesServiceTemplates.size(), 19);
+ Set<String> globalTypeFolders = globalTypesServiceTemplates.keySet();
+ List<String> onapGlobalTypes = globalTypeFolders.stream()
+ .filter(resourceKey -> resourceKey.contains("onap"))
+ .collect(Collectors.toList());
+ Assert.assertEquals(onapGlobalTypes.size(), 0);
+ }
+
+ @Test
+ public void testGetGlobalTypesServiceTemplatesOnboardingMethodManual() {
+ Map<String, ServiceTemplate> globalTypesServiceTemplates =
+ getGlobalTypesServiceTemplates(OnboardingTypesEnum.MANUAL);
+ Assert.assertNotNull(globalTypesServiceTemplates);
+ Assert.assertEquals(globalTypesServiceTemplates.size(), 19);
+ Set<String> globalTypeFolders = globalTypesServiceTemplates.keySet();
+ List<String> onapGlobalTypes = globalTypeFolders.stream()
+ .filter(resourceKey -> resourceKey.contains("onap"))
+ .collect(Collectors.toList());
+ Assert.assertEquals(onapGlobalTypes.size(), 0);
+ }
+
+ @Test
+ public void testGetGlobalTypesServiceTemplatesOnboardingMethodNone() {
+ Map<String, ServiceTemplate> globalTypesServiceTemplates =
+ getGlobalTypesServiceTemplates(OnboardingTypesEnum.NONE);
+ Assert.assertNotNull(globalTypesServiceTemplates);
+ Assert.assertEquals(globalTypesServiceTemplates.size(), 19);
+ Set<String> globalTypeFolders = globalTypesServiceTemplates.keySet();
+ List<String> onapGlobalTypes = globalTypeFolders.stream()
+ .filter(resourceKey -> resourceKey.contains("onap"))
+ .collect(Collectors.toList());
+ Assert.assertEquals(onapGlobalTypes.size(), 0);
+ }
+
+}
\ No newline at end of file |