11c50a96d486b468373b4aa3d3c623db24c3b1af
[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.model.common.TemplateUtils;
22 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.File;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 public abstract class AbstractModelParser {
32   private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
33
34   public abstract String parse(String packageId, String fileLocation)
35       throws CatalogResourceException;
36   
37   public String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
38     String destPath = org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerAbsolutePath()
39         + toTempFilePath(fileLocation);
40     if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(
41         fileLocation, destPath, true)) {
42       throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
43     }
44     
45     logger.info("destPath = " + destPath);
46     return destPath;
47   }
48   
49   public String getUrlOnHttpServer(String path) {
50     return Config.getConfigration().getHttpServerAddr() + "/" + path;
51   }
52   
53   protected String toTempFilePath(String fileLocation) {
54     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
55   }
56   
57   protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
58     if (isNsType(substitutionType)) {
59       return EnumTemplateType.NS;
60     }
61
62     if (isVnfType(substitutionType)) {
63       return EnumTemplateType.VNF;
64     }
65
66     return getTemplateTypeFromNodeTemplates(ntList);
67   }
68   
69   private boolean isVnfType(String type) {
70     if (ToolUtil.isTrimedEmptyString(type)) {
71       return false;
72     }
73     return type.toUpperCase().endsWith(".VNF") || type.toUpperCase().contains(".VNF.");
74   }
75
76   private boolean isNsType(String type) {
77     if (ToolUtil.isTrimedEmptyString(type)) {
78       return false;
79     }
80     return type.toUpperCase().endsWith(".NS") || type.toUpperCase().contains(".NS.");
81   }
82   
83   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
84     for (NodeTemplate nt : ntList) {
85       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
86         return EnumTemplateType.NS;
87       }
88     }
89
90     return EnumTemplateType.VNF;
91   }
92   
93   private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
94   
95   protected String parseServiceTemplateFileName(String packageId, String fileLocation)
96       throws CatalogResourceException {
97     return File.separator + parseToscaMeta(fileLocation).get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
98   }
99   
100   private static final String TOSCA_META_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
101   protected Map<String, String> parseToscaMeta(String zipLocation) throws CatalogResourceException {
102     Map<String, String> toscaMeta = new HashMap<>();
103     String[] lines = TemplateUtils.readFromZipFile(zipLocation, TOSCA_META_FILE_NAME);
104
105     for (String line : lines) {
106       String[] tmps;
107       if (line.indexOf(":") > 0) {
108         tmps = line.split(":");
109         toscaMeta.put(tmps[0].trim(), tmps[1].trim());
110       }
111     }
112
113     return toscaMeta;
114   }
115
116   
117
118 }