2 * Copyright 2016 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.commontosca.catalog.model.plan.wso2;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
28 import lombok.AllArgsConstructor;
30 import lombok.NoArgsConstructor;
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;
46 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
47 import com.google.gson.Gson;
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);
56 * @param zipFileLocation zip file location
57 * @param planFilePath plan file path
58 * @return DeployPackageResponse
59 * @throws CatalogResourceException e1
61 public static DeployPackageResponse deployPackage(String zipFileLocation, String planFilePath)
62 throws CatalogResourceException {
63 IpPort ipPort = getIpPort(Config.getConfigration().getMsbServerAddr());
65 throw new CatalogResourceException("getMsbServerAddr failed.");
68 InputStream ins = null;
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));
76 if (res.getStatusCode() == null || res.getResult() == null) {
77 throw new CatalogResourceException(
78 "Deploy Package return null. Response = " + res);
81 if (200 == res.getStatusCode() || 201 == res.getStatusCode()) {
82 DeployPackageResponse response =
83 new Gson().fromJson(res.getResult(), DeployPackageResponse.class);
84 if (response.isSuccess()) {
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);
96 TemplateUtils.closeInputStream(ins);
97 TemplateUtils.closeZipFile(zf);
104 public static class IpPort {
109 private static IpPort getIpPort(String addr) {
110 Pattern p = Pattern.compile("//(.*?):(.*)");
111 Matcher m = p.matcher(addr);
113 return new IpPort(m.group(1), Integer.valueOf(m.group(2)));
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();
127 private static InputStream getInputStream(ZipFile zf, String planFilePath)
128 throws CatalogResourceException, IOException {
129 ZipEntry ze = TemplateUtils.getZipEntryZipFile(zf, planFilePath);
132 throw new CatalogResourceException("Can't file plan file in the csar. planFilePath = " + planFilePath);
134 return zf.getInputStream(ze);
139 * @param packageName package to delete according packageName
140 * @return DeletePackageResponse
141 * @throws CatalogResourceException e1
143 public static DeletePackageResponse deletePackage(String packageName)
144 throws CatalogResourceException {
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()) {
153 throw new CatalogResourceException(response.toString());
154 } catch (Exception e) {
155 throw new CatalogResourceException(
156 "Call Delete Package api failed. packageName = " + packageName, e);
163 * @param processId process id
164 * @param params params
165 * @return StartProcessResponse
166 * @throws CatalogResourceException e1
168 public static StartProcessResponse startProcess(String processId, Map<String, Object> params)
169 throws CatalogResourceException {
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()) {
179 throw new CatalogResourceException(response.toString());
180 } catch (Exception e) {
181 throw new CatalogResourceException("Call Start Process api failed.", e);