X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fbolt%2Fenginebolt%2FEngineService.java;h=4b6e2485afeff88fb8f768d7c497e0295bfaa989;hb=af62330fbd26c576564a4170b2ff271ca606ee93;hp=8782d7abf997f7d40759eec2deaa798a1dc687a1;hpb=348ce6e112876f552a939e58d74376704537344e;p=holmes%2Frule-management.git 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..4b6e248 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"; - - 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); - } - } + private static final String PORT = "9102"; + private static final String SEP = "//"; + private static final String COLON = ":"; - 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 delete(String packageName, String ip) { + return JerseyClient.newInstance() + .path(packageName) + .delete(getUrl(ip)) != 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 boolean check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) { + return JerseyClient.newInstance() + .header("Accept", MediaType.APPLICATION_JSON) + .post(getUrl(ip), Entity.json(correlationCheckRule4Engine)) != null; } - private void closeHttpClient(CloseableHttpClient httpClient) { - if (httpClient != null) { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } + protected String deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) { + return JerseyClient.newInstance() + .header("Accept", MediaType.APPLICATION_JSON) + .put(getUrl(ip), Entity.json(correlationDeployRule4Engine)); } - 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(COLON) + .append(SEP) + .append(ip) + .append(COLON) + .append(PORT) + .append(ENGINE_PATH) + .toString(); } }