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 java.util.ArrayList;
 
  19 import java.util.HashMap;
 
  20 import java.util.List;
 
  22 import java.util.Map.Entry;
 
  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;
 
  52 public class AriaModelParser extends AbstractModelParser {
 
  53   private static final Logger logger = LoggerFactory.getLogger(AriaModelParser.class);
 
  56    * @see org.openo.commontosca.catalog.model.parser.AbstractModelParser#parse(java.lang.String, java.lang.String)
 
  59   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
 
  60     logger.info("Parse begin.");
 
  62     String stFileLocation = parseServiceTemplateFileName(packageId, fileLocation);
 
  63     AriaParserResult result = getAriaParserResult(packageId, fileLocation, stFileLocation);
 
  66     ServiceTemplate st = parseServiceTemplate(result, packageId, stFileLocation);
 
  68     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
 
  69     st.setOperations(operations);
 
  71     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
 
  72     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
 
  74     TemplateManager.getInstance().addServiceTemplate(
 
  75         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
 
  78     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
 
  80       TemplateManager.getInstance()
 
  81           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
 
  84     return st.getServiceTemplateId();
 
  89    * @param serviceTemplateId
 
  93   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
 
  94       AriaParserResult result) {
 
  95     String type = getSubstitutionType(result);
 
  96     if (ToolUtil.isTrimedEmptyString(type)) {
 
 100     org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Instance.Substitution stm =
 
 101         result.getInstance().getSubstitution();
 
 102     return new SubstitutionMapping(
 
 105         parseSubstitutionRequirements(stm.getRequirement()),
 
 106         parseSubstitutionCapabilities(stm.getCapabilities()));
 
 111    * @param capabilities
 
 114   private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
 
 115     return parseMappings(capabilities);
 
 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()});
 
 134   private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
 
 135     return parseMappings(requirement);
 
 142   private String getSubstitutionType(AriaParserResult result) {
 
 143     if (result.getInstance().getSubstitution() == null) {
 
 146     return result.getInstance().getSubstitution().getNode_type_name();
 
 152    * @param serviceTemplateId
 
 155    * @throws CatalogResourceException 
 
 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) {
 
 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);
 
 183    * @param relationships
 
 187    * @throws CatalogResourceException 
 
 189   private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode, Node[] nodes) throws CatalogResourceException {
 
 190     List<RelationShip> retList = new ArrayList<>();
 
 192     if (relationships == null || relationships.length == 0) {
 
 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());
 
 215    * @throws CatalogResourceException 
 
 217   private Node getNodeById(Node[] nodes, String nodeId) throws CatalogResourceException {
 
 218     if (nodeId == null) {
 
 219       throw new CatalogResourceException("Target node id is null.");
 
 221     if (nodes == null || nodes.length == 0) {
 
 222       throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
 
 225     for (Node node : nodes) {
 
 226       if (nodeId.equals(node.getId())) {
 
 231     throw new CatalogResourceException("Can't find target node. nodeId = " + nodeId);
 
 241   private ServiceTemplate parseServiceTemplate(AriaParserResult result, String packageId,
 
 242       String downloadUri) {
 
 243     ServiceTemplate st = new ServiceTemplate();
 
 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));
 
 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];
 
 266     List<InputParameter> retList = new ArrayList<InputParameter>();
 
 267     for (Entry<String, Input> e : inputs.entrySet()) {
 
 271               e.getValue().getType_name(),
 
 272               e.getValue().getDescription(),
 
 273               e.getValue().getValue(),
 
 276     return retList.toArray(new InputParameter[0]);
 
 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];
 
 289     List<OutputParameter> retList = new ArrayList<OutputParameter>();
 
 290     for (Entry<String, Output> e: outputs.entrySet()) {
 
 293               e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
 
 296     return retList.toArray(new OutputParameter[0]);
 
 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());