5b5999875da1192b0f50b04ef6504aab6bafbd6c
[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.InputParameter;
25 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
26 import org.openo.commontosca.catalog.model.entity.OutputParameter;
27 import org.openo.commontosca.catalog.model.entity.RelationShip;
28 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
29 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
30 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
31 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
32 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
33 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
39 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
40 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
41 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
42 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import java.io.File;
47 import java.util.ArrayList;
48 import java.util.List;
49
50 public class ToscaYamlModelParser extends AbstractModelParser {
51   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
52
53   @Override
54   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
55     logger.info("tosca-yaml-parser parse begin.");
56     ParseYamlResult result = getParseYamlResult(fileLocation);
57     
58     // service template
59     CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
60     ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
61     // workflow
62     ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
63     st.setOperations(operations);
64     // node templates
65     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
66     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
67     // save to db
68     TemplateManager.getInstance().addServiceTemplate(
69         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
70
71     // substitution
72     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
73     if (stm != null) {
74       TemplateManager.getInstance()
75           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
76     }
77
78     return st.getServiceTemplateId();
79   }
80
81   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
82     String destPath = copyTemporaryFile2HttpServer(fileLocation);
83     try {
84       String url = getUrl(toTempFileLocalPath(fileLocation));
85       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
86     } finally {
87       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
88         (new File(destPath)).delete();
89       }
90     }
91   }
92
93   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
94     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
95     request.setPath(fileLocation);
96     return request;
97   }
98
99   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
100       ParseYamlResult result) {
101     String type = getSubstitutionType(result);
102     if (ToolUtil.isTrimedEmptyString(type)) {
103       return null;
104     }
105
106     org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
107         .TopologyTemplate.SubstitutionMapping stm =
108         result.getTopologyTemplate().getSubstitutionMappings();
109     return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
110         stm.getCapabilityList());
111   }
112
113   private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
114       String stDownloadUri) {
115     ServiceTemplate st = new ServiceTemplate();
116
117     st.setServiceTemplateId(ToolUtil.generateId());
118     st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
119     st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
120     st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
121     st.setCsarid(packageId);
122     st.setDownloadUri(stDownloadUri);
123     st.setInputs(parseInputs(result));
124     st.setOutputs(parseOutputs(result));
125     return st;
126   }
127
128   private InputParameter[] parseInputs(ParseYamlResult result) {
129     List<Input> inputList = result.getTopologyTemplate().getInputs();
130     if (inputList == null) {
131       return new InputParameter[0];
132     }
133     List<InputParameter> retList = new ArrayList<InputParameter>();
134     for (Input input : inputList) {
135       retList.add(new InputParameter(input.getName(), input.getType(),
136           input.getDescription(), input.getDefault(), input.isRequired()));
137     }
138     return retList.toArray(new InputParameter[0]);
139   }
140
141   private OutputParameter[] parseOutputs(ParseYamlResult result) {
142     List<Output> outputList = result.getTopologyTemplate().getOutputs();
143     if (outputList == null || outputList.isEmpty()) {
144       return new OutputParameter[0];
145     }
146     List<OutputParameter> retList = new ArrayList<OutputParameter>();
147     for (Output output : outputList) {
148       retList
149           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
150     }
151     return retList.toArray(new OutputParameter[0]);
152   }
153
154   private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
155       throws CatalogResourceException {
156     if (planList == null || planList.isEmpty()) {
157       return new ServiceTemplateOperation[0];
158     }
159
160     List<ServiceTemplateOperation> opList = new ArrayList<>();
161     for (Plan plan : planList) {
162       ServiceTemplateOperation op = new ServiceTemplateOperation();
163       op.setName(plan.getName());
164       op.setDescription(plan.getDescription());
165       checkPlanLanguage(plan.getPlanLanguage());
166       DeployPackageResponse response =
167           Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference());
168       op.setPackageName(parsePackageName(response));
169       op.setProcessId(response.getProcessId());
170       op.setInputs(parsePlanInputs(plan.getInputList()));
171
172       opList.add(op);
173
174     }
175     return opList.toArray(new ServiceTemplateOperation[0]);
176   }
177
178   private String parsePackageName(DeployPackageResponse response) {
179     String packageName = response.getPackageName();
180     if (packageName != null && packageName.indexOf("-") > 0) {
181       packageName = packageName.substring(0, packageName.lastIndexOf("-"));
182     }
183     return packageName;
184   }
185
186   private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
187     if (planLanguage == null || planLanguage.isEmpty()) {
188       throw new CatalogResourceException("Plan Language is empty.");
189     }
190     if (planLanguage.equalsIgnoreCase("bpel")) {
191       return;
192     }
193     if (planLanguage.equalsIgnoreCase("bpmn")) {
194       return;
195     }
196     if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
197       return;
198     }
199     throw new CatalogResourceException(
200         "Plan Language is not supported. Language = " + planLanguage);
201   }
202
203   private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
204     if (inputList == null || inputList.isEmpty()) {
205       return new InputParameter[0];
206     }
207
208     List<InputParameter> retList = new ArrayList<>();
209     for (PlanInput input : inputList) {
210       retList.add(new InputParameter(input.getName(), input.getType(),
211           input.getDescription(), input.getDefault(), input.isRequired()));
212     }
213     return retList.toArray(new InputParameter[0]);
214   }
215
216   private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
217       ParseYamlResult result) {
218     List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
219         result.getTopologyTemplate().getNodeTemplates();
220     if (nodetemplateList == null) {
221       return null;
222     }
223
224     List<NodeTemplate> retList = new ArrayList<>();
225     for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
226       NodeTemplate ret = new NodeTemplate();
227       ret.setId(nodeTemplate.getName());
228       ret.setName(nodeTemplate.getName());
229       ret.setType(nodeTemplate.getNodeType());
230       ret.setProperties(nodeTemplate.getPropertyList());
231       List<RelationShip> relationShipList =
232           parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
233       ret.setRelationShips(relationShipList);
234
235       retList.add(ret);
236     }
237
238     return retList;
239   }
240
241
242   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
243     List<RelationShip> retList = new ArrayList<>();
244
245     if (relationshipList == null) {
246       return retList;
247     }
248
249     for (Relationship relationship : relationshipList) {
250       RelationShip ret = new RelationShip();
251       ret.setSourceNodeId(relationship.getSourceNodeName());
252       ret.setSourceNodeName(relationship.getSourceNodeName());
253       ret.setTargetNodeId(relationship.getTargetNodeName());
254       ret.setTargetNodeName(relationship.getTargetNodeName());
255       ret.setType(relationship.getType());
256       retList.add(ret);
257     }
258
259     return retList;
260   }
261
262   private String getSubstitutionType(ParseYamlResult result) {
263     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
264       return null;
265     }
266     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
267   }
268
269 }