From da8b24151295d923cc632dfa2d296b3c67f6cce2 Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Mon, 8 Jul 2019 13:07:18 +0530 Subject: Move import handling to service template Moving the handling of all import formats parsing to ServiceTemplate class to make it generic and reusable Change-Id: I80595c3654a625275549a3bb3afe8ba7c9455aff Issue-ID: SDC-2421 Signed-off-by: ojasdubey --- .../core/impl/AbstractToscaConverter.java | 12 ++-- .../impl/ToscaConvertorDefinitionsTest.java | 4 +- .../services/impl/ToscaAnalyzerServiceImpl.java | 69 +--------------------- .../impl/ToscaAnalyzerServiceImplTest.java | 53 +---------------- .../mock/analyzerService/importConvertTest.yml | 14 ----- .../analyzerService/importConvertTestNoImport.yml | 4 -- .../mock/analyzerService/invalidToscaImport.yml | 5 -- 7 files changed, 11 insertions(+), 150 deletions(-) delete mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTest.yml delete mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml delete mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml (limited to 'openecomp-be') diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java index 859d3da9dc..0b0c6e42f9 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java @@ -46,6 +46,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -180,12 +181,11 @@ public abstract class AbstractToscaConverter implements ToscaConverter { } private void convertImports(ServiceTemplate serviceTemplate) { - serviceTemplate.setImports(new ArrayList<>()); - serviceTemplate.getImports() - .add(createImportMap(openecompHeatIndex, HEAT_INDEX_IMPORT_FILE)); - serviceTemplate.getImports().add(createImportMap(ONAP_INDEX, ONAP_INDEX_IMPORT_FILE)); - serviceTemplate.getImports().add(createImportMap(globalSubstitution, globalStName)); - + List> imports = new ArrayList<>(); + imports.add(createImportMap(openecompHeatIndex, HEAT_INDEX_IMPORT_FILE)); + imports.add(createImportMap(ONAP_INDEX, ONAP_INDEX_IMPORT_FILE)); + imports.add(createImportMap(globalSubstitution, globalStName)); + serviceTemplate.setImports(imports); } private Map createImportMap(String key, String fileName) { diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConvertorDefinitionsTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConvertorDefinitionsTest.java index d5a99106bc..2c3ff0ae5d 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConvertorDefinitionsTest.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConvertorDefinitionsTest.java @@ -44,8 +44,8 @@ public class ToscaConvertorDefinitionsTest { assertNotNull(serviceTemplate.getImports()); assertFalse(serviceTemplate.getImports().isEmpty()); assertEquals(1 ,serviceTemplate.getImports().size()); - assertTrue(serviceTemplate.getImports().get(0).containsKey(HEAT_INDEX)); - assertTrue(serviceTemplate.getImports().get(0).containsKey(ONAP_INDEX)); + assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(HEAT_INDEX)); + assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(ONAP_INDEX)); assertEquals(DEFININTION_VERSION, serviceTemplate.getTosca_definitions_version()); diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java index 053253a993..14cf90a272 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java @@ -169,11 +169,10 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { } void loadToscaYamlFile(ToscaServiceModel toscaServiceModel, ToscaExtensionYamlUtil toscaExtensionYamlUtil, - byte[] fileContent, String fileFullName) { + byte[] fileContent, String fileFullName) { try { - String serviceTemplateYamlString = convertServiceTemplateImport(toscaExtensionYamlUtil, fileContent); ServiceTemplate serviceTemplate = - toscaExtensionYamlUtil.yamlToObject(serviceTemplateYamlString, ServiceTemplate.class); + toscaExtensionYamlUtil.yamlToObject(new String(fileContent), ServiceTemplate.class); toscaServiceModel.addServiceTemplate(fileFullName, serviceTemplate); } catch (Exception exc) { @@ -181,70 +180,6 @@ public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { } } - String convertServiceTemplateImport(ToscaExtensionYamlUtil toscaExtensionYamlUtil, byte[] fileContent) { - Map serviceTemplateMap = toscaExtensionYamlUtil.yamlToObject(new String(fileContent), Map.class); - convertToscaImports(serviceTemplateMap, toscaExtensionYamlUtil); - return toscaExtensionYamlUtil.objectToYaml(serviceTemplateMap); - } - - private void convertToscaImports(Map serviceTemplateMap, ToscaExtensionYamlUtil toscaExtensionYamlUtil) { - List> convertedImport = new ArrayList<>(); - Object importObj = serviceTemplateMap.get(IMPORTS); - if (Objects.nonNull(importObj) && !(importObj instanceof List)) { - throw new SdcRuntimeException(new IllegalStateException("Invalid TOSCA import section")); - } - List imports = (List) importObj; - if (CollectionUtils.isEmpty(imports)) { - return; - } - for (Object importEntry : imports) { - convertToscaImportEntry(convertedImport, importEntry, toscaExtensionYamlUtil); - } - serviceTemplateMap.remove(IMPORTS); - serviceTemplateMap.put(IMPORTS, convertedImport); - } - - private void convertToscaImportEntry(List> convertedImport, Object importEntry, - ToscaExtensionYamlUtil toscaExtensionYamlUtil) { - if (importEntry instanceof String) { - convertImportShortNotation(convertedImport, importEntry.toString()); - } else if (importEntry instanceof Map) { - if (((Map) importEntry).containsKey("file")) { - Import importObject = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importEntry), - Import.class); - convertImportExtendNotation(convertedImport, importObject); - } else { - convertedImport.add((Map) importEntry); - } - } - } - - private void convertImportExtendNotation(List> convertedImport, Import importEntry) { - Map importMap = new HashMap(); - importMap.put(FileUtils.getFileWithoutExtention(getFileName(importEntry.getFile()).replaceAll("/", "_")), - importEntry); - convertedImport.add(importMap); - } - - private void convertImportShortNotation(List> convertImport, String fileFullName) { - Import importObject = new Import(); - importObject.setFile(fileFullName); - Map importMap = new HashMap(); - importMap - .put((FileUtils.getFileWithoutExtention(getFileName(fileFullName)).replaceAll("/", "_")), importObject); - convertImport.add(importMap); - } - - private static String getFileName(String relativeFileName) { - if (relativeFileName.contains("../")) { - return relativeFileName.replace("../", ""); - } else { - return relativeFileName; - } - - } - private static boolean isFile(String currentEntryName) { return !(currentEntryName.endsWith("\\") || currentEntryName.endsWith("/")); } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java index a915b9b131..f8e6759555 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImplTest.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; + import org.apache.commons.io.IOUtils; import org.hamcrest.core.StringContains; import org.junit.Assert; @@ -62,7 +63,6 @@ import org.onap.sdc.tosca.datatypes.model.Status; import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping; import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; -import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.SdcRuntimeException; import org.openecomp.sdc.tosca.TestUtil; @@ -1176,57 +1176,6 @@ public class ToscaAnalyzerServiceImplTest { assertEquals("Definitions/types/global/ImportedServiceTemplate", fileNameForImport); } - @Test - public void testConvertToscaImport() throws Exception { - String inputResourceName = "/mock/analyzerService/importConvertTest.yml"; - byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName)); - - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl(); - String convertServiceTemplateImport = - toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData); - - Assert.assertNotNull(convertServiceTemplateImport); - ServiceTemplate serviceTemplate = - new YamlUtil().yamlToObject(convertServiceTemplateImport, ServiceTemplate.class); - Assert.assertNotNull(serviceTemplate.getImports().get(0).get("data")); - Assert.assertNotNull(serviceTemplate.getImports().get(1).get("artifacts")); - Assert.assertNotNull(serviceTemplate.getImports().get(2).get("capabilities")); - Assert.assertNotNull(serviceTemplate.getImports().get(3).get("api_interfaces")); - Assert.assertNotNull(serviceTemplate.getImports().get(4).get("api_util_relationships")); - Assert.assertNotNull(serviceTemplate.getImports().get(5).get("common")); - Assert.assertNotNull(serviceTemplate.getImports().get(6).get("api_util")); - Assert.assertNotNull(serviceTemplate.getImports().get(7).get("relationshipsExt")); - } - - @Test - public void testConvertToscaImportForEmptyImport() throws Exception { - String inputResourceName = "/mock/analyzerService/importConvertTestNoImport.yml"; - byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName)); - - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl(); - String convertServiceTemplateImport = - toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData); - - Assert.assertNotNull(convertServiceTemplateImport); - ServiceTemplate serviceTemplate = - new YamlUtil().yamlToObject(convertServiceTemplateImport, ServiceTemplate.class); - Assert.assertNull(serviceTemplate.getImports()); - } - - @Test - public void testInvalidToscaImportSection() throws Exception { - thrown.expect(SdcRuntimeException.class); - thrown.expectMessage("Invalid TOSCA import section"); - String inputResourceName = "/mock/analyzerService/invalidToscaImport.yml"; - byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName)); - - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl(); - toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData); - } - @Test public void loadValidToscaYamlFileTest() throws Exception { String inputResourceName = "/mock/analyzerService/ServiceTemplateInterfaceInheritanceTest.yaml"; diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTest.yml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTest.yml deleted file mode 100644 index 08ddc3c206..0000000000 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTest.yml +++ /dev/null @@ -1,14 +0,0 @@ -tosca_definitions_version: tosca_simple_yaml_1_1 -imports: -- data.yml -- artifacts.yml -- capabilities.yml -- ../../api/interfaces.yml -- api/util/relationships.yml -- common: - file: api/common.yaml -- file: api/util.yaml -- relationshipsExt.yml -node_types: - tosca.nodes.Root: - description: The TOSCA Node Type all other TOSCA base Node Types derive from \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml deleted file mode 100644 index 536e1367e5..0000000000 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/importConvertTestNoImport.yml +++ /dev/null @@ -1,4 +0,0 @@ -tosca_definitions_version: tosca_simple_yaml_1_1 -node_types: - tosca.nodes.Root: - description: The TOSCA Node Type all other TOSCA base Node Types derive from \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml deleted file mode 100644 index 8a5cfaa9ac..0000000000 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/analyzerService/invalidToscaImport.yml +++ /dev/null @@ -1,5 +0,0 @@ -tosca_definitions_version: tosca_simple_yaml_1_1 -imports: abc -node_types: - tosca.nodes.Root: - description: The TOSCA Node Type all other TOSCA base Node Types derive from \ No newline at end of file -- cgit 1.2.3-korg