d1c3905135871d7754f17d56e8535aeba5c2cee7
[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.Config;
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     return Config.getConfigration().getHttpServerAddr() + "/" + path;
59   }
60   
61   protected String toTempFilePath(String fileLocation) {
62     return File.separator + "temp" + File.separator + (new File(fileLocation)).getName();
63   }
64   
65   protected EnumTemplateType getTemplateType(String substitutionType, List<NodeTemplate> ntList) {
66     if (isNsType(substitutionType)) {
67       return EnumTemplateType.NS;
68     }
69
70     if (isVnfType(substitutionType)) {
71       return EnumTemplateType.VNF;
72     }
73
74     return getTemplateTypeFromNodeTemplates(ntList);
75   }
76   
77   private boolean isVnfType(String type) {
78     if (ToolUtil.isTrimedEmptyString(type)) {
79       return false;
80     }
81     return type.toUpperCase().endsWith(".VNF") || type.toUpperCase().contains(".VNF.");
82   }
83
84   private boolean isNsType(String type) {
85     if (ToolUtil.isTrimedEmptyString(type)) {
86       return false;
87     }
88     return type.toUpperCase().endsWith(".NS") || type.toUpperCase().contains(".NS.");
89   }
90   
91   private EnumTemplateType getTemplateTypeFromNodeTemplates(List<NodeTemplate> ntList) {
92     for (NodeTemplate nt : ntList) {
93       if (isNsType(nt.getType()) || isVnfType(nt.getType())) {
94         return EnumTemplateType.NS;
95       }
96     }
97
98     return EnumTemplateType.VNF;
99   }
100   
101   private static final String TOSCA_META_FIELD_ENTRY_DEFINITIONS = "Entry-Definitions";
102   
103   protected String parseServiceTemplateFileName(String packageId, String fileLocation)
104       throws CatalogResourceException {
105     return File.separator + parseToscaMeta(fileLocation).get(TOSCA_META_FIELD_ENTRY_DEFINITIONS);
106   }
107   
108   private static final String TOSCA_META_FILE_NAME = "TOSCA-Metadata/TOSCA.meta";
109   protected Map<String, String> parseToscaMeta(String zipLocation) throws CatalogResourceException {
110     Map<String, String> toscaMeta = new HashMap<>();
111     String[] lines = TemplateUtils.readFromZipFile(zipLocation, TOSCA_META_FILE_NAME);
112
113     for (String line : lines) {
114       String[] tmps;
115       if (line.indexOf(":") > 0) {
116         tmps = line.split(":");
117         toscaMeta.put(tmps[0].trim(), tmps[1].trim());
118       }
119     }
120
121     return toscaMeta;
122   }
123   
124   /**
125    * @param fileLocation
126    * @return
127    * @throws CatalogResourceException
128    */
129   protected ServiceTemplateOperation[] parseOperations(String fileLocation) throws CatalogResourceException {
130     String sPlan = TemplateUtils.readStringFromZipFile(fileLocation, "Definitions/plans.yaml");
131     Map<String, Plan> plans = TemplateUtils.loadPlan(sPlan);
132     return parseAndDeployPlans(plans, fileLocation);
133   }
134   
135   /**
136    * @param plans
137    * @param fileLocation
138    * @return
139    * @throws CatalogResourceException 
140    */
141   private ServiceTemplateOperation[] parseAndDeployPlans(Map<String, Plan> plans,
142       String zipFileLocation) throws CatalogResourceException {
143     if (plans == null || plans.isEmpty()) {
144       return new ServiceTemplateOperation[0];
145     }
146
147     List<ServiceTemplateOperation> opList = new ArrayList<>();
148     for (Entry<String, Plan> plan : plans.entrySet()) {
149       ServiceTemplateOperation op = new ServiceTemplateOperation();
150       op.setName(plan.getKey());
151       op.setDescription(plan.getValue().getDescription());
152       checkPlanLanguage(plan.getValue().getPlanLanguage());
153       DeployPackageResponse response =
154           Wso2ServiceConsumer.deployPackage(zipFileLocation, plan.getValue().getReference());
155       op.setPackageName(parsePackageName(response));
156       op.setProcessId(response.getProcessId());
157       op.setInputs(parsePlanInputs(plan.getValue().getInputs()));
158
159       opList.add(op);
160     }
161     
162     return opList.toArray(new ServiceTemplateOperation[0]);
163   }
164   
165   private String parsePackageName(DeployPackageResponse response) {
166     String packageName = response.getPackageName();
167     if (packageName != null && packageName.indexOf("-") > 0) {
168       packageName = packageName.substring(0, packageName.lastIndexOf("-"));
169     }
170     return packageName;
171   }
172
173   private void checkPlanLanguage(String planLanguage) throws CatalogResourceException {
174     if (planLanguage == null || planLanguage.isEmpty()) {
175       throw new CatalogResourceException("Plan Language is empty.");
176     }
177     if (planLanguage.equalsIgnoreCase("bpel")) {
178       return;
179     }
180     if (planLanguage.equalsIgnoreCase("bpmn")) {
181       return;
182     }
183     if (planLanguage.equalsIgnoreCase("bpmn4tosca")) {
184       return;
185     }
186     throw new CatalogResourceException(
187         "Plan Language is not supported. Language = " + planLanguage);
188   }
189
190   /**
191    * @param inputs
192    * @return
193    */
194   private InputParameter[] parsePlanInputs(
195       Map<String, Input> inputs) {
196     if (inputs == null || inputs.isEmpty()) {
197       return new InputParameter[0];
198     }
199
200     List<InputParameter> retList = new ArrayList<>();
201     for (Entry<String, Input> input : inputs.entrySet()) {
202       retList.add(new InputParameter(
203           input.getKey(),
204           input.getValue().getType(),
205           input.getValue().getDescription(),
206           input.getValue().getDefault(),
207           input.getValue().isRequired()));
208     }
209     return retList.toArray(new InputParameter[0]);
210   }
211   
212 }