2  * Copyright 2016 [ZTE] and others.
 
   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.
 
  17 package org.openo.commontosca.catalog.model.parser.yaml.zte;
 
  19 import org.openo.commontosca.catalog.common.MsbAddrConfig;
 
  20 import org.openo.commontosca.catalog.common.ToolUtil;
 
  21 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
 
  22 import org.openo.commontosca.catalog.db.resource.TemplateManager;
 
  23 import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
 
  24 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
 
  25 import org.openo.commontosca.catalog.model.entity.EnumDataType;
 
  26 import org.openo.commontosca.catalog.model.entity.InputParameter;
 
  27 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
 
  28 import org.openo.commontosca.catalog.model.entity.OutputParameter;
 
  29 import org.openo.commontosca.catalog.model.entity.RelationShip;
 
  30 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
 
  31 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
 
  32 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
 
  33 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
 
  34 import org.openo.commontosca.catalog.model.parser.EnumTemplateType;
 
  35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
 
  36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
 
  37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
 
  38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
 
  39 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
 
  40 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
 
  41 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
 
  42 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
 
  43 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
 
  44 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
 
  45 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
 
  46 import org.openo.commontosca.catalog.wrapper.PackageWrapper;
 
  47 import org.slf4j.Logger;
 
  48 import org.slf4j.LoggerFactory;
 
  50 import java.io.BufferedInputStream;
 
  51 import java.io.BufferedReader;
 
  53 import java.io.FileInputStream;
 
  54 import java.io.IOException;
 
  55 import java.io.InputStream;
 
  56 import java.io.InputStreamReader;
 
  57 import java.util.ArrayList;
 
  58 import java.util.HashMap;
 
  59 import java.util.List;
 
  61 import java.util.zip.ZipEntry;
 
  62 import java.util.zip.ZipFile;
 
  63 import java.util.zip.ZipInputStream;
 
  66 public class ToscaYamlModelParser extends AbstractModelParser {
 
  68   private static final Object TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
 
  69   private static final Logger LOGGER = LoggerFactory.getLogger(ToscaYamlModelParser.class);
 
  72   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
 
  73     ParseYamlResult result = getParseYamlResult(fileLocation);
 
  75     Map<String, String> toscaMeta = parseToscaMeta(fileLocation);
 
  76     String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
 
  77     CsarFileUriResponse stDownloadUri =
 
  78         PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName);
 
  80     ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
 
  81     ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
 
  82     st.setOperations(operations);
 
  83     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
 
  84     st.setType(getTemplateType(result, ntList).toString());
 
  86     TemplateManager.getInstance().addServiceTemplate(
 
  87         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
 
  89     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
 
  91       TemplateManager.getInstance()
 
  92           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
 
  95     return st.getServiceTemplateId();
 
  98   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
 
  99     String destPath = copyTemporaryFile2HttpServer(fileLocation);
 
 101       String url = getUrl(toTempFileLocalPath(fileLocation));
 
 102       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
 
 104       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
 
 105         (new File(destPath)).delete();
 
 110   private String toTempFileLocalPath(String fileLocation) {
 
 111     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
 
 114   private String getUrl(String uri) {
 
 116     if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) {
 
 117       url = MsbAddrConfig.getMsbAddress() + uri.substring(1);
 
 119     url = MsbAddrConfig.getMsbAddress() + uri;
 
 120     String urlresult = url.replace("\\", "/");
 
 124   private String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
 
 125     String destPath = Class.class.getClass().getResource("/").getPath()
 
 126         + org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerPath()
 
 127         + toTempFileLocalPath(fileLocation);
 
 128     if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(fileLocation, destPath,
 
 130       throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
 
 135   @SuppressWarnings("resource")
 
 136   private Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
 
 137     Map<String, String> toscaMeta = new HashMap<>();
 
 139     ZipInputStream zin = null;
 
 140     BufferedReader br = null;
 
 142       InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
 
 143       zin = new ZipInputStream(in);
 
 145       while ((ze = zin.getNextEntry()) != null) {
 
 146         if (("TOSCA-Metadata" + File.separator + "TOSCA.meta").equals(ze.getName())
 
 147             || "TOSCA-Metadata/TOSCA.meta".equals(ze.getName())) {
 
 148           ZipFile zf = new ZipFile(fileLocation);
 
 149           br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
 
 152           while ((line = br.readLine()) != null) {
 
 153             if (line.indexOf(":") > 0) {
 
 154               tmps = line.split(":");
 
 155               toscaMeta.put(tmps[0].trim(), tmps[1].trim());
 
 163     } catch (IOException e1) {
 
 164       throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
 
 166       closeStreamAndReader(zin, br);
 
 172   private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
 
 176       } catch (IOException e1) {
 
 177         LOGGER.error("Buffered reader close failed !");
 
 183       } catch (IOException e2) {
 
 184         LOGGER.error("Zip inputStream close failed !");
 
 189   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
 
 190     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
 
 191     request.setPath(fileLocation);
 
 195   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
 
 196       ParseYamlResult result) {
 
 197     String type = getSubstitutionMappingType(result);
 
 198     if (ToolUtil.isTrimedEmptyString(type)) {
 
 202     org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
 
 203         .TopologyTemplate.SubstitutionMapping stm =
 
 204         result.getTopologyTemplate().getSubstitutionMappings();
 
 205     return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
 
 206         stm.getCapabilityList());
 
 209   private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
 
 210       String stDownloadUri) {
 
 211     ServiceTemplate st = new ServiceTemplate();
 
 213     st.setServiceTemplateId(ToolUtil.generateId());
 
 214     st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
 
 215     st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
 
 216     st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
 
 217     st.setCsarid(packageId);
 
 218     st.setDownloadUri(stDownloadUri);
 
 219     st.setInputs(parseInputs(result));
 
 220     st.setOutputs(parseOutputs(result));
 
 224   private InputParameter[] parseInputs(ParseYamlResult result) {
 
 225     List<Input> inputList = result.getTopologyTemplate().getInputs();
 
 226     if (inputList == null) {
 
 227       return new InputParameter[0];
 
 229     List<InputParameter> retList = new ArrayList<InputParameter>();
 
 230     for (Input input : inputList) {
 
 231       retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()),
 
 232           input.getDescription(), input.getDefault(), input.isRequired()));
 
 234     return retList.toArray(new InputParameter[0]);
 
 237   private OutputParameter[] parseOutputs(ParseYamlResult result) {
 
 238     List<Output> outputList = result.getTopologyTemplate().getOutputs();
 
 239     if (outputList == null || outputList.isEmpty()) {
 
 240       return new OutputParameter[0];
 
 242     List<OutputParameter> retList = new ArrayList<OutputParameter>();
 
 243     for (Output output : outputList) {
 
 245           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
 
 247     return retList.toArray(new OutputParameter[0]);
 
 250   private ServiceTemplateOperation[] parseOperations(List<Plan> planList, String zipFileLocation)
 
 251       throws CatalogResourceException {
 
 252     if (planList == null || planList.isEmpty()) {
 
 253       return new ServiceTemplateOperation[0];
 
 256     List<ServiceTemplateOperation> opList = new ArrayList<>();
 
 257     for (Plan plan : planList) {
 
 258       ServiceTemplateOperation op = new ServiceTemplateOperation();
 
 259       op.setName(plan.getName());
 
 260       op.setDescription(plan.getDescription());
 
 261       checkPlanLanguage(plan.getPlanLanguage());
 
 262       DeployPackageResponse response =
 
 263           Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference());
 
 264       op.setPackageName(parsePackageName(response));
 
 265       op.setProcessId(response.getProcessId());
 
 266       op.setInputs(parsePlanInputs(plan.getInputList()));
 
 271     return opList.toArray(new ServiceTemplateOperation[0]);
 
 274   private String parsePackageName(DeployPackageResponse response) {
 
 275     String packageName = response.getPackageName();
 
 276     if (packageName != null && packageName.indexOf("-") > 0) {
 
 277       packageName = packageName.substring(0, packageName.lastIndexOf("-"));
 
 282   private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
 
 283     if (planLanguage == null || planLanguage.isEmpty()) {
 
 284       throw new CatalogResourceException("Plan Language is empty.");
 
 286     if (planLanguage.equalsIgnoreCase("bpel")) {
 
 289     if (planLanguage.equalsIgnoreCase("bpmn")) {
 
 292     if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
 
 295     throw new CatalogResourceException(
 
 296         "Plan Language is not supported. Language = " + planLanguage);
 
 299   private InputParameter[] parsePlanInputs(List<PlanInput> inputList) {
 
 300     if (inputList == null || inputList.isEmpty()) {
 
 301       return new InputParameter[0];
 
 304     List<InputParameter> retList = new ArrayList<>();
 
 305     for (PlanInput input : inputList) {
 
 306       retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()),
 
 307           input.getDescription(), input.getDefault(), input.isRequired()));
 
 309     return retList.toArray(new InputParameter[0]);
 
 312   private EnumDataType getEnumDataType(String type) {
 
 313     if (EnumDataType.INTEGER.toString().equalsIgnoreCase(type)) {
 
 314       return EnumDataType.INTEGER;
 
 317     if (EnumDataType.FLOAT.toString().equalsIgnoreCase(type)) {
 
 318       return EnumDataType.FLOAT;
 
 321     if (EnumDataType.BOOLEAN.toString().equalsIgnoreCase(type)) {
 
 322       return EnumDataType.BOOLEAN;
 
 325     return EnumDataType.STRING;
 
 328   private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
 
 329       ParseYamlResult result) {
 
 330     List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
 
 331         result.getTopologyTemplate().getNodeTemplates();
 
 332     if (nodetemplateList == null) {
 
 336     List<NodeTemplate> retList = new ArrayList<>();
 
 337     for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
 
 338       NodeTemplate ret = new NodeTemplate();
 
 339       ret.setId(nodeTemplate.getName());
 
 340       ret.setName(nodeTemplate.getName());
 
 341       ret.setType(nodeTemplate.getNodeType());
 
 342       ret.setProperties(nodeTemplate.getPropertyList());
 
 343       List<RelationShip> relationShipList =
 
 344           parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
 
 345       ret.setRelationShips(relationShipList);
 
 354   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
 
 355     List<RelationShip> retList = new ArrayList<>();
 
 357     if (relationshipList == null) {
 
 361     for (Relationship relationship : relationshipList) {
 
 362       RelationShip ret = new RelationShip();
 
 363       ret.setSourceNodeId(relationship.getSourceNodeName());
 
 364       ret.setSourceNodeName(relationship.getSourceNodeName());
 
 365       ret.setTargetNodeId(relationship.getTargetNodeName());
 
 366       ret.setTargetNodeName(relationship.getTargetNodeName());
 
 367       ret.setType(relationship.getType());
 
 374   private EnumTemplateType getTemplateType(ParseYamlResult result, List<NodeTemplate> ntList) {
 
 375     String type = getSubstitutionMappingType(result);
 
 376     if (isNsType(type)) {
 
 377       return EnumTemplateType.NS;
 
 380     if (isVnfType(type)) {
 
 381       return EnumTemplateType.VNF;
 
 384     return getTemplateTypeFromNodeTemplates(ntList);
 
 387   private String getSubstitutionMappingType(ParseYamlResult result) {
 
 388     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
 
 391     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
 
 394   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
 
 395     for (NodeTemplate nt : ntList) {
 
 396       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
 
 397         return EnumTemplateType.NS;
 
 401     return EnumTemplateType.VNF;
 
 404   private boolean isVnfType(String type) {
 
 405     if (ToolUtil.isTrimedEmptyString(type)) {
 
 408     return type.toUpperCase().contains(".VNF");
 
 411   private boolean isNsType(String type) {
 
 412     if (ToolUtil.isTrimedEmptyString(type)) {
 
 415     return type.toUpperCase().contains(".NS");