2 * Copyright 2016 ZTE Corporation.
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.
16 package org.openo.commontosca.catalog.model.parser;
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;
27 import java.util.HashMap;
28 import java.util.List;
31 public abstract class AbstractModelParser {
32 private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
34 public abstract String parse(String packageId, String fileLocation)
35 throws CatalogResourceException;
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.");
45 logger.info("destPath = " + destPath);
49 public String getUrlOnHttpServer(String path) {
50 return Config.getConfigration().getHttpServerAddr() + "/" + path;
53 protected String toTempFilePath(String fileLocation) {
54 return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
57 protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
58 if (isNsType(substitutionType)) {
59 return EnumTemplateType.NS;
62 if (isVnfType(substitutionType)) {
63 return EnumTemplateType.VNF;
66 return getTemplateTypeFromNodeTemplates(ntList);
69 private boolean isVnfType(String type) {
70 if (ToolUtil.isTrimedEmptyString(type)) {
73 return type.toUpperCase().endsWith(".VNF") || type.toUpperCase().contains(".VNF.");
76 private boolean isNsType(String type) {
77 if (ToolUtil.isTrimedEmptyString(type)) {
80 return type.toUpperCase().endsWith(".NS") || type.toUpperCase().contains(".NS.");
83 private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
84 for (NodeTemplate nt : ntList) {
85 if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
86 return EnumTemplateType.NS;
90 return EnumTemplateType.VNF;
93 private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
95 protected String parseServiceTemplateFileName(String packageId, String fileLocation)
96 throws CatalogResourceException {
97 return File.separator + parseToscaMeta(fileLocation).get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
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);
105 for (String line : lines) {
107 if (line.indexOf(":") > 0) {
108 tmps = line.split(":");
109 toscaMeta.put(tmps[0].trim(), tmps[1].trim());