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.EnumDataType;
25 import org.openo.commontosca.catalog.model.entity.InputParameter;
26 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
27 import org.openo.commontosca.catalog.model.entity.OutputParameter;
28 import org.openo.commontosca.catalog.model.entity.RelationShip;
29 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
30 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
31 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
32 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
33 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
39 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
40 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
41 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
42 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
43 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 import java.util.ArrayList;
49 import java.util.List;
51 public class ToscaYamlModelParser extends AbstractModelParser {
52 private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
55 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
56 logger.info("tosca-yaml-parser parse begin.");
57 ParseYamlResult result = getParseYamlResult(fileLocation);
60 CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
61 ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
63 ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
64 st.setOperations(operations);
66 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
67 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
69 TemplateManager.getInstance().addServiceTemplate(
70 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
73 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
75 TemplateManager.getInstance()
76 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
79 return st.getServiceTemplateId();
82 private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
83 String destPath = copyTemporaryFile2HttpServer(fileLocation);
85 String url = getUrl(toTempFileLocalPath(fileLocation));
86 return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
88 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
89 (new File(destPath)).delete();
94 private ParseYamlRequestParemeter comboRequest(String fileLocation) {
95 ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
96 request.setPath(fileLocation);
100 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
101 ParseYamlResult result) {
102 String type = getSubstitutionType(result);
103 if (ToolUtil.isTrimedEmptyString(type)) {
107 org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
108 .TopologyTemplate.SubstitutionMapping stm =
109 result.getTopologyTemplate().getSubstitutionMappings();
110 return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
111 stm.getCapabilityList());
114 private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
115 String stDownloadUri) {
116 ServiceTemplate st = new ServiceTemplate();
118 st.setServiceTemplateId(ToolUtil.generateId());
119 st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
120 st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
121 st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
122 st.setCsarid(packageId);
123 st.setDownloadUri(stDownloadUri);
124 st.setInputs(parseInputs(result));
125 st.setOutputs(parseOutputs(result));
129 private InputParameter[] parseInputs(ParseYamlResult result) {
130 List<Input> inputList = result.getTopologyTemplate().getInputs();
131 if (inputList == null) {
132 return new InputParameter[0];
134 List<InputParameter> retList = new ArrayList<InputParameter>();
135 for (Input input : inputList) {
136 retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()),
137 input.getDescription(), input.getDefault(), input.isRequired()));
139 return retList.toArray(new InputParameter[0]);
142 private OutputParameter[] parseOutputs(ParseYamlResult result) {
143 List<Output> outputList = result.getTopologyTemplate().getOutputs();
144 if (outputList == null || outputList.isEmpty()) {
145 return new OutputParameter[0];
147 List<OutputParameter> retList = new ArrayList<OutputParameter>();
148 for (Output output : outputList) {
150 .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
152 return retList.toArray(new OutputParameter[0]);
155 private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
156 throws CatalogResourceException {
157 if (planList == null || planList.isEmpty()) {
158 return new ServiceTemplateOperation[0];
161 List<ServiceTemplateOperation> opList = new ArrayList<>();
162 for (Plan plan : planList) {
163 ServiceTemplateOperation op = new ServiceTemplateOperation();
164 op.setName(plan.getName());
165 op.setDescription(plan.getDescription());
166 checkPlanLanguage(plan.getPlanLanguage());
167 DeployPackageResponse response =
168 Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference());
169 op.setPackageName(parsePackageName(response));
170 op.setProcessId(response.getProcessId());
171 op.setInputs(parsePlanInputs(plan.getInputList()));
176 return opList.toArray(new ServiceTemplateOperation[0]);
179 private String parsePackageName(DeployPackageResponse response) {
180 String packageName = response.getPackageName();
181 if (packageName != null && packageName.indexOf("-") > 0) {
182 packageName = packageName.substring(0, packageName.lastIndexOf("-"));
187 private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
188 if (planLanguage == null || planLanguage.isEmpty()) {
189 throw new CatalogResourceException("Plan Language is empty.");
191 if (planLanguage.equalsIgnoreCase("bpel")) {
194 if (planLanguage.equalsIgnoreCase("bpmn")) {
197 if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
200 throw new CatalogResourceException(
201 "Plan Language is not supported. Language = " + planLanguage);
204 private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
205 if (inputList == null || inputList.isEmpty()) {
206 return new InputParameter[0];
209 List<InputParameter> retList = new ArrayList<>();
210 for (PlanInput input : inputList) {
211 retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()),
212 input.getDescription(), input.getDefault(), input.isRequired()));
214 return retList.toArray(new InputParameter[0]);
217 private EnumDataType getEnumDataType(String type) {
218 if (EnumDataType.INTEGER.toString().equalsIgnoreCase(type)) {
219 return EnumDataType.INTEGER;
222 if (EnumDataType.FLOAT.toString().equalsIgnoreCase(type)) {
223 return EnumDataType.FLOAT;
226 if (EnumDataType.BOOLEAN.toString().equalsIgnoreCase(type)) {
227 return EnumDataType.BOOLEAN;
230 return EnumDataType.STRING;
233 private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
234 ParseYamlResult result) {
235 List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
236 result.getTopologyTemplate().getNodeTemplates();
237 if (nodetemplateList == null) {
241 List<NodeTemplate> retList = new ArrayList<>();
242 for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
243 NodeTemplate ret = new NodeTemplate();
244 ret.setId(nodeTemplate.getName());
245 ret.setName(nodeTemplate.getName());
246 ret.setType(nodeTemplate.getNodeType());
247 ret.setProperties(nodeTemplate.getPropertyList());
248 List<RelationShip> relationShipList =
249 parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
250 ret.setRelationShips(relationShipList);
259 private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
260 List<RelationShip> retList = new ArrayList<>();
262 if (relationshipList == null) {
266 for (Relationship relationship : relationshipList) {
267 RelationShip ret = new RelationShip();
268 ret.setSourceNodeId(relationship.getSourceNodeName());
269 ret.setSourceNodeName(relationship.getSourceNodeName());
270 ret.setTargetNodeId(relationship.getTargetNodeName());
271 ret.setTargetNodeName(relationship.getTargetNodeName());
272 ret.setType(relationship.getType());
279 private String getSubstitutionType(ParseYamlResult result) {
280 if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
283 return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();