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.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;
 
  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 = 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,
 
  55       throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
 
  60   public String getUrl(String uri) {
 
  62     if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) {
 
  63       url = MsbAddrConfig.getMsbAddress() + uri.substring(1);
 
  65     url = MsbAddrConfig.getMsbAddress() + uri;
 
  66     String urlresult = url.replace("\\", "/");
 
  70   protected String toTempFileLocalPath(String fileLocation) {
 
  71     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
 
  74   protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
 
  75     if (isNsType(substitutionType)) {
 
  76       return EnumTemplateType.NS;
 
  79     if (isVnfType(substitutionType)) {
 
  80       return EnumTemplateType.VNF;
 
  83     return getTemplateTypeFromNodeTemplates(ntList);
 
  86   private boolean isVnfType(String type) {
 
  87     if (ToolUtil.isTrimedEmptyString(type)) {
 
  90     return type.toUpperCase().contains(".VNF");
 
  93   private boolean isNsType(String type) {
 
  94     if (ToolUtil.isTrimedEmptyString(type)) {
 
  97     return type.toUpperCase().contains(".NS");
 
 100   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
 
 101     for (NodeTemplate nt : ntList) {
 
 102       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
 
 103         return EnumTemplateType.NS;
 
 107     return EnumTemplateType.VNF;
 
 110   private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
 
 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;
 
 121   @SuppressWarnings("resource")
 
 122   protected Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
 
 123     Map<String, String> toscaMeta = new HashMap<>();
 
 125     ZipInputStream zin = null;
 
 126     BufferedReader br = null;
 
 128       InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
 
 129       zin = new ZipInputStream(in);
 
 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)));
 
 138           while ((line = br.readLine()) != null) {
 
 139             if (line.indexOf(":") > 0) {
 
 140               tmps = line.split(":");
 
 141               toscaMeta.put(tmps[0].trim(), tmps[1].trim());
 
 149     } catch (IOException e1) {
 
 150       throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
 
 152       closeStreamAndReader(zin, br);
 
 158   private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
 
 162       } catch (IOException e1) {
 
 163         logger.error("Buffered reader close failed !");
 
 169       } catch (IOException e2) {
 
 170         logger.error("Zip inputStream close failed !");