From 3a9d09f02a50cf1174a6e9ccf8e7d0af88a7ecf3 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Fri, 24 Mar 2023 08:53:47 +0100 Subject: [PATCH] Minor refactorinng of datafaile collector Signed-off-by: PatrikBuhr Issue-ID: NONRTRIC-853 Change-Id: I951449142e27cb4da7ba69e7335424b4c6d024ec --- datafilecollector/config/application.yaml | 2 +- .../java/org/oran/datafile/commons/Scheme.java | 69 ------ .../org/oran/datafile/commons/SecurityUtil.java | 50 ---- .../java/org/oran/datafile/ftp/FtpesClient.java | 9 +- .../java/org/oran/datafile/ftp/SftpClient.java | 2 +- .../java/org/oran/datafile/http/DfcHttpClient.java | 3 +- .../org/oran/datafile/http/DfcHttpsClient.java | 3 +- .../oran/datafile/{service => http}/HttpUtils.java | 4 +- .../http/HttpsClientConnectionManagerUtil.java | 6 +- .../java/org/oran/datafile/model/FileData.java | 42 +++- .../{commons => model}/FileServerData.java | 2 +- .../oran/datafile/oauth2/OAuthBearerTokenJwt.java | 8 +- .../oran/datafile/tasks/CollectAndReportFiles.java | 3 +- .../org/oran/datafile/tasks/FileCollector.java | 3 +- .../org/oran/datafile/ftp/FtpesClientTest.java | 30 ++- .../java/org/oran/datafile/ftp/SftpClientTest.java | 2 +- .../org/oran/datafile/http/DfcHttpClientTest.java | 3 +- .../org/oran/datafile/http/DfcHttpsClientTest.java | 2 +- .../datafile/{service => http}/HttpUtilsTest.java | 4 +- .../datafile/{scheme => model}/SchemeTest.java | 4 +- .../java/org/oran/datafile/utils/JsonMessage.java | 260 --------------------- influxlogger/config/application.yaml | 2 +- pmproducer/config/application.yaml | 2 +- 23 files changed, 81 insertions(+), 434 deletions(-) delete mode 100644 datafilecollector/src/main/java/org/oran/datafile/commons/Scheme.java delete mode 100644 datafilecollector/src/main/java/org/oran/datafile/commons/SecurityUtil.java rename datafilecollector/src/main/java/org/oran/datafile/{service => http}/HttpUtils.java (99%) rename datafilecollector/src/main/java/org/oran/datafile/{commons => model}/FileServerData.java (97%) rename datafilecollector/src/test/java/org/oran/datafile/{service => http}/HttpUtilsTest.java (98%) rename datafilecollector/src/test/java/org/oran/datafile/{scheme => model}/SchemeTest.java (95%) delete mode 100644 datafilecollector/src/test/java/org/oran/datafile/utils/JsonMessage.java diff --git a/datafilecollector/config/application.yaml b/datafilecollector/config/application.yaml index 9875abb..1b85bb9 100644 --- a/datafilecollector/config/application.yaml +++ b/datafilecollector/config/application.yaml @@ -40,7 +40,7 @@ app: client-id: datafile-1 # input topic file-ready-event-topic: file-ready - # Configues if oath2 tokens shall be used. If set to true, auth-token-file must also be configured + # Configures if oath2 tokens shall be used. If set to true, auth-token-file must also be configured use-oath-token: false ssl: key-store-type: PEM diff --git a/datafilecollector/src/main/java/org/oran/datafile/commons/Scheme.java b/datafilecollector/src/main/java/org/oran/datafile/commons/Scheme.java deleted file mode 100644 index 2dcc75d..0000000 --- a/datafilecollector/src/main/java/org/oran/datafile/commons/Scheme.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019-2023 Nordix Foundation. All rights reserved. - * Copyright (C) 2020-2021 Nokia. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.oran.datafile.commons; - -import org.oran.datafile.exceptions.DatafileTaskException; - -/** - * Enum specifying the schemes that DFC support for downloading files. - * - */ -public enum Scheme { - FTPES, SFTP, HTTP, HTTPS; - - public static final String DFC_DOES_NOT_SUPPORT_PROTOCOL_ERROR_MSG = "DFC does not support protocol "; - public static final String SUPPORTED_PROTOCOLS_ERROR_MESSAGE = - ". Supported protocols are FTPeS, sFTP, HTTP and HTTPS"; - - /** - * Get a Scheme from a string. - * - * @param schemeString the string to convert to Scheme. - * @return The corresponding Scheme - * @throws DatafileTaskException if the value of the string doesn't match any - * defined scheme. - */ - public static Scheme getSchemeFromString(String schemeString) throws DatafileTaskException { - Scheme result; - if ("FTPES".equalsIgnoreCase(schemeString)) { - result = Scheme.FTPES; - } else if ("SFTP".equalsIgnoreCase(schemeString)) { - result = Scheme.SFTP; - } else if ("HTTP".equalsIgnoreCase(schemeString)) { - result = Scheme.HTTP; - } else if ("HTTPS".equalsIgnoreCase(schemeString)) { - result = Scheme.HTTPS; - } else { - throw new DatafileTaskException( - DFC_DOES_NOT_SUPPORT_PROTOCOL_ERROR_MSG + schemeString + SUPPORTED_PROTOCOLS_ERROR_MESSAGE); - } - return result; - } - - /** - * Check if Scheme is FTP type or HTTP type. - * - * @param scheme the Scheme which has to be checked. - * @return true if Scheme is FTP type or false if it is HTTP type - */ - public static boolean isFtpScheme(Scheme scheme) { - return scheme == SFTP || scheme == FTPES; - } -} diff --git a/datafilecollector/src/main/java/org/oran/datafile/commons/SecurityUtil.java b/datafilecollector/src/main/java/org/oran/datafile/commons/SecurityUtil.java deleted file mode 100644 index 856274a..0000000 --- a/datafilecollector/src/main/java/org/oran/datafile/commons/SecurityUtil.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START====================================================================== - * Copyright (C) 2021 Nokia. All rights reserved. - * =============================================================================================== - * 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. - * ============LICENSE_END======================================================================== - */ -package org.oran.datafile.commons; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Utility class containing functions used for certificates configuration - */ -public final class SecurityUtil { - private SecurityUtil() { - } - - private static final Logger logger = LoggerFactory.getLogger(SecurityUtil.class); - - public static String getKeystorePasswordFromFile(String passwordPath) { - return getPasswordFromFile(passwordPath, "Keystore"); - } - - public static String getTruststorePasswordFromFile(String passwordPath) { - return getPasswordFromFile(passwordPath, "Truststore"); - } - - public static String getPasswordFromFile(String passwordPath, String element) { - try { - return new String(Files.readAllBytes(Paths.get(passwordPath))); - } catch (IOException e) { - logger.error("{} password file at path: {} cannot be opened ", element, passwordPath); - } - return ""; - } -} diff --git a/datafilecollector/src/main/java/org/oran/datafile/ftp/FtpesClient.java b/datafilecollector/src/main/java/org/oran/datafile/ftp/FtpesClient.java index 06313e5..8e61558 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/ftp/FtpesClient.java +++ b/datafilecollector/src/main/java/org/oran/datafile/ftp/FtpesClient.java @@ -22,7 +22,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.KeyStoreException; @@ -39,10 +41,9 @@ import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPSClient; import org.oran.datafile.commons.FileCollectClient; -import org.oran.datafile.commons.FileServerData; -import org.oran.datafile.commons.SecurityUtil; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; +import org.oran.datafile.model.FileServerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.FileSystemResource; @@ -195,7 +196,7 @@ public class FtpesClient implements FileCollectClient { throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException { synchronized (FtpesClient.class) { if (theTrustManager == null && trustedCaPath != null) { - String trustedCaPassword = SecurityUtil.getTruststorePasswordFromFile(trustedCaPasswordPath); + String trustedCaPassword = Files.readString(Paths.get((trustedCaPasswordPath))); theTrustManager = createTrustManager(trustedCaPath, trustedCaPassword); } return theTrustManager; @@ -207,7 +208,7 @@ public class FtpesClient implements FileCollectClient { synchronized (FtpesClient.class) { if (theKeyManager == null) { - String keyCertPassword = SecurityUtil.getKeystorePasswordFromFile(keyCertPasswordPath); + String keyCertPassword = Files.readString(Paths.get((keyCertPasswordPath))); theKeyManager = createKeyManager(keyCertPath, keyCertPassword); } return theKeyManager; diff --git a/datafilecollector/src/main/java/org/oran/datafile/ftp/SftpClient.java b/datafilecollector/src/main/java/org/oran/datafile/ftp/SftpClient.java index a7a74a0..5326ee7 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/ftp/SftpClient.java +++ b/datafilecollector/src/main/java/org/oran/datafile/ftp/SftpClient.java @@ -26,9 +26,9 @@ import com.jcraft.jsch.SftpException; import java.nio.file.Path; import org.oran.datafile.commons.FileCollectClient; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; +import org.oran.datafile.model.FileServerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java index 0b44dc9..7c1ae92 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java +++ b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpClient.java @@ -24,10 +24,9 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import org.oran.datafile.commons.FileCollectClient; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; -import org.oran.datafile.service.HttpUtils; +import org.oran.datafile.model.FileServerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java index fde36d2..74a1398 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java +++ b/datafilecollector/src/main/java/org/oran/datafile/http/DfcHttpsClient.java @@ -38,10 +38,9 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; import org.oran.datafile.commons.FileCollectClient; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; -import org.oran.datafile.service.HttpUtils; +import org.oran.datafile.model.FileServerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/datafilecollector/src/main/java/org/oran/datafile/service/HttpUtils.java b/datafilecollector/src/main/java/org/oran/datafile/http/HttpUtils.java similarity index 99% rename from datafilecollector/src/main/java/org/oran/datafile/service/HttpUtils.java rename to datafilecollector/src/main/java/org/oran/datafile/http/HttpUtils.java index 83dc02d..02155d3 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/service/HttpUtils.java +++ b/datafilecollector/src/main/java/org/oran/datafile/http/HttpUtils.java @@ -17,14 +17,14 @@ * ============LICENSE_END======================================================================== */ -package org.oran.datafile.service; +package org.oran.datafile.http; import java.util.Base64; import java.util.List; import org.apache.hc.core5.http.NameValuePair; import org.apache.http.HttpStatus; -import org.oran.datafile.commons.FileServerData; +import org.oran.datafile.model.FileServerData; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/datafilecollector/src/main/java/org/oran/datafile/http/HttpsClientConnectionManagerUtil.java b/datafilecollector/src/main/java/org/oran/datafile/http/HttpsClientConnectionManagerUtil.java index 768e900..61b35cf 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/http/HttpsClientConnectionManagerUtil.java +++ b/datafilecollector/src/main/java/org/oran/datafile/http/HttpsClientConnectionManagerUtil.java @@ -18,6 +18,7 @@ package org.oran.datafile.http; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.nio.file.Paths; import java.security.KeyStore; import java.security.KeyStoreException; @@ -37,7 +38,6 @@ import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.SSLContexts; -import org.oran.datafile.commons.SecurityUtil; import org.oran.datafile.exceptions.DatafileTaskException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -107,7 +107,7 @@ public class HttpsClientConnectionManagerUtil { private static SSLContextBuilder supplyKeyInfo(String keyCertPath, String keyCertPasswordPath, SSLContextBuilder sslBuilder) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException { - String keyPass = SecurityUtil.getKeystorePasswordFromFile(keyCertPasswordPath); + String keyPass = Files.readString(Paths.get((keyCertPasswordPath))); KeyStore keyFile = createKeyStore(keyCertPath, keyPass); return sslBuilder.loadKeyMaterial(keyFile, keyPass.toCharArray()); } @@ -130,7 +130,7 @@ public class HttpsClientConnectionManagerUtil { private static SSLContextBuilder supplyTrustInfo(String trustedCaPath, String trustedCaPasswordPath, SSLContextBuilder sslBuilder) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { - String trustPass = SecurityUtil.getTruststorePasswordFromFile(trustedCaPasswordPath); + String trustPass = Files.readString(Paths.get((trustedCaPasswordPath))); File trustStoreFile = new File(trustedCaPath); return sslBuilder.loadTrustMaterial(trustStoreFile, trustPass.toCharArray()); } diff --git a/datafilecollector/src/main/java/org/oran/datafile/model/FileData.java b/datafilecollector/src/main/java/org/oran/datafile/model/FileData.java index c2ef76c..4b013be 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/model/FileData.java +++ b/datafilecollector/src/main/java/org/oran/datafile/model/FileData.java @@ -33,10 +33,9 @@ import lombok.Builder; import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.net.URIBuilder; -import org.oran.datafile.commons.FileServerData; -import org.oran.datafile.commons.FileServerData.FileServerDataBuilder; -import org.oran.datafile.commons.Scheme; import org.oran.datafile.configuration.AppConfig; +import org.oran.datafile.exceptions.DatafileTaskException; +import org.oran.datafile.model.FileServerData.FileServerDataBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,6 +47,43 @@ import org.slf4j.LoggerFactory; @Builder public class FileData { + public enum Scheme { + FTPES, SFTP, HTTP, HTTPS; + + public static final String DFC_DOES_NOT_SUPPORT_PROTOCOL_ERROR_MSG = "DFC does not support protocol "; + public static final String SUPPORTED_PROTOCOLS_ERROR_MESSAGE = + ". Supported protocols are FTPeS, sFTP, HTTP and HTTPS"; + + /** + * Get a Scheme from a string. + * + * @param schemeString the string to convert to Scheme. + * @return The corresponding Scheme + * @throws DatafileTaskException if the value of the string doesn't match any + * defined scheme. + */ + public static Scheme getSchemeFromString(String schemeString) throws DatafileTaskException { + Scheme result; + if ("FTPES".equalsIgnoreCase(schemeString)) { + result = Scheme.FTPES; + } else if ("SFTP".equalsIgnoreCase(schemeString)) { + result = Scheme.SFTP; + } else if ("HTTP".equalsIgnoreCase(schemeString)) { + result = Scheme.HTTP; + } else if ("HTTPS".equalsIgnoreCase(schemeString)) { + result = Scheme.HTTPS; + } else { + throw new DatafileTaskException( + DFC_DOES_NOT_SUPPORT_PROTOCOL_ERROR_MSG + schemeString + SUPPORTED_PROTOCOLS_ERROR_MESSAGE); + } + return result; + } + + public static boolean isFtpScheme(Scheme scheme) { + return scheme == SFTP || scheme == FTPES; + } + } + private static final Logger logger = LoggerFactory.getLogger(FileData.class); public FileReadyMessage.ArrayOfNamedHashMap fileInfo; diff --git a/datafilecollector/src/main/java/org/oran/datafile/commons/FileServerData.java b/datafilecollector/src/main/java/org/oran/datafile/model/FileServerData.java similarity index 97% rename from datafilecollector/src/main/java/org/oran/datafile/commons/FileServerData.java rename to datafilecollector/src/main/java/org/oran/datafile/model/FileServerData.java index 14ddd43..0bb41a2 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/commons/FileServerData.java +++ b/datafilecollector/src/main/java/org/oran/datafile/model/FileServerData.java @@ -15,7 +15,7 @@ * ============LICENSE_END======================================================================== */ -package org.oran.datafile.commons; +package org.oran.datafile.model; import java.util.ArrayList; import java.util.List; diff --git a/datafilecollector/src/main/java/org/oran/datafile/oauth2/OAuthBearerTokenJwt.java b/datafilecollector/src/main/java/org/oran/datafile/oauth2/OAuthBearerTokenJwt.java index f6da196..4da3535 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/oauth2/OAuthBearerTokenJwt.java +++ b/datafilecollector/src/main/java/org/oran/datafile/oauth2/OAuthBearerTokenJwt.java @@ -17,9 +17,6 @@ package org.oran.datafile.oauth2; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; - import java.util.Base64; import java.util.HashSet; import java.util.Set; @@ -46,8 +43,7 @@ public class OAuthBearerTokenJwt implements OAuthBearerToken { String scope = ""; } - public static OAuthBearerTokenJwt create(String tokenRaw) - throws DatafileTaskException, JsonMappingException, JsonProcessingException { + public static OAuthBearerTokenJwt create(String tokenRaw) throws DatafileTaskException { String[] chunks = tokenRaw.split("\\."); Base64.Decoder decoder = Base64.getUrlDecoder(); if (chunks.length < 2) { @@ -56,7 +52,7 @@ public class OAuthBearerTokenJwt implements OAuthBearerToken { } String payloadStr = new String(decoder.decode(chunks[1])); JwtTokenBody token = gson.fromJson(payloadStr, JwtTokenBody.class); - logger.error("Token: {}", token); + logger.debug("Token: {}", token); return new OAuthBearerTokenJwt(token, tokenRaw); } diff --git a/datafilecollector/src/main/java/org/oran/datafile/tasks/CollectAndReportFiles.java b/datafilecollector/src/main/java/org/oran/datafile/tasks/CollectAndReportFiles.java index 22ddf6f..a78d969 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/tasks/CollectAndReportFiles.java +++ b/datafilecollector/src/main/java/org/oran/datafile/tasks/CollectAndReportFiles.java @@ -32,7 +32,6 @@ import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.header.Header; import org.apache.kafka.common.header.internals.RecordHeader; import org.apache.kafka.common.serialization.StringSerializer; -import org.oran.datafile.commons.Scheme; import org.oran.datafile.configuration.AppConfig; import org.oran.datafile.configuration.CertificateConfig; import org.oran.datafile.datastore.DataStore; @@ -258,7 +257,7 @@ public class CollectAndReportFiles { Path localFilePath = fileData.getLocalFilePath(this.appConfig); logger.error("File fetching failed, path {}, reason: {}", fileData.remoteFilePath(), t.getMessage()); deleteFile(localFilePath); - if (Scheme.isFtpScheme(fileData.scheme())) { + if (FileData.Scheme.isFtpScheme(fileData.scheme())) { counters.incNoOfFailedFtp(); } else { counters.incNoOfFailedHttp(); diff --git a/datafilecollector/src/main/java/org/oran/datafile/tasks/FileCollector.java b/datafilecollector/src/main/java/org/oran/datafile/tasks/FileCollector.java index 812e5b0..26c3df6 100644 --- a/datafilecollector/src/main/java/org/oran/datafile/tasks/FileCollector.java +++ b/datafilecollector/src/main/java/org/oran/datafile/tasks/FileCollector.java @@ -24,7 +24,6 @@ import java.time.Duration; import java.util.Optional; import org.oran.datafile.commons.FileCollectClient; -import org.oran.datafile.commons.Scheme; import org.oran.datafile.configuration.AppConfig; import org.oran.datafile.configuration.CertificateConfig; import org.oran.datafile.exceptions.DatafileTaskException; @@ -123,7 +122,7 @@ public class FileCollector { } private void incFailedAttemptsCounter(FileData fileData) { - if (Scheme.isFtpScheme(fileData.scheme())) { + if (FileData.Scheme.isFtpScheme(fileData.scheme())) { counters.incNoOfFailedFtpAttempts(); } else { counters.incNoOfFailedHttpAttempts(); diff --git a/datafilecollector/src/test/java/org/oran/datafile/ftp/FtpesClientTest.java b/datafilecollector/src/test/java/org/oran/datafile/ftp/FtpesClientTest.java index 21f9c71..ef3310a 100644 --- a/datafilecollector/src/test/java/org/oran/datafile/ftp/FtpesClientTest.java +++ b/datafilecollector/src/test/java/org/oran/datafile/ftp/FtpesClientTest.java @@ -42,7 +42,7 @@ import org.apache.commons.net.ftp.FTPSClient; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; -import org.oran.datafile.commons.FileServerData; +import org.oran.datafile.model.FileServerData; import org.springframework.http.HttpStatus; public class FtpesClientTest { @@ -52,7 +52,7 @@ public class FtpesClientTest { private static final String XNF_ADDRESS = "127.0.0.1"; private static final int PORT = 8021; private static final String FTP_KEY_PATH = "ftpKeyPath"; - private static final String FTP_KEY_PASSWORD = "ftpKeyPassword"; + private static final String FTP_KEY_PASSWORD_PATH = "ftpKeyPasswordPath"; private static final Path TRUSTED_CA_PATH = Paths.get("trustedCaPath"); private static final String TRUSTED_CA_PASSWORD = "trustedCaPassword"; @@ -77,7 +77,7 @@ public class FtpesClientTest { @BeforeEach protected void setUp() throws Exception { - clientUnderTestSpy = spy(new FtpesClient(createFileServerData(), Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD, + clientUnderTestSpy = spy(new FtpesClient(createFileServerData(), Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH, TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD)); clientUnderTestSpy.realFtpsClient = ftpsClientMock; } @@ -103,7 +103,7 @@ public class FtpesClientTest { @Test public void collectFile_allOk() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); @@ -129,8 +129,7 @@ public class FtpesClientTest { public void collectFileFaultyOwnKey_shouldFail() throws Exception { doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessageContaining("Could not open connection: java.io.FileNotFoundException:"); + assertThatThrownBy(() -> clientUnderTestSpy.open()).hasMessageContaining("Could not open connection:"); verify(ftpsClientMock).setNeedClientAuth(true); @@ -143,27 +142,26 @@ public class FtpesClientTest { @Test public void collectFileFaultTrustedCA_shouldFail_no_trustedCA_file() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doThrow(new IOException("problem")).when(clientUnderTestSpy).createInputStream(TRUSTED_CA_PATH); - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessage("Could not open connection: java.io.IOException: problem"); + assertThatThrownBy(() -> clientUnderTestSpy.open()).hasMessageContaining("Could not open connection:"); + } @Test public void collectFileFaultTrustedCA_shouldFail_empty_trustedCA_file() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doReturn(inputStreamMock).when(clientUnderTestSpy).createInputStream(TRUSTED_CA_PATH); - assertThatThrownBy(() -> clientUnderTestSpy.open()) - .hasMessage("Could not open connection: java.io.EOFException"); + assertThatThrownBy(() -> clientUnderTestSpy.open()).hasMessageContaining("Could not open connection: "); } @Test public void collectFileFaultyLogin_shouldFail() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); doReturn(false).when(ftpsClientMock).login(USERNAME, PASSWORD); @@ -179,7 +177,7 @@ public class FtpesClientTest { @Test public void collectFileBadRequestResponse_shouldFail() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); @@ -199,7 +197,7 @@ public class FtpesClientTest { @Test public void collectFile_shouldFail() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); @@ -218,7 +216,7 @@ public class FtpesClientTest { @Test public void collectFile_shouldFail_ioexception() throws Exception { - doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD); + doReturn(keyManagerMock).when(clientUnderTestSpy).getKeyManager(Paths.get(FTP_KEY_PATH), FTP_KEY_PASSWORD_PATH); doReturn(trustManagerMock).when(clientUnderTestSpy).getTrustManager(TRUSTED_CA_PATH, TRUSTED_CA_PASSWORD); doReturn(outputStreamMock).when(clientUnderTestSpy).createOutputStream(LOCAL_FILE_PATH); doReturn(true).when(ftpsClientMock).login(USERNAME, PASSWORD); diff --git a/datafilecollector/src/test/java/org/oran/datafile/ftp/SftpClientTest.java b/datafilecollector/src/test/java/org/oran/datafile/ftp/SftpClientTest.java index 87ff172..5268839 100644 --- a/datafilecollector/src/test/java/org/oran/datafile/ftp/SftpClientTest.java +++ b/datafilecollector/src/test/java/org/oran/datafile/ftp/SftpClientTest.java @@ -41,10 +41,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.configuration.SftpConfig; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; +import org.oran.datafile.model.FileServerData; @ExtendWith(MockitoExtension.class) public class SftpClientTest { diff --git a/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpClientTest.java b/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpClientTest.java index 5b349a1..0e9a8d0 100644 --- a/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpClientTest.java +++ b/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpClientTest.java @@ -39,9 +39,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.exceptions.DatafileTaskException; -import org.oran.datafile.service.HttpUtils; +import org.oran.datafile.model.FileServerData; import reactor.core.publisher.Flux; import reactor.netty.http.client.HttpClientConfig; diff --git a/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpsClientTest.java b/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpsClientTest.java index 468b8fe..c74edbd 100644 --- a/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpsClientTest.java +++ b/datafilecollector/src/test/java/org/oran/datafile/http/DfcHttpsClientTest.java @@ -42,9 +42,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.oran.datafile.commons.FileServerData; import org.oran.datafile.exceptions.DatafileTaskException; import org.oran.datafile.exceptions.NonRetryableDatafileTaskException; +import org.oran.datafile.model.FileServerData; @ExtendWith(MockitoExtension.class) class DfcHttpsClientTest { diff --git a/datafilecollector/src/test/java/org/oran/datafile/service/HttpUtilsTest.java b/datafilecollector/src/test/java/org/oran/datafile/http/HttpUtilsTest.java similarity index 98% rename from datafilecollector/src/test/java/org/oran/datafile/service/HttpUtilsTest.java rename to datafilecollector/src/test/java/org/oran/datafile/http/HttpUtilsTest.java index a0448c4..a72d698 100644 --- a/datafilecollector/src/test/java/org/oran/datafile/service/HttpUtilsTest.java +++ b/datafilecollector/src/test/java/org/oran/datafile/http/HttpUtilsTest.java @@ -15,7 +15,7 @@ * ============LICENSE_END======================================================================== */ -package org.oran.datafile.service; +package org.oran.datafile.http; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -27,7 +27,7 @@ import java.util.List; import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.net.URIBuilder; import org.junit.jupiter.api.Test; -import org.oran.datafile.commons.FileServerData; +import org.oran.datafile.model.FileServerData; class HttpUtilsTest { diff --git a/datafilecollector/src/test/java/org/oran/datafile/scheme/SchemeTest.java b/datafilecollector/src/test/java/org/oran/datafile/model/SchemeTest.java similarity index 95% rename from datafilecollector/src/test/java/org/oran/datafile/scheme/SchemeTest.java rename to datafilecollector/src/test/java/org/oran/datafile/model/SchemeTest.java index b1e5eeb..3b42ede 100644 --- a/datafilecollector/src/test/java/org/oran/datafile/scheme/SchemeTest.java +++ b/datafilecollector/src/test/java/org/oran/datafile/model/SchemeTest.java @@ -17,14 +17,14 @@ * ============LICENSE_END========================================================= */ -package org.oran.datafile.scheme; +package org.oran.datafile.model; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; -import org.oran.datafile.commons.Scheme; import org.oran.datafile.exceptions.DatafileTaskException; +import org.oran.datafile.model.FileData.Scheme; public class SchemeTest { diff --git a/datafilecollector/src/test/java/org/oran/datafile/utils/JsonMessage.java b/datafilecollector/src/test/java/org/oran/datafile/utils/JsonMessage.java deleted file mode 100644 index 6fe4362..0000000 --- a/datafilecollector/src/test/java/org/oran/datafile/utils/JsonMessage.java +++ /dev/null @@ -1,260 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018-2023 Nordix Foundation. All rights reserved. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.oran.datafile.utils; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * Utility class to produce correctly formatted fileReady event Json messages. - */ -public class JsonMessage { - private String eventName; - private String changeIdentifier; - private String changeType; - private String notificationFieldsVersion; - private List arrayOfAdditionalFields; - - public List getAdditionalFields() { - return arrayOfAdditionalFields; - } - - @Override - public String toString() { - return "[" + getParsed() + "]"; - } - - /** - * Gets the message in parsed format. - * - * @return the massage in parsed format. - */ - public String getParsed() { - StringBuffer additionalFieldsString = new StringBuffer(); - if (arrayOfAdditionalFields.size() > 0) { - additionalFieldsString.append("\"arrayOfNamedHashMap\":["); - for (Iterator iterator = arrayOfAdditionalFields.iterator(); iterator.hasNext();) { - AdditionalField additionalField = iterator.next(); - additionalFieldsString.append(additionalField.toString()); - if (iterator.hasNext()) { - additionalFieldsString.append(","); - } - } - additionalFieldsString.append("]"); - } - return "{" // - + "\"event\":" // - + "{" // - + "\"commonEventHeader\":" // - + "{" // - + "\"domain\":\"notification\"," // - + "\"eventId\":\"<>-reg\"," // - + "\"eventName\":\"" + eventName + "\"," // - + "\"eventType\":\"fileReady\"," // - + "\"internalHeaderFields\":{}," // - + "\"lastEpochMicrosec\":1519837825682," // - + "\"nfNamingCode\":\"5GRAN\"," // - + "\"nfcNamingCode\":\"5DU\"," // - + "\"priority\":\"Normal\"," // - + "\"reportingEntityName\":\"5GRAN_DU\"," // - + "\"sequence\":0," // - + "\"sourceId\":\"<>\"," // - + "\"sourceName\":\"5GRAN_DU\"," // - + "\"timeZoneOffset\":\"UTC+05:00\"," // - + "\"startEpochMicrosec\":\"1519837825682\"," // - + "\"version\":3" // - + "}," // - + "\"notificationFields\":" // - + "{" // - + getAsStringIfParameterIsSet("changeIdentifier", changeIdentifier, - changeType != null || notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0) - + getAsStringIfParameterIsSet("changeType", changeType, - notificationFieldsVersion != null || arrayOfAdditionalFields.size() > 0) - + getAsStringIfParameterIsSet("notificationFieldsVersion", notificationFieldsVersion, - arrayOfAdditionalFields.size() > 0) - + additionalFieldsString.toString() // - + "}" // - + "}" // - + "}"; - } - - private JsonMessage(final JsonMessageBuilder builder) { - this.eventName = builder.eventName; - this.changeIdentifier = builder.changeIdentifier; - this.changeType = builder.changeType; - this.notificationFieldsVersion = builder.notificationFieldsVersion; - this.arrayOfAdditionalFields = builder.arrayOfAdditionalFields; - } - - public static class AdditionalField { - private String name; - private String location; - private String compression; - private String fileFormatType; - private String fileFormatVersion; - - @Override - public String toString() { - return "{" // - + getAsStringIfParameterIsSet("name", name, true) // - + "\"hashMap\":" // - + "{" - + getAsStringIfParameterIsSet("location", location, - compression != null || fileFormatType != null || fileFormatVersion != null) - + getAsStringIfParameterIsSet("compression", compression, - fileFormatType != null || fileFormatVersion != null) - + getAsStringIfParameterIsSet("fileFormatType", fileFormatType, fileFormatVersion != null) - + getAsStringIfParameterIsSet("fileFormatVersion", fileFormatVersion, false) // - + "}" // - + "}"; - } - - private AdditionalField(AdditionalFieldBuilder builder) { - this.name = builder.name; - this.location = builder.location; - this.compression = builder.compression; - this.fileFormatType = builder.fileFormatType; - this.fileFormatVersion = builder.fileFormatVersion; - } - - } - - public static class AdditionalFieldBuilder { - private String name; - private String location; - private String compression; - private String fileFormatType; - private String fileFormatVersion; - - public AdditionalFieldBuilder name(String name) { - this.name = name; - return this; - } - - public AdditionalFieldBuilder location(String location) { - this.location = location; - return this; - } - - public AdditionalFieldBuilder compression(String compression) { - this.compression = compression; - return this; - } - - public AdditionalFieldBuilder fileFormatType(String fileFormatType) { - this.fileFormatType = fileFormatType; - return this; - } - - public AdditionalFieldBuilder fileFormatVersion(String fileFormatVersion) { - this.fileFormatVersion = fileFormatVersion; - return this; - } - - public AdditionalField build() { - return new AdditionalField(this); - } - } - - public static class JsonMessageBuilder { - private String eventName; - private String changeIdentifier; - private String changeType; - private String notificationFieldsVersion; - private List arrayOfAdditionalFields = new ArrayList(); - - public JsonMessageBuilder eventName(String eventName) { - this.eventName = eventName; - return this; - } - - public JsonMessageBuilder changeIdentifier(String changeIdentifier) { - this.changeIdentifier = changeIdentifier; - return this; - } - - public JsonMessageBuilder changeType(String changeType) { - this.changeType = changeType; - return this; - } - - public JsonMessageBuilder notificationFieldsVersion(String notificationFieldsVersion) { - this.notificationFieldsVersion = notificationFieldsVersion; - return this; - } - - public JsonMessageBuilder addAdditionalField(AdditionalField additionalField) { - this.arrayOfAdditionalFields.add(additionalField); - return this; - } - - public JsonMessage build() { - return new JsonMessage(this); - } - } - - private static String getAsStringIfParameterIsSet(String parameterName, String parameterValue, - boolean withSeparator) { - String result = ""; - if (parameterValue != null) { - result = "\"" + parameterName + "\":\"" + parameterValue + "\""; - - if (withSeparator) { - result = result + ","; - } - } - return result; - } - - /** - * Can be used to produce a correct test Json message. Tip! Check the formatting - * with - * Json formatter - * - * @param args Not used - */ - public static void main(String[] args) { - AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder() // - .name("A20161224.1030-1045.bin.gz") // - .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") // - .compression("gzip") // - .fileFormatType("org.3GPP.32.435#measCollec") // - .fileFormatVersion("V10") // - .build(); - AdditionalField secondAdditionalField = new JsonMessage.AdditionalFieldBuilder() // - .name("A20161224.1030-1045.bin.gz") // - .location("sftp://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz") // - .compression("gzip") // - .fileFormatType("org.3GPP.32.435#measCollec") // - .fileFormatVersion("V10") // - .build(); - JsonMessage message = new JsonMessage.JsonMessageBuilder() // - .eventName("Noti_NrRadio-Ericsson_FileReady") // - .changeIdentifier("PM_MEAS_FILES") // - .changeType("FileReady") // - .notificationFieldsVersion("2.0") // - .addAdditionalField(additionalField) // - .addAdditionalField(secondAdditionalField) // - .build(); - System.out.println(message.toString()); - } -} diff --git a/influxlogger/config/application.yaml b/influxlogger/config/application.yaml index 9da01ab..0d814c3 100644 --- a/influxlogger/config/application.yaml +++ b/influxlogger/config/application.yaml @@ -84,7 +84,7 @@ app: max-poll-records: 500 group-id: kafkaGroupId client-id: kafkaClientId - # Configues if oath2 tokens shall be used. If set to true, auth-token-file must also be configured + # Configures if oath2 tokens shall be used. If set to true, auth-token-file must also be configured use-oath-token: false ssl: key-store-type: PEM diff --git a/pmproducer/config/application.yaml b/pmproducer/config/application.yaml index 0e611b5..c0d6d35 100644 --- a/pmproducer/config/application.yaml +++ b/pmproducer/config/application.yaml @@ -87,7 +87,7 @@ app: bootstrap-servers: localhost:9092 # The maximum number of records returned in a single call to poll() (default 100) max-poll-records: 500 - # Configues if oath2 tokens shall be used. If set to true, auth-token-file must also be configured + # Configures if oath2 tokens shall be used. If set to true, auth-token-file must also be configured use-oath-token: false ssl: key-store-type: PEM -- 2.16.6