Fix invalid versions in pom.xml
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / DownloadCsarManager.java
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.IOException;
24 import java.io.InputStream;
25 import java.util.Enumeration;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
28
29 import org.apache.http.Header;
30 import org.apache.http.HeaderElement;
31 import org.apache.http.HttpEntity;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.NameValuePair;
34 import org.apache.http.client.methods.CloseableHttpResponse;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.impl.client.CloseableHttpClient;
37 import org.apache.http.impl.client.HttpClients;
38 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Utility class to download CSAR
44  *
45  * @author
46  * @version VFC 1.0 Sep 5, 2016
47  */
48 public class DownloadCsarManager {
49
50     private static final Logger LOG = LoggerFactory.getLogger(DownloadCsarManager.class);
51
52     public static final int CACHE = 100 * 1024;
53
54     private DownloadCsarManager() {
55         // private constructor
56     }
57
58     /**
59      * Download from given URL.
60      *
61      * @param url String
62      * @return
63      */
64     public static String download(String url) {
65         return download(url, null);
66     }
67
68     /**
69      * Download from given URL to given file location.
70      *
71      * @param url String
72      * @param filepath String
73      * @return
74      */
75     public static String download(String url, String filepath) {
76         String status = "";
77         try (CloseableHttpClient client = HttpClients.createDefault()){
78             HttpGet httpget = new HttpGet(url);
79             CloseableHttpResponse response = client.execute(httpget);
80
81             HttpEntity entity = response.getEntity();
82             try(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                 try(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                     fileout.flush();
97                     status = Constant.DOWNLOADCSAR_SUCCESS;
98                 } catch(Exception e) {
99                     status = Constant.DOWNLOADCSAR_FAIL;
100                     LOG.error("Download csar file failed! " + e.getMessage(), e);
101                 }
102             } catch(Exception e) {
103                 status = Constant.DOWNLOADCSAR_FAIL;
104                 LOG.error("Download csar file failed! " + e.getMessage(), e);
105             }
106         } catch(Exception e) {
107             status = Constant.DOWNLOADCSAR_FAIL;
108             LOG.error("Download csar file failed! " + e.getMessage(), e);
109         }
110
111         return status;
112     }
113
114     /**
115      * Retrieve file path from given response.
116      *
117      * @param response HttpResponse
118      * @return
119      */
120     public static String getFilePath(HttpResponse response) {
121         String filepath = System.getProperty("java.home");
122         String filename = getFileName(response);
123
124         if(filename != null) {
125             filepath += filename;
126         } else {
127             filepath += getRandomFileName();
128         }
129         return filepath;
130     }
131
132     /**
133      * Retrieve file name from given response.
134      *
135      * @param response HttpResponse
136      * @return
137      */
138     public static String getFileName(HttpResponse response) {
139         Header contentHeader = response.getFirstHeader("Content-Disposition");
140         String filename = null;
141         if(contentHeader != null) {
142             HeaderElement[] values = contentHeader.getElements();
143             if(values.length == 1) {
144                 NameValuePair param = values[0].getParameterByName("filename");
145                 if(param != null) {
146                     try {
147                         filename = param.getValue();
148                     } catch(Exception e) {
149                         LOG.error("getting filename failed! " + e.getMessage(), e);
150                     }
151                 }
152             }
153         }
154         return filename;
155     }
156
157     /**
158      * Provides random file name.
159      *
160      * @return
161      */
162     public static String getRandomFileName() {
163         return String.valueOf(System.currentTimeMillis());
164     }
165
166     /**
167      * unzip CSAR packge
168      *
169      * @param fileName filePath
170      * @return
171      * @throws IOException
172      */
173     public static int unzipCSAR(String fileName, String filePath) {
174         final int BUFFER = 2048;
175         int status = 0;
176         ZipFile zipFile = null;
177         try {
178             zipFile = new ZipFile(fileName);
179             Enumeration emu = zipFile.entries();
180             while(emu.hasMoreElements()) {
181                 ZipEntry entry = (ZipEntry)emu.nextElement();
182                 // read directory as file first,so only need to create directory
183                 if(entry.isDirectory()) {
184                     new File(filePath + entry.getName()).mkdirs();
185                     continue;
186                 }
187                 BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
188                 File file = new File(filePath + entry.getName());
189                 // Because that is random to read zipfile,maybe the file is read first
190                 // before the directory is read,so we need to create directory first.
191                 File parent = file.getParentFile();
192                 if(parent != null && (!parent.exists())) {
193                     parent.mkdirs();
194                 }
195                 try(FileOutputStream fos = new FileOutputStream(file)){
196                     try(BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER)){
197
198                     int count;
199                     byte data[] = new byte[BUFFER];
200                     while((count = bis.read(data, 0, BUFFER)) != -1) {
201                         bos.write(data, 0, count);
202                     }
203                     bos.flush();
204                     }
205                 }
206             }
207
208             status = Constant.UNZIP_SUCCESS;
209
210         } catch(Exception e) {
211             status = Constant.UNZIP_FAIL;
212             LOG.error("Exception: " + e);
213         } finally {
214             if(zipFile != null) {
215                 try {
216                     zipFile.close();
217                 } catch(IOException e) {
218                     LOG.error("IOException: " + e);
219                     ;
220                 }
221             }
222         }
223         return status;
224     }
225 }