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