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