summaryrefslogtreecommitdiffstats
path: root/rulemgt
diff options
context:
space:
mode:
authorShiwei Tian <tian.shiwei@zte.com.cn>2018-04-09 09:32:39 +0800
committerShiwei Tian <tian.shiwei@zte.com.cn>2018-04-09 09:32:39 +0800
commit37b3bb9b015069ba5e9ad1845de30467decb61d4 (patch)
tree2ddfa4adfb99e78353dc5a402131b03dce60a82d /rulemgt
parent1985d463e60ab79a2fadc4065a9ea19861933692 (diff)
fix https bug
Issue-ID: HOLMES-104 Change-Id: I6922584f94aa0ec79fda1e2b3dcc7da6c874c9da Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
Diffstat (limited to 'rulemgt')
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java36
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java37
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java16
-rw-r--r--rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java31
4 files changed, 110 insertions, 10 deletions
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 13507d6..aa0bf32 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
@@ -15,11 +15,13 @@
*/
package org.onap.holmes.rulemgt.bolt.enginebolt;
+import java.io.IOException;
import java.util.HashMap;
import javax.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
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;
@@ -38,7 +40,13 @@ public class EngineService {
protected HttpResponse delete(String packageName, String ip) throws Exception {
HashMap headers = createHeaders();
String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH + "/" + packageName;
- return HttpsUtils.delete(url, headers);
+ CloseableHttpClient httpClient = null;
+ try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ return HttpsUtils.delete(url, headers, httpClient);
+ } finally {
+ closeHttpClient(httpClient);
+ }
}
protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip)
@@ -46,14 +54,36 @@ public class EngineService {
String content = GsonUtil.beanToJson(correlationCheckRule4Engine);
HashMap headers = createHeaders();
String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;
- return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content));
+ CloseableHttpClient httpClient = null;
+ try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content), httpClient);
+ } finally {
+ closeHttpClient(httpClient);
+ }
}
protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) throws Exception {
String content = GsonUtil.beanToJson(correlationDeployRule4Engine);
HashMap headers = createHeaders();
String url = PREFIX + ip + PORT + RuleMgtConstant.ENGINE_PATH;
- return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content));
+ CloseableHttpClient httpClient = null;
+ try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content),httpClient);
+ } finally {
+ closeHttpClient(httpClient);
+ }
+ }
+
+ private void closeHttpClient(CloseableHttpClient httpClient) {
+ if (httpClient != null) {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ log.warn("Failed to close http client!");
+ }
+ }
}
private HashMap<String, String> createHeaders() {
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 e8fa8b0..d1b2aba 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
@@ -30,6 +30,7 @@ import javax.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
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;
@@ -83,6 +84,8 @@ public class DcaeConfigurationPolling implements Runnable {
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);
}
}
if (ruleQueryListResponse != null) {
@@ -100,9 +103,15 @@ public class DcaeConfigurationPolling implements Runnable {
public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", MediaType.APPLICATION_JSON);
- HttpResponse httpResponse = HttpsUtils.get(url, headers);
- String response = HttpsUtils.extractResponseEntity(httpResponse);
- return JSON.parseObject(response,RuleQueryListResponse.class);
+ CloseableHttpClient httpClient = null;
+ try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpResponse httpResponse = HttpsUtils.get(url, headers, httpClient);
+ String response = HttpsUtils.extractResponseEntity(httpResponse);
+ return JSON.parseObject(response,RuleQueryListResponse.class);
+ } finally {
+ closeHttpClient(httpClient);
+ }
}
private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
@@ -119,13 +128,17 @@ public class DcaeConfigurationPolling implements Runnable {
headers.put("Content-Type", MediaType.APPLICATION_JSON);
headers.put("Accept", MediaType.APPLICATION_JSON);
HttpResponse httpResponse;
+ CloseableHttpClient httpClient = null;
try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
httpResponse = HttpsUtils
- .put(url, headers, new HashMap<>(), new StringEntity(content));
+ .put(url, 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 {
+ closeHttpClient(httpClient);
}
if (httpResponse != null) {
suc = httpResponse.getStatusLine().getStatusCode() == 200;
@@ -141,11 +154,15 @@ public class DcaeConfigurationPolling implements Runnable {
ruleResult4APIs.forEach(correlationRule ->{
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", MediaType.APPLICATION_JSON);
+ CloseableHttpClient httpClient = null;
try {
- HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers);
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers, httpClient);
} catch (Exception e) {
log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId()
+ " exception messge is : " + e.getMessage(), e);
+ } finally {
+ closeHttpClient(httpClient);
}
});
}
@@ -159,4 +176,14 @@ 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!");
+ }
+ }
+ }
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java
index cfccd18..992785f 100644
--- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java
+++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/msb/EngineIpList.java
@@ -16,7 +16,10 @@
package org.onap.holmes.rulemgt.msb;
+import java.io.IOException;
+import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.api.entity.ServiceEntity;
import org.onap.holmes.common.api.entity.ServiceNode4Query;
@@ -30,6 +33,7 @@ import java.util.HashMap;
import java.util.List;
@Service
+@Slf4j
public class EngineIpList {
private String[] msbAddrInfo;
@@ -47,12 +51,22 @@ public class EngineIpList {
public List<String> getServiceCount()throws Exception{
String response;
+ CloseableHttpClient httpClient = null;
try {
+ httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
HttpResponse httpResponse = HttpsUtils
- .get(url, new HashMap<>());
+ .get(url, new HashMap<>(), httpClient);
response = HttpsUtils.extractResponseEntity(httpResponse);
} catch (Exception e) {
throw e;
+ } finally {
+ if (httpClient != null) {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ log.warn("Failed to close http client!");
+ }
+ }
}
ServiceEntity service = GsonUtil.jsonToBean(response, ServiceEntity.class);
List<ServiceNode4Query> nodesList = service.getNodes();
diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java
index b3cb93d..836b210 100644
--- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java
+++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java
@@ -18,19 +18,31 @@
package org.onap.holmes.rulemgt.bolt.enginebolt;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.any;
+import static org.hamcrest.Matchers.equalTo;
+
+import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
+import org.easymock.EasyMock;
+import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
+import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.onap.holmes.common.utils.HttpsUtils;
import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
import org.powermock.api.easymock.PowerMock;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
+import org.powermock.reflect.Whitebox;
-@PrepareForTest({HttpClients.class, CloseableHttpClient.class})
+@PrepareForTest({HttpClients.class, CloseableHttpClient.class, HttpsUtils.class})
+@PowerMockIgnore("javax.net.ssl.*")
public class EngineServiceTest {
@Rule
@@ -53,4 +65,21 @@ public class EngineServiceTest {
correlationDeployRule4Engine.setContent("{\"package\":\"test\"}");
correlationDeployRule4Engine.setEngineId("engine_id");
}
+
+ @Test
+ public void testEngineService_createHeaders_ok() throws Exception {
+ PowerMock.resetAll();
+ HashMap<String, String> headers = Whitebox.invokeMethod(engineService, "createHeaders");
+ assertThat(headers.get("Content-Type"), equalTo("application/json"));
+ assertThat(headers.get("Accept"), equalTo("application/json"));
+ }
+
+ @Test
+ public void testEngineService_closeHttpClient_ok() throws Exception {
+ PowerMock.resetAll();
+ CloseableHttpClient closeableHttpClient = HttpsUtils
+ .getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
+ Whitebox.invokeMethod(engineService, "closeHttpClient", closeableHttpClient);
+ }
+
} \ No newline at end of file