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.MsbAddrConfig;
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.InputParameter;
23 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
24 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
25 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Input;
26 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Plan;
27 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
28 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Map.Entry;
39 public abstract class AbstractModelParser {
40 private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
42 public abstract String parse(String packageId, String fileLocation)
43 throws CatalogResourceException;
45 public String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
46 String destPath = org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerAbsolutePath()
47 + toTempFilePath(fileLocation);
48 if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(
49 fileLocation, destPath, true)) {
50 throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
53 logger.info("destPath = " + destPath);
57 public String getUrlOnHttpServer(String path) {
58 if (MsbAddrConfig.getMsbAddress().endsWith("/")) {
59 return MsbAddrConfig.getMsbAddress() + "files/catalog-http" + path;
61 return MsbAddrConfig.getMsbAddress() + "/files/catalog-http" + path;
65 protected String toTempFilePath(String fileLocation) {
66 return "/temp/" + (new File(fileLocation)).getName();
69 protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
70 if (isNsType(substitutionType)) {
71 return EnumTemplateType.NS;
74 if (isVnfType(substitutionType)) {
75 return EnumTemplateType.VNF;
78 return getTemplateTypeFromNodeTemplates(ntList);
81 private boolean isVnfType(String type) {
82 if (ToolUtil.isTrimedEmptyString(type)) {
85 return type.toUpperCase().endsWith(".VNF") || type.toUpperCase().contains(".VNF.");
88 private boolean isNsType(String type) {
89 if (ToolUtil.isTrimedEmptyString(type)) {
92 return type.toUpperCase().endsWith(".NS") || type.toUpperCase().contains(".NS.");
95 private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
96 for (NodeTemplate nt : ntList) {
97 if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
98 return EnumTemplateType.NS;
102 return EnumTemplateType.VNF;
105 private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
107 protected String parseServiceTemplateFileName(String packageId, String fileLocation)
108 throws CatalogResourceException {
109 return "/" + parseToscaMeta(fileLocation).get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
112 private static final String TOSCA_META_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
113 protected Map<String, String> parseToscaMeta(String zipLocation) throws CatalogResourceException {
114 Map<String, String> toscaMeta = new HashMap<>();
115 String[] lines = TemplateUtils.readFromZipFile(zipLocation, TOSCA_META_FILE_NAME);
117 for (String line : lines) {
119 if (line.indexOf(":") > 0) {
120 tmps = line.split(":");
121 toscaMeta.put(tmps[0].trim(), tmps[1].trim());
129 * @param fileLocation
131 * @throws CatalogResourceException
133 protected ServiceTemplateOperation[] parseOperations(String fileLocation) throws CatalogResourceException {
134 String sPlan = TemplateUtils.readStringFromZipFile(fileLocation, "Definitions/plans.yaml");
135 Map<String, Plan> plans = TemplateUtils.loadPlan(sPlan);
136 return parseAndDeployPlans(plans, fileLocation);
141 * @param fileLocation
143 * @throws CatalogResourceException
145 private ServiceTemplateOperation[] parseAndDeployPlans(Map<String, Plan> plans,
146 String zipFileLocation) throws CatalogResourceException {
147 if (plans == null || plans.isEmpty()) {
148 return new ServiceTemplateOperation[0];
151 List<ServiceTemplateOperation> opList = new ArrayList<>();
152 for (Entry<String, Plan> plan : plans.entrySet()) {
153 ServiceTemplateOperation op = new ServiceTemplateOperation();
154 op.setName(plan.getKey());
155 op.setDescription(plan.getValue().getDescription());
156 checkPlanLanguage(plan.getValue().getPlanLanguage());
157 DeployPackageResponse response =
158 Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getValue().getReference());
159 op.setPackageName(parsePackageName(response));
160 op.setProcessId(response.getProcessId());
161 op.setInputs(parsePlanInputs(plan.getValue().getInputs()));
166 return opList.toArray(new ServiceTemplateOperation[0]);
169 private String parsePackageName(DeployPackageResponse response) {
170 String packageName = response.getPackageName();
171 if (packageName != null && packageName.indexOf("-") > 0) {
172 packageName = packageName.substring(0, packageName.lastIndexOf("-"));
177 private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
178 if (planLanguage == null || planLanguage.isEmpty()) {
179 throw new CatalogResourceException("Plan Language is empty.");
181 if (planLanguage.equalsIgnoreCase("bpel")) {
184 if (planLanguage.equalsIgnoreCase("bpmn")) {
187 if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
190 throw new CatalogResourceException(
191 "Plan Language is not supported. Language = " + planLanguage);
198 private InputParameter[] parsePlanInputs(
199 Map<String, Input> inputs) {
200 if (inputs == null || inputs.isEmpty()) {
201 return new InputParameter[0];
204 List<InputParameter> retList = new ArrayList<>();
205 for (Entry<String, Input> input : inputs.entrySet()) {
206 retList.add(new InputParameter(
208 input.getValue().getType(),
209 input.getValue().getDescription(),
210 input.getValue().getDefault(),
211 input.getValue().isRequired()));
213 return retList.toArray(new InputParameter[0]);
220 public String parserId(Map<String, String> metadata) {
221 if (metadata.containsKey("id")) {
222 return metadata.get("id");
224 return metadata.get("template_name");
231 public String parserServiceTemplateName(Map<String, String> metadata) {
232 if (metadata.containsKey("name")) {
233 return metadata.get("name");
235 return metadata.get("template_name");
242 public String parserServiceTemplateVendor(Map<String, String> metadata) {
243 if (metadata.containsKey("vendor")) {
244 return metadata.get("vendor");
246 return metadata.get("template_author");
253 public String parserServiceTemplateVersion(Map<String, String> metadata) {
254 if (metadata.containsKey("version")) {
255 return metadata.get("version");
257 return metadata.get("template_version");