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.TopologyTemplate.Input;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 import java.util.ArrayList;
42 import java.util.List;
44 public class ToscaYamlModelParser extends AbstractModelParser {
45 private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
48 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
49 logger.info("tosca-yaml-parser parse begin.");
50 ParseYamlResult result = getParseYamlResult(fileLocation);
53 ServiceTemplate st = parseServiceTemplate(
54 packageId, result, parseServiceTemplateFileName(packageId, fileLocation));
56 ServiceTemplateOperation[] operations = parseOperations(fileLocation);
57 st.setOperations(operations);
59 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
60 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
62 TemplateManager.getInstance().addServiceTemplate(
63 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
66 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
68 TemplateManager.getInstance()
69 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
72 return st.getServiceTemplateId();
75 private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
76 String destPath = copyTemporaryFile2HttpServer(fileLocation);
78 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
79 return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
81 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
82 (new File(destPath)).delete();
87 private ParseYamlRequestParemeter comboRequest(String fileLocation) {
88 ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
89 request.setPath(fileLocation);
93 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
94 ParseYamlResult result) {
95 String type = getSubstitutionType(result);
96 if (ToolUtil.isTrimedEmptyString(type)) {
100 org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
101 .TopologyTemplate.SubstitutionMapping stm =
102 result.getTopologyTemplate().getSubstitutionMappings();
103 return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
104 stm.getCapabilityList());
107 private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
108 String stDownloadUri) {
109 ServiceTemplate st = new ServiceTemplate();
111 st.setServiceTemplateId(ToolUtil.generateId());
112 st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
113 st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
114 st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
115 st.setCsarid(packageId);
116 st.setDownloadUri(stDownloadUri);
117 st.setInputs(parseInputs(result));
118 st.setOutputs(parseOutputs(result));
122 private InputParameter[] parseInputs(ParseYamlResult result) {
123 List<Input> inputList = result.getTopologyTemplate().getInputs();
124 if (inputList == null) {
125 return new InputParameter[0];
127 List<InputParameter> retList = new ArrayList<InputParameter>();
128 for (Input input : inputList) {
129 retList.add(new InputParameter(input.getName(), input.getType(),
130 input.getDescription(), input.getDefault(), input.isRequired()));
132 return retList.toArray(new InputParameter[0]);
135 private OutputParameter[] parseOutputs(ParseYamlResult result) {
136 List<Output> outputList = result.getTopologyTemplate().getOutputs();
137 if (outputList == null || outputList.isEmpty()) {
138 return new OutputParameter[0];
140 List<OutputParameter> retList = new ArrayList<OutputParameter>();
141 for (Output output : outputList) {
143 .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
145 return retList.toArray(new OutputParameter[0]);
148 private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
149 ParseYamlResult result) {
150 List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
151 result.getTopologyTemplate().getNodeTemplates();
152 if (nodetemplateList == null) {
156 List<NodeTemplate> retList = new ArrayList<>();
157 for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
158 NodeTemplate ret = new NodeTemplate();
159 ret.setId(nodeTemplate.getName());
160 ret.setName(nodeTemplate.getName());
161 ret.setType(nodeTemplate.getNodeType());
162 ret.setProperties(nodeTemplate.getPropertyList());
163 List<RelationShip> relationShipList =
164 parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
165 ret.setRelationShips(relationShipList);
174 private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
175 List<RelationShip> retList = new ArrayList<>();
177 if (relationshipList == null) {
181 for (Relationship relationship : relationshipList) {
182 RelationShip ret = new RelationShip();
183 ret.setSourceNodeId(relationship.getSourceNodeName());
184 ret.setSourceNodeName(relationship.getSourceNodeName());
185 ret.setTargetNodeId(relationship.getTargetNodeName());
186 ret.setTargetNodeName(relationship.getTargetNodeName());
187 ret.setType(relationship.getType());
194 private String getSubstitutionType(ParseYamlResult result) {
195 if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
198 return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();