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.ServiceTemplateOperation;
 
  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     ServiceTemplate st = parseServiceTemplate(
 
  60         result, packageId, parseServiceTemplateFileName(packageId, fileLocation));
 
  62     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
 
  63     st.setOperations(operations);
 
  65     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
 
  66     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
 
  68     TemplateManager.getInstance().addServiceTemplate(
 
  69         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
 
  72     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
 
  74       TemplateManager.getInstance()
 
  75           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
 
  78     return st.getServiceTemplateId();
 
  83    * @param serviceTemplateId
 
  87   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
 
  88       AriaParserResult result) {
 
  89     String type = getSubstitutionType(result);
 
  90     if (ToolUtil.isTrimedEmptyString(type)) {
 
  94     org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
 
  95         result.getSubstitution();
 
  96     return new SubstitutionMapping(
 
  99         parseSubstitutionRequirements(stm.getRequirement()),
 
 100         parseSubstitutionCapabilities(stm.getCapabilities()));
 
 105    * @param capabilities
 
 108   private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
 
 109     return parseMappings(capabilities);
 
 113   private Map<String, String[]> parseMappings(Mapping[] mappings) {
 
 114     Map<String, String[]> ret = new HashMap<>();
 
 115     if (mappings != null) {
 
 116       for (Mapping mapping : mappings) {
 
 117         ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
 
 128   private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
 
 129     return parseMappings(requirement);
 
 136   private String getSubstitutionType(AriaParserResult result) {
 
 137     if (result.getSubstitution() == null) {
 
 140     return result.getSubstitution().getNode_type_name();
 
 146    * @param serviceTemplateId
 
 149    * @throws CatalogResourceException 
 
 151   private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
 
 152       AriaParserResult result) throws CatalogResourceException {
 
 153     Node[] nodes = result.getNodes();
 
 154     if (nodes == null || nodes.length == 0) {
 
 158     List<NodeTemplate> retList = new ArrayList<>();
 
 159     for (Node node : nodes) {
 
 160       NodeTemplate ret = new NodeTemplate();
 
 161       ret.setId(node.getTemplate_name());
 
 162       ret.setName(node.getTemplate_name());
 
 163       ret.setType(node.getType_name());
 
 164       ret.setProperties(node.getPropertyAssignments());
 
 165       List<RelationShip> relationShipList =
 
 166           parseNodeTemplateRelationShip(node.getRelationships(), node, nodes);
 
 167       ret.setRelationShips(relationShipList);
 
 177    * @param relationships
 
 181    * @throws CatalogResourceException 
 
 183   private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode, Node[] nodes) throws CatalogResourceException {
 
 184     List<RelationShip> retList = new ArrayList<>();
 
 186     if (relationships == null || relationships.length == 0) {
 
 190     for (Relationship relationship : relationships) {
 
 191       RelationShip ret = new RelationShip();
 
 192       ret.setSourceNodeId(sourceNode.getTemplate_name());
 
 193       ret.setSourceNodeName(sourceNode.getTemplate_name());
 
 194       Node targetNode = getNodeById(nodes, relationship.getTarget_node_id());
 
 195       ret.setTargetNodeId(targetNode.getTemplate_name());
 
 196       ret.setTargetNodeName(targetNode.getTemplate_name());
 
 197       ret.setType(relationship.getType_name());
 
 209    * @throws CatalogResourceException 
 
 211   private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
 
 212     if (nodeId == null) {
 
 213       throw new CatalogResourceException("Target node id is null.");
 
 215     if (nodes == null || nodes.length == 0) {
 
 216       throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
 
 219     for (Node node : nodes) {
 
 220       if (nodeId.equals(node.getId())) {
 
 225     throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
 
 235   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
 
 236       String downloadUri) {
 
 237     ServiceTemplate st = new ServiceTemplate();
 
 239     st.setServiceTemplateId(ToolUtil.generateId());
 
 240     st.setTemplateName(result.getMetadata().get("template_name"));
 
 241     st.setVendor(result.getMetadata().get("template_author"));
 
 242     st.setVersion(result.getMetadata().get("template_version"));
 
 243     st.setCsarid(packageId);
 
 244     st.setDownloadUri(downloadUri);
 
 245     st.setInputs(parseInputs(result));
 
 246     st.setOutputs(parseOutputs(result));
 
 255   private InputParameter[] parseInputs(AriaParserResult result) {
 
 256     Map<String, Input> inputs = result.getInputs();
 
 257     if (inputs == null || inputs.isEmpty()) {
 
 258       return new InputParameter[0];
 
 260     List<InputParameter> retList = new ArrayList<InputParameter>();
 
 261     for (Entry<String, Input> e : inputs.entrySet()) {
 
 265               e.getValue().getType_name(),
 
 266               e.getValue().getDescription(),
 
 267               e.getValue().getValue(),
 
 270     return retList.toArray(new InputParameter[0]);
 
 277   private OutputParameter[] parseOutputs(AriaParserResult result) {
 
 278     Map<String, Output> outputs = result.getOutpus();
 
 279     if (outputs == null || outputs.isEmpty()) {
 
 280       return new OutputParameter[0];
 
 283     List<OutputParameter> retList = new ArrayList<OutputParameter>();
 
 284     for (Entry<String, Output> e: outputs.entrySet()) {
 
 287               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
 
 290     return retList.toArray(new OutputParameter[0]);
 
 293   private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
 
 294     String destPath = copyTemporaryFile2HttpServer(fileLocation);
 
 296       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
 
 297       return AriaParserServiceConsumer.parseCsarPackage(url);
 
 299       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
 
 300         (new File(destPath)).delete();