2465beab2c4f00cb433e9984c49d184b5e7e8962
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.commontosca.catalog.model.parser.yaml.zte;
17
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;
43
44 import java.io.File;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 public class ToscaYamlModelParser extends AbstractModelParser {
49   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
50
51   @Override
52   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
53     logger.info("tosca-yaml-parser parse begin.");
54     ParseYamlResult result = getParseYamlResult(fileLocation);
55     
56     // service template
57     ServiceTemplate st = parseServiceTemplate(
58         packageId, result, parseServiceTemplateFileName(packageId, fileLocation));
59     // workflow
60     ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
61     st.setOperations(operations);
62     // node templates
63     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
64     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
65     // save to db
66     TemplateManager.getInstance().addServiceTemplate(
67         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
68
69     // substitution
70     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
71     if (stm != null) {
72       TemplateManager.getInstance()
73           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
74     }
75
76     return st.getServiceTemplateId();
77   }
78
79   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
80     String destPath = copyTemporaryFile2HttpServer(fileLocation);
81     try {
82       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
83       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
84     } finally {
85       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
86         (new File(destPath)).delete();
87       }
88     }
89   }
90
91   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
92     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
93     request.setPath(fileLocation);
94     return request;
95   }
96
97   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
98       ParseYamlResult result) {
99     String type = getSubstitutionType(result);
100     if (ToolUtil.isTrimedEmptyString(type)) {
101       return null;
102     }
103
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());
109   }
110
111   private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
112       String stDownloadUri) {
113     ServiceTemplate st = new ServiceTemplate();
114
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));
123     return st;
124   }
125
126   private InputParameter[] parseInputs(ParseYamlResult result) {
127     List<Input> inputList = result.getTopologyTemplate().getInputs();
128     if (inputList == null) {
129       return new InputParameter[0];
130     }
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()));
135     }
136     return retList.toArray(new InputParameter[0]);
137   }
138
139   private OutputParameter[] parseOutputs(ParseYamlResult result) {
140     List<Output> outputList = result.getTopologyTemplate().getOutputs();
141     if (outputList == null || outputList.isEmpty()) {
142       return new OutputParameter[0];
143     }
144     List<OutputParameter> retList = new ArrayList<OutputParameter>();
145     for (Output output : outputList) {
146       retList
147           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
148     }
149     return retList.toArray(new OutputParameter[0]);
150   }
151
152   private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
153       throws CatalogResourceException {
154     if (planList == null || planList.isEmpty()) {
155       return new ServiceTemplateOperation[0];
156     }
157
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()));
169
170       opList.add(op);
171
172     }
173     return opList.toArray(new ServiceTemplateOperation[0]);
174   }
175
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("-"));
180     }
181     return packageName;
182   }
183
184   private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
185     if (planLanguage == null || planLanguage.isEmpty()) {
186       throw new CatalogResourceException("Plan Language is empty.");
187     }
188     if (planLanguage.equalsIgnoreCase("bpel")) {
189       return;
190     }
191     if (planLanguage.equalsIgnoreCase("bpmn")) {
192       return;
193     }
194     if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
195       return;
196     }
197     throw new CatalogResourceException(
198         "Plan Language is not supported. Language = " + planLanguage);
199   }
200
201   private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
202     if (inputList == null || inputList.isEmpty()) {
203       return new InputParameter[0];
204     }
205
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()));
210     }
211     return retList.toArray(new InputParameter[0]);
212   }
213
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) {
219       return null;
220     }
221
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);
232
233       retList.add(ret);
234     }
235
236     return retList;
237   }
238
239
240   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
241     List<RelationShip> retList = new ArrayList<>();
242
243     if (relationshipList == null) {
244       return retList;
245     }
246
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());
254       retList.add(ret);
255     }
256
257     return retList;
258   }
259
260   private String getSubstitutionType(ParseYamlResult result) {
261     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
262       return null;
263     }
264     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
265   }
266
267 }