5588dd40db11e9a25b9fb605a64b62a01dcec356
[holmes/rule-management.git] / rulemgt / src / main / java / org / openo / holmes / rulemgt / bolt / enginebolt / EngineService.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.openo.holmes.rulemgt.bolt.enginebolt;\r
17 \r
18 import com.fasterxml.jackson.databind.ObjectMapper;\r
19 import java.io.ByteArrayOutputStream;\r
20 import java.io.IOException;\r
21 import java.io.UnsupportedEncodingException;\r
22 import lombok.extern.slf4j.Slf4j;\r
23 import org.apache.commons.lang3.StringUtils;\r
24 import org.apache.http.HttpEntity;\r
25 import org.apache.http.HttpResponse;\r
26 import org.apache.http.client.methods.HttpDelete;\r
27 import org.apache.http.client.methods.HttpGet;\r
28 import org.apache.http.client.methods.HttpPost;\r
29 import org.apache.http.client.methods.HttpPut;\r
30 import org.apache.http.client.methods.HttpRequestBase;\r
31 import org.apache.http.entity.BufferedHttpEntity;\r
32 import org.apache.http.entity.ByteArrayEntity;\r
33 import org.apache.http.impl.client.CloseableHttpClient;\r
34 import org.apache.http.impl.client.HttpClients;\r
35 import org.apache.http.util.EntityUtils;\r
36 import org.jvnet.hk2.annotations.Service;\r
37 import org.openo.holmes.common.config.MicroServiceConfig;\r
38 import org.openo.holmes.common.exception.CorrelationException;\r
39 import org.openo.holmes.common.utils.I18nProxy;\r
40 import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
41 import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
42 import org.openo.holmes.rulemgt.constant.RuleMgtConstant;\r
43 \r
44 @Slf4j\r
45 @Service\r
46 public class EngineService {\r
47 \r
48     String url = "http://10.250.0.3:9102";\r
49 \r
50     protected HttpResponse delete(String packageName) throws IOException {\r
51         return deleteRequest(url + RuleMgtConstant.ENGINE_PATH + "/" + packageName);\r
52     }\r
53 \r
54     protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
55             throws IOException {\r
56         ObjectMapper mapper = new ObjectMapper();\r
57         String content = mapper.writeValueAsString(correlationCheckRule4Engine);\r
58         String queryUrl = MicroServiceConfig.getMsbServerAddr()\r
59                 + "/openoapi/microservices/v1/services/holmes-engine/version/v1";\r
60         HttpGet httpGet = new HttpGet(queryUrl);\r
61         CloseableHttpClient httpClient = HttpClients.createDefault();\r
62         try {\r
63             HttpResponse httpResponse = httpClient.execute(httpGet);\r
64             log.info("response entity:" + EntityUtils.toString(httpResponse.getEntity()));\r
65         } finally {\r
66             httpClient.close();\r
67         }\r
68         return postRequest(url + RuleMgtConstant.ENGINE_PATH, content);\r
69     }\r
70 \r
71     protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {\r
72         ObjectMapper mapper = new ObjectMapper();\r
73         String content = mapper.writeValueAsString(correlationDeployRule4Engine);\r
74         return putRequest(url + RuleMgtConstant.ENGINE_PATH, content);\r
75     }\r
76 \r
77     private HttpResponse postRequest(String url, String content) throws IOException {\r
78         CloseableHttpClient httpClient = HttpClients.createDefault();\r
79         try {\r
80             HttpPost httpPost = new HttpPost(url);\r
81             log.info("url:" + url + "," + "post:" + httpPost);\r
82             setHeader(httpPost);\r
83             if (StringUtils.isNotEmpty(content)) {\r
84                 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
85             }\r
86             return httpClient.execute(httpPost);\r
87         } finally {\r
88             httpClient.close();\r
89         }\r
90     }\r
91 \r
92     private HttpResponse putRequest(String url, String content) throws IOException {\r
93         CloseableHttpClient httpClient = HttpClients.createDefault();\r
94         try {\r
95             HttpPut httpPut = new HttpPut(url);\r
96             setHeader(httpPut);\r
97             if (StringUtils.isNotEmpty(content)) {\r
98                 httpPut.setEntity(new ByteArrayEntity(content.getBytes()));\r
99             }\r
100             return httpClient.execute(httpPut);\r
101         } finally {\r
102             httpClient.close();\r
103         }\r
104     }\r
105 \r
106     private HttpResponse deleteRequest(String url) throws IOException {\r
107         CloseableHttpClient httpClient = HttpClients.createDefault();\r
108         try {\r
109             HttpDelete httpDelete = new HttpDelete(url);\r
110             setHeader(httpDelete);\r
111             return httpClient.execute(httpDelete);\r
112         } finally {\r
113             httpClient.close();\r
114         }\r
115     }\r
116 \r
117     private void setHeader(HttpRequestBase httpRequestBase) {\r
118         httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");\r
119         httpRequestBase.setHeader("Accept", "application/json");\r
120         httpRequestBase.setHeader("Content-Type", "application/json");\r
121         httpRequestBase.setHeader("Content-Length", "2000");\r
122     }\r
123 \r
124     public byte[] getData(HttpEntity httpEntity) throws IOException {\r
125         BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);\r
126         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\r
127         bufferedHttpEntity.writeTo(byteArrayOutputStream);\r
128         byte[] responseBytes = byteArrayOutputStream.toByteArray();\r
129         return responseBytes;\r
130     }\r
131 \r
132     public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {\r
133         byte[] dataByte;\r
134         String result = null;\r
135         try {\r
136             HttpEntity httpEntity = httpResponse.getEntity();\r
137             if (httpEntity != null) {\r
138                 byte[] responseBytes = getData(httpEntity);\r
139                 dataByte = responseBytes;\r
140                 result = bytesToString(dataByte);\r
141             }\r
142             return result;\r
143         } catch (Exception e) {\r
144             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);\r
145         }\r
146     }\r
147 \r
148     private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {\r
149         if (bytes != null) {\r
150             String returnStr = new String(bytes, "utf-8");\r
151             returnStr = StringUtils.trim(returnStr);\r
152             return returnStr;\r
153         }\r
154         return null;\r
155     }\r
156 }\r