2 * Copyright 2016 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.commontosca.catalog.model.parser.yaml.aria;
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.model.common.TemplateDataHelper;
22 import org.openo.commontosca.catalog.model.entity.InputParameter;
23 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
24 import org.openo.commontosca.catalog.model.entity.OutputParameter;
25 import org.openo.commontosca.catalog.model.entity.RelationShip;
26 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
27 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
28 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
29 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
30 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Input;
31 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node;
32 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node.Relationship;
33 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Output;
34 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution.Mapping;
35 import org.openo.commontosca.catalog.model.parser.yaml.aria.service.AriaParserServiceConsumer;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
42 import java.util.Map.Entry;
48 public class AriaModelParser extends AbstractModelParser {
51 * @see org.openo.commontosca.catalog.model.parser.AbstractModelParser#parse(java.lang.String, java.lang.String)
54 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
55 AriaParserResult result = getAriaParserResult(fileLocation);
58 ServiceTemplate st = parseServiceTemplate(
59 result, packageId, parseServiceTemplateFileName(packageId, fileLocation));
61 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
62 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
64 TemplateManager.getInstance().addServiceTemplate(
65 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
68 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
70 TemplateManager.getInstance()
71 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
74 return st.getServiceTemplateId();
79 * @param serviceTemplateId
83 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
84 AriaParserResult result) {
85 String type = getSubstitutionType(result);
86 if (ToolUtil.isTrimedEmptyString(type)) {
90 org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
91 result.getSubstitution();
92 return new SubstitutionMapping(
95 parseSubstitutionRequirements(stm.getRequirement()),
96 parseSubstitutionCapabilities(stm.getCapabilities()));
101 * @param capabilities
104 private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
105 return parseMappings(capabilities);
109 private Map<String, String[]> parseMappings(Mapping[] mappings) {
110 Map<String, String[]> ret = new HashMap<>();
111 if (mappings != null) {
112 for (Mapping mapping : mappings) {
113 ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
124 private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
125 return parseMappings(requirement);
132 private String getSubstitutionType(AriaParserResult result) {
133 if (result.getSubstitution() == null) {
136 return result.getSubstitution().getNode_type_name();
142 * @param serviceTemplateId
145 * @throws CatalogResourceException
147 private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
148 AriaParserResult result) throws CatalogResourceException {
149 Node[] nodes = result.getNodes();
150 if (nodes == null || nodes.length == 0) {
154 List<NodeTemplate> retList = new ArrayList<>();
155 for (Node node : nodes) {
156 NodeTemplate ret = new NodeTemplate();
157 ret.setId(node.getTemplate_name());
158 ret.setName(node.getTemplate_name());
159 ret.setType(node.getType_name());
160 ret.setProperties(node.getPropertyAssignments());
161 List<RelationShip> relationShipList =
162 parseNodeTemplateRelationShip(node.getRelationships(), node, nodes);
163 ret.setRelationShips(relationShipList);
173 * @param relationships
177 * @throws CatalogResourceException
179 private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode, Node[] nodes) throws CatalogResourceException {
180 List<RelationShip> retList = new ArrayList<>();
182 if (relationships == null || relationships.length == 0) {
186 for (Relationship relationship : relationships) {
187 RelationShip ret = new RelationShip();
188 ret.setSourceNodeId(sourceNode.getTemplate_name());
189 ret.setSourceNodeName(sourceNode.getTemplate_name());
190 Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
191 ret.setTargetNodeId(targetNode.getTemplate_name());
192 ret.setTargetNodeName(targetNode.getTemplate_name());
193 ret.setType(relationship.getType_name());
205 * @throws CatalogResourceException
207 private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
208 if (nodeId == null) {
209 throw new CatalogResourceException("Target node id is null.");
211 if (nodes == null || nodes.length == 0) {
212 throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
215 for (Node node : nodes) {
216 if (nodeId.equals(node.getId())) {
221 throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
231 private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
232 String downloadUri) {
233 ServiceTemplate st = new ServiceTemplate();
235 st.setServiceTemplateId(ToolUtil.generateId());
236 st.setTemplateName(result.getMetadata().get("template_name"));
237 st.setVendor(result.getMetadata().get("template_author"));
238 st.setVersion(result.getMetadata().get("template_version"));
239 st.setCsarid(packageId);
240 st.setDownloadUri(downloadUri);
241 st.setInputs(parseInputs(result));
242 st.setOutputs(parseOutputs(result));
251 private InputParameter[] parseInputs(AriaParserResult result) {
252 Map<String, Input> inputs = result.getInputs();
253 if (inputs == null || inputs.isEmpty()) {
254 return new InputParameter[0];
256 List<InputParameter> retList = new ArrayList<InputParameter>();
257 for (Entry<String, Input> e : inputs.entrySet()) {
261 e.getValue().getType_name(),
262 e.getValue().getDescription(),
263 e.getValue().getValue(),
266 return retList.toArray(new InputParameter[0]);
273 private OutputParameter[] parseOutputs(AriaParserResult result) {
274 Map<String, Output> outputs = result.getOutpus();
275 if (outputs == null || outputs.isEmpty()) {
276 return new OutputParameter[0];
279 List<OutputParameter> retList = new ArrayList<OutputParameter>();
280 for (Entry<String, Output> e: outputs.entrySet()) {
283 e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
286 return retList.toArray(new OutputParameter[0]);
289 private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
290 String destPath = copyTemporaryFile2HttpServer(fileLocation);
292 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
293 return AriaParserServiceConsumer.parseCsarPackage(url);
295 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
296 (new File(destPath)).delete();