diff options
author | vempo <vitaliy.emporopulo@amdocs.com> | 2017-10-23 13:28:46 +0300 |
---|---|---|
committer | vempo <vitaliy.emporopulo@amdocs.com> | 2017-10-23 13:39:10 +0300 |
commit | 5f3a0ea3230fc09bb66a5b6fbd65868fe2a46e6c (patch) | |
tree | 6d5928c7947746eab1c38cf7d6ebbed060ca6390 /openecomp-be/lib | |
parent | b8aabbe3590c90be5c6f3e2c63b57c0e396baed6 (diff) |
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 <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib')
3 files changed, 50 insertions, 60 deletions
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<String, Pattern> patterns; + private final Map<String, Pattern> 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<String, byte[]> 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<String> 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<String, List<ErrorMessage>> 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<String, List<ErrorMessage>> 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<ManifestContent> manifestContentOptional = - candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles); - if (!manifestContentOptional.isPresent()) { - throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()); + if (Objects.isNull(manifest)) { + Optional<ManifestContent> 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<String, ServiceTemplate> 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<String, ServiceTemplate> 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<String, ServiceTemplate> 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) { - } } } } |