2 * Copyright 2016 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.commontosca.catalog.model.common;
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;
24 import com.esotericsoftware.yamlbeans.YamlConfig;
25 import com.esotericsoftware.yamlbeans.YamlException;
26 import com.esotericsoftware.yamlbeans.YamlReader;
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;
36 import java.util.zip.ZipEntry;
37 import java.util.zip.ZipFile;
38 import java.util.zip.ZipInputStream;
40 public class TemplateUtils {
41 private static final Logger logger = LoggerFactory.getLogger(TemplateUtils.class);
43 public static Map<String, Plan> loadPlan(String yamlString) throws CatalogResourceException {
44 ServiceTemplate st = loadServiceTemplate(yamlString);
51 * @throws CatalogResourceException
53 public static ServiceTemplate loadServiceTemplate(String yamlString) throws CatalogResourceException {
54 if (yamlString == null || yamlString.isEmpty()) {
55 return new ServiceTemplate();
57 final YamlReader reader = new YamlReader(yamlString);
58 adjustConfig(reader.getConfig());
60 return reader.read(ServiceTemplate.class);
61 } catch (final YamlException e) {
62 throw new CatalogResourceException("Load plan information failed.", e);
67 } catch (IOException e) {
77 private static void adjustConfig(YamlConfig config) {
78 config.setPropertyElementType(ServiceTemplate.class, "plans", Plan.class);
86 * @throws CatalogResourceException
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());
101 * @throws CatalogResourceException
103 @SuppressWarnings("resource")
104 public static String[] readFromZipFile(String zipFileName, String zipEntryName)
105 throws CatalogResourceException {
106 ZipInputStream zipIns = null;
107 BufferedReader zipEntryBr = null;
109 ZipFile zipFile = new ZipFile(zipFileName);
111 zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
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<>();
119 while ((line = zipEntryBr.readLine()) != null) {
123 return lineList.toArray(new String[0]);
126 } catch (IOException e) {
127 throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
129 closeStreamAndReader(zipIns, zipEntryBr);
131 return new String[0];
134 private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
138 } catch (IOException e1) {
139 logger.error("Buffered reader close failed !");
145 } catch (IOException e2) {
146 logger.error("Zip inputStream close failed !");