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.yaml.zte;
19 import org.openo.commontosca.catalog.common.ToolUtil;
20 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
21 import org.openo.commontosca.catalog.db.resource.TemplateManager;
22 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
23 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
24 import org.openo.commontosca.catalog.model.entity.InputParameter;
25 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
26 import org.openo.commontosca.catalog.model.entity.OutputParameter;
27 import org.openo.commontosca.catalog.model.entity.RelationShip;
28 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
29 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
30 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
31 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
32 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
33 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
39 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
40 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
41 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
42 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 import java.util.ArrayList;
48 import java.util.List;
50 public class ToscaYamlModelParser extends AbstractModelParser {
51 private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
54 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
55 logger.info("tosca-yaml-parser parse begin.");
56 ParseYamlResult result = getParseYamlResult(fileLocation);
59 CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
60 ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
62 ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
63 st.setOperations(operations);
65 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
66 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
68 TemplateManager.getInstance().addServiceTemplate(
69 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
72 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
74 TemplateManager.getInstance()
75 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
78 return st.getServiceTemplateId();
81 private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
82 String destPath = copyTemporaryFile2HttpServer(fileLocation);
84 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
85 return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
87 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
88 (new File(destPath)).delete();
93 private ParseYamlRequestParemeter comboRequest(String fileLocation) {
94 ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
95 request.setPath(fileLocation);
99 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
100 ParseYamlResult result) {
101 String type = getSubstitutionType(result);
102 if (ToolUtil.isTrimedEmptyString(type)) {
106 org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
107 .TopologyTemplate.SubstitutionMapping stm =
108 result.getTopologyTemplate().getSubstitutionMappings();
109 return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
110 stm.getCapabilityList());
113 private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
114 String stDownloadUri) {
115 ServiceTemplate st = new ServiceTemplate();
117 st.setServiceTemplateId(ToolUtil.generateId());
118 st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
119 st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
120 st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
121 st.setCsarid(packageId);
122 st.setDownloadUri(stDownloadUri);
123 st.setInputs(parseInputs(result));
124 st.setOutputs(parseOutputs(result));
128 private InputParameter[] parseInputs(ParseYamlResult result) {
129 List<Input> inputList = result.getTopologyTemplate().getInputs();
130 if (inputList == null) {
131 return new InputParameter[0];
133 List<InputParameter> retList = new ArrayList<InputParameter>();
134 for (Input input : inputList) {
135 retList.add(new InputParameter(input.getName(), input.getType(),
136 input.getDescription(), input.getDefault(), input.isRequired()));
138 return retList.toArray(new InputParameter[0]);
141 private OutputParameter[] parseOutputs(ParseYamlResult result) {
142 List<Output> outputList = result.getTopologyTemplate().getOutputs();
143 if (outputList == null || outputList.isEmpty()) {
144 return new OutputParameter[0];
146 List<OutputParameter> retList = new ArrayList<OutputParameter>();
147 for (Output output : outputList) {
149 .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
151 return retList.toArray(new OutputParameter[0]);
154 private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
155 throws CatalogResourceException {
156 if (planList == null || planList.isEmpty()) {
157 return new ServiceTemplateOperation[0];
160 List<ServiceTemplateOperation> opList = new ArrayList<>();
161 for (Plan plan : planList) {
162 ServiceTemplateOperation op = new ServiceTemplateOperation();
163 op.setName(plan.getName());
164 op.setDescription(plan.getDescription());
165 checkPlanLanguage(plan.getPlanLanguage());
166 DeployPackageResponse response =
167 Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference());
168 op.setPackageName(parsePackageName(response));
169 op.setProcessId(response.getProcessId());
170 op.setInputs(parsePlanInputs(plan.getInputList()));
175 return opList.toArray(new ServiceTemplateOperation[0]);
178 private String parsePackageName(DeployPackageResponse response) {
179 String packageName = response.getPackageName();
180 if (packageName != null && packageName.indexOf("-") > 0) {
181 packageName = packageName.substring(0, packageName.lastIndexOf("-"));
186 private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
187 if (planLanguage == null || planLanguage.isEmpty()) {
188 throw new CatalogResourceException("Plan Language is empty.");
190 if (planLanguage.equalsIgnoreCase("bpel")) {
193 if (planLanguage.equalsIgnoreCase("bpmn")) {
196 if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
199 throw new CatalogResourceException(
200 "Plan Language is not supported. Language = " + planLanguage);
203 private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
204 if (inputList == null || inputList.isEmpty()) {
205 return new InputParameter[0];
208 List<InputParameter> retList = new ArrayList<>();
209 for (PlanInput input : inputList) {
210 retList.add(new InputParameter(input.getName(), input.getType(),
211 input.getDescription(), input.getDefault(), input.isRequired()));
213 return retList.toArray(new InputParameter[0]);
216 private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
217 ParseYamlResult result) {
218 List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
219 result.getTopologyTemplate().getNodeTemplates();
220 if (nodetemplateList == null) {
224 List<NodeTemplate> retList = new ArrayList<>();
225 for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
226 NodeTemplate ret = new NodeTemplate();
227 ret.setId(nodeTemplate.getName());
228 ret.setName(nodeTemplate.getName());
229 ret.setType(nodeTemplate.getNodeType());
230 ret.setProperties(nodeTemplate.getPropertyList());
231 List<RelationShip> relationShipList =
232 parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
233 ret.setRelationShips(relationShipList);
242 private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
243 List<RelationShip> retList = new ArrayList<>();
245 if (relationshipList == null) {
249 for (Relationship relationship : relationshipList) {
250 RelationShip ret = new RelationShip();
251 ret.setSourceNodeId(relationship.getSourceNodeName());
252 ret.setSourceNodeName(relationship.getSourceNodeName());
253 ret.setTargetNodeId(relationship.getTargetNodeName());
254 ret.setTargetNodeName(relationship.getTargetNodeName());
255 ret.setType(relationship.getType());
262 private String getSubstitutionType(ParseYamlResult result) {
263 if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
266 return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();