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