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;
19 import java.util.ArrayList;
20 import java.util.List;
22 import java.util.Map.Entry;
24 import org.openo.commontosca.catalog.common.ToolUtil;
25 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
26 import org.openo.commontosca.catalog.db.resource.TemplateManager;
27 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
28 import org.openo.commontosca.catalog.model.entity.CapReqMapping;
29 import org.openo.commontosca.catalog.model.entity.InputParameter;
30 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
31 import org.openo.commontosca.catalog.model.entity.OutputParameter;
32 import org.openo.commontosca.catalog.model.entity.RelationShip;
33 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
34 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
35 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
36 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
39 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
40 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
41 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
42 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class ToscaYamlModelParser extends AbstractModelParser {
47 private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
50 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
51 logger.info("tosca-yaml-parser parse begin.");
52 ParseYamlResult result = getParseYamlResult(fileLocation);
55 ServiceTemplate st = parseServiceTemplate(
56 result, packageId, parseServiceTemplateFileName(packageId, fileLocation));
58 ServiceTemplateOperation[] operations = parseOperations(fileLocation);
59 st.setOperations(operations);
61 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
62 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
64 TemplateManager.getInstance().addServiceTemplate(
65 TemplateDataHelper.convert2TemplateData(st, result.getRawData(), ntList));
68 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
70 TemplateManager.getInstance()
71 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
74 return st.getServiceTemplateId();
77 private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
78 String destPath = copyTemporaryFile2HttpServer(fileLocation);
80 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
81 return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
83 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
84 (new File(destPath)).delete();
89 private ParseYamlRequestParemeter comboRequest(String fileLocation) {
90 ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
91 request.setPath(fileLocation);
95 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
96 ParseYamlResult result) {
97 String type = getSubstitutionType(result);
98 if (ToolUtil.isTrimedEmptyString(type)) {
102 org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
103 .TopologyTemplate.SubstitutionMapping stm =
104 result.getTopologyTemplate().getSubstitutionMappings();
105 return new SubstitutionMapping(
106 serviceTemplateId, type,
107 parseSubstitutionRequirements(stm.getRequirementList()),
108 parseSubstitutionCapabilities(stm.getCapabilityList()));
112 * @param requirementList
115 private CapReqMapping[] parseSubstitutionRequirements(Map<String, String[]> requirementList) {
116 return parseMappings(requirementList);
120 * @param capabilityList
123 private CapReqMapping[] parseSubstitutionCapabilities(Map<String, String[]> capabilityList) {
124 return parseMappings(capabilityList);
127 private CapReqMapping[] parseMappings(Map<String, String[]> mappings) {
128 List<CapReqMapping> ret = new ArrayList<>();
129 if (mappings != null) {
130 for (Entry<String, String[]> mapping : mappings.entrySet()) {
131 if (mapping.getValue().length >= 2) {
132 ret.add(new CapReqMapping(
133 mapping.getKey(), mapping.getValue()[0], mapping.getValue()[1]));
137 return ret.toArray(new CapReqMapping[0]);
140 private ServiceTemplate parseServiceTemplate(ParseYamlResult result, String packageId,
141 String stDownloadUri) {
142 ServiceTemplate st = new ServiceTemplate();
144 st.setServiceTemplateId(ToolUtil.generateId());
145 st.setId(parserId(result.getMetadata()));
146 st.setTemplateName(parserServiceTemplateName(result.getMetadata()));
147 st.setVendor(parserServiceTemplateVendor(result.getMetadata()));
148 st.setVersion(parserServiceTemplateVersion(result.getMetadata()));
149 st.setCsarId(packageId);
150 st.setDownloadUri(stDownloadUri);
151 st.setInputs(parseInputs(result));
152 st.setOutputs(parseOutputs(result));
156 private InputParameter[] parseInputs(ParseYamlResult result) {
157 List<Input> inputList = result.getTopologyTemplate().getInputs();
158 if (inputList == null) {
159 return new InputParameter[0];
161 List<InputParameter> retList = new ArrayList<InputParameter>();
162 for (Input input : inputList) {
163 retList.add(new InputParameter(input.getName(), input.getType(),
164 input.getDescription(), input.getDefault(), input.isRequired()));
166 return retList.toArray(new InputParameter[0]);
169 private OutputParameter[] parseOutputs(ParseYamlResult result) {
170 List<Output> outputList = result.getTopologyTemplate().getOutputs();
171 if (outputList == null || outputList.isEmpty()) {
172 return new OutputParameter[0];
174 List<OutputParameter> retList = new ArrayList<OutputParameter>();
175 for (Output output : outputList) {
177 .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
179 return retList.toArray(new OutputParameter[0]);
182 private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
183 ParseYamlResult result) {
184 List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
185 result.getTopologyTemplate().getNodeTemplates();
186 if (nodetemplateList == null) {
190 List<NodeTemplate> retList = new ArrayList<>();
191 for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
192 NodeTemplate ret = new NodeTemplate();
193 ret.setId(nodeTemplate.getName());
194 ret.setName(nodeTemplate.getName());
195 ret.setType(nodeTemplate.getNodeType());
196 ret.setProperties(nodeTemplate.getPropertyList());
197 List<RelationShip> relationShipList =
198 parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
199 ret.setRelationShips(relationShipList);
208 private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
209 List<RelationShip> retList = new ArrayList<>();
211 if (relationshipList == null) {
215 for (Relationship relationship : relationshipList) {
216 RelationShip ret = new RelationShip();
217 ret.setSourceNodeId(relationship.getSourceNodeName());
218 ret.setSourceNodeName(relationship.getSourceNodeName());
219 ret.setTargetNodeId(relationship.getTargetNodeName());
220 ret.setTargetNodeName(relationship.getTargetNodeName());
221 ret.setType(relationship.getType());
228 private String getSubstitutionType(ParseYamlResult result) {
229 if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
232 return result.getTopologyTemplate().getSubstitutionMappings().getNode_type();