09c3e12765c5837b88dd0c47c513913fcf871044
[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;
17
18 import org.openo.commontosca.catalog.common.MsbAddrConfig;
19 import org.openo.commontosca.catalog.common.ToolUtil;
20 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
21 import org.openo.commontosca.catalog.model.common.TemplateUtils;
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.ServiceTemplateOperation;
25 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Input;
26 import org.openo.commontosca.catalog.model.parser.yaml.yamlmodel.Plan;
27 import org.openo.commontosca.catalog.model.plan.wso2.Wso2ServiceConsumer;
28 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39 public abstract class AbstractModelParser {
40   private static final Logger logger = LoggerFactory.getLogger(AbstractModelParser.class);
41
42   public abstract String parse(String packageId, String fileLocation)
43       throws CatalogResourceException;
44   
45   public String copyTemporaryFile2HttpServer(String fileLocation) throws CatalogResourceException {
46     String destPath = org.openo.commontosca.catalog.filemanage.http.ToolUtil.getHttpServerAbsolutePath()
47         + toTempFilePath(fileLocation);
48     if (!org.openo.commontosca.catalog.filemanage.http.ToolUtil.copyFile(
49         fileLocation, destPath, true)) {
50       throw new CatalogResourceException("Copy Temporary To HttpServer Failed.");
51     }
52     
53     logger.info("destPath = " + destPath);
54     return destPath;
55   }
56   
57   public String getUrlOnHttpServer(String path) {
58     if (MsbAddrConfig.getMsbAddress().endsWith("/")) {
59       return MsbAddrConfig.getMsbAddress() + "files/catalog-http" + path;
60     } else {
61       return MsbAddrConfig.getMsbAddress() + "/files/catalog-http" + path;
62     }
63   }
64   
65   protected String toTempFilePath(String fileLocation) {
66     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
67   }
68   
69   protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
70     if (isNsType(substitutionType)) {
71       return EnumTemplateType.NS;
72     }
73
74     if (isVnfType(substitutionType)) {
75       return EnumTemplateType.VNF;
76     }
77
78     return getTemplateTypeFromNodeTemplates(ntList);
79   }
80   
81   private boolean isVnfType(String type) {
82     if (ToolUtil.isTrimedEmptyString(type)) {
83       return false;
84     }
85     return type.toUpperCase().endsWith(".VNF") || type.toUpperCase().contains(".VNF.");
86   }
87
88   private boolean isNsType(String type) {
89     if (ToolUtil.isTrimedEmptyString(type)) {
90       return false;
91     }
92     return type.toUpperCase().endsWith(".NS") || type.toUpperCase().contains(".NS.");
93   }
94   
95   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
96     for (NodeTemplate nt : ntList) {
97       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
98         return EnumTemplateType.NS;
99       }
100     }
101
102     return EnumTemplateType.VNF;
103   }
104   
105   private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
106   
107   protected String parseServiceTemplateFileName(String packageId, String fileLocation)
108       throws CatalogResourceException {
109     return File.separator + parseToscaMeta(fileLocation).get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
110   }
111   
112   private static final String TOSCA_META_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
113   protected Map<String, String> parseToscaMeta(String zipLocation) throws CatalogResourceException {
114     Map<String, String> toscaMeta = new HashMap<>();
115     String[] lines = TemplateUtils.readFromZipFile(zipLocation, TOSCA_META_FILE_NAME);
116
117     for (String line : lines) {
118       String[] tmps;
119       if (line.indexOf(":") > 0) {
120         tmps = line.split(":");
121         toscaMeta.put(tmps[0].trim(), tmps[1].trim());
122       }
123     }
124
125     return toscaMeta;
126   }
127   
128   /**
129    * @param fileLocation
130    * @return
131    * @throws CatalogResourceException
132    */
133   protected ServiceTemplateOperation[] parseOperations(String fileLocation) throws CatalogResourceException {
134     String sPlan = TemplateUtils.readStringFromZipFile(fileLocation, "Definitions/plans.yaml");
135     Map<String, Plan> plans = TemplateUtils.loadPlan(sPlan);
136     return parseAndDeployPlans(plans, fileLocation);
137   }
138   
139   /**
140    * @param plans
141    * @param fileLocation
142    * @return
143    * @throws CatalogResourceException 
144    */
145   private ServiceTemplateOperation[] parseAndDeployPlans(Map<String, Plan> plans,
146       String zipFileLocation) throws CatalogResourceException {
147     if (plans == null || plans.isEmpty()) {
148       return new ServiceTemplateOperation[0];
149     }
150
151     List<ServiceTemplateOperation> opList = new ArrayList<>();
152     for (Entry<String, Plan> plan : plans.entrySet()) {
153       ServiceTemplateOperation op = new ServiceTemplateOperation();
154       op.setName(plan.getKey());
155       op.setDescription(plan.getValue().getDescription());
156       checkPlanLanguage(plan.getValue().getPlanLanguage());
157       DeployPackageResponse response =
158           Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getValue().getReference());
159       op.setPackageName(parsePackageName(response));
160       op.setProcessId(response.getProcessId());
161       op.setInputs(parsePlanInputs(plan.getValue().getInputs()));
162
163       opList.add(op);
164     }
165     
166     return opList.toArray(new ServiceTemplateOperation[0]);
167   }
168   
169   private String parsePackageName(DeployPackageResponse response) {
170     String packageName = response.getPackageName();
171     if (packageName != null && packageName.indexOf("-") > 0) {
172       packageName = packageName.substring(0, packageName.lastIndexOf("-"));
173     }
174     return packageName;
175   }
176
177   private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
178     if (planLanguage == null || planLanguage.isEmpty()) {
179       throw new CatalogResourceException("Plan Language is empty.");
180     }
181     if (planLanguage.equalsIgnoreCase("bpel")) {
182       return;
183     }
184     if (planLanguage.equalsIgnoreCase("bpmn")) {
185       return;
186     }
187     if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
188       return;
189     }
190     throw new CatalogResourceException(
191         "Plan Language is not supported. Language = " + planLanguage);
192   }
193
194   /**
195    * @param inputs
196    * @return
197    */
198   private InputParameter[] parsePlanInputs(
199       Map<String, Input> inputs) {
200     if (inputs == null || inputs.isEmpty()) {
201       return new InputParameter[0];
202     }
203
204     List<InputParameter> retList = new ArrayList<>();
205     for (Entry<String, Input> input : inputs.entrySet()) {
206       retList.add(new InputParameter(
207           input.getKey(),
208           input.getValue().getType(),
209           input.getValue().getDescription(),
210           input.getValue().getDefault(),
211           input.getValue().isRequired()));
212     }
213     return retList.toArray(new InputParameter[0]);
214   }
215   
216 }