b2157ed983b07304c9c4798d6de49765a6de92e2
[vfc/nfvo/driver/vnfm/svnfm.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.util.Enumeration;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
28 import org.apache.http.Header;
29 import org.apache.http.HeaderElement;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.NameValuePair;
33 import org.apache.http.client.methods.CloseableHttpResponse;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.impl.client.HttpClients;
37 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Utility class to download CSAR
43  *
44  * @author
45  * @version VFC 1.0 Sep 5, 2016
46  */
47 public class DownloadCsarManager {
48
49     private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
50
51     public static final int CACHE = 100 * 1024;
52
53     private DownloadCsarManager() {
54         // private constructor
55     }
56
57     /**
58      * Download from given URL.
59      * 
60      * @param url String
61      * @return
62      */
63     public static String download(String url) {
64         return download(url, null);
65     }
66
67     /**
68      * Download from given URL to given file location.
69      * 
70      * @param url String
71      * @param filepath String
72      * @return
73      */
74     public static String download(String url, String filepath) {
75         String status = "";
76         try {
77             CloseableHttpClient client = HttpClients.createDefault();
78             HttpGet httpget = new HttpGet(url);
79             CloseableHttpResponse response = client.execute(httpget);
80
81             HttpEntity entity = response.getEntity();
82             InputStream is = entity.getContent();
83             if(filepath == null) {
84                 filepath = getFilePath(response); // NOSONAR
85             }
86
87             File file = new File(filepath);
88             file.getParentFile().mkdirs();
89             FileOutputStream fileout = new FileOutputStream(file);
90
91             byte[] buffer = new byte[CACHE];
92             int ch;
93             while((ch = is.read(buffer)) != -1) {
94                 fileout.write(buffer, 0, ch);
95             }
96             is.close();
97             fileout.flush();
98             fileout.close();
99             status = Constant.DOWNLOADCSAR_SUCCESS;
100
101         } catch(Exception e) {
102             status = Constant.DOWNLOADCSAR_FAIL;
103             LOG.error("Download csar file failed! " + e.getMessage(), e);
104         }
105         return status;
106     }
107
108     /**
109      * Retrieve file path from given response.
110      * 
111      * @param response HttpResponse
112      * @return
113      */
114     public static String getFilePath(HttpResponse response) {
115         String filepath = System.getProperty("java.home");
116         String filename = getFileName(response);
117
118         if(filename != null) {
119             filepath += filename;
120         } else {
121             filepath += getRandomFileName();
122         }
123         return filepath;
124     }
125
126     /**
127      * Retrieve file name from given response.
128      * 
129      * @param response HttpResponse
130      * @return
131      */
132     public static String getFileName(HttpResponse response) {
133         Header contentHeader = response.getFirstHeader("Content-Disposition");
134         String filename = null;
135         if(contentHeader != null) {
136             HeaderElement[] values = contentHeader.getElements();
137             if(values.length == 1) {
138                 NameValuePair param = values[0].getParameterByName("filename");
139                 if(param != null) {
140                     try {
141                         filename = param.getValue();
142                     } catch(Exception e) {
143                         LOG.error("getting filename failed! " + e.getMessage(), e);
144                     }
145                 }
146             }
147         }
148         return filename;
149     }
150
151     /**
152      * Provides random file name.
153      * 
154      * @return
155      */
156     public static String getRandomFileName() {
157         return String.valueOf(System.currentTimeMillis());
158     }
159
160     /**
161      * unzip CSAR packge
162      * 
163      * @param fileName filePath
164      * @return
165      */
166     public static int unzipCSAR(String fileName, String filePath) {
167         final int BUFFER = 2048;
168         int status = 0;
169
170         try {
171             ZipFile zipFile = new ZipFile(fileName);
172             Enumeration emu = zipFile.entries();
173             int i = 0;
174             while(emu.hasMoreElements()) {
175                 ZipEntry entry = (ZipEntry)emu.nextElement();
176                 // read directory as file first,so only need to create directory
177                 if(entry.isDirectory()) {
178                     new File(filePath + entry.getName()).mkdirs();
179                     continue;
180                 }
181                 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
182                 File file = new File(filePath + entry.getName());
183                 // Because that is random to read zipfile,maybe the file is read first
184                 // before the directory is read,so we need to create directory first.
185                 File parent = file.getParentFile();
186                 if(parent != null && (!parent.exists())) {
187                     parent.mkdirs();
188                 }
189                 FileOutputStream fos = new FileOutputStream(file);
190                 BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
191
192                 int count;
193                 byte data[] = new byte[BUFFER];
194                 while((count = bis.read(data, 0, BUFFER)) != -1) {
195                     bos.write(data, 0, count);
196                 }
197                 bos.flush();
198                 bos.close();
199                 bis.close();
200             }
201             status = Constant.UNZIP_SUCCESS;
202             zipFile.close();
203         } catch(Exception e) {
204             status = Constant.UNZIP_FAIL;
205             e.printStackTrace();
206         }
207         return status;
208     }
209 }