4cb59b0862e7d0b4b59ba8e9a7b528959b0acf09
[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.MsbAddrConfig;
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 = Class.class.getClass().getResource("/").getPath()
51         + org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerPath()
52         + toTempFileLocalPath(fileLocation);
53     if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(fileLocation, destPath,
54         true)) {
55       throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
56     }
57     return destPath;
58   }
59   
60   public String getUrl(String uri) {
61     String url = null;
62     if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) {
63       url = MsbAddrConfig.getMsbAddress() + uri.substring(1);
64     }
65     url = MsbAddrConfig.getMsbAddress() + uri;
66     String urlresult = url.replace("\\", "/");
67     return urlresult;
68   }
69   
70   protected String toTempFileLocalPath(String fileLocation) {
71     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
72   }
73   
74   protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
75     if (isNsType(substitutionType)) {
76       return EnumTemplateType.NS;
77     }
78
79     if (isVnfType(substitutionType)) {
80       return EnumTemplateType.VNF;
81     }
82
83     return getTemplateTypeFromNodeTemplates(ntList);
84   }
85   
86   private boolean isVnfType(String type) {
87     if (ToolUtil.isTrimedEmptyString(type)) {
88       return false;
89     }
90     return type.toUpperCase().contains(".VNF");
91   }
92
93   private boolean isNsType(String type) {
94     if (ToolUtil.isTrimedEmptyString(type)) {
95       return false;
96     }
97     return type.toUpperCase().contains(".NS");
98   }
99   
100   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
101     for (NodeTemplate nt : ntList) {
102       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
103         return EnumTemplateType.NS;
104       }
105     }
106
107     return EnumTemplateType.VNF;
108   }
109   
110   private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
111   
112   protected CsarFileUriResponse buildServiceTemplateDownloadUri(String packageId, String fileLocation)
113       throws CatalogResourceException {
114     Map<String, String> toscaMeta = parseToscaMeta(fileLocation);
115     String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
116     CsarFileUriResponse stDownloadUri =
117         PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName);
118     return stDownloadUri;
119   }
120   
121   @SuppressWarnings("resource")
122   protected Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
123     Map<String, String> toscaMeta = new HashMap<>();
124
125     ZipInputStream zin = null;
126     BufferedReader br = null;
127     try {
128       InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
129       zin = new ZipInputStream(in);
130       ZipEntry ze;
131       while ((ze = zin.getNextEntry()) != null) {
132         if (("TOSCA-Metadata" + File.separator + "TOSCA.meta").equals(ze.getName())
133             || "TOSCA-Metadata/TOSCA.meta".equals(ze.getName())) {
134           ZipFile zf = new ZipFile(fileLocation);
135           br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
136           String line;
137           String[] tmps;
138           while ((line = br.readLine()) != null) {
139             if (line.indexOf(":") > 0) {
140               tmps = line.split(":");
141               toscaMeta.put(tmps[0].trim(), tmps[1].trim());
142             }
143           }
144
145           return toscaMeta;
146         }
147       }
148
149     } catch (IOException e1) {
150       throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
151     } finally {
152       closeStreamAndReader(zin, br);
153     }
154
155     return toscaMeta;
156   }
157   
158   private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
159     if (br != null) {
160       try {
161         br.close();
162       } catch (IOException e1) {
163         logger.error("Buffered reader close failed !");
164       }
165     }
166     if (zin != null) {
167       try {
168         zin.closeEntry();
169       } catch (IOException e2) {
170         logger.error("Zip inputStream close failed !");
171       }
172     }
173   }
174
175 }