4b44a6ce38cde84680c6ba31db6e16b06ca3ed5e
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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
17 package org.openo.commontosca.catalog.model.parser;
18
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;
27
28 import java.io.BufferedInputStream;
29 import java.io.BufferedReader;
30 import java.io.File;
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;
37 import java.util.Map;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipFile;
40 import java.util.zip.ZipInputStream;
41
42 public abstract class AbstractModelParser {
43   private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
44
45   
46   public abstract String parse(String packageId, String fileLocation)
47       throws CatalogResourceException;
48   
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.");
55     }
56     return destPath;
57   }
58   
59   public String getUrlOnHttpServer(String path) {
60     return Config.getConfigration().getHttpServerAddr() + "/" + path;
61   }
62   
63   protected String toTempFilePath(String fileLocation) {
64     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
65   }
66   
67   protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
68     if (isNsType(substitutionType)) {
69       return EnumTemplateType.NS;
70     }
71
72     if (isVnfType(substitutionType)) {
73       return EnumTemplateType.VNF;
74     }
75
76     return getTemplateTypeFromNodeTemplates(ntList);
77   }
78   
79   private boolean isVnfType(String type) {
80     if (ToolUtil.isTrimedEmptyString(type)) {
81       return false;
82     }
83     return type.toUpperCase().contains(".VNF");
84   }
85
86   private boolean isNsType(String type) {
87     if (ToolUtil.isTrimedEmptyString(type)) {
88       return false;
89     }
90     return type.toUpperCase().contains(".NS");
91   }
92   
93   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
94     for (NodeTemplate nt : ntList) {
95       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
96         return EnumTemplateType.NS;
97       }
98     }
99
100     return EnumTemplateType.VNF;
101   }
102   
103   private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
104   
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;
112   }
113   
114   @SuppressWarnings("resource")
115   protected Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
116     Map<String, String> toscaMeta = new HashMap<>();
117
118     ZipInputStream zin = null;
119     BufferedReader br = null;
120     try {
121       InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
122       zin = new ZipInputStream(in);
123       ZipEntry ze;
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)));
129           String line;
130           String[] tmps;
131           while ((line = br.readLine()) != null) {
132             if (line.indexOf(":") > 0) {
133               tmps = line.split(":");
134               toscaMeta.put(tmps[0].trim(), tmps[1].trim());
135             }
136           }
137
138           return toscaMeta;
139         }
140       }
141
142     } catch (IOException e1) {
143       throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
144     } finally {
145       closeStreamAndReader(zin, br);
146     }
147
148     return toscaMeta;
149   }
150   
151   private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
152     if (br != null) {
153       try {
154         br.close();
155       } catch (IOException e1) {
156         logger.error("Buffered reader close failed !");
157       }
158     }
159     if (zin != null) {
160       try {
161         zin.closeEntry();
162       } catch (IOException e2) {
163         logger.error("Zip inputStream close failed !");
164       }
165     }
166   }
167
168 }