From 8182df37f0308ad04433a47570e7ca9612fcfd84 Mon Sep 17 00:00:00 2001 From: tang peng Date: Wed, 2 Jun 2021 17:14:36 +0800 Subject: Removed Dependency: httpclient Issue-ID: HOLMES-414 Signed-off-by: tang peng Change-Id: I3d7771d25d4bd0f67a2e02373ceb2ec392ad4f40 --- .../java/org/onap/holmes/rulemgt/Initializer.java | 9 +- .../org/onap/holmes/rulemgt/RuleActiveApp.java | 3 +- .../bean/request/CorrelationDeployRule4Engine.java | 4 +- .../rulemgt/bolt/enginebolt/EngineService.java | 102 +++++++-------------- .../rulemgt/bolt/enginebolt/EngineWrapper.java | 38 ++------ .../rulemgt/dcae/DcaeConfigurationPolling.java | 93 +++---------------- 6 files changed, 57 insertions(+), 192 deletions(-) (limited to 'rulemgt/src/main/java') diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java index 92c77ab..6769d65 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/Initializer.java @@ -19,7 +19,7 @@ package org.onap.holmes.rulemgt; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.CommonUtils; import org.onap.holmes.common.utils.MsbRegister; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; import org.onap.msb.sdk.discovery.entity.Node; @@ -31,7 +31,8 @@ import javax.inject.Inject; import java.util.HashSet; import java.util.Set; -import static org.onap.holmes.common.config.MicroServiceConfig.*; +import static org.onap.holmes.common.utils.CommonUtils.getEnv; +import static org.onap.holmes.common.utils.CommonUtils.isIpAddress; @Service public class Initializer { @@ -62,13 +63,13 @@ public class Initializer { msinfo.setProtocol("REST"); msinfo.setVisualRange("0|1"); msinfo.setLb_policy("round-robin"); - msinfo.setEnable_ssl(HttpsUtils.isHttpsEnabled()); + msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled()); Set nodes = new HashSet<>(); Node node = new Node(); node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_RULE_MGMT_SERVICE_HOST")); node.setPort("9101"); /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now - String msbAddrTemplate = (HttpsUtils.isHttpsEnabled() ? "https" : "http") + String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http") + "://%s:%s/api/holmes-rule-mgmt/v1/healthcheck"; node.setCheckType("HTTP"); node.setCheckUrl(String.format(msbAddrTemplate, serviceAddrInfo[0], "9101")); diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java index 0385f91..f77b909 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java @@ -19,6 +19,7 @@ package org.onap.holmes.rulemgt; import io.dropwizard.setup.Environment; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.dropwizard.ioc.bundle.IOCApplication; +import org.onap.holmes.common.utils.CommonUtils; import org.onap.holmes.common.utils.transactionid.TransactionIdFilter; import org.onap.holmes.rulemgt.dcae.DcaeConfigurationPolling; import org.slf4j.Logger; @@ -43,7 +44,7 @@ public class RuleActiveApp extends IOCApplication { if (!"1".equals(System.getenv("TESTING"))) { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate( - new DcaeConfigurationPolling(MicroServiceConfig.getEnv(MicroServiceConfig.HOSTNAME)), 0, + new DcaeConfigurationPolling(CommonUtils.getEnv(MicroServiceConfig.HOSTNAME)), 0, DcaeConfigurationPolling.POLLING_PERIOD, TimeUnit.MILLISECONDS); } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java index 4c578e4..714a95c 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java @@ -22,9 +22,7 @@ import lombok.Setter; @Getter @Setter -public class CorrelationDeployRule4Engine { - - private String content; +public class CorrelationDeployRule4Engine extends CorrelationCheckRule4Engine { private String engineId; diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java index 8782d7a..794ea93 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,94 +15,54 @@ */ package org.onap.holmes.rulemgt.bolt.enginebolt; -import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; import org.jvnet.hk2.annotations.Service; -import org.onap.holmes.common.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.CommonUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; -import org.onap.holmes.rulemgt.constant.RuleMgtConstant; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.util.HashMap; -@Slf4j +import static org.onap.holmes.rulemgt.constant.RuleMgtConstant.ENGINE_PATH; + @Service public class EngineService { - private static final String HTTPS = "https://"; - private static final String HTTP = "http://"; - private static final String PORT = ":9102"; + private static final String PORT = "9102"; + private static final String SEP = "//"; + private static final String COLON = ":"; - protected HttpResponse delete(String packageName, String ip) throws Exception { - HashMap headers = createHeaders(); - String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName; - CloseableHttpClient httpClient = null; - HttpDelete httpDelete = new HttpDelete(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - return HttpsUtils.delete(httpDelete, headers, httpClient); - } finally { - httpDelete.releaseConnection(); - closeHttpClient(httpClient); - } + protected boolean delete(String packageName, String ip) { + return new JerseyClient() + .path(packageName) + .delete(getUrl(ip)) != null; } - protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) - throws Exception { - String content = GsonUtil.beanToJson(correlationCheckRule4Engine); - HashMap headers = createHeaders(); - String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH; - CloseableHttpClient httpClient = null; - HttpPost httpPost = new HttpPost(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - return HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content), httpClient); - } finally { - httpPost.releaseConnection(); - closeHttpClient(httpClient); - } + protected boolean check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) { + return new JerseyClient() + .header("Accept", MediaType.APPLICATION_JSON) + .post(getUrl(ip), Entity.json(correlationCheckRule4Engine)) != null; } - protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception { - String content = GsonUtil.beanToJson(correlationDeployRule4Engine); - HashMap headers = createHeaders(); - String url = getRequestPref() + ip + PORT + RuleMgtConstant.ENGINE_PATH; - CloseableHttpClient httpClient = null; - HttpPut httpPut = new HttpPut(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - return HttpsUtils.put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient); - } finally { - closeHttpClient(httpClient); - } + protected String deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) { + return new JerseyClient() + .header("Accept", MediaType.APPLICATION_JSON) + .put(getUrl(ip), Entity.json(correlationDeployRule4Engine)); } - private void closeHttpClient(CloseableHttpClient httpClient) { - if (httpClient != null) { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } - } - private HashMap createHeaders() { - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - headers.put("Accept", MediaType.APPLICATION_JSON); - return headers; + private String getRequestPref() { + return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP; } - private String getRequestPref() { - return HttpsUtils.isHttpsEnabled() ? HTTPS : HTTP; + private String getUrl(String ip) { + return new StringBuilder(getRequestPref()) + .append(SEP) + .append(ip) + .append(COLON) + .append(PORT) + .append(ENGINE_PATH) + .toString(); } } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java index 4fe5896..7c927c0 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,8 @@ package org.onap.holmes.rulemgt.bolt.enginebolt; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.HttpsUtils; import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; import org.onap.holmes.rulemgt.constant.RuleMgtConstant; @@ -36,16 +34,10 @@ public class EngineWrapper { private EngineService engineService; public String deployEngine(CorrelationDeployRule4Engine correlationRule, String ip) throws CorrelationException { - HttpResponse response; - try { - response = engineService.deploy(correlationRule, ip); - } catch (Exception e) { - throw new CorrelationException("Failed to call the rule deployment RESTful API.", e); - } - if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Succeeded in calling the rule deployment RESTful API from the engine management service."); + String response = engineService.deploy(correlationRule, ip); + if (response != null) { try { - JsonObject json = JsonParser.parseString(HttpsUtils.extractResponseEntity(response)).getAsJsonObject(); + JsonObject json = JsonParser.parseString(response).getAsJsonObject(); return json.get(RuleMgtConstant.PACKAGE).getAsString(); } catch (Exception e) { throw new CorrelationException("Failed to parse the value returned by the engine management service.", e); @@ -56,14 +48,7 @@ public class EngineWrapper { } public boolean deleteRuleFromEngine(String packageName, String ip) throws CorrelationException { - HttpResponse response; - try { - response = engineService.delete(packageName, ip); - } catch (Exception e) { - throw new CorrelationException("Failed to call the rule deleting RESTful API.", e); - } - if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Succeeded in calling the rule deleting RESTful API from the engine management service."); + if (engineService.delete(packageName, ip)) { return true; } else { throw new CorrelationException("Failed to delete the rule!"); @@ -73,18 +58,9 @@ public class EngineWrapper { public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) throws CorrelationException { log.info("Rule Contents: " + correlationCheckRule4Engine.getContent()); - HttpResponse response; - try { - response = engineService.check(correlationCheckRule4Engine, ip); - } catch (Exception e) { - throw new CorrelationException("Failed to call the rule verification RESTful API.", e); - } - if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) { - log.info("Succeeded in calling the rule verification RESTful API from the engine management service."); - return true; - } else { - log.info(response.getStatusLine().getStatusCode() + " " + response.getEntity()); + if (!engineService.check(correlationCheckRule4Engine, ip)) { throw new CorrelationException("Failed to verify the rule. The contents of the rule are invalid."); } + return true; } } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java index e6bc790..036bd9a 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -14,29 +14,19 @@ package org.onap.holmes.rulemgt.dcae; -import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; import org.onap.holmes.common.dcae.DcaeConfigurationQuery; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.entity.Rule; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.onap.holmes.common.utils.Md5Util; import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; import org.onap.holmes.rulemgt.bean.response.RuleResult4API; +import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.HashMap; import java.util.List; @Slf4j @@ -77,12 +67,8 @@ public class DcaeConfigurationPolling implements Runnable { if (dcaeConfigurations != null) { try { ruleQueryListResponse = getAllCorrelationRules(); - } catch (CorrelationException e) { - log.error("Failed to get right response!" + e.getMessage(), e); - } catch (IOException e) { - log.error("Failed to extract response entity. " + e.getMessage(), e); } catch (Exception e) { - log.error("Failed to build http client. " + e.getMessage(), e); + log.error("Failed to get deployed rules from the rule management module: " + e.getMessage(), e); } } if (ruleQueryListResponse != null) { @@ -97,53 +83,17 @@ public class DcaeConfigurationPolling implements Runnable { } } - public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException { - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - CloseableHttpClient httpClient = null; - HttpGet httpGet = new HttpGet(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpResponse httpResponse = HttpsUtils.get(httpGet, headers, httpClient); - String response = HttpsUtils.extractResponseEntity(httpResponse); - return GsonUtil.jsonToBean(response, RuleQueryListResponse.class); - } finally { - httpGet.releaseConnection(); - closeHttpClient(httpClient); - } + private RuleQueryListResponse getAllCorrelationRules() { + return new JerseyClient().get(url, RuleQueryListResponse.class); } private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException { boolean suc = false; for (Rule rule : dcaeConfigurations.getDefaultRules()) { RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule); - String content = ""; - try { - content = GsonUtil.beanToJson(ruleCreateRequest); - } catch (Exception e) { - throw new CorrelationException("Failed to convert the message object to a json string.", e); - } - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - headers.put("Accept", MediaType.APPLICATION_JSON); - HttpResponse httpResponse; - CloseableHttpClient httpClient = null; - HttpPut httpPut = new HttpPut(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - httpResponse = HttpsUtils - .put(httpPut, headers, new HashMap<>(), new StringEntity(content), httpClient); - } catch (UnsupportedEncodingException e) { - throw new CorrelationException("Failed to create https entity.", e); - } catch (Exception e) { - throw new CorrelationException(e.getMessage()); - } finally { - httpPut.releaseConnection(); - closeHttpClient(httpClient); - } - if (httpResponse != null) { - suc = httpResponse.getStatusLine().getStatusCode() == 200; - } + suc = new JerseyClient().header("Accept", MediaType.APPLICATION_JSON) + .put(url, Entity.json(ruleCreateRequest)) != null; + if (!suc) { break; } @@ -153,19 +103,8 @@ public class DcaeConfigurationPolling implements Runnable { private void deleteAllCorrelationRules(List ruleResult4APIs) { ruleResult4APIs.forEach(correlationRule -> { - HashMap headers = new HashMap<>(); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - CloseableHttpClient httpClient = null; - HttpDelete httpDelete = new HttpDelete(url + "/" + correlationRule.getRuleId()); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - HttpsUtils.delete(httpDelete, headers, httpClient); - } catch (Exception e) { - log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId() - + " exception messge is : " + e.getMessage(), e); - } finally { - httpDelete.releaseConnection(); - closeHttpClient(httpClient); + if (null == new JerseyClient().delete(url + "/" + correlationRule.getRuleId())) { + log.warn("Failed to delete rule, the rule id is: {}", correlationRule.getRuleId()); } }); } @@ -179,14 +118,4 @@ public class DcaeConfigurationPolling implements Runnable { ruleCreateRequest.setEnabled(1); return ruleCreateRequest; } - - private void closeHttpClient(CloseableHttpClient httpClient) { - if (httpClient != null) { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } - } } -- cgit 1.2.3-korg