2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.tasks;
22 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
23 import org.onap.dcaegen2.collectors.datafile.configuration.Config;
24 import org.onap.dcaegen2.collectors.datafile.configuration.FtpesConfig;
25 import org.onap.dcaegen2.collectors.datafile.ftp.FileCollectClient;
26 import org.onap.dcaegen2.collectors.datafile.ftp.FileCollectResult;
27 import org.onap.dcaegen2.collectors.datafile.ftp.FileServerData;
28 import org.onap.dcaegen2.collectors.datafile.ftp.FtpsClient;
29 import org.onap.dcaegen2.collectors.datafile.ftp.ImmutableFileServerData;
30 import org.onap.dcaegen2.collectors.datafile.ftp.SftpClient;
31 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
32 import org.onap.dcaegen2.collectors.datafile.model.FileData;
33 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
39 import reactor.core.publisher.Flux;
42 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
45 public class XnfCollectorTaskImpl implements XnfCollectorTask {
47 private static final String FTPES = "ftpes";
48 private static final String FTPS = "ftps";
49 private static final String SFTP = "sftp";
51 private static final Logger logger = LoggerFactory.getLogger(XnfCollectorTaskImpl.class);
52 private Config datafileAppConfig;
54 private final FtpsClient ftpsClient;
55 private final SftpClient sftpClient;
56 private RetryTimer retryTimer;
59 protected XnfCollectorTaskImpl(AppConfig datafileAppConfig, FtpsClient ftpsCleint, SftpClient sftpClient) {
60 this.datafileAppConfig = datafileAppConfig;
61 this.ftpsClient = ftpsCleint;
62 this.sftpClient = sftpClient;
66 public Flux<ConsumerDmaapModel> execute(FileData fileData) {
67 logger.trace("Entering execute with {}", fileData);
70 String localFile = collectFile(fileData);
72 if (localFile != null) {
73 ConsumerDmaapModel consumerDmaapModel = getConsumerDmaapModel(fileData, localFile);
74 logger.trace("Exiting execute with {}", consumerDmaapModel);
75 return Flux.just(consumerDmaapModel);
77 logger.trace("Exiting execute with empty");
82 public FtpesConfig resolveConfiguration() {
83 return datafileAppConfig.getFtpesConfiguration();
86 private void resolveKeyStore() {
87 FtpesConfig ftpesConfig = resolveConfiguration();
88 ftpsClient.setKeyCertPath(ftpesConfig.keyCert());
89 ftpsClient.setKeyCertPassword(ftpesConfig.keyPassword());
90 ftpsClient.setTrustedCAPath(ftpesConfig.trustedCA());
91 ftpsClient.setTrustedCAPassword(ftpesConfig.trustedCAPassword());
94 private String collectFile(FileData fileData) {
95 logger.trace("starting to collectFile");
96 String location = fileData.location();
97 URI uri = URI.create(location);
98 FileServerData fileServerData = getFileServerData(uri);
99 String remoteFile = uri.getPath();
100 String localFile = "target" + File.separator + fileData.name();
102 FileCollectClient currentClient = selectClient(fileData, uri);
104 if (currentClient != null) {
105 FileCollectResult fileCollectResult = currentClient.collectFile(fileServerData, remoteFile, localFile);
106 if (!fileCollectResult.downloadSuccessful()) {
107 fileCollectResult = retry(fileCollectResult, currentClient);
109 if (!fileCollectResult.downloadSuccessful()) {
111 logger.error("Download of file aborted after maximum number of retries. Data: {} Error causes {}",
112 fileServerData, fileCollectResult.getErrorData());
120 private FileServerData getFileServerData(URI uri) {
121 String[] userInfo = getUserNameAndPasswordIfGiven(uri.getUserInfo());
123 return ImmutableFileServerData.builder()
124 .serverAddress(uri.getHost())
125 .userId(userInfo != null ? userInfo[0] : "")
126 .password(userInfo != null ? userInfo[1] : "")
132 private String[] getUserNameAndPasswordIfGiven(String userInfoString) {
133 String[] userInfo = null;
134 if (userInfoString != null && !userInfoString.isEmpty()) {
135 userInfo = userInfoString.split(":");
140 private FileCollectClient selectClient(FileData fileData, URI uri) {
141 FileCollectClient selectedClient = null;
142 String scheme = uri.getScheme();
143 if (FTPES.equals(scheme) || FTPS.equals(scheme)) {
144 selectedClient = ftpsClient;
145 } else if (SFTP.equals(scheme)) {
146 selectedClient = sftpClient;
148 logger.error("DFC does not support protocol {}. Supported protocols are {}, {}, and {}. Data: {}", scheme,
149 FTPES, FTPS, SFTP, fileData);
151 return selectedClient;
154 private FileCollectResult retry(FileCollectResult fileCollectResult, FileCollectClient fileCollectClient) {
156 FileCollectResult newResult = fileCollectResult;
157 while (!newResult.downloadSuccessful() && retryCount++ < 3) {
158 getRetryTimer().waitRetryTime();
159 newResult = fileCollectClient.retryCollectFile();
164 private ConsumerDmaapModel getConsumerDmaapModel(FileData fileData, String localFile) {
165 String productName = fileData.productName();
166 String vendorName = fileData.vendorName();
167 String lastEpochMicrosec = fileData.lastEpochMicrosec();
168 String sourceName = fileData.sourceName();
169 String startEpochMicrosec = fileData.startEpochMicrosec();
170 String timeZoneOffset = fileData.timeZoneOffset();
171 String name = fileData.name();
172 String location = fileData.location();
173 String internalLocation = localFile;
174 String compression = fileData.compression();
175 String fileFormatType = fileData.fileFormatType();
176 String fileFormatVersion = fileData.fileFormatVersion();
179 return ImmutableConsumerDmaapModel.builder()
180 .productName(productName)
181 .vendorName(vendorName)
182 .lastEpochMicrosec(lastEpochMicrosec)
183 .sourceName(sourceName)
184 .startEpochMicrosec(startEpochMicrosec)
185 .timeZoneOffset(timeZoneOffset)
188 .internalLocation(internalLocation)
189 .compression(compression)
190 .fileFormatType(fileFormatType)
191 .fileFormatVersion(fileFormatVersion)
196 private RetryTimer getRetryTimer() {
197 if (retryTimer == null) {
198 retryTimer = new RetryTimer();
203 protected void setRetryTimer(RetryTimer retryTimer) {
204 this.retryTimer = retryTimer;