From 6395962b153816a24eeada88a01449ce0c59dcbb Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Wed, 13 Dec 2017 16:23:43 +0530 Subject: 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 --- .../datatypes/heattotosca/TranslationContext.java | 18 ++-- .../services/heattotosca/HeatToToscaUtil.java | 18 ++-- .../globaltypes/GlobalTypesGenerator.java | 24 +++--- .../globaltypes/GlobalTypesServiceTemplates.java | 96 +++++++++++++++++----- .../GlobalTypesServiceTemplatesTest.java | 93 +++++++++++++++++++++ 5 files changed, 195 insertions(+), 54 deletions(-) create mode 100644 openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/globaltypes/GlobalTypesServiceTemplatesTest.java (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core') 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 serviceTemplates = new ArrayList<>(); - serviceTemplates.addAll(GlobalTypesGenerator.getGlobalTypesServiceTemplate().values()); + serviceTemplates.addAll(GlobalTypesGenerator + .getGlobalTypesServiceTemplate(OnboardingTypesEnum.ZIP).values()); serviceTemplates.addAll(translationContext.getTranslatedServiceTemplates().values()); Map 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 getGlobalTypesServiceTemplate() { - return GlobalTypesServiceTemplates.getGlobalTypesServiceTemplates(); + public static Map getGlobalTypesServiceTemplate(OnboardingTypesEnum + onboardingType) { + return GlobalTypesServiceTemplates.getGlobalTypesServiceTemplates(onboardingType); } /** @@ -53,7 +51,7 @@ public class GlobalTypesGenerator { List> globalImports = new ArrayList<>(); Map globalImportMap = new HashMap<>(); Map 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> globalImports = new ArrayList<>(); Map globalImportMap = new HashMap<>(); Map 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 globalTypesServiceTemplates; + private static final Map> + onboardingGlobalTypesServiceTemplates; - public static Map getGlobalTypesServiceTemplates() { - if (globalTypesServiceTemplates == null) { - synchronized (GlobalTypesServiceTemplates.class) { - if (globalTypesServiceTemplates == null) { - init(); - } - } - } - return globalTypesServiceTemplates; + private GlobalTypesServiceTemplates() { } - private static void init() { - globalTypesServiceTemplates = new HashMap<>(); - Map globalTypes = null; + static { + Map 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 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> + init(Map globalTypes) { + Map> + onboardingGlobalTypesServiceTemplates = new EnumMap<>(OnboardingTypesEnum.class); + Map zipOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.ZIP); + Map csarOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.CSAR); + Map manualOnboardingGlobalTypes = + getOnboardingGlobalTypes(globalTypes, OnboardingTypesEnum.MANUAL); + Map 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 getOnboardingGlobalTypes(Map + globalTypes, + OnboardingTypesEnum + onboardingType) { + Map globalTypesServiceTemplates = new HashMap<>(); ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); for (Map.Entry 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 globalTypesServiceTemplates = getGlobalTypesServiceTemplates(null); + } + + @Test + public void testGetGlobalTypesServiceTemplatesOnboardingMethodToscaCsar() { + Map globalTypesServiceTemplates = + getGlobalTypesServiceTemplates(OnboardingTypesEnum.CSAR); + Assert.assertNotNull(globalTypesServiceTemplates); + Assert.assertEquals(globalTypesServiceTemplates.size(), 24); + Set globalTypeFolders = globalTypesServiceTemplates.keySet(); + List onapGlobalTypes = globalTypeFolders.stream() + .filter(resourceKey -> resourceKey.contains("onap")) + .collect(Collectors.toList()); + Assert.assertNotNull(onapGlobalTypes); + Assert.assertEquals(onapGlobalTypes.size(), 5); + } + + @Test + public void testGetGlobalTypesServiceTemplatesOnboardingMethodHeat() { + Map globalTypesServiceTemplates = + getGlobalTypesServiceTemplates(OnboardingTypesEnum.ZIP); + Assert.assertNotNull(globalTypesServiceTemplates); + Assert.assertEquals(globalTypesServiceTemplates.size(), 19); + Set globalTypeFolders = globalTypesServiceTemplates.keySet(); + List onapGlobalTypes = globalTypeFolders.stream() + .filter(resourceKey -> resourceKey.contains("onap")) + .collect(Collectors.toList()); + Assert.assertEquals(onapGlobalTypes.size(), 0); + } + + @Test + public void testGetGlobalTypesServiceTemplatesOnboardingMethodManual() { + Map globalTypesServiceTemplates = + getGlobalTypesServiceTemplates(OnboardingTypesEnum.MANUAL); + Assert.assertNotNull(globalTypesServiceTemplates); + Assert.assertEquals(globalTypesServiceTemplates.size(), 19); + Set globalTypeFolders = globalTypesServiceTemplates.keySet(); + List onapGlobalTypes = globalTypeFolders.stream() + .filter(resourceKey -> resourceKey.contains("onap")) + .collect(Collectors.toList()); + Assert.assertEquals(onapGlobalTypes.size(), 0); + } + + @Test + public void testGetGlobalTypesServiceTemplatesOnboardingMethodNone() { + Map globalTypesServiceTemplates = + getGlobalTypesServiceTemplates(OnboardingTypesEnum.NONE); + Assert.assertNotNull(globalTypesServiceTemplates); + Assert.assertEquals(globalTypesServiceTemplates.size(), 19); + Set globalTypeFolders = globalTypesServiceTemplates.keySet(); + List onapGlobalTypes = globalTypeFolders.stream() + .filter(resourceKey -> resourceKey.contains("onap")) + .collect(Collectors.toList()); + Assert.assertEquals(onapGlobalTypes.size(), 0); + } + +} \ No newline at end of file -- cgit 1.2.3-korg