summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java
diff options
context:
space:
mode:
Diffstat (limited to 'rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java')
-rw-r--r--rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java102
1 files changed, 31 insertions, 71 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 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.
* <p>
* 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<String, String> createHeaders() {
- HashMap<String, String> 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();
}
}