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.parser;
18 import org.openo.commontosca.catalog.common.Config;
19 import org.openo.commontosca.catalog.common.ToolUtil;
20 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
21 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
22 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
23 import org.openo.commontosca.catalog.wrapper.PackageWrapper;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import java.io.BufferedInputStream;
28 import java.io.BufferedReader;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.zip.ZipEntry;
38 import java.util.zip.ZipFile;
39 import java.util.zip.ZipInputStream;
41 public abstract class AbstractModelParser {
42 private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
45 public abstract String parse(String packageId, String fileLocation)
46 throws CatalogResourceException;
48 public String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
49 String destPath = org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerAbsolutePath()
50 + toTempFilePath(fileLocation);
51 if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(
52 fileLocation, destPath, true)) {
53 throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
58 public String getUrlOnHttpServer(String path) {
59 return Config.getConfigration().getHttpServerAddr() + "/" + path;
62 protected String toTempFilePath(String fileLocation) {
63 return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
66 protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
67 if (isNsType(substitutionType)) {
68 return EnumTemplateType.NS;
71 if (isVnfType(substitutionType)) {
72 return EnumTemplateType.VNF;
75 return getTemplateTypeFromNodeTemplates(ntList);
78 private boolean isVnfType(String type) {
79 if (ToolUtil.isTrimedEmptyString(type)) {
82 return type.toUpperCase().contains(".VNF");
85 private boolean isNsType(String type) {
86 if (ToolUtil.isTrimedEmptyString(type)) {
89 return type.toUpperCase().contains(".NS");
92 private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
93 for (NodeTemplate nt : ntList) {
94 if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
95 return EnumTemplateType.NS;
99 return EnumTemplateType.VNF;
102 private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
104 protected CsarFileUriResponse buildServiceTemplateDownloadUri(String packageId, String fileLocation)
105 throws CatalogResourceException {
106 Map<String, String> toscaMeta = parseToscaMeta(fileLocation);
107 String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
108 CsarFileUriResponse stDownloadUri =
109 PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName);
110 return stDownloadUri;
113 @SuppressWarnings("resource")
114 protected Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
115 Map<String, String> toscaMeta = new HashMap<>();
117 ZipInputStream zin = null;
118 BufferedReader br = null;
120 InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
121 zin = new ZipInputStream(in);
123 while ((ze = zin.getNextEntry()) != null) {
124 if (("TOSCA-Metadata" + File.separator + "TOSCA.meta").equals(ze.getName())
125 || "TOSCA-Metadata/TOSCA.meta".equals(ze.getName())) {
126 ZipFile zf = new ZipFile(fileLocation);
127 br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
130 while ((line = br.readLine()) != null) {
131 if (line.indexOf(":") > 0) {
132 tmps = line.split(":");
133 toscaMeta.put(tmps[0].trim(), tmps[1].trim());
141 } catch (IOException e1) {
142 throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
144 closeStreamAndReader(zin, br);
150 private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
154 } catch (IOException e1) {
155 logger.error("Buffered reader close failed !");
161 } catch (IOException e2) {
162 logger.error("Zip inputStream close failed !");