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.yaml.zte;
18 import org.openo.commontosca.catalog.common.ToolUtil;
19 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
20 import org.openo.commontosca.catalog.db.resource.TemplateManager;
21 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
22 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
23 import org.openo.commontosca.catalog.model.entity.InputParameter;
24 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
25 import org.openo.commontosca.catalog.model.entity.OutputParameter;
26 import org.openo.commontosca.catalog.model.entity.RelationShip;
27 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
28 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
29 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
30 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
31 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
32 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
33 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
39 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
40 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
41 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 import java.util.ArrayList;
47 import java.util.List;
49 public class ToscaYamlModelParser extends AbstractModelParser {
50 private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
53 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
54 logger.info("tosca-yaml-parser parse begin.");
55 ParseYamlResult result = getParseYamlResult(fileLocation);
58 CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
59 ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
61 ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
62 st.setOperations(operations);
64 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
65 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
67 TemplateManager.getInstance().addServiceTemplate(
68 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
71 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
73 TemplateManager.getInstance()
74 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
77 return st.getServiceTemplateId();
80 private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
81 String destPath = copyTemporaryFile2HttpServer(fileLocation);
83 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
84 return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
86 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
87 (new File(destPath)).delete();
92 private ParseYamlRequestParemeter comboRequest(String fileLocation) {
93 ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
94 request.setPath(fileLocation);
98 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
99 ParseYamlResult result) {
100 String type = getSubstitutionType(result);
101 if (ToolUtil.isTrimedEmptyString(type)) {
105 org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
106 .TopologyTemplate.SubstitutionMapping stm =
107 result.getTopologyTemplate().getSubstitutionMappings();
108 return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
109 stm.getCapabilityList());
112 private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
113 String stDownloadUri) {
114 ServiceTemplate st = new ServiceTemplate();
116 st.setServiceTemplateId(ToolUtil.generateId());
117 st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
118 st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
119 st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
120 st.setCsarid(packageId);
121 st.setDownloadUri(stDownloadUri);
122 st.setInputs(parseInputs(result));
123 st.setOutputs(parseOutputs(result));
127 private InputParameter[] parseInputs(ParseYamlResult result) {
128 List<Input> inputList = result.getTopologyTemplate().getInputs();
129 if (inputList == null) {
130 return new InputParameter[0];
132 List<InputParameter> retList = new ArrayList<InputParameter>();
133 for (Input input : inputList) {
134 retList.add(new InputParameter(input.getName(), input.getType(),
135 input.getDescription(), input.getDefault(), input.isRequired()));
137 return retList.toArray(new InputParameter[0]);
140 private OutputParameter[] parseOutputs(ParseYamlResult result) {
141 List<Output> outputList = result.getTopologyTemplate().getOutputs();
142 if (outputList == null || outputList.isEmpty()) {
143 return new OutputParameter[0];
145 List<OutputParameter> retList = new ArrayList<OutputParameter>();
146 for (Output output : outputList) {
148 .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
150 return retList.toArray(new OutputParameter[0]);
153 private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
154 throws CatalogResourceException {
155 if (planList == null || planList.isEmpty()) {
156 return new ServiceTemplateOperation[0];
159 List<ServiceTemplateOperation> opList = new ArrayList<>();
160 for (Plan plan : planList) {
161 ServiceTemplateOperation op = new ServiceTemplateOperation();
162 op.setName(plan.getName());
163 op.setDescription(plan.getDescription());
164 checkPlanLanguage(plan.getPlanLanguage());
165 DeployPackageResponse response =
166 Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference());
167 op.setPackageName(parsePackageName(response));
168 op.setProcessId(response.getProcessId());
169 op.setInputs(parsePlanInputs(plan.getInputList()));
174 return opList.toArray(new ServiceTemplateOperation[0]);
177 private String parsePackageName(DeployPackageResponse response) {
178 String packageName = response.getPackageName();
179 if (packageName != null && packageName.indexOf("-") > 0) {
180 packageName = packageName.substring(0, packageName.lastIndexOf("-"));
185 private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
186 if (planLanguage == null || planLanguage.isEmpty()) {
187 throw new CatalogResourceException("Plan Language is empty.");
189 if (planLanguage.equalsIgnoreCase("bpel")) {
192 if (planLanguage.equalsIgnoreCase("bpmn")) {
195 if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
198 throw new CatalogResourceException(
199 "Plan Language is not supported. Language = " + planLanguage);
202 private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
203 if (inputList == null || inputList.isEmpty()) {
204 return new InputParameter[0];
207 List<InputParameter> retList = new ArrayList<>();
208 for (PlanInput input : inputList) {
209 retList.add(new InputParameter(input.getName(), input.getType(),
210 input.getDescription(), input.getDefault(), input.isRequired()));
212 return retList.toArray(new InputParameter[0]);
215 private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
216 ParseYamlResult result) {
217 List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
218 result.getTopologyTemplate().getNodeTemplates();
219 if (nodetemplateList == null) {
223 List<NodeTemplate> retList = new ArrayList<>();
224 for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
225 NodeTemplate ret = new NodeTemplate();
226 ret.setId(nodeTemplate.getName());
227 ret.setName(nodeTemplate.getName());
228 ret.setType(nodeTemplate.getNodeType());
229 ret.setProperties(nodeTemplate.getPropertyList());
230 List<RelationShip> relationShipList =
231 parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
232 ret.setRelationShips(relationShipList);
241 private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
242 List<RelationShip> retList = new ArrayList<>();
244 if (relationshipList == null) {
248 for (Relationship relationship : relationshipList) {
249 RelationShip ret = new RelationShip();
250 ret.setSourceNodeId(relationship.getSourceNodeName());
251 ret.setSourceNodeName(relationship.getSourceNodeName());
252 ret.setTargetNodeId(relationship.getTargetNodeName());
253 ret.setTargetNodeName(relationship.getTargetNodeName());
254 ret.setType(relationship.getType());
261 private String getSubstitutionType(ParseYamlResult result) {
262 if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
265 return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();