[SDC-29] rebase continue work to align source
[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 java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.stream.Collectors;
37
38 import org.apache.commons.io.output.ByteArrayOutputStream;
39 import org.apache.commons.net.ftp.FTP;
40 import org.apache.commons.net.ftp.FTPClient;
41 import org.apache.commons.net.ftp.FTPFile;
42 import org.apache.commons.net.ftp.FTPReply;
43
44 public class AttFtpClient {
45
46         private static final AttFtpClient instance = new AttFtpClient();
47
48         public static AttFtpClient getInstance() {
49                 return instance;
50         }
51
52         private FTPClient apacheFtpClient;
53
54         private AttFtpClient() {
55                 apacheFtpClient = new FTPClient();
56         };
57
58
59         public void init(String server, int port, String user, String pass) {
60
61                 try {
62                         apacheFtpClient.connect(server, port);
63                         showServerReply(apacheFtpClient);
64                 
65                         
66                         int replyCode = apacheFtpClient.getReplyCode();
67                         if (!FTPReply.isPositiveCompletion(replyCode)) {
68                                 System.out.println("Connect failed");
69                                 return;
70                         }
71
72                         boolean success = apacheFtpClient.login(user, pass);
73                         showServerReply(apacheFtpClient);
74
75                         if (!success) {
76                                 System.out.println("Could not login to the server");
77                                 return;
78                         }
79                         
80 //                      else{
81 //                              apacheFtpClient.enterLocalPassiveMode();
82 //                              apacheFtpClient.setFileType(FTP.BINARY_FILE_TYPE);      
83 //                      }
84                 } catch (IOException ex) {
85                         System.out.println("Oops! Something wrong happened");
86                         ex.printStackTrace();
87                 }
88
89         }
90
91         public File retrieveLastModifiedFileFromFTP() throws IOException {
92                 FTPFile[] files1 = retrieveListOfFile();
93
94                 // sort list by TimeStamp
95                 List<FTPFile> sorted = Arrays.asList(files1).stream()
96                                 .sorted((e1, e2) -> e1.getTimestamp().compareTo(e2.getTimestamp())).collect(Collectors.toList());
97                 printFileDetailsList(sorted);
98
99                 // retrieve file from FTP
100                 FTPFile ftpFile = sorted.get(sorted.size() - 1);
101
102                 return retrieveFileFromFTP(ftpFile);
103
104         }
105
106         public FTPFile[] retrieveListOfFile() throws IOException {
107                 // Lists files and directories
108                 FTPFile[] files = apacheFtpClient.listFiles("");
109                 
110                 printNames(files);
111                 return files;
112         }
113
114         public File retrieveFileFromFTP(FTPFile ftpFile) throws IOException {
115                 
116         File downloadFile1 = new File("tmp");
117         OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
118         boolean success = apacheFtpClient.retrieveFile(ftpFile.getName(), outputStream1);
119         outputStream1.close();
120
121         if (success) {
122             System.out.println("File #1 has been downloaded successfully.");
123         }
124                 
125
126                 return downloadFile1;
127
128         }
129
130         public void deleteFilesFromFTPserver() throws IOException {
131                 FTPFile[] files = retrieveListOfFile();
132                 deleteFiles(files);
133         }
134
135         public void terminateClient() throws IOException {
136
137                 String status = apacheFtpClient.getStatus();
138
139                 // logs out and disconnects from server
140                 try {
141                         if (apacheFtpClient.isConnected()) {
142                                 apacheFtpClient.logout();
143                                 apacheFtpClient.disconnect();
144                         }
145                 } catch (IOException ex) {
146                         ex.printStackTrace();
147                 }
148         }
149
150         private void printFileDetailsList(List<FTPFile> list) {
151                 DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
152
153                 for (FTPFile ftpFile : list) {
154
155                         String details = ftpFile.getName();
156                         if (ftpFile.isDirectory()) {
157                                 details = "[" + details + "]";
158                         }
159                         details += "\t\t" + ftpFile.getSize();
160                         details += "\t\t" + dateFormater.format(ftpFile.getTimestamp().getTime());
161
162                         System.out.println(details);
163                 }
164         }
165
166         private void printNames(FTPFile[] files) {
167                 if (files != null && files.length > 0) {
168                         for (FTPFile aFile : files) {
169                                 System.out.println(aFile);
170                         }
171                 }
172         }
173
174         private void showServerReply(FTPClient ftpClient) {
175                 String[] replies = ftpClient.getReplyStrings();
176                 if (replies != null && replies.length > 0) {
177                         for (String aReply : replies) {
178                                 System.out.println("SERVER: " + aReply);
179                         }
180                 }
181         }
182
183         public class LastModifiedComparator implements Comparator<FTPFile> {
184
185                 public int compare(FTPFile f1, FTPFile f2) {
186                         return f1.getTimestamp().compareTo(f2.getTimestamp());
187                 }
188         }
189
190         public FTPFile getMaxLastModified(FTPFile[] ftpFiles) {
191                 return Collections.max(Arrays.asList(ftpFiles), new LastModifiedComparator());
192         }
193
194         public static void displayFiles(File[] files) {
195                 for (File file : files) {
196                         System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName());
197                 }
198         }
199
200         public void deleteFiles(FTPFile[] files) {
201
202                 for (FTPFile file : files) {
203
204                         boolean deleted = false;
205                         try {
206                                 deleted = apacheFtpClient.deleteFile(file.getName());
207                         } catch (IOException e) {
208                                 // TODO Auto-generated catch block
209                                 e.printStackTrace();
210                         }
211
212                         if (deleted) {
213                                 System.out.println("The file was deleted successfully.");
214                         } else {
215                                 System.out.println("Could not delete theĀ  file, it may not exist.");
216                         }
217                 }
218
219         }
220
221 }