Update get operation status part
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / common / util / CommonUtil.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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.driver.vnfm.svnfm.common.util;
18
19 import java.io.BufferedInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public final class CommonUtil {
31         private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
32         
33         public static String getJsonStrFromFile(String filePath) throws IOException {
34         String fileName = getAppRoot() + filePath;
35         String fileContent = getJsonStrFromFilePath(fileName);
36                 return fileContent;
37         }
38
39         public static String getJsonStrFromFilePath(String fileName) throws IOException {
40                 InputStream ins = null;
41         BufferedInputStream bins = null;
42         String fileContent = "";
43
44         try {
45             ins = new FileInputStream(fileName);
46             bins = new BufferedInputStream(ins);
47
48             byte[] contentByte = new byte[ins.available()];
49             int num = bins.read(contentByte);
50
51             if(num > 0) {
52                 fileContent = new String(contentByte);
53             }
54         } catch(FileNotFoundException e) {
55                 logger.error(fileName + " is not found!", e);
56         } finally {
57             if(ins != null) {
58                 ins.close();
59             }
60             if(bins != null) {
61                 bins.close();
62             }
63         }
64                 return fileContent;
65         }
66         
67         public static String getAppRoot() {
68         String appRoot = System.getProperty("catalina.base");
69         if(appRoot != null) {
70             appRoot = getCanonicalPath(appRoot);
71         }
72         return appRoot;
73     }
74
75     private static String getCanonicalPath(final String inPath) {
76         String path = null;
77         try {
78             if(inPath != null) {
79                 final File file = new File(inPath);
80                 path = file.getCanonicalPath();
81             }
82         } catch(final IOException e) {
83             logger.error("file.getCanonicalPath() IOException:", e);
84         }
85         return path;
86     }
87     
88     public static byte[] getBytes(String filePath){  
89         byte[] buffer = null;  
90         try {  
91             File file = new File(filePath);  
92             FileInputStream fis = new FileInputStream(file);  
93             ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
94             byte[] b = new byte[1000];  
95             int n;  
96             while ((n = fis.read(b)) != -1) {  
97                 bos.write(b, 0, n);  
98             }  
99             fis.close();  
100             bos.close();  
101             buffer = bos.toByteArray();  
102         } catch (FileNotFoundException e) {  
103                 logger.error("file " + filePath + " is not found.", e);
104         } catch (IOException e) {  
105                 logger.error("file " + filePath + " IOException.", e); 
106         }  
107         return buffer;  
108     }  
109  
110 }