From d8f44a13678bc542ebeded9618484c951a463eb1 Mon Sep 17 00:00:00 2001 From: YuanHu Date: Tue, 13 Sep 2016 15:02:29 +0800 Subject: Change the package name of these class. org.openo.commontosca.catalog.model.parser.yaml --> org.openo.commontosca.catalog.model.parser.yaml.zte Change-Id: I02c7f3468205f0341bedf03fbd66a2fed07a2155 Signed-off-by: YuanHu --- .../catalog/model/parser/ModelParserFactory.java | 1 + .../catalog/model/parser/ToscaYamlModelParser.java | 415 ---------- .../catalog/model/parser/yaml/IYamlParseRest.java | 35 - .../parser/yaml/YamlParseServiceConsumer.java | 52 -- .../yaml/entity/EnumYamlServiceTemplateInfo.java | 41 - .../yaml/entity/ParseYamlRequestParemeter.java | 66 -- .../model/parser/yaml/entity/ParseYamlResult.java | 890 --------------------- .../parser/yaml/zte/YamlParseServiceConsumer.java | 52 ++ .../zte/entity/EnumYamlServiceTemplateInfo.java | 41 + .../yaml/zte/entity/ParseYamlRequestParemeter.java | 66 ++ .../parser/yaml/zte/entity/ParseYamlResult.java | 890 +++++++++++++++++++++ .../parser/yaml/zte/service/IYamlParseRest.java | 35 + .../yaml/zte/service/ToscaYamlModelParser.java | 417 ++++++++++ .../catalog/resources/TemplateResource.java | 2 +- 14 files changed, 1503 insertions(+), 1500 deletions(-) delete mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ToscaYamlModelParser.java delete mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/IYamlParseRest.java delete mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/YamlParseServiceConsumer.java delete mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/EnumYamlServiceTemplateInfo.java delete mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlRequestParemeter.java delete mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlResult.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/YamlParseServiceConsumer.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/EnumYamlServiceTemplateInfo.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlRequestParemeter.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/IYamlParseRest.java create mode 100644 catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/ToscaYamlModelParser.java (limited to 'catalog-core/catalog-mgr/src') diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ModelParserFactory.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ModelParserFactory.java index acd02068..7d4b2e22 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ModelParserFactory.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ModelParserFactory.java @@ -17,6 +17,7 @@ package org.openo.commontosca.catalog.model.parser; import org.openo.commontosca.catalog.db.exception.CatalogResourceException; +import org.openo.commontosca.catalog.model.parser.yaml.zte.service.ToscaYamlModelParser; import java.util.HashMap; import java.util.Map; diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ToscaYamlModelParser.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ToscaYamlModelParser.java deleted file mode 100644 index 739ed5ed..00000000 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/ToscaYamlModelParser.java +++ /dev/null @@ -1,415 +0,0 @@ -/** - * 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; - -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; -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.ServiceTemplateOperation; -import org.openo.commontosca.catalog.model.entity.SubstitutionMapping; -import org.openo.commontosca.catalog.model.parser.yaml.YamlParseServiceConsumer; -import org.openo.commontosca.catalog.model.parser.yaml.entity.EnumYamlServiceTemplateInfo; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlRequestParemeter; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult.Plan; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult.Plan.PlanValue.PlanInput; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult.TopologyTemplate.Input; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult.TopologyTemplate.Output; -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); - - @Override - public String parse(String packageId, String fileLocation) throws CatalogResourceException { - ParseYamlResult result = getParseYamlResult(fileLocation); - - Map toscaMeta = parseToscaMeta(fileLocation); - String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS); - CsarFileUriResponse stDownloadUri = - PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName); - - ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri()); - ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation); - st.setOperations(operations); - List ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result); - st.setType(getTemplateType(result, ntList).toString()); - - TemplateManager.getInstance().addServiceTemplate( - TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList)); - - SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result); - if (stm != null) { - TemplateManager.getInstance() - .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm)); - } - - return st.getServiceTemplateId(); - } - - private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException { - String destPath = copyTemporaryFile2HttpServer(fileLocation); - try { - String url = getUrl(toTempFileLocalPath(fileLocation)); - return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url)); - } finally { - if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) { - (new File(destPath)).delete(); - } - } - } - - 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 parseToscaMeta(String fileLocation) throws CatalogResourceException { - Map 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); - return request; - } - - private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId, - ParseYamlResult result) { - String type = getSubstitutionMappingType(result); - if (ToolUtil.isTrimedEmptyString(type)) { - return null; - } - - org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult - .TopologyTemplate.SubstitutionMapping stm = - result.getTopologyTemplate().getSubstitutionMappings(); - return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(), - stm.getCapabilityList()); - } - - private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result, - String stDownloadUri) { - ServiceTemplate st = new ServiceTemplate(); - - st.setServiceTemplateId(ToolUtil.generateId()); - st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName())); - st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName())); - st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName())); - st.setCsarid(packageId); - st.setDownloadUri(stDownloadUri); - st.setInputs(parseInputs(result)); - st.setOutputs(parseOutputs(result)); - return st; - } - - private InputParameter[] parseInputs(ParseYamlResult result) { - List inputList = result.getTopologyTemplate().getInputs(); - if (inputList == null) { - return new InputParameter[0]; - } - List retList = new ArrayList(); - for (Input input : inputList) { - retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()), - input.getDescription(), input.getDefault(), input.isRequired())); - } - return retList.toArray(new InputParameter[0]); - } - - private OutputParameter[] parseOutputs(ParseYamlResult result) { - List outputList = result.getTopologyTemplate().getOutputs(); - if (outputList == null || outputList.isEmpty()) { - return new OutputParameter[0]; - } - List retList = new ArrayList(); - for (Output output : outputList) { - retList - .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue())); - } - return retList.toArray(new OutputParameter[0]); - } - - private ServiceTemplateOperation[] parseOperations(List planList, String zipFileLocation) - throws CatalogResourceException { - if (planList == null || planList.isEmpty()) { - return new ServiceTemplateOperation[0]; - } - - List opList = new ArrayList<>(); - for (Plan plan : planList) { - ServiceTemplateOperation op = new ServiceTemplateOperation(); - op.setName(plan.getName()); - op.setDescription(plan.getDescription()); - checkPlanLanguage(plan.getPlanLanguage()); - DeployPackageResponse response = - Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference()); - op.setPackageName(parsePackageName(response)); - op.setProcessId(response.getProcessId()); - op.setInputs(parsePlanInputs(plan.getInputList())); - - opList.add(op); - - } - return opList.toArray(new ServiceTemplateOperation[0]); - } - - private String parsePackageName(DeployPackageResponse response) { - String packageName = response.getPackageName(); - if (packageName != null && packageName.indexOf("-") > 0) { - packageName = packageName.substring(0, packageName.lastIndexOf("-")); - } - return packageName; - } - - private void checkPlanLanguage(String planLanguage) throws CatalogResourceException { - if (planLanguage == null || planLanguage.isEmpty()) { - throw new CatalogResourceException("Plan Language is empty."); - } - if (planLanguage.equalsIgnoreCase("bpel")) { - return; - } - if (planLanguage.equalsIgnoreCase("bpmn")) { - return; - } - if (planLanguage.equalsIgnoreCase("bpmn4tosca")) { - return; - } - throw new CatalogResourceException( - "Plan Language is not supported. Language = " + planLanguage); - } - - private InputParameter[] parsePlanInputs(List inputList) { - if (inputList == null || inputList.isEmpty()) { - return new InputParameter[0]; - } - - List retList = new ArrayList<>(); - for (PlanInput input : inputList) { - retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()), - input.getDescription(), input.getDefault(), input.isRequired())); - } - return retList.toArray(new InputParameter[0]); - } - - 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; - } - - private List parseNodeTemplates(String csarId, String templateId, - ParseYamlResult result) { - List nodetemplateList = - result.getTopologyTemplate().getNodeTemplates(); - if (nodetemplateList == null) { - return null; - } - - List retList = new ArrayList<>(); - for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) { - NodeTemplate ret = new NodeTemplate(); - ret.setId(nodeTemplate.getName()); - ret.setName(nodeTemplate.getName()); - ret.setType(nodeTemplate.getNodeType()); - ret.setProperties(nodeTemplate.getPropertyList()); - List relationShipList = - parseNodeTemplateRelationShip(nodeTemplate.getRelationships()); - ret.setRelationShips(relationShipList); - - retList.add(ret); - } - - return retList; - } - - - private List parseNodeTemplateRelationShip(List relationshipList) { - List retList = new ArrayList<>(); - - if (relationshipList == null) { - return retList; - } - - for (Relationship relationship : relationshipList) { - RelationShip ret = new RelationShip(); - ret.setSourceNodeId(relationship.getSourceNodeName()); - ret.setSourceNodeName(relationship.getSourceNodeName()); - ret.setTargetNodeId(relationship.getTargetNodeName()); - ret.setTargetNodeName(relationship.getTargetNodeName()); - ret.setType(relationship.getType()); - retList.add(ret); - } - - return retList; - } - - private EnumTemplateType getTemplateType(ParseYamlResult result, List 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) { - if (result.getTopologyTemplate().getSubstitutionMappings() == null) { - return null; - } - return result.getTopologyTemplate().getSubstitutionMappings().getNodeType(); - } - - private EnumTemplateType getTemplateTypeFromNodeTemplates(List 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"); - } -} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/IYamlParseRest.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/IYamlParseRest.java deleted file mode 100644 index 39e419cc..00000000 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/IYamlParseRest.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * 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; - -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlRequestParemeter; - -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; - - -@Path("/parse") -public interface IYamlParseRest { - - @POST - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - String parse(ParseYamlRequestParemeter request) throws Exception; -} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/YamlParseServiceConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/YamlParseServiceConsumer.java deleted file mode 100644 index 2b5ee9aa..00000000 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/YamlParseServiceConsumer.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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; - -import com.google.gson.Gson; - -import com.eclipsesource.jaxrs.consumer.ConsumerFactory; - -import org.glassfish.jersey.client.ClientConfig; -import org.openo.commontosca.catalog.common.MsbUtil; -import org.openo.commontosca.catalog.db.exception.CatalogResourceException; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlRequestParemeter; -import org.openo.commontosca.catalog.model.parser.yaml.entity.ParseYamlResult; - - -public class YamlParseServiceConsumer { - /** - * get service template by template id from container service. - * - * @param request parse yaml request - * @return parase yaml result - * @throws CatalogResourceException e - */ - public static ParseYamlResult getServiceTemplates(final ParseYamlRequestParemeter request) - throws CatalogResourceException { - try { - ClientConfig config = new ClientConfig(); - IYamlParseRest yamlParseProxy = - ConsumerFactory.createConsumer(MsbUtil.getYamlParseBaseUrl(), config, - IYamlParseRest.class); - String jsonStr = yamlParseProxy.parse(request); - return new Gson().fromJson(jsonStr, ParseYamlResult.class); - } catch (Exception e1) { - throw new CatalogResourceException("Call parser api failed.", e1); - } - - } -} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/EnumYamlServiceTemplateInfo.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/EnumYamlServiceTemplateInfo.java deleted file mode 100644 index 54540bf0..00000000 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/EnumYamlServiceTemplateInfo.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * 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.entity; - - -public enum EnumYamlServiceTemplateInfo { - ID { - @Override - public String getName() { - return "id"; - } - }, - VERSION { - @Override - public String getName() { - return "version"; - } - }, - PROVIDER { - @Override - public String getName() { - return "vendor"; - } - }; - - public abstract String getName(); -} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlRequestParemeter.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlRequestParemeter.java deleted file mode 100644 index 8bfb1650..00000000 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlRequestParemeter.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * 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.entity; - -import java.util.List; - -public class ParseYamlRequestParemeter { - private String path = "E:\\NFVO\\130. yaml2xml\\0. tosca-parser\\sample\\ag-vnfd-floatingIp.zip"; - - private List extensionList; - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public List getExtensionList() { - return extensionList; - } - - public void setExtensionList(List extensionList) { - this.extensionList = extensionList; - } - - public Extension createExtension() { - return new Extension(); - } - - public class Extension { - private String name; - private String value; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - } -} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlResult.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlResult.java deleted file mode 100644 index 61014a1f..00000000 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/entity/ParseYamlResult.java +++ /dev/null @@ -1,890 +0,0 @@ -/** - * 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.entity; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.annotations.SerializedName; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - - -public class ParseYamlResult { - private String toscaDefinitionsVersion; - private String description; - private JsonObject nodeTypes; - private JsonObject capabilityTypes; - private JsonObject relationshipTypes; - private JsonObject policyTypes; - private TopologyTemplate topologyTemplate; - private Map metadata; - private JsonObject plans; - - - public String getToscaDefinitionsVersion() { - return toscaDefinitionsVersion; - } - - public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) { - this.toscaDefinitionsVersion = toscaDefinitionsVersion; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public JsonObject getNodeTypes() { - return nodeTypes; - } - - public void setNodeTypes(JsonObject nodeTypes) { - this.nodeTypes = nodeTypes; - } - - public List getNodeTypeList() { - return jsonObject2NodeTypes(nodeTypes); - } - - private ArrayList jsonObject2NodeTypes(JsonObject nodeTypes) { - ArrayList nodeTypeList = new ArrayList(); - Iterator> iterator = nodeTypes.entrySet().iterator(); - while (iterator.hasNext()) { - NodeType type = new NodeType(); - Entry next = iterator.next(); - type.setType(next.getKey()); - type.setValue(new Gson().fromJson(next.getValue(), NodeType.NodeTypeValue.class)); - nodeTypeList.add(type); - } - return nodeTypeList; - } - - public JsonObject getCapabilityTypes() { - return capabilityTypes; - } - - public void setCapabilityTypes(JsonObject capabilityTypes) { - this.capabilityTypes = capabilityTypes; - } - - public JsonObject getRelationshipTypes() { - return relationshipTypes; - } - - public void setRelationshipTypes(JsonObject relationshipTypes) { - this.relationshipTypes = relationshipTypes; - } - - public List getRelationshipTypeList() { - return jsonObject2RelationshipTypes(relationshipTypes); - } - - private ArrayList jsonObject2RelationshipTypes(JsonObject relationshipTypes) { - ArrayList relationshipTypeList = new ArrayList(); - Iterator> iterator = relationshipTypes.entrySet().iterator(); - while (iterator.hasNext()) { - RelationshipType type = new RelationshipType(); - Entry next = iterator.next(); - type.setType(next.getKey()); - type.setValue(new Gson().fromJson(next.getValue(), RelationshipType.RelationshipValue.class)); - relationshipTypeList.add(type); - } - return relationshipTypeList; - } - - public JsonObject getPolicyTypes() { - return policyTypes; - } - - public void setPolicyTypes(JsonObject policyTypes) { - this.policyTypes = policyTypes; - } - - public TopologyTemplate getTopologyTemplate() { - return topologyTemplate; - } - - public void setTopologyTemplate(TopologyTemplate topologyTemplate) { - this.topologyTemplate = topologyTemplate; - } - - public Map getMetadata() { - return metadata; - } - - public void setMetadata(Map metadata) { - this.metadata = metadata; - } - - public JsonObject getPlans() { - return plans; - } - - public void setPlans(JsonObject plans) { - this.plans = plans; - } - - public List getPlanList() { - return jsonObject2PlanList(this.plans); - } - - private List jsonObject2PlanList(JsonObject plans) { - if (plans == null) { - return new ArrayList<>(); - } - List retList = new ArrayList<>(); - Iterator> iterator = plans.entrySet().iterator(); - while (iterator.hasNext()) { - Plan ret = new Plan(); - Entry next = iterator.next(); - ret.setName(next.getKey()); - ret.setValue(new Gson().fromJson(next.getValue(), Plan.PlanValue.class)); - retList.add(ret); - } - return retList; - } - - public class TopologyTemplate { - private String description; - private List inputs; - private List outputs; - private List nodeTemplates; - private SubstitutionMapping substitutionMappings; - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public List getInputs() { - return inputs; - } - - public void setInputs(List inputs) { - this.inputs = inputs; - } - - public List getOutputs() { - return outputs; - } - - public void setOutputs(List outputs) { - this.outputs = outputs; - } - - public List getNodeTemplates() { - return nodeTemplates; - } - - public void setNodeTemplates(List nodeTemplates) { - this.nodeTemplates = nodeTemplates; - } - - public SubstitutionMapping getSubstitutionMappings() { - return substitutionMappings; - } - - public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) { - this.substitutionMappings = substitutionMappings; - } - - public class Input { - private String name; - private String type; - private String description; - private String defaultValue; - private boolean required; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getDefault() { - return defaultValue; - } - - public void setDefault(String defaultValue) { - this.defaultValue = defaultValue; - } - - public boolean isRequired() { - return required; - } - - public void setRequired(boolean required) { - this.required = required; - } - } - - public class Output { - private String name; - private String description; - private Object value; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Object getValue() { - return value; - } - - public void setValue(Object value) { - this.value = value; - } - } - - public class NodeTemplate { - private String name; - private String nodeType; - private JsonObject properties; - private JsonObject[] requirements; - private JsonObject capabilities; - private List relationships; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getNodeType() { - return nodeType; - } - - public void setNodeType(String nodeType) { - this.nodeType = nodeType; - } - - public JsonObject getProperties() { - return properties; - } - - public void setProperties(JsonObject properties) { - this.properties = properties; - } - - public Map getPropertyList() { - return jsonObject2Properties(properties); - } - - private Map jsonObject2Properties(JsonObject properties) { - Map ret = new HashMap<>(); - Iterator> iterator = properties.entrySet().iterator(); - while (iterator.hasNext()) { - Entry next = iterator.next(); - ret.put(next.getKey(), next.getValue().getAsString()); - } - return ret; - } - - public JsonObject[] getRequirements() { - return requirements; - } - - public void setRequirements(JsonObject[] requirements) { - this.requirements = requirements; - } - - public JsonObject getCapabilities() { - return capabilities; - } - - public void setCapabilities(JsonObject capabilities) { - this.capabilities = capabilities; - } - - public List getRelationships() { - return relationships; - } - - public void setRelationships(List relationships) { - this.relationships = relationships; - } - - /** - * get scalable. - * @return NodeTemplateScalable - */ - public NodeTemplateScalable getScalable() { - if (capabilities == null) { - return null; - } - JsonElement scaleableJson = capabilities.get("scalable"); - if (scaleableJson == null || !scaleableJson.isJsonObject()) { - return null; - } - JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties"); - if (propertyJson == null || !propertyJson.isJsonObject()) { - return null; - } - - NodeTemplateScalable scalable = new NodeTemplateScalable(); - scalable - .setMin_instances(propertyJson.getAsJsonObject().get("min_instances").getAsString()); - scalable - .setMax_instances(propertyJson.getAsJsonObject().get("max_instances").getAsString()); - scalable.setDefault_instances( - propertyJson.getAsJsonObject().get("default_instances").getAsString()); - return scalable; - } - - public class Relationship { - private String targetNodeName; - private String type; - private String sourceNodeName; - - public String getTargetNodeName() { - return targetNodeName; - } - - public void setTargetNodeName(String targetNodeName) { - this.targetNodeName = targetNodeName; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getSourceNodeName() { - return sourceNodeName; - } - - public void setSourceNodeName(String sourceNodeName) { - this.sourceNodeName = sourceNodeName; - } - } - - public class NodeTemplateScalable { - private String minInstances; - private String maxInstances; - private String defaultInstances; - - public String getMin_instances() { - return minInstances; - } - - public void setMin_instances(String minInstances) { - this.minInstances = minInstances; - } - - public String getMax_instances() { - return maxInstances; - } - - public void setMax_instances(String maxInstances) { - this.maxInstances = maxInstances; - } - - public String getDefault_instances() { - return defaultInstances; - } - - public void setDefault_instances(String defaultInstances) { - this.defaultInstances = defaultInstances; - } - } - } - - public class SubstitutionMapping { - private String nodeType; - private JsonObject requirements; - private JsonObject capabilities; - private JsonObject properties; - - public String getNodeType() { - return nodeType; - } - - public void setNodeType(String nodeType) { - this.nodeType = nodeType; - } - - public JsonObject getRequirements() { - return requirements; - } - - public void setRequirements(JsonObject requirements) { - this.requirements = requirements; - } - - public Map getRequirementList() { - return jsonObjects2Requirements(this.requirements); - } - - private Map jsonObjects2Requirements(JsonObject requirements) { - Map ret = new HashMap(); - - Iterator> iterator = requirements.entrySet().iterator(); - while (iterator.hasNext()) { - Entry next = iterator.next(); - if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) { - ret.put(next.getKey(), new String[] {next.getValue().getAsString()}); - continue; - } - - if (next.getValue().isJsonArray()) { - String[] value = parseListValue((JsonArray) next.getValue()); - ret.put(next.getKey(), value); - } - } - - return ret; - } - - private String[] parseListValue(JsonArray jsonArray) { - String[] value = new String[jsonArray.size()]; - for (int i = 0, size = jsonArray.size(); i < size; i++) { - value[i] = jsonArray.get(i).getAsString(); - } - return value; - } - - public JsonObject getCapabilities() { - return capabilities; - } - - public void setCapabilities(JsonObject capabilities) { - this.capabilities = capabilities; - } - - public Map getCapabilityList() { - return jsonObject2Capabilities(this.capabilities); - } - - private Map jsonObject2Capabilities(JsonObject capabilities) { - Map ret = new HashMap(); - - Iterator> iterator = capabilities.entrySet().iterator(); - while (iterator.hasNext()) { - Entry next = iterator.next(); - - if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) { - ret.put(next.getKey(), new String[] {next.getValue().getAsString()}); - continue; - } - - if (next.getValue().isJsonArray()) { - String[] value = parseListValue((JsonArray) next.getValue()); - ret.put(next.getKey(), value); - } - } - - return ret; - } - - public JsonObject getProperties() { - return properties; - } - - public void setProperties(JsonObject properties) { - this.properties = properties; - } - - public Map getPropertyList() { - return jsonObject2Properties(properties); - } - - private Map jsonObject2Properties(JsonObject properties) { - Map ret = new HashMap<>(); - Iterator> iterator = properties.entrySet().iterator(); - while (iterator.hasNext()) { - Entry next = iterator.next(); - ret.put(next.getKey(), next.getValue().getAsString()); - } - return ret; - } - } - } - - - public class RelationshipType { - private String type; - private RelationshipValue value; - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public RelationshipValue getValue() { - return value; - } - - public void setValue(RelationshipValue value) { - this.value = value; - } - - public class RelationshipValue { - private String derivedFrom; - private String[] validTargetTypes; - - public String getDerivedFrom() { - return derivedFrom; - } - - public void setDerivedFrom(String derivedFrom) { - this.derivedFrom = derivedFrom; - } - - public String[] getValid_target_types() { - return validTargetTypes; - } - - public void setValid_target_types(String[] validTargetTypes) { - this.validTargetTypes = validTargetTypes; - } - } - } - - - public class NodeType { - private String type; - private NodeTypeValue value; - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public NodeTypeValue getValue() { - return value; - } - - public void setValue(NodeTypeValue value) { - this.value = value; - } - - public class NodeTypeValue { - private String derivedFrom; - private JsonObject properties; - private JsonObject[] requirements; - private JsonObject capabilities; - - public String getDerivedFrom() { - return derivedFrom; - } - - public void setDerived_from(String derivedFrom) { - this.derivedFrom = derivedFrom; - } - - public JsonObject getProperties() { - return properties; - } - - public void setProperties(JsonObject properties) { - this.properties = properties; - } - - public List getPropertyList() { - return jsonObject2Properties(properties); - } - - private List jsonObject2Properties(JsonObject properties) { - List propertieList = new ArrayList(); - Iterator> iterator = properties.entrySet().iterator(); - while (iterator.hasNext()) { - NodeTypeProperty type = new NodeTypeProperty(); - Entry next = iterator.next(); - type.setKey(next.getKey()); - type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class)); - propertieList.add(type); - } - return propertieList; - } - - public class NodeTypeProperty { - private String key; - private JsonObject value; - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - /** - * get default value. - * @return default value - */ - public String getDefaultValue() { - JsonElement defaultValue = value.get("default"); - if (defaultValue == null || defaultValue.isJsonObject()) { - return ""; - } - - return defaultValue.getAsString(); - } - - public JsonObject getValue() { - return value; - } - - public void setValue(JsonObject value) { - this.value = value; - } - } - - public JsonObject[] getRequirements() { - return requirements; - } - - public void setRequirements(JsonObject[] requirements) { - this.requirements = requirements; - } - - public JsonObject getCapabilities() { - return capabilities; - } - - public void setCapabilities(JsonObject capabilities) { - this.capabilities = capabilities; - } - } - } - - public class Plan { - private String name; - private PlanValue value; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return value.getDescription(); - } - - public String getReference() { - return value.getReference(); - } - - public String getPlanLanguage() { - return value.getPlanLanguage(); - } - - public List getInputList() { - return value.getInputList(); - } - - public PlanValue getValue() { - return value; - } - - public void setValue(PlanValue value) { - this.value = value; - } - - public class PlanValue { - private String description; - private String reference; - private String planLanguage; - private JsonObject inputs; - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getReference() { - return reference; - } - - public void setReference(String reference) { - this.reference = reference; - } - - public String getPlanLanguage() { - return planLanguage; - } - - public void setPlanLanguage(String planLanguage) { - this.planLanguage = planLanguage; - } - - public JsonObject getInputs() { - return inputs; - } - - public void setInputs(JsonObject inputs) { - this.inputs = inputs; - } - - public List getInputList() { - return jsonObject2PlanInputList(inputs); - - } - - private List jsonObject2PlanInputList(JsonObject inputs) { - List retList = new ArrayList(); - Iterator> iterator = inputs.entrySet().iterator(); - while (iterator.hasNext()) { - PlanInput ret = new PlanInput(); - Entry next = iterator.next(); - ret.setName(next.getKey()); - ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class)); - retList.add(ret); - } - return retList; - } - - public class PlanInput { - private String name; - private PlanInputValue value; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getType() { - return value.getType(); - } - - public String getDescription() { - return value.getDescription(); - } - - public String getDefault() { - return value.getDefaultValue(); - } - - public boolean isRequired() { - return value.isRequired(); - } - - public PlanInputValue getValue() { - return value; - } - - public void setValue(PlanInputValue value) { - this.value = value; - } - - public class PlanInputValue { - private String type; - private String description; - @SerializedName("default") - private String defaultValue; - private boolean required; - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getDefaultValue() { - return defaultValue; - } - - public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; - } - - public boolean isRequired() { - return required; - } - - public void setRequired(boolean required) { - this.required = required; - } - } - } - } - } -} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/YamlParseServiceConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/YamlParseServiceConsumer.java new file mode 100644 index 00000000..14ac8a37 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/YamlParseServiceConsumer.java @@ -0,0 +1,52 @@ +/** + * 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.zte; + +import com.google.gson.Gson; +import com.eclipsesource.jaxrs.consumer.ConsumerFactory; + +import org.glassfish.jersey.client.ClientConfig; +import org.openo.commontosca.catalog.common.MsbUtil; +import org.openo.commontosca.catalog.db.exception.CatalogResourceException; +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.service.IYamlParseRest; + + +public class YamlParseServiceConsumer { + /** + * get service template by template id from container service. + * + * @param request parse yaml request + * @return parase yaml result + * @throws CatalogResourceException e + */ + public static ParseYamlResult getServiceTemplates(final ParseYamlRequestParemeter request) + throws CatalogResourceException { + try { + ClientConfig config = new ClientConfig(); + IYamlParseRest yamlParseProxy = + ConsumerFactory.createConsumer(MsbUtil.getYamlParseBaseUrl(), config, + IYamlParseRest.class); + String jsonStr = yamlParseProxy.parse(request); + return new Gson().fromJson(jsonStr, ParseYamlResult.class); + } catch (Exception e1) { + throw new CatalogResourceException("Call parser api failed.", e1); + } + + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/EnumYamlServiceTemplateInfo.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/EnumYamlServiceTemplateInfo.java new file mode 100644 index 00000000..9380c8c2 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/EnumYamlServiceTemplateInfo.java @@ -0,0 +1,41 @@ +/** + * 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.zte.entity; + + +public enum EnumYamlServiceTemplateInfo { + ID { + @Override + public String getName() { + return "id"; + } + }, + VERSION { + @Override + public String getName() { + return "version"; + } + }, + PROVIDER { + @Override + public String getName() { + return "vendor"; + } + }; + + public abstract String getName(); +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlRequestParemeter.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlRequestParemeter.java new file mode 100644 index 00000000..9dbb7a1c --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlRequestParemeter.java @@ -0,0 +1,66 @@ +/** + * 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.zte.entity; + +import java.util.List; + +public class ParseYamlRequestParemeter { + private String path = "E:\\NFVO\\130. yaml2xml\\0. tosca-parser\\sample\\ag-vnfd-floatingIp.zip"; + + private List extensionList; + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public List getExtensionList() { + return extensionList; + } + + public void setExtensionList(List extensionList) { + this.extensionList = extensionList; + } + + public Extension createExtension() { + return new Extension(); + } + + public class Extension { + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java new file mode 100644 index 00000000..5d2b864d --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/entity/ParseYamlResult.java @@ -0,0 +1,890 @@ +/** + * 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.zte.entity; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + + +public class ParseYamlResult { + private String toscaDefinitionsVersion; + private String description; + private JsonObject nodeTypes; + private JsonObject capabilityTypes; + private JsonObject relationshipTypes; + private JsonObject policyTypes; + private TopologyTemplate topologyTemplate; + private Map metadata; + private JsonObject plans; + + + public String getToscaDefinitionsVersion() { + return toscaDefinitionsVersion; + } + + public void setToscaDefinitionsVersion(String toscaDefinitionsVersion) { + this.toscaDefinitionsVersion = toscaDefinitionsVersion; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public JsonObject getNodeTypes() { + return nodeTypes; + } + + public void setNodeTypes(JsonObject nodeTypes) { + this.nodeTypes = nodeTypes; + } + + public List getNodeTypeList() { + return jsonObject2NodeTypes(nodeTypes); + } + + private ArrayList jsonObject2NodeTypes(JsonObject nodeTypes) { + ArrayList nodeTypeList = new ArrayList(); + Iterator> iterator = nodeTypes.entrySet().iterator(); + while (iterator.hasNext()) { + NodeType type = new NodeType(); + Entry next = iterator.next(); + type.setType(next.getKey()); + type.setValue(new Gson().fromJson(next.getValue(), NodeType.NodeTypeValue.class)); + nodeTypeList.add(type); + } + return nodeTypeList; + } + + public JsonObject getCapabilityTypes() { + return capabilityTypes; + } + + public void setCapabilityTypes(JsonObject capabilityTypes) { + this.capabilityTypes = capabilityTypes; + } + + public JsonObject getRelationshipTypes() { + return relationshipTypes; + } + + public void setRelationshipTypes(JsonObject relationshipTypes) { + this.relationshipTypes = relationshipTypes; + } + + public List getRelationshipTypeList() { + return jsonObject2RelationshipTypes(relationshipTypes); + } + + private ArrayList jsonObject2RelationshipTypes(JsonObject relationshipTypes) { + ArrayList relationshipTypeList = new ArrayList(); + Iterator> iterator = relationshipTypes.entrySet().iterator(); + while (iterator.hasNext()) { + RelationshipType type = new RelationshipType(); + Entry next = iterator.next(); + type.setType(next.getKey()); + type.setValue(new Gson().fromJson(next.getValue(), RelationshipType.RelationshipValue.class)); + relationshipTypeList.add(type); + } + return relationshipTypeList; + } + + public JsonObject getPolicyTypes() { + return policyTypes; + } + + public void setPolicyTypes(JsonObject policyTypes) { + this.policyTypes = policyTypes; + } + + public TopologyTemplate getTopologyTemplate() { + return topologyTemplate; + } + + public void setTopologyTemplate(TopologyTemplate topologyTemplate) { + this.topologyTemplate = topologyTemplate; + } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public JsonObject getPlans() { + return plans; + } + + public void setPlans(JsonObject plans) { + this.plans = plans; + } + + public List getPlanList() { + return jsonObject2PlanList(this.plans); + } + + private List jsonObject2PlanList(JsonObject plans) { + if (plans == null) { + return new ArrayList<>(); + } + List retList = new ArrayList<>(); + Iterator> iterator = plans.entrySet().iterator(); + while (iterator.hasNext()) { + Plan ret = new Plan(); + Entry next = iterator.next(); + ret.setName(next.getKey()); + ret.setValue(new Gson().fromJson(next.getValue(), Plan.PlanValue.class)); + retList.add(ret); + } + return retList; + } + + public class TopologyTemplate { + private String description; + private List inputs; + private List outputs; + private List nodeTemplates; + private SubstitutionMapping substitutionMappings; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getInputs() { + return inputs; + } + + public void setInputs(List inputs) { + this.inputs = inputs; + } + + public List getOutputs() { + return outputs; + } + + public void setOutputs(List outputs) { + this.outputs = outputs; + } + + public List getNodeTemplates() { + return nodeTemplates; + } + + public void setNodeTemplates(List nodeTemplates) { + this.nodeTemplates = nodeTemplates; + } + + public SubstitutionMapping getSubstitutionMappings() { + return substitutionMappings; + } + + public void setSubstitutionMappings(SubstitutionMapping substitutionMappings) { + this.substitutionMappings = substitutionMappings; + } + + public class Input { + private String name; + private String type; + private String description; + private String defaultValue; + private boolean required; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDefault() { + return defaultValue; + } + + public void setDefault(String defaultValue) { + this.defaultValue = defaultValue; + } + + public boolean isRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + } + + public class Output { + private String name; + private String description; + private Object value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + } + + public class NodeTemplate { + private String name; + private String nodeType; + private JsonObject properties; + private JsonObject[] requirements; + private JsonObject capabilities; + private List relationships; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getNodeType() { + return nodeType; + } + + public void setNodeType(String nodeType) { + this.nodeType = nodeType; + } + + public JsonObject getProperties() { + return properties; + } + + public void setProperties(JsonObject properties) { + this.properties = properties; + } + + public Map getPropertyList() { + return jsonObject2Properties(properties); + } + + private Map jsonObject2Properties(JsonObject properties) { + Map ret = new HashMap<>(); + Iterator> iterator = properties.entrySet().iterator(); + while (iterator.hasNext()) { + Entry next = iterator.next(); + ret.put(next.getKey(), next.getValue().getAsString()); + } + return ret; + } + + public JsonObject[] getRequirements() { + return requirements; + } + + public void setRequirements(JsonObject[] requirements) { + this.requirements = requirements; + } + + public JsonObject getCapabilities() { + return capabilities; + } + + public void setCapabilities(JsonObject capabilities) { + this.capabilities = capabilities; + } + + public List getRelationships() { + return relationships; + } + + public void setRelationships(List relationships) { + this.relationships = relationships; + } + + /** + * get scalable. + * @return NodeTemplateScalable + */ + public NodeTemplateScalable getScalable() { + if (capabilities == null) { + return null; + } + JsonElement scaleableJson = capabilities.get("scalable"); + if (scaleableJson == null || !scaleableJson.isJsonObject()) { + return null; + } + JsonElement propertyJson = scaleableJson.getAsJsonObject().get("properties"); + if (propertyJson == null || !propertyJson.isJsonObject()) { + return null; + } + + NodeTemplateScalable scalable = new NodeTemplateScalable(); + scalable + .setMin_instances(propertyJson.getAsJsonObject().get("min_instances").getAsString()); + scalable + .setMax_instances(propertyJson.getAsJsonObject().get("max_instances").getAsString()); + scalable.setDefault_instances( + propertyJson.getAsJsonObject().get("default_instances").getAsString()); + return scalable; + } + + public class Relationship { + private String targetNodeName; + private String type; + private String sourceNodeName; + + public String getTargetNodeName() { + return targetNodeName; + } + + public void setTargetNodeName(String targetNodeName) { + this.targetNodeName = targetNodeName; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getSourceNodeName() { + return sourceNodeName; + } + + public void setSourceNodeName(String sourceNodeName) { + this.sourceNodeName = sourceNodeName; + } + } + + public class NodeTemplateScalable { + private String minInstances; + private String maxInstances; + private String defaultInstances; + + public String getMin_instances() { + return minInstances; + } + + public void setMin_instances(String minInstances) { + this.minInstances = minInstances; + } + + public String getMax_instances() { + return maxInstances; + } + + public void setMax_instances(String maxInstances) { + this.maxInstances = maxInstances; + } + + public String getDefault_instances() { + return defaultInstances; + } + + public void setDefault_instances(String defaultInstances) { + this.defaultInstances = defaultInstances; + } + } + } + + public class SubstitutionMapping { + private String nodeType; + private JsonObject requirements; + private JsonObject capabilities; + private JsonObject properties; + + public String getNodeType() { + return nodeType; + } + + public void setNodeType(String nodeType) { + this.nodeType = nodeType; + } + + public JsonObject getRequirements() { + return requirements; + } + + public void setRequirements(JsonObject requirements) { + this.requirements = requirements; + } + + public Map getRequirementList() { + return jsonObjects2Requirements(this.requirements); + } + + private Map jsonObjects2Requirements(JsonObject requirements) { + Map ret = new HashMap(); + + Iterator> iterator = requirements.entrySet().iterator(); + while (iterator.hasNext()) { + Entry next = iterator.next(); + if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) { + ret.put(next.getKey(), new String[] {next.getValue().getAsString()}); + continue; + } + + if (next.getValue().isJsonArray()) { + String[] value = parseListValue((JsonArray) next.getValue()); + ret.put(next.getKey(), value); + } + } + + return ret; + } + + private String[] parseListValue(JsonArray jsonArray) { + String[] value = new String[jsonArray.size()]; + for (int i = 0, size = jsonArray.size(); i < size; i++) { + value[i] = jsonArray.get(i).getAsString(); + } + return value; + } + + public JsonObject getCapabilities() { + return capabilities; + } + + public void setCapabilities(JsonObject capabilities) { + this.capabilities = capabilities; + } + + public Map getCapabilityList() { + return jsonObject2Capabilities(this.capabilities); + } + + private Map jsonObject2Capabilities(JsonObject capabilities) { + Map ret = new HashMap(); + + Iterator> iterator = capabilities.entrySet().iterator(); + while (iterator.hasNext()) { + Entry next = iterator.next(); + + if (next.getValue().isJsonPrimitive() || next.getValue().isJsonObject()) { + ret.put(next.getKey(), new String[] {next.getValue().getAsString()}); + continue; + } + + if (next.getValue().isJsonArray()) { + String[] value = parseListValue((JsonArray) next.getValue()); + ret.put(next.getKey(), value); + } + } + + return ret; + } + + public JsonObject getProperties() { + return properties; + } + + public void setProperties(JsonObject properties) { + this.properties = properties; + } + + public Map getPropertyList() { + return jsonObject2Properties(properties); + } + + private Map jsonObject2Properties(JsonObject properties) { + Map ret = new HashMap<>(); + Iterator> iterator = properties.entrySet().iterator(); + while (iterator.hasNext()) { + Entry next = iterator.next(); + ret.put(next.getKey(), next.getValue().getAsString()); + } + return ret; + } + } + } + + + public class RelationshipType { + private String type; + private RelationshipValue value; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public RelationshipValue getValue() { + return value; + } + + public void setValue(RelationshipValue value) { + this.value = value; + } + + public class RelationshipValue { + private String derivedFrom; + private String[] validTargetTypes; + + public String getDerivedFrom() { + return derivedFrom; + } + + public void setDerivedFrom(String derivedFrom) { + this.derivedFrom = derivedFrom; + } + + public String[] getValid_target_types() { + return validTargetTypes; + } + + public void setValid_target_types(String[] validTargetTypes) { + this.validTargetTypes = validTargetTypes; + } + } + } + + + public class NodeType { + private String type; + private NodeTypeValue value; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public NodeTypeValue getValue() { + return value; + } + + public void setValue(NodeTypeValue value) { + this.value = value; + } + + public class NodeTypeValue { + private String derivedFrom; + private JsonObject properties; + private JsonObject[] requirements; + private JsonObject capabilities; + + public String getDerivedFrom() { + return derivedFrom; + } + + public void setDerived_from(String derivedFrom) { + this.derivedFrom = derivedFrom; + } + + public JsonObject getProperties() { + return properties; + } + + public void setProperties(JsonObject properties) { + this.properties = properties; + } + + public List getPropertyList() { + return jsonObject2Properties(properties); + } + + private List jsonObject2Properties(JsonObject properties) { + List propertieList = new ArrayList(); + Iterator> iterator = properties.entrySet().iterator(); + while (iterator.hasNext()) { + NodeTypeProperty type = new NodeTypeProperty(); + Entry next = iterator.next(); + type.setKey(next.getKey()); + type.setValue(new Gson().fromJson(next.getValue(), JsonObject.class)); + propertieList.add(type); + } + return propertieList; + } + + public class NodeTypeProperty { + private String key; + private JsonObject value; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + /** + * get default value. + * @return default value + */ + public String getDefaultValue() { + JsonElement defaultValue = value.get("default"); + if (defaultValue == null || defaultValue.isJsonObject()) { + return ""; + } + + return defaultValue.getAsString(); + } + + public JsonObject getValue() { + return value; + } + + public void setValue(JsonObject value) { + this.value = value; + } + } + + public JsonObject[] getRequirements() { + return requirements; + } + + public void setRequirements(JsonObject[] requirements) { + this.requirements = requirements; + } + + public JsonObject getCapabilities() { + return capabilities; + } + + public void setCapabilities(JsonObject capabilities) { + this.capabilities = capabilities; + } + } + } + + public class Plan { + private String name; + private PlanValue value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return value.getDescription(); + } + + public String getReference() { + return value.getReference(); + } + + public String getPlanLanguage() { + return value.getPlanLanguage(); + } + + public List getInputList() { + return value.getInputList(); + } + + public PlanValue getValue() { + return value; + } + + public void setValue(PlanValue value) { + this.value = value; + } + + public class PlanValue { + private String description; + private String reference; + private String planLanguage; + private JsonObject inputs; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getReference() { + return reference; + } + + public void setReference(String reference) { + this.reference = reference; + } + + public String getPlanLanguage() { + return planLanguage; + } + + public void setPlanLanguage(String planLanguage) { + this.planLanguage = planLanguage; + } + + public JsonObject getInputs() { + return inputs; + } + + public void setInputs(JsonObject inputs) { + this.inputs = inputs; + } + + public List getInputList() { + return jsonObject2PlanInputList(inputs); + + } + + private List jsonObject2PlanInputList(JsonObject inputs) { + List retList = new ArrayList(); + Iterator> iterator = inputs.entrySet().iterator(); + while (iterator.hasNext()) { + PlanInput ret = new PlanInput(); + Entry next = iterator.next(); + ret.setName(next.getKey()); + ret.setValue(new Gson().fromJson(next.getValue(), PlanInput.PlanInputValue.class)); + retList.add(ret); + } + return retList; + } + + public class PlanInput { + private String name; + private PlanInputValue value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return value.getType(); + } + + public String getDescription() { + return value.getDescription(); + } + + public String getDefault() { + return value.getDefaultValue(); + } + + public boolean isRequired() { + return value.isRequired(); + } + + public PlanInputValue getValue() { + return value; + } + + public void setValue(PlanInputValue value) { + this.value = value; + } + + public class PlanInputValue { + private String type; + private String description; + @SerializedName("default") + private String defaultValue; + private boolean required; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public boolean isRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + } + } + } + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/IYamlParseRest.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/IYamlParseRest.java new file mode 100644 index 00000000..4069c82f --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/IYamlParseRest.java @@ -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.zte.service; + +import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter; + +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; + + +@Path("/parse") +public interface IYamlParseRest { + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + String parse(ParseYamlRequestParemeter request) throws Exception; +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/ToscaYamlModelParser.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/ToscaYamlModelParser.java new file mode 100644 index 00000000..243cc361 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/parser/yaml/zte/service/ToscaYamlModelParser.java @@ -0,0 +1,417 @@ +/** + * 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.zte.service; + +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; +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.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.YamlParseServiceConsumer; +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.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); + + @Override + public String parse(String packageId, String fileLocation) throws CatalogResourceException { + ParseYamlResult result = getParseYamlResult(fileLocation); + + Map toscaMeta = parseToscaMeta(fileLocation); + String stFileName = toscaMeta.get(TOSCA_META_FIELD_ENTRY_DEFINITIONS); + CsarFileUriResponse stDownloadUri = + PackageWrapper.getInstance().getCsarFileDownloadUri(packageId, stFileName); + + ServiceTemplate st = parseServiceTemplate(packageId, result, stDownloadUri.getDownloadUri()); + ServiceTemplateOperation[] operations = parseOperations(result.getPlanList(), fileLocation); + st.setOperations(operations); + List ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result); + st.setType(getTemplateType(result, ntList).toString()); + + TemplateManager.getInstance().addServiceTemplate( + TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList)); + + SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result); + if (stm != null) { + TemplateManager.getInstance() + .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm)); + } + + return st.getServiceTemplateId(); + } + + private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException { + String destPath = copyTemporaryFile2HttpServer(fileLocation); + try { + String url = getUrl(toTempFileLocalPath(fileLocation)); + return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url)); + } finally { + if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) { + (new File(destPath)).delete(); + } + } + } + + 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 parseToscaMeta(String fileLocation) throws CatalogResourceException { + Map 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); + return request; + } + + private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId, + ParseYamlResult result) { + String type = getSubstitutionMappingType(result); + if (ToolUtil.isTrimedEmptyString(type)) { + return null; + } + + org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult + .TopologyTemplate.SubstitutionMapping stm = + result.getTopologyTemplate().getSubstitutionMappings(); + return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(), + stm.getCapabilityList()); + } + + private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result, + String stDownloadUri) { + ServiceTemplate st = new ServiceTemplate(); + + st.setServiceTemplateId(ToolUtil.generateId()); + st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName())); + st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName())); + st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName())); + st.setCsarid(packageId); + st.setDownloadUri(stDownloadUri); + st.setInputs(parseInputs(result)); + st.setOutputs(parseOutputs(result)); + return st; + } + + private InputParameter[] parseInputs(ParseYamlResult result) { + List inputList = result.getTopologyTemplate().getInputs(); + if (inputList == null) { + return new InputParameter[0]; + } + List retList = new ArrayList(); + for (Input input : inputList) { + retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()), + input.getDescription(), input.getDefault(), input.isRequired())); + } + return retList.toArray(new InputParameter[0]); + } + + private OutputParameter[] parseOutputs(ParseYamlResult result) { + List outputList = result.getTopologyTemplate().getOutputs(); + if (outputList == null || outputList.isEmpty()) { + return new OutputParameter[0]; + } + List retList = new ArrayList(); + for (Output output : outputList) { + retList + .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue())); + } + return retList.toArray(new OutputParameter[0]); + } + + private ServiceTemplateOperation[] parseOperations(List planList, String zipFileLocation) + throws CatalogResourceException { + if (planList == null || planList.isEmpty()) { + return new ServiceTemplateOperation[0]; + } + + List opList = new ArrayList<>(); + for (Plan plan : planList) { + ServiceTemplateOperation op = new ServiceTemplateOperation(); + op.setName(plan.getName()); + op.setDescription(plan.getDescription()); + checkPlanLanguage(plan.getPlanLanguage()); + DeployPackageResponse response = + Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getReference()); + op.setPackageName(parsePackageName(response)); + op.setProcessId(response.getProcessId()); + op.setInputs(parsePlanInputs(plan.getInputList())); + + opList.add(op); + + } + return opList.toArray(new ServiceTemplateOperation[0]); + } + + private String parsePackageName(DeployPackageResponse response) { + String packageName = response.getPackageName(); + if (packageName != null && packageName.indexOf("-") > 0) { + packageName = packageName.substring(0, packageName.lastIndexOf("-")); + } + return packageName; + } + + private void checkPlanLanguage(String planLanguage) throws CatalogResourceException { + if (planLanguage == null || planLanguage.isEmpty()) { + throw new CatalogResourceException("Plan Language is empty."); + } + if (planLanguage.equalsIgnoreCase("bpel")) { + return; + } + if (planLanguage.equalsIgnoreCase("bpmn")) { + return; + } + if (planLanguage.equalsIgnoreCase("bpmn4tosca")) { + return; + } + throw new CatalogResourceException( + "Plan Language is not supported. Language = " + planLanguage); + } + + private InputParameter[] parsePlanInputs(List inputList) { + if (inputList == null || inputList.isEmpty()) { + return new InputParameter[0]; + } + + List retList = new ArrayList<>(); + for (PlanInput input : inputList) { + retList.add(new InputParameter(input.getName(), getEnumDataType(input.getType()), + input.getDescription(), input.getDefault(), input.isRequired())); + } + return retList.toArray(new InputParameter[0]); + } + + 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; + } + + private List parseNodeTemplates(String csarId, String templateId, + ParseYamlResult result) { + List nodetemplateList = + result.getTopologyTemplate().getNodeTemplates(); + if (nodetemplateList == null) { + return null; + } + + List retList = new ArrayList<>(); + for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) { + NodeTemplate ret = new NodeTemplate(); + ret.setId(nodeTemplate.getName()); + ret.setName(nodeTemplate.getName()); + ret.setType(nodeTemplate.getNodeType()); + ret.setProperties(nodeTemplate.getPropertyList()); + List relationShipList = + parseNodeTemplateRelationShip(nodeTemplate.getRelationships()); + ret.setRelationShips(relationShipList); + + retList.add(ret); + } + + return retList; + } + + + private List parseNodeTemplateRelationShip(List relationshipList) { + List retList = new ArrayList<>(); + + if (relationshipList == null) { + return retList; + } + + for (Relationship relationship : relationshipList) { + RelationShip ret = new RelationShip(); + ret.setSourceNodeId(relationship.getSourceNodeName()); + ret.setSourceNodeName(relationship.getSourceNodeName()); + ret.setTargetNodeId(relationship.getTargetNodeName()); + ret.setTargetNodeName(relationship.getTargetNodeName()); + ret.setType(relationship.getType()); + retList.add(ret); + } + + return retList; + } + + private EnumTemplateType getTemplateType(ParseYamlResult result, List 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) { + if (result.getTopologyTemplate().getSubstitutionMappings() == null) { + return null; + } + return result.getTopologyTemplate().getSubstitutionMappings().getNodeType(); + } + + private EnumTemplateType getTemplateTypeFromNodeTemplates(List 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"); + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/resources/TemplateResource.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/resources/TemplateResource.java index 658a2f4e..29d20d5c 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/resources/TemplateResource.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/resources/TemplateResource.java @@ -35,7 +35,7 @@ import org.openo.commontosca.catalog.model.entity.QueryRawDataCondition; import org.openo.commontosca.catalog.model.entity.ServiceTemplate; import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation; import org.openo.commontosca.catalog.model.entity.ServiceTemplateRawData; -import org.openo.commontosca.catalog.model.parser.ToscaYamlModelParser; +import org.openo.commontosca.catalog.model.parser.yaml.zte.service.ToscaYamlModelParser; import org.openo.commontosca.catalog.model.service.ModelService; import org.openo.commontosca.catalog.model.wrapper.ServiceTemplateWrapper; import org.slf4j.Logger; -- cgit 1.2.3-korg