13a53c33b5b177f86bbf594d65afc0d6932a9aee
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / common / ToolUtil.java
1 /**
2  * Copyright 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.vnfsdk.marketplace.common;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.text.DecimalFormat;
25 import java.util.Collection;
26 import java.util.UUID;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.Gson;
32
33
34 /**
35  * common utility class.
36  */
37 public class ToolUtil {
38
39     private static final Logger LOG = LoggerFactory.getLogger(ToolUtil.class);
40
41     public static final String CATALOGUE_CSAR_DIR_NAME = "csar";
42
43     public static final String CATALOGUE_IMAGE_DIR_NAME = "image";
44
45     public static final String CSAR_EXTENSION = ".csar";
46
47     public static final int FILE_PERCENT = 1024 * 1024; // 1M
48
49     private ToolUtil() {
50     }
51
52     public static boolean isEmptyString(String val) {
53         return val == null || "".equals(val);
54     }
55
56     public static boolean isTrimedEmptyString(String val) {
57         return val == null || "".equals(val.trim());
58     }
59
60     public static boolean isTrimedEmptyArray(String[] val) {
61         return val == null || val.length == 0;
62     }
63
64     public static boolean isEmptyCollection(Collection<?> coll) {
65         return null == coll || coll.isEmpty();
66     }
67
68     /**
69      * store chunk file to local temp directory.
70      *
71      * @param dirName directory name
72      * @param fileName file name
73      * @param uploadedInputStream upload input stream
74      * @return String
75      * @throws IOException e
76      */
77     public static String storeChunkFileInLocal(String dirName, String fileName, InputStream uploadedInputStream)
78             throws IOException {
79         File tmpDir = new File(dirName);
80         LOG.info("tmpdir = " + File.separator + dirName);
81         if(!tmpDir.exists()) {
82             tmpDir.mkdirs();
83         }
84         File file = new File(tmpDir + File.separator + fileName);
85
86         try (OutputStream os = new FileOutputStream(file, true);) {
87             int read = 0;
88             byte[] bytes = new byte[1024];
89             while((read = uploadedInputStream.read(bytes)) != -1) {
90                 os.write(bytes, 0, read);
91             }
92             os.flush();
93             return file.getAbsolutePath();
94         }
95     }
96
97     /**
98      * get temp dirctory when upload package.
99      *
100      * @param dirName temp directory name
101      * @param fileName package name
102      * @return String
103      */
104     public static String getTempDir(String dirName, String fileName) {
105         return Thread.currentThread().getContextClassLoader().getResource("/").getPath() + dirName + File.separator
106                 + fileName.replace(CSAR_EXTENSION, "");
107     }
108
109     public static String getUnzipDir(String dirName) {
110         File tmpDir = new File(File.separator + dirName);
111         return tmpDir.getAbsolutePath().replace(CSAR_EXTENSION, "");
112     }
113
114     /**
115      * delete file.
116      *
117      * @param dirName the directory of file
118      * @param fileName file name
119      * @return boolean
120      */
121     public static boolean deleteFile(String dirName, String fileName) {
122         File tmpDir = new File(File.separator + dirName);
123         if(!tmpDir.exists()) {
124             return true;
125         }
126         File file = new File(tmpDir.getAbsolutePath() + File.separator + fileName);
127         if(file.exists()) {
128             return file.delete();
129         }
130         return true;
131     }
132
133     public static String getCatalogueCsarPath() {
134         return File.separator + CATALOGUE_CSAR_DIR_NAME;
135     }
136
137     public static String getCatalogueImagePath() {
138         return File.separator + CATALOGUE_IMAGE_DIR_NAME;
139     }
140
141     /**
142      * get file size.
143      *
144      * @param file file which to get the size
145      * @param fileUnit file unit
146      * @return String file size
147      */
148     public static String getFileSize(File file, int fileUnit) {
149         String fileSize = "";
150         DecimalFormat format = new DecimalFormat("#0.00");
151         if(file.exists()) {
152             fileSize = format.format((double)file.length() / fileUnit) + "M";
153         }
154         return fileSize;
155     }
156
157     public static String formatFileSize(double fileLength, int fileUnit) {
158         DecimalFormat format = new DecimalFormat("#0.00");
159         return format.format(fileLength / fileUnit) + "M";
160     }
161
162     /**
163      * fix package format.
164      *
165      * @param csarId package ID
166      * @return String
167      */
168     public static String formatCsar(String csarId) {
169         String result = csarId;
170         if(csarId.indexOf(CSAR_EXTENSION) < 0) {
171             result += CSAR_EXTENSION;
172         }
173         return result;
174     }
175
176     /**
177      * judge the file's format is yaml or not.
178      *
179      * @param file file to judge
180      * @return boolean
181      */
182     public static boolean isYamlFile(File file) {
183         return !file.isDirectory() && file.getName().indexOf(".yaml") != -1;
184     }
185
186     /**
187      * remove the csar suffix.
188      *
189      * @param csarName package name
190      * @return String
191      */
192     public static String removeCsarSuffix(String csarName) {
193         return csarName.replaceAll(CSAR_EXTENSION, "");
194     }
195
196     /**
197      * add the csar fuffix.
198      *
199      * @param csarName package name
200      * @return String
201      */
202     public static String addCsarSuffix(String csarName) {
203         if(csarName.indexOf(CSAR_EXTENSION) == -1) {
204             return csarName + CSAR_EXTENSION;
205         }
206         return csarName;
207     }
208
209     /**
210      * process file name.
211      *
212      * @param fileName file's name
213      * @return String
214      */
215     public static String processFileName(String fileName) {
216         int index = fileName.indexOf(".zip");
217         if(index == -1) {
218             return fileName;
219         }
220
221         return addCsarSuffix(fileName.replaceAll(".zip", ""));
222     }
223
224     /**
225      * exchange object to string.
226      *
227      * @param obj object
228      * @return String
229      */
230     public static String objectToString(Object obj) {
231         if(obj == null) {
232             return "";
233         }
234         Gson gson = new Gson();
235
236         return gson.toJson(obj);
237     }
238
239     public static String generateId() {
240         return UUID.randomUUID().toString();
241     }
242
243     /**
244      * get the size format according file size.
245      *
246      * @param fileSize file size
247      * @return size format
248      */
249     public static String getFormatFileSize(long fileSize) {
250         long kb = 1024;
251         long mb = kb * 1024;
252         long gb = mb * 1024;
253
254         if(fileSize >= gb) {
255             return String.format("%.1f GB", (float)fileSize / gb);
256         } else if(fileSize >= mb) {
257             float fi = (float)fileSize / mb;
258             return String.format(fi > 100 ? "%.0f MB" : "%.1f MB", fi);
259         } else if(fileSize >= kb) {
260             float fi = (float)fileSize / kb;
261             return String.format(fi > 100 ? "%.0f KB" : "%.1f KB", fi);
262         } else {
263             return String.format("%d B", fileSize);
264         }
265     }
266
267     /**
268      * get gson from json.
269      * 
270      * @param jsonString json string
271      * @param templateClass template class
272      * @return Template
273      */
274     public static <T> T fromJson(String jsonString, Class<T> templateClass) {
275         Gson gson = new Gson();
276         return gson.fromJson(jsonString, templateClass);
277     }
278
279 }