e1e89d193033d7c8bd1d4d65ada37beae73d9ca3
[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 org.openo.commontosca.catalog.db.exception.CatalogResourceException;
19 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Plan;
20 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.ServiceTemplate;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.esotericsoftware.yamlbeans.YamlConfig;
25 import com.esotericsoftware.yamlbeans.YamlException;
26 import com.esotericsoftware.yamlbeans.YamlReader;
27
28 import java.io.BufferedInputStream;
29 import java.io.BufferedReader;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipFile;
38 import java.util.zip.ZipInputStream;
39
40 public class TemplateUtils {
41   private static final Logger logger = LoggerFactory.getLogger(TemplateUtils.class);
42   
43   public static Map<String, Plan> loadPlan(String yamlString) throws CatalogResourceException {
44     ServiceTemplate st = loadServiceTemplate(yamlString);
45     return st.getPlans();
46   }
47   
48   /**
49    * @param yamlString
50    * @return
51    * @throws CatalogResourceException
52    */
53   public static ServiceTemplate loadServiceTemplate(String yamlString) throws CatalogResourceException {
54     if (yamlString == null || yamlString.isEmpty()) {
55       return new ServiceTemplate();
56     }
57     final YamlReader reader = new YamlReader(yamlString);
58     adjustConfig(reader.getConfig());
59     try {
60         return reader.read(ServiceTemplate.class);
61     } catch (final YamlException e) {
62         throw new CatalogResourceException("Load plan information failed.", e);
63     } finally {
64       if (reader != null) {
65         try {
66             reader.close();
67         } catch (IOException e) {
68         }
69       }
70     }
71 }
72   
73   
74   /**
75    * @param config
76    */
77   private static void adjustConfig(YamlConfig config) {
78     config.setPropertyElementType(ServiceTemplate.class, "plans", Plan.class);
79   }
80
81
82   /**
83    * @param zipFileName
84    * @param zipEntryName
85    * @return
86    * @throws CatalogResourceException
87    */
88   public static String readStringFromZipFile(String zipFileName, String zipEntryName) throws CatalogResourceException {
89     String[] lines = readFromZipFile(zipFileName, zipEntryName);
90     StringBuffer sb = new StringBuffer();
91     for (String line : lines) {
92       sb.append(line).append(System.lineSeparator());
93     }
94     return sb.toString();
95   }
96   
97   /**
98    * @param zipFileName
99    * @param zipEntryName
100    * @return
101    * @throws CatalogResourceException
102    */
103   @SuppressWarnings("resource")
104   public static String[] readFromZipFile(String zipFileName, String zipEntryName)
105       throws CatalogResourceException {
106     ZipInputStream zipIns = null;
107     BufferedReader zipEntryBr = null;
108     try {
109       ZipFile zipFile = new ZipFile(zipFileName);
110       
111       zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
112       ZipEntry zipEntry;
113       while ((zipEntry = zipIns.getNextEntry()) != null) {
114         if (zipEntryName.equals(zipEntry.getName())
115             || (zipEntryName.replaceAll("/", "\\\\")).equals(zipEntry.getName())) {
116           zipEntryBr = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
117           List<String> lineList = new ArrayList<>();
118           String line;
119           while ((line = zipEntryBr.readLine()) != null) {
120             lineList.add(line);
121           }
122           
123           return lineList.toArray(new String[0]);
124         }
125       }
126     } catch (IOException e) {
127       throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
128     } finally {
129       closeStreamAndReader(zipIns, zipEntryBr);
130     }
131     return new String[0];
132   }
133   
134   private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
135     if (br != null) {
136       try {
137         br.close();
138       } catch (IOException e1) {
139         logger.error("Buffered reader close failed !");
140       }
141     }
142     if (zin != null) {
143       try {
144         zin.closeEntry();
145       } catch (IOException e2) {
146         logger.error("Zip inputStream close failed !");
147       }
148     }
149   }
150 }