017cf358e2bf9db9f5172c0f44ef2e9d56233dfd
[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.glassfish.jersey.client.ClientConfig;
25 import org.onap.workflow.common.Config;
26 import org.onap.workflow.common.EnumModuleUrl;
27 import org.onap.workflow.common.RestClient;
28 import org.onap.workflow.common.RestClientUtils;
29 import org.onap.workflow.common.RestResponse;
30 import org.onap.workflow.common.ToolUtil;
31 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiDeployResponse;
32 import org.onap.workflow.externalservice.entity.activitientitiy.ActivitiStartProcessRequest;
33 import org.onap.workflow.tools.Constants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
38 import com.google.gson.Gson;
39
40 /**
41  * 
42  * @author 10090474
43  * 
44  */
45 public class ActivitiServiceConsumer {
46   private static final Logger logger = LoggerFactory.getLogger(ActivitiServiceConsumer.class);
47   private static final String DEPLOY_BPMNFILE_URL =
48       EnumModuleUrl.ACTIVITI.getApiRootDomain() + "/repository/deployments";
49
50   private static IActivitiRestService activitiServiceProxy = null;
51
52   private static IActivitiRestService getActivitiService() {
53     if (activitiServiceProxy == null) {
54       ClientConfig config = new ClientConfig();
55       activitiServiceProxy = ConsumerFactory.createConsumer(
56           EnumModuleUrl.getBaseUrl(EnumModuleUrl.ACTIVITI), config, IActivitiRestService.class);
57
58     }
59     return activitiServiceProxy;
60   }
61
62   public static RestResponse undeploybpmnfile(String deploymentId) {
63     /*
64      * IActivitiRestService activitiProxy = getActivitiService();
65      * activitiProxy.undeployBpmnFile(deploymentId);
66      */
67     RestResponse res = deleteDeployProcess(deploymentId);
68     // activitiProxy.startProcess(request);
69
70     return res;
71   }
72
73   public static RestResponse startBpmnProcess(ActivitiStartProcessRequest request) {
74     // IActivitiRestService activitiProxy = getActivitiService();
75     // startProcess( request);
76     // activitiProxy.startProcess(request);
77     return startProcess(request);
78   }
79
80   public static RestResponse deleteDeployProcess(String deploymentId) {
81     // TODO Auto-generated method stub
82     RestResponse res = null;
83     try {
84       res = RestClient.post(null, null, Constants.DEPLOY_BPMNFILE_URL + "/" + deploymentId);
85     } catch (ClientProtocolException e) {
86       // TODO Auto-generated catch block
87       e.printStackTrace();
88     } catch (IOException e) {
89       // TODO Auto-generated catch block
90       e.printStackTrace();
91     }
92
93     return res;
94   }
95
96   public static RestResponse startProcess(ActivitiStartProcessRequest request) {
97     // TODO Auto-generated method stub
98     RestResponse res = null;
99     try {
100       res = RestClient.post(null, null, Constants.ACITIVI_START_INSTANCE_URL, request);
101     } catch (ClientProtocolException e) {
102       // TODO Auto-generated catch block
103       e.printStackTrace();
104     } catch (IOException e) {
105       // TODO Auto-generated catch block
106       e.printStackTrace();
107     }
108
109     // if (Response.Status.OK.getStatusCode() == res.getStatusCode()
110     // || Response.Status.CREATED.getStatusCode() == res.getStatusCode()) {
111     // Response response = new Gson().fromJson(res.getResult(), Response.class);
112     // return response;
113     // }
114     return res;
115   }
116
117   public static ActivitiDeployResponse deploybpmnfile(InputStream ins, String filename)
118       throws ClientProtocolException, IOException {
119     try {
120       return deployPackage2Activiti(ins, filename);
121     } finally {
122       ToolUtil.closeInputStream(ins);
123     }
124   }
125
126   private static ActivitiDeployResponse deployPackage2Activiti(InputStream ins, String filename)
127       throws ClientProtocolException, IOException {
128     String ip = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrIp();
129     int port = Config.getWorkflowAppConfig().getMsbClientConfig().getMsbSvrPort();
130     RestResponse res = RestClient.post(ip, port, DEPLOY_BPMNFILE_URL,
131         RestClientUtils.buildMultipartRequest(ins, filename));
132     logger.info("deployfile to activiti return. {}", res);
133
134     if (Response.Status.OK.getStatusCode() == res.getStatusCode()
135         || Response.Status.CREATED.getStatusCode() == res.getStatusCode()) {
136       ActivitiDeployResponse response =
137           new Gson().fromJson(res.getResult(), ActivitiDeployResponse.class);
138       return response;
139     }
140     return null;
141   }
142 }