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.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.SubstitutionMapping;
29 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
30 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
31 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Input;
32 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node;
33 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node.Relationship;
34 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Output;
35 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution.Mapping;
36 import org.openo.commontosca.catalog.model.parser.yaml.aria.service.AriaParserServiceConsumer;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
43 import java.util.Map.Entry;
49 public class AriaModelParser extends AbstractModelParser {
52 * @see org.openo.commontosca.catalog.model.parser.AbstractModelParser#parse(java.lang.String, java.lang.String)
55 public String parse(String packageId, String fileLocation) throws CatalogResourceException {
56 AriaParserResult result = getAriaParserResult(fileLocation);
59 CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
60 ServiceTemplate st = parseServiceTemplate(result, packageId, stDownloadUri.getDownloadUri());
62 List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
63 st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
65 TemplateManager.getInstance().addServiceTemplate(
66 TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
69 SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
71 TemplateManager.getInstance()
72 .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
75 return st.getServiceTemplateId();
80 * @param serviceTemplateId
84 private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
85 AriaParserResult result) {
86 String type = getSubstitutionType(result);
87 if (ToolUtil.isTrimedEmptyString(type)) {
91 org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
92 result.getSubstitution();
93 return new SubstitutionMapping(
96 parseSubstitutionRequirements(stm.getRequirement()),
97 parseSubstitutionCapabilities(stm.getCapabilities()));
102 * @param capabilities
105 private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
106 return parseMappings(capabilities);
110 private Map<String, String[]> parseMappings(Mapping[] mappings) {
111 Map<String, String[]> ret = new HashMap<>();
112 if (mappings != null) {
113 for (Mapping mapping : mappings) {
114 ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
125 private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
126 return parseMappings(requirement);
133 private String getSubstitutionType(AriaParserResult result) {
134 if (result.getSubstitution() == null) {
137 return result.getSubstitution().getNode_type_name();
143 * @param serviceTemplateId
146 * @throws CatalogResourceException
148 private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
149 AriaParserResult result) throws CatalogResourceException {
150 Node[] nodes = result.getNodes();
151 if (nodes == null || nodes.length == 0) {
155 List<NodeTemplate> retList = new ArrayList<>();
156 for (Node node : nodes) {
157 NodeTemplate ret = new NodeTemplate();
158 ret.setId(node.getTemplate_name());
159 ret.setName(node.getTemplate_name());
160 ret.setType(node.getType_name());
161 ret.setProperties(node.getPropertyAssignments());
162 List<RelationShip> relationShipList =
163 parseNodeTemplateRelationShip(node.getRelationships(), node, nodes);
164 ret.setRelationShips(relationShipList);
174 * @param relationships
178 * @throws CatalogResourceException
180 private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode, Node[] nodes) throws CatalogResourceException {
181 List<RelationShip> retList = new ArrayList<>();
183 if (relationships == null || relationships.length == 0) {
187 for (Relationship relationship : relationships) {
188 RelationShip ret = new RelationShip();
189 ret.setSourceNodeId(sourceNode.getTemplate_name());
190 ret.setSourceNodeName(sourceNode.getTemplate_name());
191 Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
192 ret.setTargetNodeId(targetNode.getTemplate_name());
193 ret.setTargetNodeName(targetNode.getTemplate_name());
194 ret.setType(relationship.getType_name());
206 * @throws CatalogResourceException
208 private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
209 if (nodeId == null) {
210 throw new CatalogResourceException("Target node id is null.");
212 if (nodes == null || nodes.length == 0) {
213 throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
216 for (Node node : nodes) {
217 if (nodeId.equals(node.getId())) {
222 throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
232 private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
233 String downloadUri) {
234 ServiceTemplate st = new ServiceTemplate();
236 st.setServiceTemplateId(ToolUtil.generateId());
237 st.setTemplateName(result.getMetadata().get("template_name"));
238 st.setVendor(result.getMetadata().get("template_author"));
239 st.setVersion(result.getMetadata().get("template_version"));
240 st.setCsarid(packageId);
241 st.setDownloadUri(downloadUri);
242 st.setInputs(parseInputs(result));
243 st.setOutputs(parseOutputs(result));
252 private InputParameter[] parseInputs(AriaParserResult result) {
253 Map<String, Input> inputs = result.getInputs();
254 if (inputs == null || inputs.isEmpty()) {
255 return new InputParameter[0];
257 List<InputParameter> retList = new ArrayList<InputParameter>();
258 for (Entry<String, Input> e : inputs.entrySet()) {
262 e.getValue().getType_name(),
263 e.getValue().getDescription(),
264 e.getValue().getValue(),
267 return retList.toArray(new InputParameter[0]);
274 private OutputParameter[] parseOutputs(AriaParserResult result) {
275 Map<String, Output> outputs = result.getOutpus();
276 if (outputs == null || outputs.isEmpty()) {
277 return new OutputParameter[0];
280 List<OutputParameter> retList = new ArrayList<OutputParameter>();
281 for (Entry<String, Output> e: outputs.entrySet()) {
284 e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
287 return retList.toArray(new OutputParameter[0]);
290 private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
291 String destPath = copyTemporaryFile2HttpServer(fileLocation);
293 String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
294 return AriaParserServiceConsumer.parseCsarPackage(url);
296 if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
297 (new File(destPath)).delete();