Delete the redundant Configuration Items. Such as opentoscaServerAddr, yamlParseAddr...
[vfc/nfvo/catalog.git] / catalog-core / catalog-mgr / src / main / java / org / openo / commontosca / catalog / model / parser / yaml / aria / AriaModelParser.java
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 java.io.File;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 import org.openo.commontosca.catalog.common.ToolUtil;
26 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
27 import org.openo.commontosca.catalog.db.resource.TemplateManager;
28 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
29 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
30 import org.openo.commontosca.catalog.model.entity.EnumDataType;
31 import org.openo.commontosca.catalog.model.entity.InputParameter;
32 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
33 import org.openo.commontosca.catalog.model.entity.OutputParameter;
34 import org.openo.commontosca.catalog.model.entity.RelationShip;
35 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
36 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
37 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
38 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
39 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Input;
40 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node;
41 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node.Relationship;
42 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Output;
43 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution.Mapping;
44 import org.openo.commontosca.catalog.model.parser.yaml.aria.service.AriaParserServiceConsumer;
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     AriaParserResult result = getAriaParserResult(fileLocation);
58     
59     // service template
60     CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
61     ServiceTemplate st = parseServiceTemplate(result, packageId, stDownloadUri.getDownloadUri());
62     // node templates
63     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
64     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
65     // save to db
66     TemplateManager.getInstance().addServiceTemplate(
67         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
68     
69     // substitution
70     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
71     if (stm != null) {
72       TemplateManager.getInstance()
73           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
74     }
75     
76     return st.getServiceTemplateId();
77   }
78   
79
80   /**
81    * @param serviceTemplateId
82    * @param result
83    * @return
84    */
85   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
86       AriaParserResult result) {
87     String type = getSubstitutionType(result);
88     if (ToolUtil.isTrimedEmptyString(type)) {
89       return null;
90     }
91     
92     org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
93         result.getSubstitution();
94     return new SubstitutionMapping(
95         serviceTemplateId,
96         type,
97         parseSubstitutionRequirements(stm.getRequirement()),
98         parseSubstitutionCapabilities(stm.getCapabilities()));
99   }
100
101
102   /**
103    * @param capabilities
104    * @return
105    */
106   private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
107     return parseMappings(capabilities);
108   }
109
110
111   private Map<String, String[]> parseMappings(Mapping[] mappings) {
112     Map<String, String[]> ret = new HashMap<>();
113     if (mappings != null) {
114       for (Mapping mapping : mappings) {
115         ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
116       }
117     }
118
119     return ret;
120   }
121
122   /**
123    * @param requirement
124    * @return
125    */
126   private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
127     return parseMappings(requirement);
128   }
129
130   /**
131    * @param result
132    * @return
133    */
134   private String getSubstitutionType(AriaParserResult result) {
135     if (result.getSubstitution() == null) {
136       return null;
137     }
138     return result.getSubstitution().getNode_type_name();
139   }
140
141
142   /**
143    * @param packageId
144    * @param serviceTemplateId
145    * @param result
146    * @return
147    */
148   private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
149       AriaParserResult result) {
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.getName());
159       ret.setName(node.getName());
160       ret.setType(node.getType_name());
161       ret.setProperties(node.getPropertyAssignments());
162       List<RelationShip> relationShipList =
163           parseNodeTemplateRelationShip(node.getRelationships(), node);
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    * @return
177    */
178   private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode) {
179     List<RelationShip> retList = new ArrayList<>();
180
181     if (relationships == null || relationships.length == 0) {
182       return retList;
183     }
184
185     for (Relationship relationship : relationships) {
186       RelationShip ret = new RelationShip();
187       ret.setSourceNodeId(sourceNode.getName());
188       ret.setSourceNodeName(sourceNode.getName());
189       ret.setTargetNodeId(relationship.getTemplate_name());
190       ret.setTargetNodeName(relationship.getTemplate_name());
191       ret.setType(relationship.getType_name());
192       retList.add(ret);
193     }
194
195     return retList;
196   }
197
198
199   /**
200    * @param result
201    * @param packageId
202    * @param downloadUri
203    * @return
204    */
205   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
206       String downloadUri) {
207     ServiceTemplate st = new ServiceTemplate();
208
209     st.setServiceTemplateId(ToolUtil.generateId());
210     st.setTemplateName(result.getMetadata().get("template_name"));
211     st.setVendor(result.getMetadata().get("template_author"));
212     st.setVersion(result.getMetadata().get("template_version"));
213     st.setCsarid(packageId);
214     st.setDownloadUri(downloadUri);
215     st.setInputs(parseInputs(result));
216     st.setOutputs(parseOutputs(result));
217     return st;
218   }
219
220
221   /**
222    * @param result
223    * @return
224    */
225   private InputParameter[] parseInputs(AriaParserResult result) {
226     Map<String, Input> inputs = result.getInputs();
227     if (inputs == null || inputs.isEmpty()) {
228       return new InputParameter[0];
229     }
230     List<InputParameter> retList = new ArrayList<InputParameter>();
231     for (Entry<String, Input> e : inputs.entrySet()) {
232       retList.add(
233           new InputParameter(
234               e.getKey(),
235               getEnumDataType(e.getValue().getType_name()),
236               e.getValue().getDescription(),
237               e.getValue().getValue(),
238               false));
239     }
240     return retList.toArray(new InputParameter[0]);
241   }
242   
243   /**
244    * @param type
245    * @return
246    */
247   private EnumDataType getEnumDataType(String type) {
248     if (EnumDataType.INTEGER.toString().equalsIgnoreCase(type)) {
249       return EnumDataType.INTEGER;
250     }
251
252     if (EnumDataType.FLOAT.toString().equalsIgnoreCase(type)) {
253       return EnumDataType.FLOAT;
254     }
255
256     if (EnumDataType.BOOLEAN.toString().equalsIgnoreCase(type)) {
257       return EnumDataType.BOOLEAN;
258     }
259
260     return EnumDataType.STRING;
261   }
262
263
264   /**
265    * @param result
266    * @return
267    */
268   private OutputParameter[] parseOutputs(AriaParserResult result) {
269     Map<String, Output> outputs = result.getOutpus();
270     if (outputs == null || outputs.isEmpty()) {
271       return new OutputParameter[0];
272     }
273     
274     List<OutputParameter> retList = new ArrayList<OutputParameter>();
275     for (Entry<String, Output> e: outputs.entrySet()) {
276       retList.add(
277           new OutputParameter(
278               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
279     }
280
281     return retList.toArray(new OutputParameter[0]);
282   }
283
284   private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
285     String destPath = copyTemporaryFile2HttpServer(fileLocation);
286     try {
287       String url = getUrl(toTempFileLocalPath(fileLocation));
288       return AriaParserServiceConsumer.parseCsarPackage(url);
289     } finally {
290       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
291         (new File(destPath)).delete();
292       }
293     }
294   }
295
296 }