632d60c429f7b39c7878cf499bc0b2997638041b
[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 import java.util.Map;
22 import java.util.Map.Entry;
23
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;
45
46 public class ToscaYamlModelParser extends AbstractModelParser {
47   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
48
49   @Override
50   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
51     logger.info("tosca-yaml-parser parse begin.");
52     ParseYamlResult result = getParseYamlResult(fileLocation);
53     
54     // service template
55     ServiceTemplate st = parseServiceTemplate(
56         result, packageId, parseServiceTemplateFileName(packageId, fileLocation));
57     // workflow
58     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
59     st.setOperations(operations);
60     // node templates
61     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
62     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
63     // save to db
64     TemplateManager.getInstance().addServiceTemplate(
65         TemplateDataHelper.convert2TemplateData(st, result.getRawData(), ntList));
66
67     // substitution
68     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
69     if (stm != null) {
70       TemplateManager.getInstance()
71           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
72     }
73
74     return st.getServiceTemplateId();
75   }
76
77   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
78     String destPath = copyTemporaryFile2HttpServer(fileLocation);
79     try {
80       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
81       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
82     } finally {
83       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
84         (new File(destPath)).delete();
85       }
86     }
87   }
88
89   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
90     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
91     request.setPath(fileLocation);
92     return request;
93   }
94
95   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
96       ParseYamlResult result) {
97     String type = getSubstitutionType(result);
98     if (ToolUtil.isTrimedEmptyString(type)) {
99       return null;
100     }
101
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()));
109   }
110   
111   /**
112    * @param requirementList
113    * @return
114    */
115   private CapReqMapping[] parseSubstitutionRequirements(Map<String, String[]> requirementList) {
116     return parseMappings(requirementList);
117   }
118
119   /**
120    * @param capabilityList
121    * @return
122    */
123   private CapReqMapping[] parseSubstitutionCapabilities(Map<String, String[]> capabilityList) {
124     return parseMappings(capabilityList);
125   }
126
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]));
134         }
135       }
136     }
137     return ret.toArray(new CapReqMapping[0]);
138   }
139
140   private ServiceTemplate parseServiceTemplate(ParseYamlResult result, String packageId,
141       String stDownloadUri) {
142     ServiceTemplate st = new ServiceTemplate();
143
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));
153     return st;
154   }
155
156   private InputParameter[] parseInputs(ParseYamlResult result) {
157     List<Input> inputList = result.getTopologyTemplate().getInputs();
158     if (inputList == null) {
159       return new InputParameter[0];
160     }
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()));
165     }
166     return retList.toArray(new InputParameter[0]);
167   }
168
169   private OutputParameter[] parseOutputs(ParseYamlResult result) {
170     List<Output> outputList = result.getTopologyTemplate().getOutputs();
171     if (outputList == null || outputList.isEmpty()) {
172       return new OutputParameter[0];
173     }
174     List<OutputParameter> retList = new ArrayList<OutputParameter>();
175     for (Output output : outputList) {
176       retList
177           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
178     }
179     return retList.toArray(new OutputParameter[0]);
180   }
181
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) {
187       return null;
188     }
189
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);
200
201       retList.add(ret);
202     }
203
204     return retList;
205   }
206
207
208   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
209     List<RelationShip> retList = new ArrayList<>();
210
211     if (relationshipList == null) {
212       return retList;
213     }
214
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());
222       retList.add(ret);
223     }
224
225     return retList;
226   }
227
228   private String getSubstitutionType(ParseYamlResult result) {
229     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
230       return null;
231     }
232     return result.getTopologyTemplate().getSubstitutionMappings().getNode_type();
233   }
234
235 }