4a50b02dbcce1373e35a7406ab7c64bb89abafbb
[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 java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.openo.commontosca.catalog.common.ToolUtil;
23 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
24 import org.openo.commontosca.catalog.db.resource.TemplateManager;
25 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
26 import org.openo.commontosca.catalog.model.entity.InputParameter;
27 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
28 import org.openo.commontosca.catalog.model.entity.OutputParameter;
29 import org.openo.commontosca.catalog.model.entity.RelationShip;
30 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
31 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
32 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
33 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
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.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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ToscaYamlModelParser extends AbstractModelParser {
44   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
45
46   @Override
47   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
48     logger.info("tosca-yaml-parser parse begin.");
49     ParseYamlResult result = getParseYamlResult(fileLocation);
50     
51     // service template
52     ServiceTemplate st = parseServiceTemplate(
53         result, packageId, parseServiceTemplateFileName(packageId, fileLocation));
54     // workflow
55     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
56     st.setOperations(operations);
57     // node templates
58     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
59     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
60     // save to db
61     TemplateManager.getInstance().addServiceTemplate(
62         TemplateDataHelper.convert2TemplateData(st, result.getRawData(), ntList));
63
64     // substitution
65     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
66     if (stm != null) {
67       TemplateManager.getInstance()
68           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
69     }
70
71     return st.getServiceTemplateId();
72   }
73
74   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
75     String destPath = copyTemporaryFile2HttpServer(fileLocation);
76     try {
77       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
78       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
79     } finally {
80       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
81         (new File(destPath)).delete();
82       }
83     }
84   }
85
86   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
87     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
88     request.setPath(fileLocation);
89     return request;
90   }
91
92   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
93       ParseYamlResult result) {
94     String type = getSubstitutionType(result);
95     if (ToolUtil.isTrimedEmptyString(type)) {
96       return null;
97     }
98
99     org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
100         .TopologyTemplate.SubstitutionMapping stm =
101         result.getTopologyTemplate().getSubstitutionMappings();
102     return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
103         stm.getCapabilityList());
104   }
105
106   private ServiceTemplate parseServiceTemplate(ParseYamlResult result, String packageId,
107       String stDownloadUri) {
108     ServiceTemplate st = new ServiceTemplate();
109
110     st.setServiceTemplateId(ToolUtil.generateId());
111     st.setId(parserServiceTemplateName(result.getMetadata()));  // TODO
112     st.setTemplateName(parserServiceTemplateName(result.getMetadata()));
113     st.setVendor(parserServiceTemplateVendor(result.getMetadata()));
114     st.setVersion(parserServiceTemplateVersion(result.getMetadata()));
115     st.setCsarId(packageId);
116     st.setDownloadUri(stDownloadUri);
117     st.setInputs(parseInputs(result));
118     st.setOutputs(parseOutputs(result));
119     return st;
120   }
121
122   private InputParameter[] parseInputs(ParseYamlResult result) {
123     List<Input> inputList = result.getTopologyTemplate().getInputs();
124     if (inputList == null) {
125       return new InputParameter[0];
126     }
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()));
131     }
132     return retList.toArray(new InputParameter[0]);
133   }
134
135   private OutputParameter[] parseOutputs(ParseYamlResult result) {
136     List<Output> outputList = result.getTopologyTemplate().getOutputs();
137     if (outputList == null || outputList.isEmpty()) {
138       return new OutputParameter[0];
139     }
140     List<OutputParameter> retList = new ArrayList<OutputParameter>();
141     for (Output output : outputList) {
142       retList
143           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
144     }
145     return retList.toArray(new OutputParameter[0]);
146   }
147
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) {
153       return null;
154     }
155
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);
166
167       retList.add(ret);
168     }
169
170     return retList;
171   }
172
173
174   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
175     List<RelationShip> retList = new ArrayList<>();
176
177     if (relationshipList == null) {
178       return retList;
179     }
180
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());
188       retList.add(ret);
189     }
190
191     return retList;
192   }
193
194   private String getSubstitutionType(ParseYamlResult result) {
195     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
196       return null;
197     }
198     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
199   }
200
201 }