ffb4a4dc3ee0947127bdd1cb267450b9d937db88
[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.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
28 import java.util.zip.ZipInputStream;
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   @SuppressWarnings("resource")
106   public static String[] readFromZipFile(String zipFileName, String zipEntryName)
107       throws CatalogResourceException {
108     ZipInputStream zipIns = null;
109     BufferedReader zipEntryBr = null;
110     try {
111       ZipFile zipFile = new ZipFile(zipFileName);
112       
113       zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
114       ZipEntry zipEntry;
115       while ((zipEntry = zipIns.getNextEntry()) != null) {
116         if (zipEntryName.equals(zipEntry.getName())
117             || (zipEntryName.replaceAll("/", "\\\\")).equals(zipEntry.getName())) {
118           zipEntryBr = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
119           List<String> lineList = new ArrayList<>();
120           String line;
121           while ((line = zipEntryBr.readLine()) != null) {
122             lineList.add(line);
123           }
124           
125           return lineList.toArray(new String[0]);
126         }
127       }
128     } catch (IOException e) {
129       throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
130     } finally {
131       closeStreamAndReader(zipIns, zipEntryBr);
132     }
133     return new String[0];
134   }
135   
136   private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
137     if (br != null) {
138       try {
139         br.close();
140       } catch (IOException e1) {
141         logger.error("Buffered reader close failed !");
142       }
143     }
144     if (zin != null) {
145       try {
146         zin.closeEntry();
147       } catch (IOException e2) {
148         logger.error("Zip inputStream close failed !");
149       }
150     }
151   }
152 }