33c1676aeba3a05579c2fc8ffb148f73617e0773
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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.entity.response.CsarFileUriResponse;
22 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
23 import org.openo.commontosca.catalog.model.entity.InputParameter;
24 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
25 import org.openo.commontosca.catalog.model.entity.OutputParameter;
26 import org.openo.commontosca.catalog.model.entity.RelationShip;
27 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
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     CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
60     ServiceTemplate st = parseServiceTemplate(result, packageId, stDownloadUri.getDownloadUri());
61     // node templates
62     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
63     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
64     // save to db
65     TemplateManager.getInstance().addServiceTemplate(
66         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
67     
68     // substitution
69     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
70     if (stm != null) {
71       TemplateManager.getInstance()
72           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
73     }
74     
75     return st.getServiceTemplateId();
76   }
77   
78
79   /**
80    * @param serviceTemplateId
81    * @param result
82    * @return
83    */
84   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
85       AriaParserResult result) {
86     String type = getSubstitutionType(result);
87     if (ToolUtil.isTrimedEmptyString(type)) {
88       return null;
89     }
90     
91     org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
92         result.getSubstitution();
93     return new SubstitutionMapping(
94         serviceTemplateId,
95         type,
96         parseSubstitutionRequirements(stm.getRequirement()),
97         parseSubstitutionCapabilities(stm.getCapabilities()));
98   }
99
100
101   /**
102    * @param capabilities
103    * @return
104    */
105   private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
106     return parseMappings(capabilities);
107   }
108
109
110   private Map<String, String[]> parseMappings(Mapping[] mappings) {
111     Map<String, String[]> ret = new HashMap<>();
112     if (mappings != null) {
113       for (Mapping mapping : mappings) {
114         ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
115       }
116     }
117
118     return ret;
119   }
120
121   /**
122    * @param requirement
123    * @return
124    */
125   private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
126     return parseMappings(requirement);
127   }
128
129   /**
130    * @param result
131    * @return
132    */
133   private String getSubstitutionType(AriaParserResult result) {
134     if (result.getSubstitution() == null) {
135       return null;
136     }
137     return result.getSubstitution().getNode_type_name();
138   }
139
140
141   /**
142    * @param packageId
143    * @param serviceTemplateId
144    * @param result
145    * @return
146    * @throws CatalogResourceException 
147    */
148   private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
149       AriaParserResult result) throws CatalogResourceException {
150     Node[] nodes = result.getNodes();
151     if (nodes == null || nodes.length == 0) {
152       return null;
153     }
154
155     List<NodeTemplate> retList = new ArrayList<>();
156     for (Node node : nodes) {
157       NodeTemplate ret = new NodeTemplate();
158       ret.setId(node.getTemplate_name());
159       ret.setName(node.getTemplate_name());
160       ret.setType(node.getType_name());
161       ret.setProperties(node.getPropertyAssignments());
162       List<RelationShip> relationShipList =
163           parseNodeTemplateRelationShip(node.getRelationships(), node, nodes);
164       ret.setRelationShips(relationShipList);
165
166       retList.add(ret);
167     }
168
169     return retList;
170   }
171
172
173   /**
174    * @param relationships
175    * @param sourceNode 
176    * @param nodes 
177    * @return
178    * @throws CatalogResourceException 
179    */
180   private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode, Node[] nodes) throws CatalogResourceException {
181     List<RelationShip> retList = new ArrayList<>();
182
183     if (relationships == null || relationships.length == 0) {
184       return retList;
185     }
186
187     for (Relationship relationship : relationships) {
188       RelationShip ret = new RelationShip();
189       ret.setSourceNodeId(sourceNode.getTemplate_name());
190       ret.setSourceNodeName(sourceNode.getTemplate_name());
191       Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
192       ret.setTargetNodeId(targetNode.getTemplate_name());
193       ret.setTargetNodeName(targetNode.getTemplate_name());
194       ret.setType(relationship.getType_name());
195       retList.add(ret);
196     }
197
198     return retList;
199   }
200
201
202   /**
203    * @param nodes
204    * @param nodeId
205    * @return
206    * @throws CatalogResourceException 
207    */
208   private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
209     if (nodeId == null) {
210       throw new CatalogResourceException("Target node id is null.");
211     }
212     if (nodes == null || nodes.length == 0) {
213       throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
214     }
215     
216     for (Node node : nodes) {
217       if (nodeId.equals(node.getId())) {
218         return node;
219       }
220     }
221     
222     throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
223   }
224
225
226   /**
227    * @param result
228    * @param packageId
229    * @param downloadUri
230    * @return
231    */
232   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
233       String downloadUri) {
234     ServiceTemplate st = new ServiceTemplate();
235
236     st.setServiceTemplateId(ToolUtil.generateId());
237     st.setTemplateName(result.getMetadata().get("template_name"));
238     st.setVendor(result.getMetadata().get("template_author"));
239     st.setVersion(result.getMetadata().get("template_version"));
240     st.setCsarid(packageId);
241     st.setDownloadUri(downloadUri);
242     st.setInputs(parseInputs(result));
243     st.setOutputs(parseOutputs(result));
244     return st;
245   }
246
247
248   /**
249    * @param result
250    * @return
251    */
252   private InputParameter[] parseInputs(AriaParserResult result) {
253     Map<String, Input> inputs = result.getInputs();
254     if (inputs == null || inputs.isEmpty()) {
255       return new InputParameter[0];
256     }
257     List<InputParameter> retList = new ArrayList<InputParameter>();
258     for (Entry<String, Input> e : inputs.entrySet()) {
259       retList.add(
260           new InputParameter(
261               e.getKey(),
262               e.getValue().getType_name(),
263               e.getValue().getDescription(),
264               e.getValue().getValue(),
265               false));
266     }
267     return retList.toArray(new InputParameter[0]);
268   }
269
270   /**
271    * @param result
272    * @return
273    */
274   private OutputParameter[] parseOutputs(AriaParserResult result) {
275     Map<String, Output> outputs = result.getOutpus();
276     if (outputs == null || outputs.isEmpty()) {
277       return new OutputParameter[0];
278     }
279     
280     List<OutputParameter> retList = new ArrayList<OutputParameter>();
281     for (Entry<String, Output> e: outputs.entrySet()) {
282       retList.add(
283           new OutputParameter(
284               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
285     }
286
287     return retList.toArray(new OutputParameter[0]);
288   }
289
290   private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
291     String destPath = copyTemporaryFile2HttpServer(fileLocation);
292     try {
293       String url = getUrl(toTempFileLocalPath(fileLocation));
294       return AriaParserServiceConsumer.parseCsarPackage(url);
295     } finally {
296       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
297         (new File(destPath)).delete();
298       }
299     }
300   }
301
302 }