Codes for the interface with aria parser.
authorYuanHu <yuan.hu1@zte.com.cn>
Wed, 14 Sep 2016 09:06:37 +0000 (17:06 +0800)
committerYuanHu <yuan.hu1@zte.com.cn>
Wed, 14 Sep 2016 09:06:37 +0000 (17:06 +0800)
Change-Id: I7bac354212fa12c08aaa266f057bb10bedb5c4c3
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogAppConfiguration.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/entity/InputParameter.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/AbstractModelParser.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ModelParserFactory.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/entity/AriaParserRequest.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/entity/AriaParserResult.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/service/AriaParserServiceConsumer.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/service/IAriaParserRest.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/ToscaYamlModelParser.java
catalog-core/distribution/catalog-standalone/src/main/assembly/conf/catalog.yml

index d9a0eea..7973755 100644 (file)
@@ -45,6 +45,12 @@ public class CatalogAppConfiguration extends Configuration {
 
   @NotEmpty
   private String yamlParseAddr;
+  
+  @NotEmpty
+  private String parserType;
+  
+  @NotEmpty
+  private String ariaParserAddr;
 
   @NotEmpty
   private String wso2HostIp;
@@ -143,6 +149,22 @@ public class CatalogAppConfiguration extends Configuration {
   public void setYamlParseAddr(String yamlParseAddr) {
     this.yamlParseAddr = yamlParseAddr;
   }
+  
+  public String getParserType() {
+    return parserType;
+  }
+
+  public void setParserType(String parserType) {
+    this.parserType = parserType;
+  }
+
+  public String getAriaParserAddr() {
+    return ariaParserAddr;
+  }
+
+  public void setAriaParserAddr(String ariaParserAddr) {
+    this.ariaParserAddr = ariaParserAddr;
+  }
 
   @JsonProperty
   public String getWso2HostIp() {
index 5dc9a24..a3f80a9 100644 (file)
@@ -28,6 +28,6 @@ public class InputParameter {
   private String name;
   private EnumDataType type = EnumDataType.STRING;
   private String description;
-  private String defaultValue;
+  private Object defaultValue;
   private boolean required;
 }
index 4bfcac0..4cb59b0 100644 (file)
 
 package org.openo.commontosca.catalog.model.parser;
 
+import org.openo.commontosca.catalog.common.MsbAddrConfig;
+import org.openo.commontosca.catalog.common.ToolUtil;
 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
+import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
+import org.openo.commontosca.catalog.model.entity.NodeTemplate;
+import org.openo.commontosca.catalog.wrapper.PackageWrapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
 
 public abstract class AbstractModelParser {
+  private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
+
   
   public abstract String parse(String packageId, String fileLocation)
       throws CatalogResourceException;
+  
+  public String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
+    String destPath = Class.class.getClass().getResource("/").getPath()
+        + org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerPath()
+        + toTempFileLocalPath(fileLocation);
+    if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(fileLocation, destPath,
+        true)) {
+      throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
+    }
+    return destPath;
+  }
+  
+  public String getUrl(String uri) {
+    String url = null;
+    if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) {
+      url = MsbAddrConfig.getMsbAddress() + uri.substring(1);
+    }
+    url = MsbAddrConfig.getMsbAddress() + uri;
+    String urlresult = url.replace("\\", "/");
+    return urlresult;
+  }
+  
+  protected String toTempFileLocalPath(String fileLocation) {
+    return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
+  }
+  
+  protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
+    if (isNsType(substitutionType)) {
+      return EnumTemplateType.NS;
+    }
+
+    if (isVnfType(substitutionType)) {
+      return EnumTemplateType.VNF;
+    }
+
+    return getTemplateTypeFromNodeTemplates(ntList);
+  }
+  
+  private boolean isVnfType(String type) {
+    if (ToolUtil.isTrimedEmptyString(type)) {
+      return false;
+    }
+    return type.toUpperCase().contains(".VNF");
+  }
+
+  private boolean isNsType(String type) {
+    if (ToolUtil.isTrimedEmptyString(type)) {
+      return false;
+    }
+    return type.toUpperCase().contains(".NS");
+  }
+  
+  private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
+    for (NodeTemplate nt : ntList) {
+      if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
+        return EnumTemplateType.NS;
+      }
+    }
+
+    return EnumTemplateType.VNF;
+  }
+  
+  private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
+  
+  protected CsarFileUriResponse buildServiceTemplateDownloadUri(String packageId, String fileLocation)
+      throws CatalogResourceException {
+    Map<String, String> toscaMeta = parseToscaMeta(fileLocation);
+    String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
+    CsarFileUriResponse stDownloadUri =
+        PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName);
+    return stDownloadUri;
+  }
+  
+  @SuppressWarnings("resource")
+  protected Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
+    Map<String, String> toscaMeta = new HashMap<>();
+
+    ZipInputStream zin = null;
+    BufferedReader br = null;
+    try {
+      InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
+      zin = new ZipInputStream(in);
+      ZipEntry ze;
+      while ((ze = zin.getNextEntry()) != null) {
+        if (("TOSCA-Metadata" + File.separator + "TOSCA.meta").equals(ze.getName())
+            || "TOSCA-Metadata/TOSCA.meta".equals(ze.getName())) {
+          ZipFile zf = new ZipFile(fileLocation);
+          br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
+          String line;
+          String[] tmps;
+          while ((line = br.readLine()) != null) {
+            if (line.indexOf(":") > 0) {
+              tmps = line.split(":");
+              toscaMeta.put(tmps[0].trim(), tmps[1].trim());
+            }
+          }
+
+          return toscaMeta;
+        }
+      }
+
+    } catch (IOException e1) {
+      throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
+    } finally {
+      closeStreamAndReader(zin, br);
+    }
+
+    return toscaMeta;
+  }
+  
+  private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
+    if (br != null) {
+      try {
+        br.close();
+      } catch (IOException e1) {
+        logger.error("Buffered reader close failed !");
+      }
+    }
+    if (zin != null) {
+      try {
+        zin.closeEntry();
+      } catch (IOException e2) {
+        logger.error("Zip inputStream close failed !");
+      }
+    }
+  }
 
 }
