2 * Copyright 2017-2020 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.onap.holmes.rulemgt.bolt.enginebolt;
\r
18 import lombok.extern.slf4j.Slf4j;
\r
19 import org.apache.http.HttpResponse;
\r
20 import org.apache.http.client.methods.HttpDelete;
\r
21 import org.apache.http.client.methods.HttpPost;
\r
22 import org.apache.http.client.methods.HttpPut;
\r
23 import org.apache.http.entity.StringEntity;
\r
24 import org.apache.http.impl.client.CloseableHttpClient;
\r
25 import org.jvnet.hk2.annotations.Service;
\r
26 import org.onap.holmes.common.utils.GsonUtil;
\r
27 import org.onap.holmes.common.utils.HttpsUtils;
\r
28 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
\r
29 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
\r
30 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
\r
32 import javax.ws.rs.core.MediaType;
\r
33 import java.io.IOException;
\r
34 import java.util.HashMap;
\r
38 public class EngineService {
\r
40 private static final String HTTPS = "https://";
\r
41 private static final String HTTP = "http://";
\r
42 private static final String PORT = ":9102";
\r
44 protected HttpResponse delete(String packageName, String ip) throws Exception {
\r
45 HashMap headers = createHeaders();
\r
46 String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;
\r
47 CloseableHttpClient httpClient = null;
\r
48 HttpDelete httpDelete = new HttpDelete(url);
\r
50 httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
\r
51 return HttpsUtils.delete(httpDelete, headers, httpClient);
\r
53 httpDelete.releaseConnection();
\r
54 closeHttpClient(httpClient);
\r
58 protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)
\r
60 String content = GsonUtil.beanToJson(correlationCheckRule4Engine);
\r
61 HashMap headers = createHeaders();
\r
62 String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;
\r
63 CloseableHttpClient httpClient = null;
\r
64 HttpPost httpPost = new HttpPost(url);
\r
66 httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
\r
67 return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient);
\r
69 httpPost.releaseConnection();
\r
70 closeHttpClient(httpClient);
\r
74 protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {
\r
75 String content = GsonUtil.beanToJson(correlationDeployRule4Engine);
\r
76 HashMap headers = createHeaders();
\r
77 String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH;
\r
78 CloseableHttpClient httpClient = null;
\r
79 HttpPut httpPut = new HttpPut(url);
\r
81 httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);
\r
82 return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient);
\r
84 closeHttpClient(httpClient);
\r
88 private void closeHttpClient(CloseableHttpClient httpClient) {
\r
89 if (httpClient != null) {
\r
92 } catch (IOException e) {
\r
93 log.warn("Failed to close http client!");
\r
98 private HashMap<String, String> createHeaders() {
\r
99 HashMap<String, String> headers = new HashMap<>();
\r
100 headers.put("Content-Type", MediaType.APPLICATION_JSON);
\r
101 headers.put("Accept", MediaType.APPLICATION_JSON);
\r
105 private String getRequestPref() {
\r
106 return HttpsUtils.isHttpsEnabled() ? HTTPS : HTTP;
\r