re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / AttFtpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.setup;
22
23 import org.apache.commons.net.ftp.FTPClient;
24 import org.apache.commons.net.ftp.FTPFile;
25 import org.apache.commons.net.ftp.FTPReply;
26
27 import java.io.*;
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
30 import java.util.*;
31 import java.util.stream.Collectors;
32
33 public class AttFtpClient {
34
35         private static final AttFtpClient instance = new AttFtpClient();
36
37         public static AttFtpClient getInstance() {
38                 return instance;
39         }
40
41         private FTPClient apacheFtpClient;
42
43         private AttFtpClient() {
44                 apacheFtpClient = new FTPClient();
45         };
46
47
48         public void init(String server, int port, String user, String pass) {
49
50                 try {
51                         apacheFtpClient.connect(server, port);
52                         showServerReply(apacheFtpClient);
53                 
54                         
55                         int replyCode = apacheFtpClient.getReplyCode();
56                         if (!FTPReply.isPositiveCompletion(replyCode)) {
57                                 System.out.println("Connect failed");
58                                 return;
59                         }
60
61                         boolean success = apacheFtpClient.login(user, pass);
62                         showServerReply(apacheFtpClient);
63
64                         if (!success) {
65                                 System.out.println("Could not login to the server");
66                                 return;
67                         }
68                         
69 //                      else{
70 //                              apacheFtpClient.enterLocalPassiveMode();
71 //                              apacheFtpClient.setFileType(FTP.BINARY_FILE_TYPE);      
72 //                      }
73                 } catch (IOException ex) {
74                         System.out.println("Oops! Something wrong happened");
75                         ex.printStackTrace();
76                 }
77
78         }
79
80         public File retrieveLastModifiedFileFromFTP() throws IOException {
81                 FTPFile[] files1 = retrieveListOfFile();
82
83                 // sort list by TimeStamp
84                 List<FTPFile> sorted = Arrays.asList(files1).stream()
85                                 .sorted((e1, e2) -> e1.getTimestamp().compareTo(e2.getTimestamp())).collect(Collectors.toList());
86                 printFileDetailsList(sorted);
87
88                 // retrieve file from FTP
89                 FTPFile ftpFile = sorted.get(sorted.size() - 1);
90
91                 return retrieveFileFromFTP(ftpFile);
92
93         }
94
95         public FTPFile[] retrieveListOfFile() throws IOException {
96                 // Lists files and directories
97                 FTPFile[] files = apacheFtpClient.listFiles("");
98                 
99                 printNames(files);
100                 return files;
101         }
102
103         public File retrieveFileFromFTP(FTPFile ftpFile) throws IOException {
104                 
105         File downloadFile1 = new File("tmp");
106         OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
107         boolean success = apacheFtpClient.retrieveFile(ftpFile.getName(), outputStream1);
108         outputStream1.close();
109
110         if (success) {
111             System.out.println("File #1 has been downloaded successfully.");
112         }
113                 
114
115                 return downloadFile1;
116
117         }
118
119         public void deleteFilesFromFTPserver() throws IOException {
120                 FTPFile[] files = retrieveListOfFile();
121                 deleteFiles(files);
122         }
123
124         public void terminateClient() throws IOException {
125
126                 String status = apacheFtpClient.getStatus();
127
128                 // logs out and disconnects from server
129                 try {
130                         if (apacheFtpClient.isConnected()) {
131                                 apacheFtpClient.logout();
132                                 apacheFtpClient.disconnect();
133                         }
134                 } catch (IOException ex) {
135                         ex.printStackTrace();
136                 }
137         }
138
139         private void printFileDetailsList(List<FTPFile> list) {
140                 DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
141
142                 for (FTPFile ftpFile : list) {
143
144                         String details = ftpFile.getName();
145                         if (ftpFile.isDirectory()) {
146                                 details = "[" + details + "]";
147                         }
148                         details += "\t\t" + ftpFile.getSize();
149                         details += "\t\t" + dateFormater.format(ftpFile.getTimestamp().getTime());
150
151                         System.out.println(details);
152                 }
153         }
154
155         private void printNames(FTPFile[] files) {
156                 if (files != null && files.length > 0) {
157                         for (FTPFile aFile : files) {
158                                 System.out.println(aFile);
159                         }
160                 }
161         }
162
163         private void showServerReply(FTPClient ftpClient) {
164                 String[] replies = ftpClient.getReplyStrings();
165                 if (replies != null && replies.length > 0) {
166                         for (String aReply : replies) {
167                                 System.out.println("SERVER: " + aReply);
168                         }
169                 }
170         }
171
172         public class LastModifiedComparator implements Comparator<FTPFile> {
173
174                 public int compare(FTPFile f1, FTPFile f2) {
175                         return f1.getTimestamp().compareTo(f2.getTimestamp());
176                 }
177         }
178
179         public FTPFile getMaxLastModified(FTPFile[] ftpFiles) {
180                 return Collections.max(Arrays.asList(ftpFiles), new LastModifiedComparator());
181         }
182
183         public static void displayFiles(File[] files) {
184                 for (File file : files) {
185                         System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName());
186                 }
187         }
188
189         public void deleteFiles(FTPFile[] files) {
190
191                 for (FTPFile file : files) {
192
193                         boolean deleted = false;
194                         try {
195                                 deleted = apacheFtpClient.deleteFile(file.getName());
196                         } catch (IOException e) {
197                                 e.printStackTrace();
198                         }
199
200                         if (deleted) {
201                                 System.out.println("The file was deleted successfully.");
202                         } else {
203                                 System.out.println("Could not delete theĀ  file, it may not exist.");
204                         }
205                 }
206
207         }
208
209 }