8d37319c81b9600b3c6eaffdaeb44b8d9639b310
[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 java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22
23 import org.openo.commontosca.catalog.common.ToolUtil;
24 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
25 import org.openo.commontosca.catalog.db.resource.TemplateManager;
26 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
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.aria.entity.AriaParserResult;
38 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Input;
39 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Node;
40 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Node.Relationship;
41 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Output;
42 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Substitution.Mapping;
43 import org.openo.commontosca.catalog.model.parser.yaml.aria.service.AriaParserServiceConsumer;
44 import org.openo.commontosca.catalog.wrapper.PackageWrapper;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * @author 10090474
50  *
51  */
52 public class AriaModelParser extends AbstractModelParser {
53   private static final Logger logger = LoggerFactory.getLogger(AriaModelParser.class);
54
55   /* (non-Javadoc)
56    * @see org.openo.commontosca.catalog.model.parser.AbstractModelParser#parse(java.lang.String, java.lang.String)
57    */
58   @Override
59   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
60     logger.info("Parse begin.");
61     
62     String stFileLocation = parseServiceTemplateFileName(packageId, fileLocation);
63     AriaParserResult result = getAriaParserResult(packageId, stFileLocation);
64     
65     // service template
66     ServiceTemplate st = parseServiceTemplate(result, packageId, stFileLocation);
67     // workflow
68     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
69     st.setOperations(operations);
70     // node templates
71     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
72     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
73     // save to db
74     TemplateManager.getInstance().addServiceTemplate(
75         TemplateDataHelper.convert2TemplateData(st, result.getRawData(), ntList));
76     
77     // substitution
78     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
79     if (stm != null) {
80       TemplateManager.getInstance()
81           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
82     }
83     
84     return st.getServiceTemplateId();
85   }
86   
87
88   /**
89    * @param serviceTemplateId
90    * @param result
91    * @return
92    */
93   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
94       AriaParserResult result) {
95     String type = getSubstitutionType(result);
96     if (ToolUtil.isTrimedEmptyString(type)) {
97       return null;
98     }
99     
100     org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Substitution stm =
101         result.getInstance().getSubstitution();
102     return new SubstitutionMapping(
103         serviceTemplateId,
104         type,
105         parseSubstitutionRequirements(stm.getRequirement()),
106         parseSubstitutionCapabilities(stm.getCapabilities()));
107   }
108
109
110   /**
111    * @param capabilities
112    * @return
113    */
114   private CapReqMapping[] parseSubstitutionCapabilities(Mapping[] capabilities) {
115     return parseMappings(capabilities);
116   }
117   
118   /**
119    * @param requirement
120    * @return
121    */
122   private CapReqMapping[] parseSubstitutionRequirements(Mapping[] requirement) {
123     return parseMappings(requirement);
124   }
125
126   private CapReqMapping[] parseMappings(Mapping[] mappings) {
127     List<CapReqMapping> ret = new ArrayList<>();
128     if (mappings != null) {
129       for (Mapping mapping : mappings) {
130         ret.add(new CapReqMapping(
131             mapping.getMapped_name(), mapping.getNode_id(), mapping.getName()));
132       }
133     }
134     
135     return ret.toArray(new CapReqMapping[0]);
136   }
137
138   /**
139    * @param result
140    * @return
141    */
142   private String getSubstitutionType(AriaParserResult result) {
143     if (result.getInstance().getSubstitution() == null) {
144       return null;
145     }
146     return result.getInstance().getSubstitution().getNode_type_name();
147   }
148
149
150   /**
151    * @param packageId
152    * @param serviceTemplateId
153    * @param result
154    * @return
155    * @throws CatalogResourceException 
156    */
157   private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
158       AriaParserResult result) throws CatalogResourceException {
159     Node[] nodes = result.getInstance().getNodes();
160     if (nodes == null || nodes.length == 0) {
161       return null;
162     }
163
164     List<NodeTemplate> retList = new ArrayList<>();
165     for (Node node : nodes) {
166       NodeTemplate ret = new NodeTemplate();
167       ret.setId(node.getTemplate_name());
168       ret.setName(node.getTemplate_name());
169       ret.setType(node.getType_name());
170       ret.setProperties(node.getPropertyAssignments());
171       List<RelationShip> relationShipList =
172           parseNodeTemplateRelationShip(node.getRelationships(), node, nodes);
173       ret.setRelationShips(relationShipList);
174
175       retList.add(ret);
176     }
177
178     return retList;
179   }
180
181
182   /**
183    * @param relationships
184    * @param sourceNode 
185    * @param nodes 
186    * @return
187    * @throws CatalogResourceException 
188    */
189   private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships,
190       Node sourceNode, Node[] nodes) throws CatalogResourceException {
191     List<RelationShip> retList = new ArrayList<>();
192
193     if (relationships == null || relationships.length == 0) {
194       return retList;
195     }
196
197     for (Relationship relationship : relationships) {
198       if (relationship.getTarget_node_id().equals(sourceNode.getId())) {
199         continue;  // target == source, ignore.
200       }
201       
202       RelationShip ret = new RelationShip();
203       ret.setSourceNodeId(sourceNode.getTemplate_name());
204       ret.setSourceNodeName(sourceNode.getTemplate_name());
205       Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
206       ret.setTargetNodeId(targetNode.getTemplate_name());
207       ret.setTargetNodeName(targetNode.getTemplate_name());
208       ret.setType(relationship.getType_name());
209       retList.add(ret);
210     }
211
212     return retList;
213   }
214
215
216   /**
217    * @param nodes
218    * @param nodeId
219    * @return
220    * @throws CatalogResourceException 
221    */
222   private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
223     if (nodeId == null) {
224       throw new CatalogResourceException("Target node id is null.");
225     }
226     if (nodes == null || nodes.length == 0) {
227       throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
228     }
229     
230     for (Node node : nodes) {
231       if (nodeId.equals(node.getId())) {
232         return node;
233       }
234     }
235     
236     throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
237   }
238
239
240   /**
241    * @param result
242    * @param packageId
243    * @param downloadUri
244    * @return
245    */
246   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
247       String downloadUri) {
248     ServiceTemplate st = new ServiceTemplate();
249
250     st.setServiceTemplateId(ToolUtil.generateId());
251     st.setId(parserId(result.getInstance().getMetadata()));
252     st.setTemplateName(parserServiceTemplateName(result.getInstance().getMetadata()));
253     st.setVendor(parserServiceTemplateVendor(result.getInstance().getMetadata()));
254     st.setVersion(parserServiceTemplateVersion(result.getInstance().getMetadata()));
255     st.setCsarId(packageId);
256     st.setDownloadUri(downloadUri);
257     st.setInputs(parseInputs(result));
258     st.setOutputs(parseOutputs(result));
259     return st;
260   }
261
262
263   /**
264    * @param result
265    * @return
266    */
267   private InputParameter[] parseInputs(AriaParserResult result) {
268     Map<String, Input> inputs = result.getInstance().getInputs();
269     if (inputs == null || inputs.isEmpty()) {
270       return new InputParameter[0];
271     }
272     List<InputParameter> retList = new ArrayList<InputParameter>();
273     for (Entry<String, Input> e : inputs.entrySet()) {
274       retList.add(
275           new InputParameter(
276               e.getKey(),
277               e.getValue().getType_name(),
278               e.getValue().getDescription(),
279               e.getValue().getValue(),
280               false));
281     }
282     return retList.toArray(new InputParameter[0]);
283   }
284
285   /**
286    * @param result
287    * @return
288    */
289   private OutputParameter[] parseOutputs(AriaParserResult result) {
290     Map<String, Output> outputs = result.getInstance().getOutpus();
291     if (outputs == null || outputs.isEmpty()) {
292       return new OutputParameter[0];
293     }
294     
295     List<OutputParameter> retList = new ArrayList<OutputParameter>();
296     for (Entry<String, Output> e: outputs.entrySet()) {
297       retList.add(
298           new OutputParameter(
299               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
300     }
301
302     return retList.toArray(new OutputParameter[0]);
303   }
304
305   private AriaParserResult getAriaParserResult(String packageId, String stFileLocation) throws CatalogResourceException {
306     CsarFileUriResponse stDownloadUri =
307         PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileLocation);
308     return AriaParserServiceConsumer.parseCsarPackage(stDownloadUri.getDownloadUri());
309   }
310
311 }