3cf16bd38951f181b42f3f5490d48bb5d5bf2cc9
[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.aria;
17
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.aria.entity.AriaParserResult;
31 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Input;
32 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node;
33 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node.Relationship;
34 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Output;
35 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution.Mapping;
36 import org.openo.commontosca.catalog.model.parser.yaml.aria.service.AriaParserServiceConsumer;
37
38 import java.io.File;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44
45 /**
46  * @author 10090474
47  *
48  */
49 public class AriaModelParser extends AbstractModelParser {
50
51   /* (non-Javadoc)
52    * @see org.openo.commontosca.catalog.model.parser.AbstractModelParser#parse(java.lang.String, java.lang.String)
53    */
54   @Override
55   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
56     AriaParserResult result = getAriaParserResult(fileLocation);
57     
58     // service template
59     ServiceTemplate st = parseServiceTemplate(
60         result, packageId, parseServiceTemplateFileName(packageId, fileLocation));
61     // workflow
62     ServiceTemplateOperation[] operations = parseOperations(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
82   /**
83    * @param serviceTemplateId
84    * @param result
85    * @return
86    */
87   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
88       AriaParserResult result) {
89     String type = getSubstitutionType(result);
90     if (ToolUtil.isTrimedEmptyString(type)) {
91       return null;
92     }
93     
94     org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
95         result.getSubstitution();
96     return new SubstitutionMapping(
97         serviceTemplateId,
98         type,
99         parseSubstitutionRequirements(stm.getRequirement()),
100         parseSubstitutionCapabilities(stm.getCapabilities()));
101   }
102
103
104   /**
105    * @param capabilities
106    * @return
107    */
108   private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
109     return parseMappings(capabilities);
110   }
111
112
113   private Map<String, String[]> parseMappings(Mapping[] mappings) {
114     Map<String, String[]> ret = new HashMap<>();
115     if (mappings != null) {
116       for (Mapping mapping : mappings) {
117         ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
118       }
119     }
120
121     return ret;
122   }
123
124   /**
125    * @param requirement
126    * @return
127    */
128   private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
129     return parseMappings(requirement);
130   }
131
132   /**
133    * @param result
134    * @return
135    */
136   private String getSubstitutionType(AriaParserResult result) {
137     if (result.getSubstitution() == null) {
138       return null;
139     }
140     return result.getSubstitution().getNode_type_name();
141   }
142
143
144   /**
145    * @param packageId
146    * @param serviceTemplateId
147    * @param result
148    * @return
149    * @throws CatalogResourceException 
150    */
151   private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
152       AriaParserResult result) throws CatalogResourceException {
153     Node[] nodes = result.getNodes();
154     if (nodes == null || nodes.length == 0) {
155       return null;
156     }
157
158     List<NodeTemplate> retList = new ArrayList<>();
159     for (Node node : nodes) {
160       NodeTemplate ret = new NodeTemplate();
161       ret.setId(node.getTemplate_name());
162       ret.setName(node.getTemplate_name());
163       ret.setType(node.getType_name());
164       ret.setProperties(node.getPropertyAssignments());
165       List<RelationShip> relationShipList =
166           parseNodeTemplateRelationShip(node.getRelationships(), node, nodes);
167       ret.setRelationShips(relationShipList);
168
169       retList.add(ret);
170     }
171
172     return retList;
173   }
174
175
176   /**
177    * @param relationships
178    * @param sourceNode 
179    * @param nodes 
180    * @return
181    * @throws CatalogResourceException 
182    */
183   private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode, Node[] nodes) throws CatalogResourceException {
184     List<RelationShip> retList = new ArrayList<>();
185
186     if (relationships == null || relationships.length == 0) {
187       return retList;
188     }
189
190     for (Relationship relationship : relationships) {
191       RelationShip ret = new RelationShip();
192       ret.setSourceNodeId(sourceNode.getTemplate_name());
193       ret.setSourceNodeName(sourceNode.getTemplate_name());
194       Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
195       ret.setTargetNodeId(targetNode.getTemplate_name());
196       ret.setTargetNodeName(targetNode.getTemplate_name());
197       ret.setType(relationship.getType_name());
198       retList.add(ret);
199     }
200
201     return retList;
202   }
203
204
205   /**
206    * @param nodes
207    * @param nodeId
208    * @return
209    * @throws CatalogResourceException 
210    */
211   private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
212     if (nodeId == null) {
213       throw new CatalogResourceException("Target node id is null.");
214     }
215     if (nodes == null || nodes.length == 0) {
216       throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
217     }
218     
219     for (Node node : nodes) {
220       if (nodeId.equals(node.getId())) {
221         return node;
222       }
223     }
224     
225     throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
226   }
227
228
229   /**
230    * @param result
231    * @param packageId
232    * @param downloadUri
233    * @return
234    */
235   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
236       String downloadUri) {
237     ServiceTemplate st = new ServiceTemplate();
238
239     st.setServiceTemplateId(ToolUtil.generateId());
240     st.setTemplateName(result.getMetadata().get("template_name"));
241     st.setVendor(result.getMetadata().get("template_author"));
242     st.setVersion(result.getMetadata().get("template_version"));
243     st.setCsarid(packageId);
244     st.setDownloadUri(downloadUri);
245     st.setInputs(parseInputs(result));
246     st.setOutputs(parseOutputs(result));
247     return st;
248   }
249
250
251   /**
252    * @param result
253    * @return
254    */
255   private InputParameter[] parseInputs(AriaParserResult result) {
256     Map<String, Input> inputs = result.getInputs();
257     if (inputs == null || inputs.isEmpty()) {
258       return new InputParameter[0];
259     }
260     List<InputParameter> retList = new ArrayList<InputParameter>();
261     for (Entry<String, Input> e : inputs.entrySet()) {
262       retList.add(
263           new InputParameter(
264               e.getKey(),
265               e.getValue().getType_name(),
266               e.getValue().getDescription(),
267               e.getValue().getValue(),
268               false));
269     }
270     return retList.toArray(new InputParameter[0]);
271   }
272
273   /**
274    * @param result
275    * @return
276    */
277   private OutputParameter[] parseOutputs(AriaParserResult result) {
278     Map<String, Output> outputs = result.getOutpus();
279     if (outputs == null || outputs.isEmpty()) {
280       return new OutputParameter[0];
281     }
282     
283     List<OutputParameter> retList = new ArrayList<OutputParameter>();
284     for (Entry<String, Output> e: outputs.entrySet()) {
285       retList.add(
286           new OutputParameter(
287               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
288     }
289
290     return retList.toArray(new OutputParameter[0]);
291   }
292
293   private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
294     String destPath = copyTemporaryFile2HttpServer(fileLocation);
295     try {
296       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
297       return AriaParserServiceConsumer.parseCsarPackage(url);
298     } finally {
299       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
300         (new File(destPath)).delete();
301       }
302     }
303   }
304
305 }