Critical Bug fixes wfenginemgrservice
[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.onap.workflow.common.Config;\r
24 import org.onap.workflow.common.EnumModuleUrl;\r
25 import org.onap.workflow.common.RestClient;\r
26 import org.onap.workflow.common.RestClientUtils;\r
27 import org.onap.workflow.common.RestResponse;\r
28 import org.onap.workflow.common.ToolUtil;\r
29 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiDeployResponse;\r
30 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;\r
31 import org.onap.workflow.tools.Constants;\r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\r
34 \r
35 import com.google.gson.Gson;\r
36 \r
37 /**\r
38  * \r
39  * @author 10090474\r
40  * \r
41  */\r
42 public class ActivitiServiceConsumer {\r
43   private static final Logger logger = LoggerFactory.getLogger(ActivitiServiceConsumer.class);\r
44   private static final String DEPLOY_BPMNFILE_URL =\r
45       EnumModuleUrl.ACTIVITI.getApiRootDomain() + "/repository/deployments";\r
46 \r
47   public static RestResponse undeploybpmnfile(String deploymentId) {\r
48     return deleteDeployProcess(deploymentId);\r
49   }\r
50 \r
51   public static RestResponse startBpmnProcess(ActivitiStartProcessRequest request) {\r
52     return startProcess(request);\r
53   }\r
54 \r
55   public static RestResponse deleteDeployProcess(String deploymentId) {\r
56     RestResponse res = null;\r
57     try {\r
58       res = RestClient.post(null, null, Constants.DEPLOY_BPMNFILE_URL + "/" + deploymentId);\r
59     } catch (Exception e) {\r
60         logger.error("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         logger.error("Exception: ",e);\r
73       return null;\r
74     } \r
75     return res;\r
76   }\r
77 \r
78   public static ActivitiDeployResponse deploybpmnfile(InputStream ins, String filename)\r
79       throws IOException {\r
80     try {\r
81       return deployPackage2Activiti(ins, filename);\r
82     } finally {\r
83       ToolUtil.closeInputStream(ins);\r
84     }\r
85   }\r
86 \r
87   private static ActivitiDeployResponse deployPackage2Activiti(InputStream ins, String filename)\r
88       throws IOException {\r
89     String ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();\r
90     int port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();\r
91     RestResponse res = RestClient.post(ip, port, DEPLOY_BPMNFILE_URL,\r
92         RestClientUtils.buildMultipartRequest(ins, filename));\r
93     logger.info("deployfile to activiti return. {}", res);\r
94 \r
95     if (Response.Status.OK.getStatusCode() == res.getStatusCode()\r
96         || Response.Status.CREATED.getStatusCode() == res.getStatusCode()) {\r
97       ActivitiDeployResponse response =\r
98           new Gson().fromJson(res.getResult(), ActivitiDeployResponse.class);\r
99       return response;\r
100     }\r
101     return null;\r
102   }\r
103 }\r