48e1d6a0ead31823f73ecc3fef480dd5aaeb98d1
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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
17 package org.openo.commontosca.catalog.model.parser.yaml.zte;
18
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;
46
47 import java.io.File;
48 import java.util.ArrayList;
49 import java.util.List;
50
51 public class ToscaYamlModelParser extends AbstractModelParser {
52   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
53
54   @Override
55   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
56     logger.info("tosca-yaml-parser parse begin.");
57     ParseYamlResult result = getParseYamlResult(fileLocation);
58     
59     // service template
60     CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
61     ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
62     // workflow
63     ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
64     st.setOperations(operations);
65     // node templates
66     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
67     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
68     // save to db
69     TemplateManager.getInstance().addServiceTemplate(
70         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
71
72     // substitution
73     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
74     if (stm != null) {
75       TemplateManager.getInstance()
76           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
77     }
78
79     return st.getServiceTemplateId();
80   }
81
82   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
83     String destPath = copyTemporaryFile2HttpServer(fileLocation);
84     try {
85       String url = getUrl(toTempFileLocalPath(fileLocation));
86       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
87     } finally {
88       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
89         (new File(destPath)).delete();
90       }
91     }
92   }
93
94   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
95     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
96     request.setPath(fileLocation);
97     return request;
98   }
99
100   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
101       ParseYamlResult result) {
102     String type = getSubstitutionType(result);
103     if (ToolUtil.isTrimedEmptyString(type)) {
104       return null;
105     }
106
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());
112   }
113
114   private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
115       String stDownloadUri) {
116     ServiceTemplate st = new ServiceTemplate();
117
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));
126     return st;
127   }
128
129   private InputParameter[] parseInputs(ParseYamlResult result) {
130     List<Input> inputList = result.getTopologyTemplate().getInputs();
131     if (inputList == null) {
132       return new InputParameter[0];
133     }
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()));
138     }
139     return retList.toArray(new InputParameter[0]);
140   }
141
142   private OutputParameter[] parseOutputs(ParseYamlResult result) {
143     List<Output> outputList = result.getTopologyTemplate().getOutputs();
144     if (outputList == null || outputList.isEmpty()) {
145       return new OutputParameter[0];
146     }
147     List<OutputParameter> retList = new ArrayList<OutputParameter>();
148     for (Output output : outputList) {
149       retList
150           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
151     }
152     return retList.toArray(new OutputParameter[0]);
153   }
154
155   private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
156       throws CatalogResourceException {
157     if (planList == null || planList.isEmpty()) {
158       return new ServiceTemplateOperation[0];
159     }
160
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()));
172
173       opList.add(op);
174
175     }
176     return opList.toArray(new ServiceTemplateOperation[0]);
177   }
178
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("-"));
183     }
184     return packageName;
185   }
186
187   private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
188     if (planLanguage == null || planLanguage.isEmpty()) {
189       throw new CatalogResourceException("Plan Language is empty.");
190     }
191     if (planLanguage.equalsIgnoreCase("bpel")) {
192       return;
193     }
194     if (planLanguage.equalsIgnoreCase("bpmn")) {
195       return;
196     }
197     if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
198       return;
199     }
200     throw new CatalogResourceException(
201         "Plan Language is not supported. Language = " + planLanguage);
202   }
203
204   private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
205     if (inputList == null || inputList.isEmpty()) {
206       return new InputParameter[0];
207     }
208
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()));
213     }
214     return retList.toArray(new InputParameter[0]);
215   }
216
217   private EnumDataType getEnumDataType(String type) {
218     if (EnumDataType.INTEGER.toString().equalsIgnoreCase(type)) {
219       return EnumDataType.INTEGER;
220     }
221
222     if (EnumDataType.FLOAT.toString().equalsIgnoreCase(type)) {
223       return EnumDataType.FLOAT;
224     }
225
226     if (EnumDataType.BOOLEAN.toString().equalsIgnoreCase(type)) {
227       return EnumDataType.BOOLEAN;
228     }
229
230     return EnumDataType.STRING;
231   }
232
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) {
238       return null;
239     }
240
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);
251
252       retList.add(ret);
253     }
254
255     return retList;
256   }
257
258
259   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
260     List<RelationShip> retList = new ArrayList<>();
261
262     if (relationshipList == null) {
263       return retList;
264     }
265
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());
273       retList.add(ret);
274     }
275
276     return retList;
277   }
278
279   private String getSubstitutionType(ParseYamlResult result) {
280     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
281       return null;
282     }
283     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
284   }
285
286 }