index a0de847..32442b9 100644 (file)
@@ -16,7 +16,9 @@
 
 package org.openo.commontosca.catalog.model.parser;
 
+import org.openo.commontosca.catalog.common.Config;
 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.AriaModelParser;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.ToscaYamlModelParser;
 
 import java.util.HashMap;
@@ -36,7 +38,18 @@ public class ModelParserFactory {
   private ModelParserFactory() {
     // PackageParseMap.put(EnumPackageFormat.TOSCA_XML, new
     // ToscaXmlModelParser());
-    pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new ToscaYamlModelParser());
+    if (isAriaParser()) {
+      pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new AriaModelParser());
+    } else {
+      pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new ToscaYamlModelParser());
+    }
+  }
+
+  /**
+   * @return
+   */
+  private boolean isAriaParser() {
+    return "aria".equalsIgnoreCase(Config.getConfigration().getParserType());
   }
 
   /**
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/AriaModelParser.java
new file mode 100644 (file)
index 0000000..4e9e66e
--- /dev/null
@@ -0,0 +1,296 @@
+/**
+ * Copyright 2016 [ZTE] and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openo.commontosca.catalog.model.parser.yaml.aria;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.openo.commontosca.catalog.common.ToolUtil;
+import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
+import org.openo.commontosca.catalog.db.resource.TemplateManager;
+import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
+import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
+import org.openo.commontosca.catalog.model.entity.EnumDataType;
+import org.openo.commontosca.catalog.model.entity.InputParameter;
+import org.openo.commontosca.catalog.model.entity.NodeTemplate;
+import org.openo.commontosca.catalog.model.entity.OutputParameter;
+import org.openo.commontosca.catalog.model.entity.RelationShip;
+import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
+import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
+import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Input;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Node.Relationship;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Output;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution.Mapping;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.service.AriaParserServiceConsumer;
+
+/**
+ * @author 10090474
+ *
+ */
+public class AriaModelParser extends AbstractModelParser {
+
+  /* (non-Javadoc)
+   * @see org.openo.commontosca.catalog.model.parser.AbstractModelParser#parse(java.lang.String, java.lang.String)
+   */
+  @Override
+  public String parse(String packageId, String fileLocation) throws CatalogResourceException {
+    AriaParserResult result = getAriaParserResult(fileLocation);
+    
+    // service template
+    CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
+    ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
+    // node templates
+    List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
+    st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
+    // save to db
+    TemplateManager.getInstance().addServiceTemplate(
+        TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
+    
+    // substitution
+    SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
+    if (stm != null) {
+      TemplateManager.getInstance()
+          .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
+    }
+    
+    return st.getServiceTemplateId();
+  }
+  
+
+  /**
+   * @param serviceTemplateId
+   * @param result
+   * @return
+   */
+  private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
+      AriaParserResult result) {
+    String type = getSubstitutionType(result);
+    if (ToolUtil.isTrimedEmptyString(type)) {
+      return null;
+    }
+    
+    org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult.Substitution stm =
+        result.getSubstitution();
+    return new SubstitutionMapping(
+        serviceTemplateId,
+        type,
+        parseSubstitutionRequirements(stm.getRequirement()),
+        parseSubstitutionCapabilities(stm.getCapabilities()));
+  }
+
+
+  /**
+   * @param capabilities
+   * @return
+   */
+  private Map<String, String[]> parseSubstitutionCapabilities(Mapping[] capabilities) {
+    return parseMappings(capabilities);
+  }
+
+
+  private Map<String, String[]> parseMappings(Mapping[] mappings) {
+    Map<String, String[]> ret = new HashMap<>();
+    if (mappings != null) {
+      for (Mapping mapping : mappings) {
+        ret.put(mapping.getMapped_name(), new String[]{mapping.getNode_id(), mapping.getName()});
+      }
+    }
+
+    return ret;
+  }
+
+  /**
+   * @param requirement
+   * @return
+   */
+  private Map<String, String[]> parseSubstitutionRequirements(Mapping[] requirement) {
+    return parseMappings(requirement);
+  }
+
+  /**
+   * @param result
+   * @return
+   */
+  private String getSubstitutionType(AriaParserResult result) {
+    if (result.getSubstitution() == null) {
+      return null;
+    }
+    return result.getSubstitution().getNode_type_name();
+  }
+
+
+  /**
+   * @param packageId
+   * @param serviceTemplateId
+   * @param result
+   * @return
+   */
+  private List<NodeTemplate> parseNodeTemplates(String packageId, String serviceTemplateId,
+      AriaParserResult result) {
+    Node[] nodes = result.getNodes();
+    if (nodes == null || nodes.length == 0) {
+      return null;
+    }
+
+    List<NodeTemplate> retList = new ArrayList<>();
+    for (Node node : nodes) {
+      NodeTemplate ret = new NodeTemplate();
+      ret.setId(node.getName());
+      ret.setName(node.getName());
+      ret.setType(node.getType_name());
+      ret.setProperties(node.getPropertyAssignments());
+      List<RelationShip> relationShipList =
+          parseNodeTemplateRelationShip(node.getRelationships(), node);
+      ret.setRelationShips(relationShipList);
+
+      retList.add(ret);
+    }
+
+    return retList;
+  }
+
+
+  /**
+   * @param relationships
+   * @param sourceNode 
+   * @return
+   */
+  private List<RelationShip> parseNodeTemplateRelationShip(Relationship[] relationships, Node sourceNode) {
+    List<RelationShip> retList = new ArrayList<>();
+
+    if (relationships == null || relationships.length == 0) {
+      return retList;
+    }
+
+    for (Relationship relationship : relationships) {
+      RelationShip ret = new RelationShip();
+      ret.setSourceNodeId(sourceNode.getName());
+      ret.setSourceNodeName(sourceNode.getName());
+      ret.setTargetNodeId(relationship.getTemplate_name());
+      ret.setTargetNodeName(relationship.getTemplate_name());
+      ret.setType(relationship.getType_name());
+      retList.add(ret);
+    }
+
+    return retList;
+  }
+
+
+  /**
+   * @param packageId
+   * @param result
+   * @param downloadUri
+   * @return
+   */
+  private ServiceTemplate parseServiceTemplate(String packageId, AriaParserResult result,
+      String downloadUri) {
+    ServiceTemplate st = new ServiceTemplate();
+
+    st.setServiceTemplateId(ToolUtil.generateId());
+    st.setTemplateName(result.getMetadata().get("template_name"));
+    st.setVendor(result.getMetadata().get("template_author"));
+    st.setVersion(result.getMetadata().get("template_version"));
+    st.setCsarid(packageId);
+    st.setDownloadUri(downloadUri);
+    st.setInputs(parseInputs(result));
+    st.setOutputs(parseOutputs(result));
+    return st;
+  }
+
+
+  /**
+   * @param result
+   * @return
+   */
+  private InputParameter[] parseInputs(AriaParserResult result) {
+    Map<String, Input> inputs = result.getInputs();
+    if (inputs == null || inputs.isEmpty()) {
+      return new InputParameter[0];
+    }
+    List<InputParameter> retList = new ArrayList<InputParameter>();
+    for (Entry<String, Input> e : inputs.entrySet()) {
+      retList.add(
+          new InputParameter(
+              e.getKey(),
+              getEnumDataType(e.getValue().getType_name()),
+              e.getValue().getDescription(),
+              e.getValue().getValue(),
+              false));
+    }
+    return retList.toArray(new InputParameter[0]);
+  }
+  
+  /**
+   * @param type
+   * @return
+   */
+  private EnumDataType getEnumDataType(String type) {
+    if (EnumDataType.INTEGER.toString().equalsIgnoreCase(type)) {
+      return EnumDataType.INTEGER;
+    }
+
+    if (EnumDataType.FLOAT.toString().equalsIgnoreCase(type)) {
+      return EnumDataType.FLOAT;
+    }
+
+    if (EnumDataType.BOOLEAN.toString().equalsIgnoreCase(type)) {
+      return EnumDataType.BOOLEAN;
+    }
+
+    return EnumDataType.STRING;
+  }
+
+
+  /**
+   * @param result
+   * @return
+   */
+  private OutputParameter[] parseOutputs(AriaParserResult result) {
+    Map<String, Output> outputs = result.getOutpus();
+    if (outputs == null || outputs.isEmpty()) {
+      return new OutputParameter[0];
+    }
+    
+    List<OutputParameter> retList = new ArrayList<OutputParameter>();
+    for (Entry<String, Output> e: outputs.entrySet()) {
+      retList.add(
+          new OutputParameter(
+              e.getKey(), e.getValue().getDescription(), e.getValue().getValue()));
+    }
+
+    return retList.toArray(new OutputParameter[0]);
+  }
+
+  private AriaParserResult getAriaParserResult(String fileLocation) throws CatalogResourceException {
+    String destPath = copyTemporaryFile2HttpServer(fileLocation);
+    try {
+      String url = getUrl(toTempFileLocalPath(fileLocation));
+      return AriaParserServiceConsumer.parseCsarPackage(url);
+    } finally {
+      if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
+        (new File(destPath)).delete();
+      }
+    }
+  }
+
+}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/entity/AriaParserRequest.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/entity/AriaParserRequest.java
new file mode 100644 (file)
index 0000000..7de7f98
--- /dev/null
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2016 [ZTE] and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.commontosca.catalog.model.parser.yaml.aria.entity;
+
+/**
+ * 
+ */
+import java.util.List;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class AriaParserRequest {
+  private String uri;
+  private List<Parameter> inputs;
+
+  @Data
+  @NoArgsConstructor
+  @AllArgsConstructor
+  public class Parameter {
+    private String name;
+    private String value;
+  }
+}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/entity/AriaParserResult.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/entity/AriaParserResult.java
new file mode 100644 (file)
index 0000000..b610ecd
--- /dev/null
@@ -0,0 +1,123 @@
+/**
+ * Copyright 2016 [ZTE] and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.commontosca.catalog.model.parser.yaml.aria.entity;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import com.google.gson.JsonObject;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class AriaParserResult {
+  private String description;
+  private Map<String, String> metadata;
+  private Node[] nodes;
+  private JsonObject groups;
+  private JsonObject policies;
+  private Substitution substitution;
+  private Map<String, Input> inputs;
+  private Map<String, Output> outpus;
+  
+  @Data
+  public class Substitution {
+    private String node_type_name;
+    private Mapping[] requirement;
+    private Mapping[] capabilities;
+    
+    @Data
+    public class Mapping {
+      private String mapped_name;
+      private String node_id;
+      private String name;
+      
+    }
+  }
+
+  @Data
+  public class Input {
+    private String type_name;
+    private Object value;
+    private String description;
+  }
+  
+  @Data
+  public class Output {
+    private String type_name;
+    private Object value;
+    private String description;
+  }
+
+  @Data
+  public class Node {
+    private String id;
+    private String name;
+    private String type_name;
+    private Map<String, Property> properties;
+    private JsonObject[] interfaces;
+    private JsonObject[] artifacts;
+    private JsonObject[] capabilities;
+    private Relationship[] relationships;
+    
+    @Data
+    public class Property {
+      private String type_name;
+      private Object value;
+      private String description;
+    }
+
+    @Data
+    public class Relationship {
+      private String target_node_id;
+      private String target_capability_name;
+      private String type_name;
+      private String template_name;
+      private Map<String, Property> properties;
+      private JsonObject[] source_interfaces;
+      private JsonObject[] target_interfaces;
+      
+      @Data
+      public class Property {
+        private String type_name;
+        private Object value;
+        private String description;
+      }
+    }
+
+    /**
+     * @return
+     */
+    public Map<String, Object> getPropertyAssignments() {
+      if (this.properties == null || this.properties.isEmpty()) {
+        return new HashMap<String, Object>();
+      }
+      
+      Map<String, Object> ret = new HashMap<String, Object>();
+      for (Entry<String, Property> e : this.properties.entrySet()) {
+        ret.put(e.getKey(), e.getValue().getValue());
+      }
+
+      return ret;
+    }
+  }
+}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/service/AriaParserServiceConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/service/AriaParserServiceConsumer.java
new file mode 100644 (file)
index 0000000..53b1822
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+ * Copyright 2016 [ZTE] and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.commontosca.catalog.model.parser.yaml.aria.service;
+
+import org.glassfish.jersey.client.ClientConfig;
+import org.openo.commontosca.catalog.common.Config;
+import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserRequest;
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
+
+import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
+import com.google.gson.Gson;
+
+
+public class AriaParserServiceConsumer {
+  public static AriaParserResult parseCsarPackage(String uri) throws CatalogResourceException {
+    return parseCsarPackage(new AriaParserRequest(uri, null));
+  }
+  /**
+   * parse csar package via aria parser.
+   * 
+   * @param request parse yaml request
+   * @return parase yaml result
+   * @throws CatalogResourceException e
+   */
+  public static AriaParserResult parseCsarPackage(AriaParserRequest request)
+      throws CatalogResourceException {
+    try {
+      IAriaParserRest parseProxy =
+          ConsumerFactory.createConsumer(
+              Config.getConfigration().getAriaParserAddr(),
+              new ClientConfig(),
+              IAriaParserRest.class);
+      String jsonStr = parseProxy.parse(request);
+      return new Gson().fromJson(jsonStr, AriaParserResult.class);
+    } catch (Exception e) {
+      throw new CatalogResourceException("Call aria parser api failed.", e);
+    }
+
+  }
+}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/service/IAriaParserRest.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/aria/service/IAriaParserRest.java
new file mode 100644 (file)
index 0000000..4b53f34
--- /dev/null
@@ -0,0 +1,35 @@
+/**
+ * Copyright 2016 [ZTE] and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.commontosca.catalog.model.parser.yaml.aria.service;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserRequest;
+
+
+@Path("/indirect/plan")
+public interface IAriaParserRest {
+
+  @POST
+  @Consumes(MediaType.APPLICATION_JSON)
+  @Produces(MediaType.APPLICATION_JSON)
+  String parse(AriaParserRequest request) throws Exception;
+}
index 88a5ba4..48e1d6a 100644 (file)
@@ -16,7 +16,6 @@
 
 package org.openo.commontosca.catalog.model.parser.yaml.zte;
 
-import org.openo.commontosca.catalog.common.MsbAddrConfig;
 import org.openo.commontosca.catalog.common.ToolUtil;
 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
 import org.openo.commontosca.catalog.db.resource.TemplateManager;
@@ -31,61 +30,46 @@ import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
-import org.openo.commontosca.catalog.model.parser.EnumTemplateType;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.Plan.PlanValue.PlanInput;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
-import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
+import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
-import org.openo.commontosca.catalog.wrapper.PackageWrapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-import java.util.zip.ZipInputStream;
-
 
 public class ToscaYamlModelParser extends AbstractModelParser {
-
-  private static final Object TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
-  private static final Logger LOGGER = LoggerFactory.getLogger(ToscaYamlModelParser.class);
+  private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
 
   @Override
   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
+    logger.info("tosca-yaml-parser parse begin.");
     ParseYamlResult result = getParseYamlResult(fileLocation);
     
-    Map<String, String> toscaMeta = parseToscaMeta(fileLocation);
-    String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
-    CsarFileUriResponse stDownloadUri =
-        PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName);
-
+    // service template
+    CsarFileUriResponse stDownloadUri = buildServiceTemplateDownloadUri(packageId, fileLocation);
     ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri());
+    // workflow
     ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation);
     st.setOperations(operations);
+    // node templates
     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
-    st.setType(getTemplateType(result, ntList).toString());
-
+    st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
+    // save to db
     TemplateManager.getInstance().addServiceTemplate(
         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
 
+    // substitution
     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
     if (stm != null) {
       TemplateManager.getInstance()
@@ -107,85 +91,6 @@ public class ToscaYamlModelParser extends AbstractModelParser {
     }
   }
 
-  private String toTempFileLocalPath(String fileLocation) {
-    return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
-  }
-  
-  private String getUrl(String uri) {
-    String url = null;
-    if ((MsbAddrConfig.getMsbAddress().endsWith("/")) && uri.startsWith("/")) {
-      url = MsbAddrConfig.getMsbAddress() + uri.substring(1);
-    }
-    url = MsbAddrConfig.getMsbAddress() + uri;
-    String urlresult = url.replace("\\", "/");
-    return urlresult;
-  }
-
-  private String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
-    String destPath = Class.class.getClass().getResource("/").getPath()
-        + org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerPath()
-        + toTempFileLocalPath(fileLocation);
-    if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(fileLocation, destPath,
-        true)) {
-      throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
-    }
-    return destPath;
-  }
-
-  @SuppressWarnings("resource")
-  private Map<String, String> parseToscaMeta(String fileLocation) throws CatalogResourceException {
-    Map<String, String> toscaMeta = new HashMap<>();
-
-    ZipInputStream zin = null;
-    BufferedReader br = null;
-    try {
-      InputStream in = new BufferedInputStream(new FileInputStream(fileLocation));
-      zin = new ZipInputStream(in);
-      ZipEntry ze;
-      while ((ze = zin.getNextEntry()) != null) {
-        if (("TOSCA-Metadata" + File.separator + "TOSCA.meta").equals(ze.getName())
-            || "TOSCA-Metadata/TOSCA.meta".equals(ze.getName())) {
-          ZipFile zf = new ZipFile(fileLocation);
-          br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
-          String line;
-          String[] tmps;
-          while ((line = br.readLine()) != null) {
-            if (line.indexOf(":") > 0) {
-              tmps = line.split(":");
-              toscaMeta.put(tmps[0].trim(), tmps[1].trim());
-            }
-          }
-
-          return toscaMeta;
-        }
-      }
-
-    } catch (IOException e1) {
-      throw new CatalogResourceException("Parse Tosca Meta Fail.", e1);
-    } finally {
-      closeStreamAndReader(zin, br);
-    }
-
-    return toscaMeta;
-  }
-
-  private void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
-    if (br != null) {
-      try {
-        br.close();
-      } catch (IOException e1) {
-        LOGGER.error("Buffered reader close failed !");
-      }
-    }
-    if (zin != null) {
-      try {
-        zin.closeEntry();
-      } catch (IOException e2) {
-        LOGGER.error("Zip inputStream close failed !");
-      }
-    }
-  }
-
   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
     request.setPath(fileLocation);
@@ -194,7 +99,7 @@ public class ToscaYamlModelParser extends AbstractModelParser {
 
   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
       ParseYamlResult result) {
-    String type = getSubstitutionMappingType(result);
+    String type = getSubstitutionType(result);
     if (ToolUtil.isTrimedEmptyString(type)) {
       return null;
     }
@@ -371,47 +276,11 @@ public class ToscaYamlModelParser extends AbstractModelParser {
     return retList;
   }
 
-  private EnumTemplateType getTemplateType(ParseYamlResult result, List<NodeTemplate> ntList) {
-    String type = getSubstitutionMappingType(result);
-    if (isNsType(type)) {
-      return EnumTemplateType.NS;
-    }
-
-    if (isVnfType(type)) {
-      return EnumTemplateType.VNF;
-    }
-
-    return getTemplateTypeFromNodeTemplates(ntList);
-  }
-
-  private String getSubstitutionMappingType(ParseYamlResult result) {
+  private String getSubstitutionType(ParseYamlResult result) {
     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
       return null;
     }
     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
   }
 
-  private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
-    for (NodeTemplate nt : ntList) {
-      if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
-        return EnumTemplateType.NS;
-      }
-    }
-
-    return EnumTemplateType.VNF;
-  }
-
-  private boolean isVnfType(String type) {
-    if (ToolUtil.isTrimedEmptyString(type)) {
-      return false;
-    }
-    return type.toUpperCase().contains(".VNF");
-  }
-
-  private boolean isNsType(String type) {
-    if (ToolUtil.isTrimedEmptyString(type)) {
-      return false;
-    }
-    return type.toUpperCase().contains(".NS");
-  }
 }
index 07dec54..01a87b3 100644 (file)
@@ -26,6 +26,10 @@ opentoscaServerAddr: http://127.0.0.1:1337
 \r
 yamlParseAddr: http://127.0.0.1:8210\r
 \r
+parserType: zte\r
+\r
+ariaParserAddr: http://127.0.0.1:8204/openoapi/tosca/v1\r
+\r
 wso2HostIp: 127.0.0.1\r
 \r
 wso2HostPort: 9090\r