67f9a9e3e082bae1d3cd39ec77445a7e05b447ee
[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.plan.wso2;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Map;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
28 import lombok.AllArgsConstructor;
29 import lombok.Data;
30 import lombok.NoArgsConstructor;
31
32 import org.apache.http.HttpEntity;
33 import org.apache.http.entity.ContentType;
34 import org.apache.http.entity.mime.MultipartEntityBuilder;
35 import org.glassfish.jersey.client.ClientConfig;
36 import org.openo.commontosca.catalog.common.Config;
37 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
38 import org.openo.commontosca.catalog.model.common.TemplateUtils;
39 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeletePackageResponse;
40 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
41 import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessRequest;
42 import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessResponse;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
47 import com.google.gson.Gson;
48
49
50 public class Wso2ServiceConsumer {
51   public static final String WSO2_APP_URL = "/openoapi/wso2bpel/v1/package";
52   private static final Logger logger = LoggerFactory.getLogger(Wso2ServiceConsumer.class);
53   
54   /**
55    * deploy package.
56    * @param zipFileLocation zip file location
57    * @param planFilePath plan file path
58    * @return DeployPackageResponse
59    * @throws CatalogResourceException e1
60    */
61   public static DeployPackageResponse deployPackage(String zipFileLocation, String planFilePath)
62       throws CatalogResourceException {
63     IpPort ipPort = getIpPort(Config.getConfigration().getMsbServerAddr());
64     if (ipPort == null) {
65       throw new CatalogResourceException("getMsbServerAddr failed.");
66     }
67     ZipFile zf = null;
68     InputStream ins = null;
69     try {
70       zf = new ZipFile(zipFileLocation);
71       ins = getInputStream(zf, planFilePath);
72       RestResponse res = RestfulClient.post(
73           ipPort.getIp(), ipPort.getPort(), WSO2_APP_URL,
74           buildRequest(ins, planFilePath));
75
76       if (res.getStatusCode() == null || res.getResult() == null) {
77         throw new CatalogResourceException(
78             "Deploy Package return null. Response = " + res);
79       }
80       
81       if (200 == res.getStatusCode() || 201 == res.getStatusCode()) {
82         DeployPackageResponse response =
83             new Gson().fromJson(res.getResult(), DeployPackageResponse.class);
84         if (response.isSuccess()) {
85           return response;
86         }
87       }
88
89       throw new CatalogResourceException(
90           "Deploy Package return failed. Response = " + res.getResult());
91     } catch (FileNotFoundException e) {
92       throw new CatalogResourceException("buildRequest failed.", e);
93     } catch (IOException e) {
94       throw new CatalogResourceException("ZipFile failed.", e);
95     } finally {
96       TemplateUtils.closeInputStream(ins);
97       TemplateUtils.closeZipFile(zf);
98     }
99   }
100
101   @Data
102   @NoArgsConstructor
103   @AllArgsConstructor
104   public static class IpPort {
105     private String ip;
106     private int port;
107   }
108   
109   private static IpPort getIpPort(String addr) {
110     Pattern p = Pattern.compile("//(.*?):(.*)");
111     Matcher m = p.matcher(addr);
112     while(m.find()){
113       return new IpPort(m.group(1), Integer.valueOf(m.group(2)));
114     }
115     return null;
116   }
117
118   private static HttpEntity buildRequest(InputStream inputStream, String filePath)
119       throws FileNotFoundException {
120     MultipartEntityBuilder builder = MultipartEntityBuilder.create();
121     builder.seContentType(ContentType.MULTIPART_FORM_DATA);
122     builder.addBinaryBody("file", inputStream, ContentType.APPLICATION_OCTET_STREAM,
123         new File(filePath).getName());
124     return builder.build();
125   }
126
127   private static InputStream getInputStream(ZipFile zf, String planFilePath)
128       throws CatalogResourceException, IOException {
129     ZipEntry ze = TemplateUtils.getZipEntryZipFile(zf, planFilePath);
130     
131     if (ze == null) {
132       throw new CatalogResourceException("Can't file plan file in the csar. planFilePath = " + planFilePath);
133     }
134     return zf.getInputStream(ze);
135   }
136
137   /**
138    * delet package.
139    * @param packageName package to delete according packageName
140    * @return DeletePackageResponse
141    * @throws CatalogResourceException e1
142    */
143   public static DeletePackageResponse deletePackage(String packageName)
144       throws CatalogResourceException {
145     try {
146       ClientConfig config = new ClientConfig();
147       Iwso2RestService wso2Proxy = ConsumerFactory.createConsumer(
148           Config.getConfigration().getMsbServerAddr(), config, Iwso2RestService.class);
149       DeletePackageResponse response = wso2Proxy.deletePackage(packageName);
150       if (response.isSuccess()) {
151         return response;
152       }
153       throw new CatalogResourceException(response.toString());
154     } catch (Exception e) {
155       throw new CatalogResourceException(
156           "Call Delete Package api failed. packageName = " + packageName, e);
157     }
158   }
159
160
161   /**
162    * start process.
163    * @param processId process id
164    * @param params params
165    * @return StartProcessResponse
166    * @throws CatalogResourceException e1
167    */
168   public static StartProcessResponse startProcess(String processId, Map<String, Object> params)
169       throws CatalogResourceException {
170     try {
171       ClientConfig config = new ClientConfig();
172       Iwso2RestService wso2Proxy = ConsumerFactory.createConsumer(
173           Config.getConfigration().getMsbServerAddr(), config, Iwso2RestService.class);
174       StartProcessResponse response =
175           wso2Proxy.startProcess(new StartProcessRequest(processId, params));
176       if (response.isSuccess()) {
177         return response;
178       }
179       throw new CatalogResourceException(response.toString());
180     } catch (Exception e) {
181       throw new CatalogResourceException("Call Start Process api failed.", e);
182     }
183   }
184
185 }