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