From cc41ed25adcd0d5a4714ac92eee387c41eacbd3e Mon Sep 17 00:00:00 2001 From: Stanislav Marszalek Date: Mon, 12 Jul 2021 15:25:20 +0200 Subject: O1 PM Bulk support - FTP upload or file copy implementation Issue-ID: INT-1945 Signed-off-by: Stanislav Marszalek Change-Id: If08908035719798d8d7b129ddcdb6ef83f1647fe --- .../service/fileready/FtpServerService.java | 39 ++++++++++++++++++++-- src/main/resources/application.properties | 2 ++ src/test/resources/application.properties | 2 ++ 3 files changed, 41 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java b/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java index 3d751f5..d71cdf0 100644 --- a/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java +++ b/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java @@ -10,12 +10,14 @@ import java.io.IOException; import java.nio.file.Files; import java.util.zip.GZIPOutputStream; + import org.onap.a1pesimulator.data.fileready.FileData; import org.onap.a1pesimulator.exception.NotUploadedToFtpException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.util.FileCopyUtils; import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.sftp.SFTPClient; @@ -27,6 +29,14 @@ public class FtpServerService { private static final Logger log = LoggerFactory.getLogger(FtpServerService.class); + //true - file will be uploaded to FTP; false - file will be copied into xmlPmLocation + @Value("${ftp.server.upload}") + private boolean ftpServerUpload; + + // location where archived file will be copied + @Value("${xml.pm.location}") + private String xmlPmLocation; + @Value("${ftp.server.url}") private String ftpServerUrl; @@ -48,7 +58,7 @@ public class FtpServerService { public Mono uploadFileToFtp(FileData fileData) { return Mono.just(fileData) .flatMap(this::tryToCompressFile) - .flatMap(this::tryToUploadFileToFtp) + .flatMap(this::tryToUploadOrSaveFileToFtp) .onErrorResume(throwable -> resumeError(throwable, fileData)) .doOnNext(file -> deletePMBulkFile(file.getPmBulkFile())); } @@ -73,11 +83,32 @@ public class FtpServerService { log.trace("Compressing file {}", fileData.getPmBulkFile().getName()); return Mono.just(fileData); } catch (IOException e) { - log.error("Could not compress file ", e); + log.error("Could not compress file", e); return Mono.empty(); } } + /** + * Upload file to FTP or copy it to mounted location + * + * @param fileData data about file + * @return fileData for fileReadyEvent + */ + private Mono tryToUploadOrSaveFileToFtp(FileData fileData) { + if (ftpServerUpload) { + return tryToUploadFileToFtp(fileData); + } else { + File fileOnFtp = new File(xmlPmLocation, fileData.getArchivedPmBulkFile().getName()); + try { + FileCopyUtils.copy(fileData.getArchivedPmBulkFile(), fileOnFtp); + log.info("Uploading file to the location: {}", fileOnFtp); + return Mono.just(fileData); + } catch (IOException e) { + return Mono.error(new NotUploadedToFtpException("File was not copied to FTP location", e)); + } + } + } + /** * Upload file to FTP * @@ -155,6 +186,10 @@ public class FtpServerService { /** * Try to clean up things after an exception + * + * @param throwable error thrown + * @param fileData data about files which needs to be deleted + * @return empty Mono object */ private Mono resumeError(Throwable throwable, FileData fileData) { log.error("Error occurs while uploading file to FTP server", throwable); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1cb06ec..fd95743 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,6 +22,7 @@ ves.failing.throughput=1 ves.failing.latency=500 # in sec ves.failing.checkout.delay=15 +ftp.server.upload=false ftp.server.protocol=sftp ftp.server.url=localhost ftp.server.port=22222 @@ -39,6 +40,7 @@ logging.config=classpath:logback-spring.xml refresher.fixed.rate.ms=60000 restapi.version=v1 # PM Bulk File constants +xml.pm.location=/a1pesim/generated_pm_bulks xml.pm.bulk.fileFormatVersion=32.435 V7.0 xml.pm.bulk.vendorName=Samsung xml.pm.bulk.userLabel=ORAN PE Sim diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 27d2fda..77c2933 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -33,11 +33,13 @@ topology.ue.config.file=src/test/resources/ue.json refresher.fixed.rate.ms=60000 restapi.version=v1 # PM Bulk File constants +xml.pm.location=/a1pesim/generated_pm_bulks xml.pm.bulk.fileFormatVersion=32.435 V7.0 xml.pm.bulk.vendorName=Samsung xml.pm.bulk.userLabel=ORAN PE Sim xml.pm.bulk.fileSender=ORAN #File Ready Event constants +ftp.server.upload=false file.ready.version=4.0.1 file.ready.vesEventListenerVersion=7.0.1 file.ready.domain=notification -- cgit 1.2.3-korg