03352f79ca53af6181c0384744ea7288a2291708
[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.BufferedInputStream;
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
29 import java.util.zip.ZipInputStream;
30
31 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
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   }
81
82
83   /**
84    * @param zipFileName
85    * @param zipEntryName
86    * @return
87    * @throws CatalogResourceException
88    */
89   public static String readStringFromZipFile(String zipFileName, String zipEntryName) throws CatalogResourceException {
90     String[] lines = readFromZipFile(zipFileName, zipEntryName);
91     StringBuffer sb = new StringBuffer();
92     for (String line : lines) {
93       sb.append(line).append(System.lineSeparator());
94     }
95     return sb.toString();
96   }
97   
98   /**
99    * @param zipFileName
100    * @param zipEntryName
101    * @return
102    * @throws CatalogResourceException
103    */
104   @SuppressWarnings("resource")
105   public static String[] readFromZipFile(String zipFileName, String zipEntryName)
106       throws CatalogResourceException {
107     ZipInputStream zipIns = null;
108     BufferedReader zipEntryBr = null;
109     try {
110       ZipFile zipFile = new ZipFile(zipFileName);
111       
112       zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
113       ZipEntry zipEntry;
114       while ((zipEntry = zipIns.getNextEntry()) != null) {
115         if (zipEntryName.equals(zipEntry.getName())
116             || (zipEntryName.replaceAll("/", "\\\\")).equals(zipEntry.getName())) {
117           zipEntryBr = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
118           List<String> lineList = new ArrayList<>();
119           String line;
120           while ((line = zipEntryBr.readLine()) != null) {
121             lineList.add(line);
122           }
123           
124           return lineList.toArray(new String[0]);
125         }
126       }
127     } catch (IOException e) {
128       throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
129     } finally {
130       closeStreamAndReader(zipIns, zipEntryBr);
131     }
132     return new String[0];
133   }
134   
135   private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
136     if (br != null) {
137       try {
138         br.close();
139       } catch (IOException e1) {
140         logger.error("Buffered reader close failed !");
141       }
142     }
143     if (zin != null) {
144       try {
145         zin.closeEntry();
146       } catch (IOException e2) {
147         logger.error("Zip inputStream close failed !");
148       }
149     }
150   }
151 }