From 5f3a0ea3230fc09bb66a5b6fbd65868fe2a46e6c Mon Sep 17 00:00:00 2001 From: vempo Date: Mon, 23 Oct 2017 13:28:46 +0300 Subject: Fixed resources not being released, code cleanup Properly close input streams; code cleanup; fixed static analysis violations and coding convetions. Change-Id: I53dabcc9f8898a0ab25636735cb0b5f84b57c427 Issue-ID: SDC-291 Signed-off-by: vempo --- .../services/impl/HeatFileAnalyzerRowDataImpl.java | 13 ++-- .../services/utils/CandidateEntityBuilder.java | 76 +++++++++++----------- .../converter/impl/ToscaConverterImplTest.java | 21 ++---- 3 files changed, 50 insertions(+), 60 deletions(-) (limited to 'openecomp-be/lib') diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java index f7ca6463cb..dd303c7bb9 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/HeatFileAnalyzerRowDataImpl.java @@ -53,7 +53,7 @@ public class HeatFileAnalyzerRowDataImpl implements HeatFileAnalyzer { private static final String DESCRIPTION = "DESCRIPTION"; private static final String NESTED_PATTERN = "NESTED_PATTERN"; - private Map patterns; + private final Map patterns; public HeatFileAnalyzerRowDataImpl() { patterns = new HashMap<>(); @@ -72,16 +72,17 @@ public class HeatFileAnalyzerRowDataImpl implements HeatFileAnalyzer { throws IOException { AnalyzedZipHeatFiles analyzedZipHeatFiles = new AnalyzedZipHeatFiles(); - BufferedReader bfReader; for (Map.Entry fileData : files.entrySet()) { String fileName = fileData.getKey(); if (!HeatFileAnalyzer.isYamlFile(fileName)) { analyzedZipHeatFiles.addOtherNonModuleFile(fileName); continue; } + boolean foundHeatIdentifier = false; - try (InputStream is = new ByteArrayInputStream(fileData.getValue())) { - bfReader = new BufferedReader(new InputStreamReader(is)); + try (InputStream is = new ByteArrayInputStream(fileData.getValue()); + BufferedReader bfReader = new BufferedReader(new InputStreamReader(is))) { + String line; boolean isResourcesSection = false; Set nestedFilesNames = new HashSet<>(); @@ -108,11 +109,9 @@ public class HeatFileAnalyzerRowDataImpl implements HeatFileAnalyzer { } analyzedZipHeatFiles.addNestedFiles(fetchFileNamesToReturn(nestedFilesNames, foundHeatIdentifier)); - if (Objects.nonNull(bfReader)) { - bfReader.close(); - } } } + return analyzedZipHeatFiles; } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java index ca5329344b..41510ecc13 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/utils/CandidateEntityBuilder.java @@ -38,6 +38,7 @@ import org.openecomp.sdc.vendorsoftwareproduct.services.impl.HeatFileAnalyzerRow import org.openecomp.sdc.vendorsoftwareproduct.types.CandidateDataEntityTo; import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles; +import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; @@ -45,8 +46,10 @@ import java.util.Objects; import java.util.Optional; public class CandidateEntityBuilder { - private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); - private CandidateService candidateService; + + private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage(); + + private final CandidateService candidateService; public CandidateEntityBuilder(CandidateService candidateService) { this.candidateService = candidateService; @@ -57,35 +60,28 @@ public class CandidateEntityBuilder { Map> uploadErrors, String user) throws Exception { //mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId()); - InputStream zipFileManifest = contentMap.getFileContent(SdcCommon.MANIFEST_NAME); - HeatFileAnalyzer heatFileAnalyzer = new HeatFileAnalyzerRowDataImpl(); - AnalyzedZipHeatFiles analyzedZipHeatFiles = - heatFileAnalyzer.analyzeFilesNotEligibleForModulesFromFileAnalyzer(contentMap.getFiles()); - HeatStructureTree tree = getHeatStructureTree(vspDetails, contentMap, analyzedZipHeatFiles); + try (InputStream zipFileManifest = contentMap.getFileContent(SdcCommon.MANIFEST_NAME)) { + HeatFileAnalyzer heatFileAnalyzer = new HeatFileAnalyzerRowDataImpl(); + AnalyzedZipHeatFiles analyzedZipHeatFiles = + heatFileAnalyzer.analyzeFilesNotEligibleForModulesFromFileAnalyzer(contentMap.getFiles()); + HeatStructureTree tree = getHeatStructureTree(vspDetails, contentMap, analyzedZipHeatFiles); - CandidateDataEntityTo candidateDataEntityTo = - new CandidateDataEntityTo(vspDetails.getId(), user, uploadedFileData, tree, contentMap, - vspDetails.getVersion()); - candidateDataEntityTo.setErrors(uploadErrors); - OrchestrationTemplateCandidateData candidateDataEntity = - candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest, - analyzedZipHeatFiles); + CandidateDataEntityTo candidateDataEntityTo = + new CandidateDataEntityTo(vspDetails.getId(), user, uploadedFileData, tree, contentMap, + vspDetails.getVersion()); + candidateDataEntityTo.setErrors(uploadErrors); + OrchestrationTemplateCandidateData candidateDataEntity = + candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest, + analyzedZipHeatFiles); - mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId()); - return candidateDataEntity; + MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId()); + return candidateDataEntity; + } } -// public OrchestrationTemplateCandidateData buildOrchestrationTemplateFromCsar(VspDetails vspDetails, -// byte[] uploadedFileData, -// FileContentHandler contentMap, -// Map> uploadErrors, -// String user){ -// -// } - private HeatStructureTree getHeatStructureTree(VspDetails vspDetails, FileContentHandler contentMap, - AnalyzedZipHeatFiles analyzedZipHeatFiles) { + AnalyzedZipHeatFiles analyzedZipHeatFiles) throws IOException { addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles); HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap); heatTreeManager.createTree(); @@ -94,22 +90,24 @@ public class CandidateEntityBuilder { private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails, FileContentHandler fileContentHandler, - AnalyzedZipHeatFiles analyzedZipHeatFiles) { - mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId()); + AnalyzedZipHeatFiles analyzedZipHeatFiles) throws IOException { + MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP Id", vspDetails.getId()); + + try (InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME)) { - InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME); - if (Objects.isNull(manifest)) { - Optional manifestContentOptional = - candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles); - if (!manifestContentOptional.isPresent()) { - throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()); + if (Objects.isNull(manifest)) { + Optional manifestContentOptional = + candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles); + if (!manifestContentOptional.isPresent()) { + throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()); + } + ManifestContent manifestContent = manifestContentOptional.get(); + fileContentHandler.addFile( + SdcCommon.MANIFEST_NAME, + String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes()); } - ManifestContent manifestContent = manifestContentOptional.get(); - fileContentHandler.addFile( - SdcCommon.MANIFEST_NAME, - String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes()); + } finally { + MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId()); } - - mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId()); } } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java index f29ca4a427..471e3891c7 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/converter/impl/ToscaConverterImplTest.java @@ -41,22 +41,18 @@ public class ToscaConverterImplTest { private static final String VIRTUAL_LINK = "virtualLink"; private static final String UNBOUNDED = "UNBOUNDED"; - private static String inputFilesPath; - private static String outputFilesPath; - private static Map expectedOutserviceTemplates; - @Test public void testConvertMainSt() throws IOException { - inputFilesPath = "/mock/toscaConverter/convertMainSt/in"; - outputFilesPath = "/mock/toscaConverter/convertMainSt/out"; + String inputFilesPath = "/mock/toscaConverter/convertMainSt/in"; + String outputFilesPath = "/mock/toscaConverter/convertMainSt/out"; FileContentHandler fileContentHandler = createFileContentHandlerFromInput(inputFilesPath); - expectedOutserviceTemplates = new HashMap<>(); + Map expectedOutserviceTemplates = new HashMap<>(); loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(), - expectedOutserviceTemplates); + expectedOutserviceTemplates); ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler); ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName); @@ -201,18 +197,15 @@ public class ToscaConverterImplTest { private static void addServiceTemplateFiles(Map serviceTemplates, File[] files, - ToscaExtensionYamlUtil toscaExtensionYamlUtil) - throws IOException { + ToscaExtensionYamlUtil toscaExtensionYamlUtil) throws IOException { + for (File file : files) { + try (InputStream yamlFile = new FileInputStream(file)) { ServiceTemplate serviceTemplateFromYaml = toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml, toscaExtensionYamlUtil); serviceTemplates.put(file.getName(), serviceTemplateFromYaml); - try { - yamlFile.close(); - } catch (IOException ignore) { - } } } } -- cgit 1.2.3-korg