697ca1ab880dba0ad5e6a040c238f23f365836d3
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 ZTE 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 package org.openo.commontosca.catalog.model.common;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
29
30 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
31 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Input;
32 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Plan;
33 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.ServiceTemplate;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.esotericsoftware.yamlbeans.YamlConfig;
38 import com.esotericsoftware.yamlbeans.YamlException;
39 import com.esotericsoftware.yamlbeans.YamlReader;
40
41 public class TemplateUtils {
42   private static final Logger logger = LoggerFactory.getLogger(TemplateUtils.class);
43   
44   public static Map<String, Plan> loadPlan(String yamlString) throws CatalogResourceException {
45     ServiceTemplate st = loadServiceTemplate(yamlString);
46     return st.getPlans();
47   }
48   
49   /**
50    * @param yamlString
51    * @return
52    * @throws CatalogResourceException
53    */
54   public static ServiceTemplate loadServiceTemplate(String yamlString) throws CatalogResourceException {
55     if (yamlString == null || yamlString.isEmpty()) {
56       return new ServiceTemplate();
57     }
58     final YamlReader reader = new YamlReader(yamlString);
59     adjustConfig(reader.getConfig());
60     try {
61         return reader.read(ServiceTemplate.class);
62     } catch (final YamlException e) {
63         throw new CatalogResourceException("Load plan information failed.", e);
64     } finally {
65       if (reader != null) {
66         try {
67             reader.close();
68         } catch (IOException e) {
69         }
70       }
71     }
72   }
73   
74   
75   /**
76    * @param config
77    */
78   private static void adjustConfig(YamlConfig config) {
79     config.setPropertyElementType(ServiceTemplate.class, "plans", Plan.class);
80     config.setPropertyElementType(Plan.class, "inputs", Input.class);
81   }
82
83
84   /**
85    * @param zipFileName
86    * @param zipEntryName
87    * @return
88    * @throws CatalogResourceException
89    */
90   public static String readStringFromZipFile(String zipFileName, String zipEntryName) throws CatalogResourceException {
91     String[] lines = readFromZipFile(zipFileName, zipEntryName);
92     StringBuffer sb = new StringBuffer();
93     for (String line : lines) {
94       sb.append(line).append(System.lineSeparator());
95     }
96     return sb.toString();
97   }
98   
99   /**
100    * @param zipFileName
101    * @param zipEntryName
102    * @return
103    * @throws CatalogResourceException
104    */
105   public static String[] readFromZipFile(String zipFileName, String zipEntryName)
106       throws CatalogResourceException {
107     ZipFile zf = null;
108     InputStream ins = null;
109     try {
110       zf = new ZipFile(zipFileName);
111       ZipEntry ze = getZipEntryZipFile(zf, zipEntryName);
112       
113       if (ze != null) {
114         ins = zf.getInputStream(ze);
115         return readFromInputStream(ins);
116       }
117     } catch (IOException e) {
118       throw new CatalogResourceException("readFromZipFile failed.", e);
119     } finally {
120       closeInputStream(ins);
121       closeZipFile(zf);
122     }
123     return new String[0];
124   }
125
126   public static ZipEntry getZipEntryZipFile(ZipFile zf, String zipEntryName) {
127     Enumeration<?> zes = zf.entries();
128     while (zes.hasMoreElements()) {
129       ZipEntry ze = (ZipEntry) zes.nextElement();
130       if (zipEntryName.equals(ze.getName())
131           || (zipEntryName.replaceAll("\\\\", "/")).equals(ze.getName())) {
132         return ze;
133       }
134     }
135     
136     return null;
137   }
138
139   /**
140    * @param ins
141    */
142   public static void closeInputStream(InputStream ins) {
143     if (ins != null) {
144       try {
145         ins.close();
146       } catch (IOException e) {
147         logger.warn("closeInputStream failed.", e);
148       }
149     }
150   }
151   
152   /**
153    * @param zf
154    */
155   public static void closeZipFile(ZipFile zf) {
156     if (zf != null) {
157       try {
158         zf.close();
159       } catch (IOException e) {
160         logger.warn("closeZipFile failed.", e);
161       }
162     }
163   }
164
165   private static String[] readFromInputStream(InputStream ins) throws CatalogResourceException {
166     InputStreamReader insReader = new InputStreamReader(ins);
167     BufferedReader reader = new BufferedReader(insReader);
168     
169     List<String> lineList = new ArrayList<>();
170     String line;
171     try {
172       while ((line = reader.readLine()) != null) {
173         lineList.add(line);
174       }
175     } catch (IOException e) {
176       throw new CatalogResourceException("readFromInputStream failed.", e);
177     } finally {
178       closeReader(reader);
179       closeReader(insReader);
180     }
181     
182     return lineList.toArray(new String[0]);
183   }
184   
185   /**
186    * @param reader
187    */
188   private static void closeReader(Reader reader) {
189     if (reader != null) {
190       try {
191         reader.close();
192       } catch (IOException e) {
193         logger.warn("closeReader failed.", e);
194       }
195     }
196   }
197 }