2 * Copyright 2016 [ZTE] and others.
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.
17 package org.openo.commontosca.catalog.model.parser;
19 import org.openo.commontosca.catalog.common.Config;
20 import org.openo.commontosca.catalog.common.ToolUtil;
21 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
22 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
23 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
24 import org.openo.commontosca.catalog.wrapper.PackageWrapper;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.io.BufferedInputStream;
29 import java.io.BufferedReader;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipFile;
40 import java.util.zip.ZipInputStream;
42 public abstract class AbstractModelParser {
43 private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
46 public abstract String parse(String packageId, String fileLocation)
47 throws CatalogResourceException;
49 public String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
50 String destPath = org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerAbsolutePath()
51 + toTempFilePath(fileLocation);
52 if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(
53 fileLocation, destPath, true)) {
54 throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
59 public String getUrlOnHttpServer(String path) {
60 return Config.getConfigration().getHttpServerAddr() + "/" + path;
63 protected String toTempFilePath(String fileLocation) {
64 return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
67 protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
68 if (isNsType(substitutionType)) {
69 return EnumTemplateType.NS;
72 if (isVnfType(substitutionType)) {
73 return EnumTemplateType.VNF;
76 return getTemplateTypeFromNodeTemplates(ntList);
79 private boolean isVnfType(String type) {
80 if (ToolUtil.isTrimedEmptyString(type)) {
83 return type.toUpperCase().contains(".VNF");
86 private boolean isNsType(String type) {
87 if (ToolUtil.isTrimedEmptyString(type)) {
90 return type.toUpperCase().contains(".NS");
93 private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
94 for (NodeTemplate nt : ntList) {
95 if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
96 return EnumTemplateType.NS;
100 return EnumTemplateType.VNF;
103 private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
105 protected CsarFileUriResponse buildServiceTemplateDownloadUri(String packageId, String fileLocation)
106 throws CatalogResourceException {
107 Map<String, String> toscaMeta = parseToscaMeta(fileLocation);
108 String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
109 CsarFileUriResponse stDownloadUri =
110 PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName);
111 return stDownloadUri;
114 @SuppressWarnings("resource")
115 protected Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
116 Map<String, String> toscaMeta = new HashMap<>();
118 ZipInputStream zin = null;
119 BufferedReader br = null;
121 InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
122 zin = new ZipInputStream(in);
124 while ((ze = zin.getNextEntry()) != null) {
125 if (("TOSCA-Metadata" + File.separator + "TOSCA.meta").equals(ze.getName())
126 || "TOSCA-Metadata/TOSCA.meta".equals(ze.getName())) {
127 ZipFile zf = new ZipFile(fileLocation);
128 br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
131 while ((line = br.readLine()) != null) {
132 if (line.indexOf(":") > 0) {
133 tmps = line.split(":");
134 toscaMeta.put(tmps[0].trim(), tmps[1].trim());
142 } catch (IOException e1) {
143 throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
145 closeStreamAndReader(zin, br);
151 private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
155 } catch (IOException e1) {
156 logger.error("Buffered reader close failed !");
162 } catch (IOException e2) {
163 logger.error("Zip inputStream close failed !");