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