bfebe15d0d6a43b22667310b86798e98723167c1
[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.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import org.openo.commontosca.catalog.common.ToolUtil;
25 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
26 import org.openo.commontosca.catalog.db.resource.TemplateManager;
27 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
28 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
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, fileLocation, 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 Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
115     return parseMappings(capabilities);
116   }
117
118
119   private Map<String, String[]> parseMappings(Mapping[] mappings) {
120     Map<String, String[]> ret = new HashMap<>();
121     if (mappings != null) {
122       for (Mapping mapping : mappings) {
123         ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
124       }
125     }
126
127     return ret;
128   }
129
130   /**
131    * @param requirement
132    * @return
133    */
134   private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
135     return parseMappings(requirement);
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, Node sourceNode, Node[] nodes) throws CatalogResourceException {
190     List<RelationShip> retList = new ArrayList<>();
191
192     if (relationships == null || relationships.length == 0) {
193       return retList;
194     }
195
196     for (Relationship relationship : relationships) {
197       RelationShip ret = new RelationShip();
198       ret.setSourceNodeId(sourceNode.getTemplate_name());
199       ret.setSourceNodeName(sourceNode.getTemplate_name());
200       Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
201       ret.setTargetNodeId(targetNode.getTemplate_name());
202       ret.setTargetNodeName(targetNode.getTemplate_name());
203       ret.setType(relationship.getType_name());
204       retList.add(ret);
205     }
206
207     return retList;
208   }
209
210
211   /**
212    * @param nodes
213    * @param nodeId
214    * @return
215    * @throws CatalogResourceException 
216    */
217   private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
218     if (nodeId == null) {
219       throw new CatalogResourceException("Target node id is null.");
220     }
221     if (nodes == null || nodes.length == 0) {
222       throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
223     }
224     
225     for (Node node : nodes) {
226       if (nodeId.equals(node.getId())) {
227         return node;
228       }
229     }
230     
231     throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
232   }
233
234
235   /**
236    * @param result
237    * @param packageId
238    * @param downloadUri
239    * @return
240    */
241   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
242       String downloadUri) {
243     ServiceTemplate st = new ServiceTemplate();
244
245     st.setServiceTemplateId(ToolUtil.generateId());
246     st.setTemplateName(parserServiceTemplateName(result.getInstance().getMetadata()));
247     st.setVendor(parserServiceTemplateVendor(result.getInstance().getMetadata()));
248     st.setVersion(parserServiceTemplateVersion(result.getInstance().getMetadata()));
249     st.setCsarId(packageId);
250     st.setDownloadUri(downloadUri);
251     st.setInputs(parseInputs(result));
252     st.setOutputs(parseOutputs(result));
253     return st;
254   }
255
256
257   /**
258    * @param result
259    * @return
260    */
261   private InputParameter[] parseInputs(AriaParserResult result) {
262     Map<String, Input> inputs = result.getInstance().getInputs();
263     if (inputs == null || inputs.isEmpty()) {
264       return new InputParameter[0];
265     }
266     List<InputParameter> retList = new ArrayList<InputParameter>();
267     for (Entry<String, Input> e : inputs.entrySet()) {
268       retList.add(
269           new InputParameter(
270               e.getKey(),
271               e.getValue().getType_name(),
272               e.getValue().getDescription(),
273               e.getValue().getValue(),
274               false));
275     }
276     return retList.toArray(new InputParameter[0]);
277   }
278
279   /**
280    * @param result
281    * @return
282    */
283   private OutputParameter[] parseOutputs(AriaParserResult result) {
284     Map<String, Output> outputs = result.getInstance().getOutpus();
285     if (outputs == null || outputs.isEmpty()) {
286       return new OutputParameter[0];
287     }
288     
289     List<OutputParameter> retList = new ArrayList<OutputParameter>();
290     for (Entry<String, Output> e: outputs.entrySet()) {
291       retList.add(
292           new OutputParameter(
293               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
294     }
295
296     return retList.toArray(new OutputParameter[0]);
297   }
298
299   private AriaParserResult getAriaParserResult(String packageId, String fileLocation, String stFileLocation) throws CatalogResourceException {
300     CsarFileUriResponse stDownloadUri =
301         PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileLocation);
302     return AriaParserServiceConsumer.parseCsarPackage(stDownloadUri.getDownloadUri());
303   }
304
305 }