Fix the docker build error for vfc-nfvo-wfengine
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / main / java / org / onap / workflow / externalservice / service / activitiservice / ActivitiServiceConsumer.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\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.onap.workflow.externalservice.service.activitiservice;\r
17 \r
18 import java.io.IOException;\r
19 import java.io.InputStream;\r
20 \r
21 import javax.ws.rs.core.Response;\r
22 \r
23 import org.apache.http.client.ClientProtocolException;\r
24 import org.onap.workflow.common.Config;\r
25 import org.onap.workflow.common.EnumModuleUrl;\r
26 import org.onap.workflow.common.RestClient;\r
27 import org.onap.workflow.common.RestClientUtils;\r
28 import org.onap.workflow.common.RestResponse;\r
29 import org.onap.workflow.common.ToolUtil;\r
30 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiDeployResponse;\r
31 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
32 import org.onap.workflow.tools.Constants;\r
33 import org.slf4j.Logger;\r
34 import org.slf4j.LoggerFactory;\r
35 \r
36 import com.google.gson.Gson;\r
37 \r
38 /**\r
39  * \r
40  * @author 10090474\r
41  * \r
42  */\r
43 public class ActivitiServiceConsumer {\r
44   private static final Logger logger = LoggerFactory.getLogger(ActivitiServiceConsumer.class);\r
45   private static final String DEPLOY_BPMNFILE_URL =\r
46       EnumModuleUrl.ACTIVITI.getApiRootDomain() + "/repository/deployments";\r
47 \r
48   public static RestResponse undeploybpmnfile(String deploymentId) {\r
49     return deleteDeployProcess(deploymentId);\r
50   }\r
51 \r
52   public static RestResponse startBpmnProcess(ActivitiStartProcessRequest request) {\r
53     return startProcess(request);\r
54   }\r
55 \r
56   public static RestResponse deleteDeployProcess(String deploymentId) {\r
57     RestResponse res = null;\r
58     try {\r
59       res = RestClient.post(null, null, Constants.DEPLOY_BPMNFILE_URL + "/" + deploymentId);\r
60     } catch (Exception e) {\r
61      return null;\r
62     }\r
63 \r
64     return res;\r
65   }\r
66 \r
67   public static RestResponse startProcess(ActivitiStartProcessRequest request) {\r
68     RestResponse res = null;\r
69     try {\r
70       res = RestClient.post(null, null, Constants.ACITIVI_START_INSTANCE_URL, request);\r
71     } catch (Exception e) {\r
72       return null;\r
73     } \r
74     return res;\r
75   }\r
76 \r
77   public static ActivitiDeployResponse deploybpmnfile(InputStream ins, String filename)\r
78       throws ClientProtocolException, IOException {\r
79     try {\r
80       return deployPackage2Activiti(ins, filename);\r
81     } finally {\r
82       ToolUtil.closeInputStream(ins);\r
83     }\r
84   }\r
85 \r
86   private static ActivitiDeployResponse deployPackage2Activiti(InputStream ins, String filename)\r
87       throws ClientProtocolException, IOException {\r
88     String ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
89     int port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
90     RestResponse res = RestClient.post(ip, port, DEPLOY_BPMNFILE_URL,\r
91         RestClientUtils.buildMultipartRequest(ins, filename));\r
92     logger.info("deployfile to activiti return. {}", res);\r
93 \r
94     if (Response.Status.OK.getStatusCode() == res.getStatusCode()\r
95         || Response.Status.CREATED.getStatusCode() == res.getStatusCode()) {\r
96       ActivitiDeployResponse response =\r
97           new Gson().fromJson(res.getResult(), ActivitiDeployResponse.class);\r
98       return response;\r
99     }\r
100     return null;\r
101   }\r
102 }\r