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.model.common.TemplateDataHelper;
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.OutputParameter;
25 import org.openo.commontosca.catalog.model.entity.RelationShip;
26 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
27 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
28 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
29 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
30 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
31 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
32 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
33 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
39 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
40 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 import java.util.ArrayList;
46 import java.util.List;
48 public class ToscaYamlModelParser extends AbstractModelParser {
49 private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
52 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
53 logger.info("tosca-yaml-parser parse begin.");
54 ParseYamlResult result = getParseYamlResult(fileLocation);
57 ServiceTemplate st = parseServiceTemplate(
58 packageId, result, parseServiceTemplateFileName(packageId, fileLocation));
60 ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
61 st.setOperations(operations);
63 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
64 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
66 TemplateManager.getInstance().addServiceTemplate(
67 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
70 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
72 TemplateManager.getInstance()
73 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
76 return st.getServiceTemplateId();
79 private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
80 String destPath = copyTemporaryFile2HttpServer(fileLocation);
82 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
83 return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
85 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
86 (new File(destPath)).delete();
91 private ParseYamlRequestParemeter comboRequest(String fileLocation) {
92 ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
93 request.setPath(fileLocation);
97 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
98 ParseYamlResult result) {
99 String type = getSubstitutionType(result);
100 if (ToolUtil.isTrimedEmptyString(type)) {
104 org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
105 .TopologyTemplate.SubstitutionMapping stm =
106 result.getTopologyTemplate().getSubstitutionMappings();
107 return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
108 stm.getCapabilityList());
111 private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
112 String stDownloadUri) {
113 ServiceTemplate st = new ServiceTemplate();
115 st.setServiceTemplateId(ToolUtil.generateId());
116 st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
117 st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
118 st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
119 st.setCsarid(packageId);
120 st.setDownloadUri(stDownloadUri);
121 st.setInputs(parseInputs(result));
122 st.setOutputs(parseOutputs(result));
126 private InputParameter[] parseInputs(ParseYamlResult result) {
127 List<Input> inputList = result.getTopologyTemplate().getInputs();
128 if (inputList == null) {
129 return new InputParameter[0];
131 List<InputParameter> retList = new ArrayList<InputParameter>();
132 for (Input input : inputList) {
133 retList.add(new InputParameter(input.getName(), input.getType(),
134 input.getDescription(), input.getDefault(), input.isRequired()));
136 return retList.toArray(new InputParameter[0]);
139 private OutputParameter[] parseOutputs(ParseYamlResult result) {
140 List<Output> outputList = result.getTopologyTemplate().getOutputs();
141 if (outputList == null || outputList.isEmpty()) {
142 return new OutputParameter[0];
144 List<OutputParameter> retList = new ArrayList<OutputParameter>();
145 for (Output output : outputList) {
147 .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
149 return retList.toArray(new OutputParameter[0]);
152 private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
153 throws CatalogResourceException {
154 if (planList == null || planList.isEmpty()) {
155 return new ServiceTemplateOperation[0];
158 List<ServiceTemplateOperation> opList = new ArrayList<>();
159 for (Plan plan : planList) {
160 ServiceTemplateOperation op = new ServiceTemplateOperation();
161 op.setName(plan.getName());
162 op.setDescription(plan.getDescription());
163 checkPlanLanguage(plan.getPlanLanguage());
164 DeployPackageResponse response =
165 Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference());
166 op.setPackageName(parsePackageName(response));
167 op.setProcessId(response.getProcessId());
168 op.setInputs(parsePlanInputs(plan.getInputList()));
173 return opList.toArray(new ServiceTemplateOperation[0]);
176 private String parsePackageName(DeployPackageResponse response) {
177 String packageName = response.getPackageName();
178 if (packageName != null && packageName.indexOf("-") > 0) {
179 packageName = packageName.substring(0, packageName.lastIndexOf("-"));
184 private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
185 if (planLanguage == null || planLanguage.isEmpty()) {
186 throw new CatalogResourceException("Plan Language is empty.");
188 if (planLanguage.equalsIgnoreCase("bpel")) {
191 if (planLanguage.equalsIgnoreCase("bpmn")) {
194 if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
197 throw new CatalogResourceException(
198 "Plan Language is not supported. Language = " + planLanguage);
201 private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
202 if (inputList == null || inputList.isEmpty()) {
203 return new InputParameter[0];
206 List<InputParameter> retList = new ArrayList<>();
207 for (PlanInput input : inputList) {
208 retList.add(new InputParameter(input.getName(), input.getType(),
209 input.getDescription(), input.getDefault(), input.isRequired()));
211 return retList.toArray(new InputParameter[0]);
214 private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
215 ParseYamlResult result) {
216 List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
217 result.getTopologyTemplate().getNodeTemplates();
218 if (nodetemplateList == null) {
222 List<NodeTemplate> retList = new ArrayList<>();
223 for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
224 NodeTemplate ret = new NodeTemplate();
225 ret.setId(nodeTemplate.getName());
226 ret.setName(nodeTemplate.getName());
227 ret.setType(nodeTemplate.getNodeType());
228 ret.setProperties(nodeTemplate.getPropertyList());
229 List<RelationShip> relationShipList =
230 parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
231 ret.setRelationShips(relationShipList);
240 private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
241 List<RelationShip> retList = new ArrayList<>();
243 if (relationshipList == null) {
247 for (Relationship relationship : relationshipList) {
248 RelationShip ret = new RelationShip();
249 ret.setSourceNodeId(relationship.getSourceNodeName());
250 ret.setSourceNodeName(relationship.getSourceNodeName());
251 ret.setTargetNodeId(relationship.getTargetNodeName());
252 ret.setTargetNodeName(relationship.getTargetNodeName());
253 ret.setType(relationship.getType());
260 private String getSubstitutionType(ParseYamlResult result) {
261 if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
264 return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();