Parse Plan information from our own alone file. Definitions/plans.yaml
[vfc/nfvo/catalog.git] / catalog-core / catalog-mgr / src / main / java / org / openo / commontosca / catalog / model / parser / yaml / zte / ToscaYamlModelParser.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.commontosca.catalog.model.parser.yaml.zte;
17
18 import org.openo.commontosca.catalog.common.ToolUtil;
19 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
20 import org.openo.commontosca.catalog.db.resource.TemplateManager;
21 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
22 import org.openo.commontosca.catalog.model.entity.InputParameter;
23 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
24 import org.openo.commontosca.catalog.model.entity.OutputParameter;
25 import org.openo.commontosca.catalog.model.entity.RelationShip;
26 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
27 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
28 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
29 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
30 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.EnumYamlServiceTemplateInfo;
31 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
32 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
33 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import java.io.File;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 public class ToscaYamlModelParser extends AbstractModelParser {
45   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
46
47   @Override
48   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
49     logger.info("tosca-yaml-parser parse begin.");
50     ParseYamlResult result = getParseYamlResult(fileLocation);
51     
52     // service template
53     ServiceTemplate st = parseServiceTemplate(
54         packageId, result, parseServiceTemplateFileName(packageId, fileLocation));
55     // workflow
56     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
57     st.setOperations(operations);
58     // node templates
59     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
60     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
61     // save to db
62     TemplateManager.getInstance().addServiceTemplate(
63         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
64
65     // substitution
66     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
67     if (stm != null) {
68       TemplateManager.getInstance()
69           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
70     }
71
72     return st.getServiceTemplateId();
73   }
74
75   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
76     String destPath = copyTemporaryFile2HttpServer(fileLocation);
77     try {
78       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
79       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
80     } finally {
81       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
82         (new File(destPath)).delete();
83       }
84     }
85   }
86
87   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
88     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
89     request.setPath(fileLocation);
90     return request;
91   }
92
93   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
94       ParseYamlResult result) {
95     String type = getSubstitutionType(result);
96     if (ToolUtil.isTrimedEmptyString(type)) {
97       return null;
98     }
99
100     org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
101         .TopologyTemplate.SubstitutionMapping stm =
102         result.getTopologyTemplate().getSubstitutionMappings();
103     return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
104         stm.getCapabilityList());
105   }
106
107   private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
108       String stDownloadUri) {
109     ServiceTemplate st = new ServiceTemplate();
110
111     st.setServiceTemplateId(ToolUtil.generateId());
112     st.setTemplateName(result.getMetadata().get(EnumYamlServiceTemplateInfo.ID.getName()));
113     st.setVendor(result.getMetadata().get(EnumYamlServiceTemplateInfo.PROVIDER.getName()));
114     st.setVersion(result.getMetadata().get(EnumYamlServiceTemplateInfo.VERSION.getName()));
115     st.setCsarid(packageId);
116     st.setDownloadUri(stDownloadUri);
117     st.setInputs(parseInputs(result));
118     st.setOutputs(parseOutputs(result));
119     return st;
120   }
121
122   private InputParameter[] parseInputs(ParseYamlResult result) {
123     List<Input> inputList = result.getTopologyTemplate().getInputs();
124     if (inputList == null) {
125       return new InputParameter[0];
126     }
127     List<InputParameter> retList = new ArrayList<InputParameter>();
128     for (Input input : inputList) {
129       retList.add(new InputParameter(input.getName(), input.getType(),
130           input.getDescription(), input.getDefault(), input.isRequired()));
131     }
132     return retList.toArray(new InputParameter[0]);
133   }
134
135   private OutputParameter[] parseOutputs(ParseYamlResult result) {
136     List<Output> outputList = result.getTopologyTemplate().getOutputs();
137     if (outputList == null || outputList.isEmpty()) {
138       return new OutputParameter[0];
139     }
140     List<OutputParameter> retList = new ArrayList<OutputParameter>();
141     for (Output output : outputList) {
142       retList
143           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
144     }
145     return retList.toArray(new OutputParameter[0]);
146   }
147
148   private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
149       ParseYamlResult result) {
150     List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
151         result.getTopologyTemplate().getNodeTemplates();
152     if (nodetemplateList == null) {
153       return null;
154     }
155
156     List<NodeTemplate> retList = new ArrayList<>();
157     for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
158       NodeTemplate ret = new NodeTemplate();
159       ret.setId(nodeTemplate.getName());
160       ret.setName(nodeTemplate.getName());
161       ret.setType(nodeTemplate.getNodeType());
162       ret.setProperties(nodeTemplate.getPropertyList());
163       List<RelationShip> relationShipList =
164           parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
165       ret.setRelationShips(relationShipList);
166
167       retList.add(ret);
168     }
169
170     return retList;
171   }
172
173
174   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
175     List<RelationShip> retList = new ArrayList<>();
176
177     if (relationshipList == null) {
178       return retList;
179     }
180
181     for (Relationship relationship : relationshipList) {
182       RelationShip ret = new RelationShip();
183       ret.setSourceNodeId(relationship.getSourceNodeName());
184       ret.setSourceNodeName(relationship.getSourceNodeName());
185       ret.setTargetNodeId(relationship.getTargetNodeName());
186       ret.setTargetNodeName(relationship.getTargetNodeName());
187       ret.setType(relationship.getType());
188       retList.add(ret);
189     }
190
191     return retList;
192   }
193
194   private String getSubstitutionType(ParseYamlResult result) {
195     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
196       return null;
197     }
198     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
199   }
200
201 }