8ec7834801d66e5a6d9e16a1d5c3131cdf04bc5e
[vfc/nfvo/catalog.git] /
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 java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.openo.commontosca.catalog.common.ToolUtil;
23 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
24 import org.openo.commontosca.catalog.db.resource.TemplateManager;
25 import org.openo.commontosca.catalog.model.common.TemplateDataHelper;
26 import org.openo.commontosca.catalog.model.entity.InputParameter;
27 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
28 import org.openo.commontosca.catalog.model.entity.OutputParameter;
29 import org.openo.commontosca.catalog.model.entity.RelationShip;
30 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
31 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
32 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
33 import org.openo.commontosca.catalog.model.parser.AbstractModelParser;
34 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlRequestParemeter;
35 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult;
36 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Input;
37 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.NodeTemplate.Relationship;
38 import org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult.TopologyTemplate.Output;
39 import org.openo.commontosca.catalog.model.parser.yaml.zte.service.YamlParseServiceConsumer;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ToscaYamlModelParser extends AbstractModelParser {
44   private static final Logger logger = LoggerFactory.getLogger(ToscaYamlModelParser.class);
45
46   @Override
47   public String parse(String packageId, String fileLocation) throws CatalogResourceException {
48     logger.info("tosca-yaml-parser parse begin.");
49     ParseYamlResult result = getParseYamlResult(fileLocation);
50     
51     // service template
52     ServiceTemplate st = parseServiceTemplate(
53         packageId, result, parseServiceTemplateFileName(packageId, fileLocation));
54     // workflow
55     ServiceTemplateOperation[] operations = parseOperations(fileLocation);
56     st.setOperations(operations);
57     // node templates
58     List<NodeTemplate> ntList = parseNodeTemplates(packageId, st.getServiceTemplateId(), result);
59     st.setType(getTemplateType(getSubstitutionType(result), ntList).toString());
60     // save to db
61     TemplateManager.getInstance().addServiceTemplate(
62         TemplateDataHelper.convert2TemplateData(st, ToolUtil.toJson(result), ntList));
63
64     // substitution
65     SubstitutionMapping stm = parseSubstitutionMapping(st.getServiceTemplateId(), result);
66     if (stm != null) {
67       TemplateManager.getInstance()
68           .addServiceTemplateMapping(TemplateDataHelper.convert2TemplateMappingData(stm));
69     }
70
71     return st.getServiceTemplateId();
72   }
73
74   private ParseYamlResult getParseYamlResult(String fileLocation) throws CatalogResourceException {
75     String destPath = copyTemporaryFile2HttpServer(fileLocation);
76     try {
77       String url = getUrlOnHttpServer(toTempFilePath(fileLocation));
78       return YamlParseServiceConsumer.getServiceTemplates(comboRequest(url));
79     } finally {
80       if (destPath != null && !destPath.isEmpty() && (new File(destPath)).exists()) {
81         (new File(destPath)).delete();
82       }
83     }
84   }
85
86   private ParseYamlRequestParemeter comboRequest(String fileLocation) {
87     ParseYamlRequestParemeter request = new ParseYamlRequestParemeter();
88     request.setPath(fileLocation);
89     return request;
90   }
91
92   private SubstitutionMapping parseSubstitutionMapping(String serviceTemplateId,
93       ParseYamlResult result) {
94     String type = getSubstitutionType(result);
95     if (ToolUtil.isTrimedEmptyString(type)) {
96       return null;
97     }
98
99     org.openo.commontosca.catalog.model.parser.yaml.zte.entity.ParseYamlResult
100         .TopologyTemplate.SubstitutionMapping stm =
101         result.getTopologyTemplate().getSubstitutionMappings();
102     return new SubstitutionMapping(serviceTemplateId, type, stm.getRequirementList(),
103         stm.getCapabilityList());
104   }
105
106   private ServiceTemplate parseServiceTemplate(String packageId, ParseYamlResult result,
107       String stDownloadUri) {
108     ServiceTemplate st = new ServiceTemplate();
109
110     st.setServiceTemplateId(ToolUtil.generateId());
111     st.setTemplateName(parserServiceTemplateName(result.getMetadata()));
112     st.setVendor(parserServiceTemplateVendor(result.getMetadata()));
113     st.setVersion(parserServiceTemplateVersion(result.getMetadata()));
114     st.setCsarId(packageId);
115     st.setDownloadUri(stDownloadUri);
116     st.setInputs(parseInputs(result));
117     st.setOutputs(parseOutputs(result));
118     return st;
119   }
120
121   private InputParameter[] parseInputs(ParseYamlResult result) {
122     List<Input> inputList = result.getTopologyTemplate().getInputs();
123     if (inputList == null) {
124       return new InputParameter[0];
125     }
126     List<InputParameter> retList = new ArrayList<InputParameter>();
127     for (Input input : inputList) {
128       retList.add(new InputParameter(input.getName(), input.getType(),
129           input.getDescription(), input.getDefault(), input.isRequired()));
130     }
131     return retList.toArray(new InputParameter[0]);
132   }
133
134   private OutputParameter[] parseOutputs(ParseYamlResult result) {
135     List<Output> outputList = result.getTopologyTemplate().getOutputs();
136     if (outputList == null || outputList.isEmpty()) {
137       return new OutputParameter[0];
138     }
139     List<OutputParameter> retList = new ArrayList<OutputParameter>();
140     for (Output output : outputList) {
141       retList
142           .add(new OutputParameter(output.getName(), output.getDescription(), output.getValue()));
143     }
144     return retList.toArray(new OutputParameter[0]);
145   }
146
147   private List<NodeTemplate> parseNodeTemplates(String csarId, String templateId,
148       ParseYamlResult result) {
149     List<ParseYamlResult.TopologyTemplate.NodeTemplate> nodetemplateList =
150         result.getTopologyTemplate().getNodeTemplates();
151     if (nodetemplateList == null) {
152       return null;
153     }
154
155     List<NodeTemplate> retList = new ArrayList<>();
156     for (ParseYamlResult.TopologyTemplate.NodeTemplate nodeTemplate : nodetemplateList) {
157       NodeTemplate ret = new NodeTemplate();
158       ret.setId(nodeTemplate.getName());
159       ret.setName(nodeTemplate.getName());
160       ret.setType(nodeTemplate.getNodeType());
161       ret.setProperties(nodeTemplate.getPropertyList());
162       List<RelationShip> relationShipList =
163           parseNodeTemplateRelationShip(nodeTemplate.getRelationships());
164       ret.setRelationShips(relationShipList);
165
166       retList.add(ret);
167     }
168
169     return retList;
170   }
171
172
173   private List<RelationShip> parseNodeTemplateRelationShip(List<Relationship> relationshipList) {
174     List<RelationShip> retList = new ArrayList<>();
175
176     if (relationshipList == null) {
177       return retList;
178     }
179
180     for (Relationship relationship : relationshipList) {
181       RelationShip ret = new RelationShip();
182       ret.setSourceNodeId(relationship.getSourceNodeName());
183       ret.setSourceNodeName(relationship.getSourceNodeName());
184       ret.setTargetNodeId(relationship.getTargetNodeName());
185       ret.setTargetNodeName(relationship.getTargetNodeName());
186       ret.setType(relationship.getType());
187       retList.add(ret);
188     }
189
190     return retList;
191   }
192
193   private String getSubstitutionType(ParseYamlResult result) {
194     if (result.getTopologyTemplate().getSubstitutionMappings() == null) {
195       return null;
196     }
197     return result.getTopologyTemplate().getSubstitutionMappings().getNodeType();
198   }
199
200 }