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