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.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 return Config.getConfigration().getHttpServerAddr() + "/" + path;
61 protected String toTempFilePath(String fileLocation) {
62 return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
65 protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
66 if (isNsType(substitutionType)) {
67 return EnumTemplateType.NS;
70 if (isVnfType(substitutionType)) {
71 return EnumTemplateType.VNF;
74 return getTemplateTypeFromNodeTemplates(ntList);
77 private boolean isVnfType(String type) {
78 if (ToolUtil.isTrimedEmptyString(type)) {
81 return type.toUpperCase().endsWith(".VNF") || type.toUpperCase().contains(".VNF.");
84 private boolean isNsType(String type) {
85 if (ToolUtil.isTrimedEmptyString(type)) {
88 return type.toUpperCase().endsWith(".NS") || type.toUpperCase().contains(".NS.");
91 private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
92 for (NodeTemplate nt : ntList) {
93 if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
94 return EnumTemplateType.NS;
98 return EnumTemplateType.VNF;
101 private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
103 protected String parseServiceTemplateFileName(String packageId, String fileLocation)
104 throws CatalogResourceException {
105 return File.separator + parseToscaMeta(fileLocation).get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
108 private static final String TOSCA_META_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
109 protected Map<String, String> parseToscaMeta(String zipLocation) throws CatalogResourceException {
110 Map<String, String> toscaMeta = new HashMap<>();
111 String[] lines = TemplateUtils.readFromZipFile(zipLocation, TOSCA_META_FILE_NAME);
113 for (String line : lines) {
115 if (line.indexOf(":") > 0) {
116 tmps = line.split(":");
117 toscaMeta.put(tmps[0].trim(), tmps[1].trim());
125 * @param fileLocation
127 * @throws CatalogResourceException
129 protected ServiceTemplateOperation[] parseOperations(String fileLocation) throws CatalogResourceException {
130 String sPlan = TemplateUtils.readStringFromZipFile(fileLocation, "Definitions/plans.yaml");
131 Map<String, Plan> plans = TemplateUtils.loadPlan(sPlan);
132 return parseAndDeployPlans(plans, fileLocation);
137 * @param fileLocation
139 * @throws CatalogResourceException
141 private ServiceTemplateOperation[] parseAndDeployPlans(Map<String, Plan> plans,
142 String zipFileLocation) throws CatalogResourceException {
143 if (plans == null || plans.isEmpty()) {
144 return new ServiceTemplateOperation[0];
147 List<ServiceTemplateOperation> opList = new ArrayList<>();
148 for (Entry<String, Plan> plan : plans.entrySet()) {
149 ServiceTemplateOperation op = new ServiceTemplateOperation();
150 op.setName(plan.getKey());
151 op.setDescription(plan.getValue().getDescription());
152 checkPlanLanguage(plan.getValue().getPlanLanguage());
153 DeployPackageResponse response =
154 Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getValue().getReference());
155 op.setPackageName(parsePackageName(response));
156 op.setProcessId(response.getProcessId());
157 op.setInputs(parsePlanInputs(plan.getValue().getInputs()));
162 return opList.toArray(new ServiceTemplateOperation[0]);
165 private String parsePackageName(DeployPackageResponse response) {
166 String packageName = response.getPackageName();
167 if (packageName != null && packageName.indexOf("-") > 0) {
168 packageName = packageName.substring(0, packageName.lastIndexOf("-"));
173 private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
174 if (planLanguage == null || planLanguage.isEmpty()) {
175 throw new CatalogResourceException("Plan Language is empty.");
177 if (planLanguage.equalsIgnoreCase("bpel")) {
180 if (planLanguage.equalsIgnoreCase("bpmn")) {
183 if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
186 throw new CatalogResourceException(
187 "Plan Language is not supported. Language = " + planLanguage);
194 private InputParameter[] parsePlanInputs(
195 Map<String, Input> inputs) {
196 if (inputs == null || inputs.isEmpty()) {
197 return new InputParameter[0];
200 List<InputParameter> retList = new ArrayList<>();
201 for (Entry<String, Input> input : inputs.entrySet()) {
202 retList.add(new InputParameter(
204 input.getValue().getType(),
205 input.getValue().getDescription(),
206 input.getValue().getDefault(),
207 input.getValue().isRequired()));
209 return retList.toArray(new InputParameter[0]);