From 4ffe10537b208ee7ec6317e3592c398730a3ee8e Mon Sep 17 00:00:00 2001 From: Stanislav Marszalek Date: Fri, 9 Jul 2021 13:41:45 +0200 Subject: O1 PM Bulk support - upload PM Bulk File to FTP Issue-ID: INT-1945 Signed-off-by: Stanislav Marszalek Change-Id: If08908035719798d8d7b129ddcdb6ef62f1647fe --- .../exception/NotUploadedToFtpException.java | 28 +++++++++ .../service/fileready/FtpServerService.java | 72 ++++++++++++++++++++-- 2 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/onap/a1pesimulator/exception/NotUploadedToFtpException.java (limited to 'src/main') diff --git a/src/main/java/org/onap/a1pesimulator/exception/NotUploadedToFtpException.java b/src/main/java/org/onap/a1pesimulator/exception/NotUploadedToFtpException.java new file mode 100644 index 0000000..55f1d5d --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/exception/NotUploadedToFtpException.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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.onap.a1pesimulator.exception; + +/** + * Thrown from FTPServerService in upload process + */ +public class NotUploadedToFtpException extends Exception { + + public NotUploadedToFtpException(String errorMessage) { + super(errorMessage); + } + + public NotUploadedToFtpException(String errorMessage, Exception e) { + super(errorMessage, e); + } +} 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 56735fd..3d751f5 100644 --- a/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java +++ b/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java @@ -1,20 +1,28 @@ package org.onap.a1pesimulator.service.fileready; +import static java.util.Objects.nonNull; +import static org.onap.a1pesimulator.util.Constants.TEMP_DIR; + import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; 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.context.annotation.PropertySource; import org.springframework.stereotype.Service; +import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.sftp.SFTPClient; +import net.schmizz.sshj.transport.verification.PromiscuousVerifier; import reactor.core.publisher.Mono; @Service -@PropertySource("classpath:application.properties") public class FtpServerService { private static final Logger log = LoggerFactory.getLogger(FtpServerService.class); @@ -52,8 +60,22 @@ public class FtpServerService { * @return archived file */ private Mono tryToCompressFile(FileData fileData) { - fileData.setArchivedPmBulkFile(fileData.getPmBulkFile()); - return Mono.just(fileData); + File archiveBulkFile = new File(TEMP_DIR, fileData.getPmBulkFile().getName() + ".gz"); + + try (GZIPOutputStream zos = new GZIPOutputStream( + new FileOutputStream(archiveBulkFile.getAbsolutePath())); FileInputStream inputStream = new FileInputStream(fileData.getPmBulkFile())) { + byte[] buffer = new byte[1024]; + int len; + while ((len = inputStream.read(buffer)) > 0) { + zos.write(buffer, 0, len); + } + fileData.setArchivedPmBulkFile(archiveBulkFile); + log.trace("Compressing file {}", fileData.getPmBulkFile().getName()); + return Mono.just(fileData); + } catch (IOException e) { + log.error("Could not compress file ", e); + return Mono.empty(); + } } /** @@ -63,9 +85,49 @@ public class FtpServerService { * @return archived file for fileReadyEvent */ private Mono tryToUploadFileToFtp(FileData fileData) { - return Mono.just(fileData); + if (nonNull(fileData.getArchivedPmBulkFile())) { + SSHClient client = getSSHClient(); + if (nonNull(client)) { + try (client; SFTPClient sftpClient = client.newSFTPClient()) { + File archiveBulkFile = fileData.getArchivedPmBulkFile(); + sftpClient.put(archiveBulkFile.getAbsolutePath(), ftpServerFilepath + "/" + archiveBulkFile.getName()); + + log.info("Uploading file to FTP: {}", archiveBulkFile.getAbsoluteFile()); + return Mono.just(fileData); + } catch (IOException e) { + log.error("Exception while trying to upload a file", e); + } + } else { + log.error("Could not connect to FTP server"); + } + } else { + log.error("There is no file to upload"); + } + return Mono.error(new NotUploadedToFtpException("File was not uploaded to FTP")); } + /** + * Creates SSHClient instance + * + * @return SSHClient + */ + private SSHClient getSSHClient() { + SSHClient client = new SSHClient(); + try { + client.addHostKeyVerifier(new PromiscuousVerifier()); + client.connect(ftpServerUrl, Integer.parseInt(ftpServerPort)); + client.authPassword(ftpServerUsername, ftpServerPassword); + return client; + } catch (IOException e) { + log.error("There was an error while connecting to FTP server", e); + try { + client.close(); + } catch (IOException ioException) { + log.error("There was an error while closing the connection to FTP server", e); + } + return null; + } + } /** * Deletes created PM Bulk File xml from temp storage after successful upload to FTP -- cgit 1.2.3-korg