114e9d2154ff2c68d729d20fdbd83ab542bdc80e
[vfc/nfvo/catalog.git] /
1 /**\r
2  * Copyright 2016 [ZTE] and others.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.openo.commontosca.catalog.model.plan.wso2;\r
17 \r
18 import java.util.Map;\r
19 \r
20 import org.glassfish.jersey.client.ClientConfig;\r
21 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;\r
22 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeletePackageResponse;\r
23 import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessResponse;\r
24 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;\r
25 import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessRequest;\r
26 \r
27 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
28 \r
29 /**\r
30  * \r
31  * @author 10090474\r
32  * \r
33  */\r
34 public class WSO2ServiceConsumer {\r
35     public static final String WSO2_BASE_URL = "http://localhost:9449/";\r
36 \r
37     /**\r
38      * \r
39      * @param filePath\r
40      * @return\r
41      * @throws CatalogResourceException\r
42      */\r
43     public static DeployPackageResponse deployPackage(String filePath)\r
44             throws CatalogResourceException {\r
45 \r
46         // final FormDataMultiPart formData = new FormDataMultiPart();\r
47         // final BodyPart bodyPart = new FormDataBodyPart(fileDetail,\r
48         // uploadedInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE);\r
49         // formData.bodyPart(bodyPart);\r
50         // formData.setContentDisposition(fileDetail);\r
51         // formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);\r
52         // final Client client = ClientBuilder.newBuilder()\r
53         // .register(MultiPartFeature.class).build();\r
54         // final WebTarget target = client.target(getPackageURL()).path(\r
55         // "/containerapi/CSARs");\r
56         // final Response response = target.request().post(\r
57         // Entity.entity(formData, formData.getMediaType()));\r
58         // return response;\r
59 \r
60         return null;\r
61         // try {\r
62         // ClientConfig config = new ClientConfig();\r
63         // IWSO2RestService yamlParseProxy = ConsumerFactory\r
64         // .createConsumer(MSBUtil.getYamlParseBaseUrl(), config,\r
65         // IWSO2RestService.class);\r
66         // String jsonStr = yamlParseProxy.parse(request);\r
67         // return new Gson().fromJson(jsonStr, ParseYamlResult.class);\r
68         // } catch (Exception e) {\r
69         // throw new CatalogResourceException("Call parser api failed.", e);\r
70         // }\r
71 \r
72     }\r
73 \r
74 \r
75     /**\r
76      * @param packageName\r
77      * @return\r
78      * @throws CatalogResourceException\r
79      */\r
80     DeletePackageResponse deletePackage(String packageName)\r
81             throws CatalogResourceException {\r
82         try {\r
83             ClientConfig config = new ClientConfig();\r
84             IWSO2RestService wso2Proxy = ConsumerFactory.createConsumer(\r
85                     WSO2_BASE_URL, config, IWSO2RestService.class);\r
86             DeletePackageResponse response = wso2Proxy\r
87                     .deletePackage(packageName);\r
88             if (response.isSuccess()) {\r
89                 return response;\r
90             }\r
91             throw new CatalogResourceException(response.getException());\r
92         } catch (Exception e) {\r
93             throw new CatalogResourceException(\r
94                     "Call Delete Package api failed.", e);\r
95         }\r
96     }\r
97 \r
98 \r
99     /**\r
100      * @param processId\r
101      * @param params\r
102      * @return\r
103      * @throws CatalogResourceException\r
104      */\r
105     StartProcessResponse startProcess(String processId,\r
106                                       Map<String, Object> params) throws CatalogResourceException {\r
107         try {\r
108             ClientConfig config = new ClientConfig();\r
109             IWSO2RestService wso2Proxy = ConsumerFactory.createConsumer(\r
110                     WSO2_BASE_URL, config, IWSO2RestService.class);\r
111             StartProcessResponse response = wso2Proxy\r
112                     .startProcess(new StartProcessRequest(processId, params));\r
113             if (response.isSuccess()) {\r
114                 return response;\r
115             }\r
116             throw new CatalogResourceException(response.getException());\r
117         } catch (Exception e) {\r
118             throw new CatalogResourceException(\r
119                     "Call Start Process api failed.", e);\r
120         }\r
121     }\r
122 \r
123 }\r