2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.openo.holmes.rulemgt.bolt.enginebolt;
\r
18 import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
\r
20 import com.fasterxml.jackson.databind.ObjectMapper;
\r
21 import java.io.ByteArrayOutputStream;
\r
22 import java.io.IOException;
\r
23 import java.io.InputStream;
\r
24 import java.io.InputStreamReader;
\r
25 import java.io.Reader;
\r
26 import java.io.UnsupportedEncodingException;
\r
27 import java.nio.charset.Charset;
\r
28 import lombok.extern.slf4j.Slf4j;
\r
29 import org.apache.commons.lang3.StringUtils;
\r
30 import org.apache.http.ConnectionClosedException;
\r
31 import org.apache.http.HttpEntity;
\r
32 import org.apache.http.HttpResponse;
\r
33 import org.apache.http.client.methods.HttpDelete;
\r
34 import org.apache.http.client.methods.HttpGet;
\r
35 import org.apache.http.client.methods.HttpPost;
\r
36 import org.apache.http.client.methods.HttpPut;
\r
37 import org.apache.http.client.methods.HttpRequestBase;
\r
38 import org.apache.http.entity.BufferedHttpEntity;
\r
39 import org.apache.http.entity.ByteArrayEntity;
\r
40 import org.apache.http.entity.ContentType;
\r
41 import org.apache.http.impl.client.CloseableHttpClient;
\r
42 import org.apache.http.impl.client.HttpClients;
\r
43 import org.apache.http.protocol.HTTP;
\r
44 import org.apache.http.util.EntityUtils;
\r
45 import org.jvnet.hk2.annotations.Service;
\r
46 import org.openo.holmes.common.config.MicroServiceConfig;
\r
47 import org.openo.holmes.common.exception.CorrelationException;
\r
48 import org.openo.holmes.common.utils.I18nProxy;
\r
49 import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
\r
50 import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
\r
51 import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
\r
55 public class EngineService {
\r
57 String url = "http://10.250.0.3:9102";
\r
59 protected HttpResponse delete(String packageName) throws IOException {
\r
60 return deleteRequest(url + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
\r
63 protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)
\r
64 throws IOException {
\r
65 ObjectMapper mapper = new ObjectMapper();
\r
66 String content = mapper.writeValueAsString(correlationCheckRule4Engine);
\r
67 String queryUrl = MicroServiceConfig.getMsbServerAddr()
\r
68 + "/openoapi/microservices/v1/services/holmes-engine/version/v1";
\r
69 HttpGet httpGet = new HttpGet(queryUrl);
\r
70 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
72 HttpResponse httpResponse = httpClient.execute(httpGet);
\r
73 log.info("response entity:" + EntityUtils.toString(httpResponse.getEntity()));
\r
77 return postRequest(url + RuleMgtConstant.ENGINE_PATH, content);
\r
80 protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {
\r
81 ObjectMapper mapper = new ObjectMapper();
\r
82 String content = mapper.writeValueAsString(correlationDeployRule4Engine);
\r
83 return putRequest(url + RuleMgtConstant.ENGINE_PATH, content);
\r
86 private HttpResponse postRequest(String url, String content) throws IOException {
\r
87 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
89 HttpPost httpPost = new HttpPost(url);
\r
90 log.info("url:" + url + "," + "post:" + httpPost);
\r
91 setHeader(httpPost);
\r
92 if (StringUtils.isNotEmpty(content)) {
\r
93 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
\r
95 return httpClient.execute(httpPost);
\r
101 private HttpResponse putRequest(String url, String content) throws IOException {
\r
102 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
104 HttpPut httpPut = new HttpPut(url);
\r
105 setHeader(httpPut);
\r
106 if (StringUtils.isNotEmpty(content)) {
\r
107 httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
\r
109 return httpClient.execute(httpPut);
\r
111 httpClient.close();
\r
115 private HttpResponse deleteRequest(String url) throws IOException {
\r
116 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
118 HttpDelete httpDelete = new HttpDelete(url);
\r
119 setHeader(httpDelete);
\r
120 return httpClient.execute(httpDelete);
\r
122 httpClient.close();
\r
126 private void setHeader(HttpRequestBase httpRequestBase) {
\r
127 httpRequestBase.setHeader("Accept", "application/json");
\r
128 httpRequestBase.setHeader("Content-Type", "application/json");
\r
131 public String getResponseContent(HttpResponse response) {
\r
132 HttpEntity entity = response.getEntity();
\r
133 InputStream is = null;
\r
134 if (entity != null) {
\r
136 is = entity.getContent();
\r
137 final ContentType contentType = ContentType.getOrDefault(entity);
\r
138 Charset charset = contentType.getCharset();
\r
139 if (charset == null) {
\r
140 charset = HTTP.DEF_CONTENT_CHARSET;
\r
142 final StringBuilder b = new StringBuilder();
\r
143 final char[] tmp = new char[1024];
\r
144 final Reader reader = new InputStreamReader(is, charset);
\r
147 while ((l = reader.read(tmp)) != -1) {
\r
148 b.append(tmp, 0, l);
\r
150 } catch (ConnectionClosedException ignore) {
\r
152 } catch (IOException e) {
\r
153 log.info("Failed to read the contents of the input stream of the http entity.", e);
\r
155 return b.toString();
\r
156 } catch (IOException e) {
\r
157 log.info("Failed to read the contents of the http entity.", e);
\r
163 } catch (IOException e) {
\r
164 log.info("Failed to close the input stream of the http entity.", e);
\r
171 public byte[] getData(HttpEntity httpEntity) throws IOException {
\r
172 log.info("Rule deployed. Package name: " + httpEntity.getContent().toString()
\r
173 + ". Content length: " + httpEntity.getContentLength());
\r
174 BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
\r
175 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
\r
176 bufferedHttpEntity.writeTo(byteArrayOutputStream);
\r
177 byte[] responseBytes = byteArrayOutputStream.toByteArray();
\r
178 return responseBytes;
\r
181 // public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {
\r
182 // byte[] dataByte;
\r
183 // String result = null;
\r
185 // HttpEntity httpEntity = httpResponse.getEntity();
\r
186 // if (httpEntity != null) {
\r
187 // byte[] responseBytes = getData(httpEntity);
\r
188 // dataByte = responseBytes;
\r
189 // result = bytesToString(dataByte);
\r
192 // } catch (Exception e) {
\r
193 // throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);
\r
197 private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {
\r
198 if (bytes != null) {
\r
199 String returnStr = new String(bytes, "utf-8");
\r
200 returnStr = StringUtils.trim(returnStr);
\r