diff options
Diffstat (limited to 'rulemgt/src')
18 files changed, 193 insertions, 155 deletions
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 6409794..e7f30a7 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java @@ -69,6 +69,7 @@ public class RuleActiveApp extends IOCApplication<RuleAppConfig> { msinfo.setUrl("/api/holmes-rule-mgmt/v1"); msinfo.setProtocol("REST"); msinfo.setVisualRange("0|1"); + msinfo.setEnable_ssl(true); Set<Node> nodes = new HashSet<>(); Node node = new Node(); node.setIp(serviceAddrInfo[0]); diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java index c36fce7..a056653 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java @@ -22,6 +22,5 @@ import lombok.Setter; @Getter
@Setter
public class CorrelationCheckRule4Engine {
-
private String content;
}
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 b38ea47..4c578e4 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 @@ -16,6 +16,7 @@ package org.onap.holmes.rulemgt.bean.request;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
@@ -23,12 +24,9 @@ import lombok.Setter; @Setter
public class CorrelationDeployRule4Engine {
- @JsonProperty(value = "content")
private String content;
- @JsonProperty(value = "engineid")
private String engineId;
- @JsonProperty(value = "loopcontrolname")
private String loopControlName;
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java index c7e8a6f..8dc98d8 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java @@ -15,21 +15,20 @@ */
package org.onap.holmes.rulemgt.bean.request;
-import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class RuleCreateRequest {
- @JsonProperty(value = "rulename")
+
private String ruleName;
- @JsonProperty(value = "loopcontrolname")
+
private String loopControlName;
- @JsonProperty
+
private String description;
- @JsonProperty
+
private String content;
- @JsonProperty
+
private int enabled;
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java index 57aeafc..49b646a 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java @@ -15,7 +15,6 @@ */
package org.onap.holmes.rulemgt.bean.request;
-import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -27,6 +26,5 @@ import lombok.Setter; @AllArgsConstructor
@NoArgsConstructor
public class RuleDeleteRequest {
- @JsonProperty(value = "ruleid")
private String ruleId;
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java index f3b9b91..c6ba4b7 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java @@ -15,7 +15,7 @@ */
package org.onap.holmes.rulemgt.bean.request;
-import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
@@ -23,9 +23,9 @@ import lombok.Setter; @Setter
public class RuleQueryCondition {
- @JsonProperty(value = "ruleid")
+ @SerializedName(value = "ruleid")
private String rid;
- @JsonProperty(value = "rulename")
+ @SerializedName(value = "rulename")
private String name;
private int enabled;
private String creator;
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java index 99af585..1bcd494 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java @@ -15,21 +15,20 @@ */
package org.onap.holmes.rulemgt.bean.request;
-import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class RuleUpdateRequest {
- @JsonProperty
+
private String description;
- @JsonProperty
+
private String content;
- @JsonProperty
+
private int enabled;
- @JsonProperty(value="ruleid")
+
private String ruleId;
- @JsonProperty(value = "loopcontrolname")
+
private String loopControlName;
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java index 7fe432e..760ad4f 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java @@ -15,15 +15,11 @@ */
package org.onap.holmes.rulemgt.bean.response;
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
-@JsonInclude(JsonInclude.Include.ALWAYS)
@Getter
@Setter
public class RuleAddAndUpdateResponse{
- @JsonProperty(value="ruleid")
private String ruleId;
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java index aab6dc5..9305496 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java @@ -15,18 +15,16 @@ */
package org.onap.holmes.rulemgt.bean.response;
-import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
+
@Getter
@Setter
public class RuleQueryListResponse {
- @SerializedName(value = "rules")
private List<RuleResult4API> correlationRules = new ArrayList<RuleResult4API>();
- @SerializedName(value = "totalcount")
private int totalCount;
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java index e97b94d..39e1bb6 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java @@ -15,30 +15,23 @@ */
package org.onap.holmes.rulemgt.bean.response;
-import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
-@JsonInclude(JsonInclude.Include.ALWAYS)
@Setter
@Getter
public class RuleResult4API {
- @JsonProperty(value = "ruleid")
private String ruleId;
- @JsonProperty(value = "rulename")
private String ruleName;
private String description;
private String content;
- @JsonProperty(value = "createtime")
private Date createTime;
private String creator;
- @JsonProperty(value = "updatetime")
private Date updateTime;
private String modifier;
private int enabled;
- @JsonProperty(value = "loopcontrolname")
private String closedControlLoopName;
}
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 5dc97b4..319347e 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,17 +15,14 @@ */
package org.onap.holmes.rulemgt.bolt.enginebolt;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
+import java.util.HashMap;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
-import org.glassfish.jersey.client.ClientConfig;
+import org.apache.http.HttpResponse;
+import org.apache.http.entity.StringEntity;
import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.common.utils.GsonUtil;
+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;
@@ -35,32 +32,31 @@ import org.onap.holmes.common.config.MicroServiceConfig; @Service
public class EngineService {
- protected Response delete(String packageName) throws IOException {
- Client client = createClient();
- WebTarget webTarget = client
- .target(MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
- return webTarget.request(MediaType.APPLICATION_JSON).delete();
+ protected HttpResponse delete(String packageName) throws Exception {
+ HashMap headers = createHeaders();
+ String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName;
+ return HttpsUtils.delete(url, headers);
}
- private Client createClient() {
- ClientConfig clientConfig = new ClientConfig();
- return ClientBuilder.newClient(clientConfig);
+ protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)
+ throws Exception {
+ String content = GsonUtil.beanToJson(correlationCheckRule4Engine);
+ HashMap headers = createHeaders();
+ String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH;
+ return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content));
}
- protected Response check(CorrelationCheckRule4Engine correlationCheckRule4Engine)
- throws IOException {
- Client client = createClient();
- ObjectMapper mapper = new ObjectMapper();
- String content = mapper.writeValueAsString(correlationCheckRule4Engine);
- WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH);
- return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON));
+ protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {
+ String content = GsonUtil.beanToJson(correlationDeployRule4Engine);
+ HashMap headers = createHeaders();
+ String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH;
+ return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content));
}
- protected Response deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {
- Client client = createClient();
- ObjectMapper mapper = new ObjectMapper();
- String content = mapper.writeValueAsString(correlationDeployRule4Engine);
- WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH);
- return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON));
+ 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;
}
}
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 733cae0..183e52f 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 @@ -16,10 +16,11 @@ package org.onap.holmes.rulemgt.bolt.enginebolt;
import javax.inject.Inject;
-import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
+import org.apache.http.HttpResponse;
import org.jvnet.hk2.annotations.Service;
+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;
@@ -33,16 +34,16 @@ public class EngineWrapper { private EngineService engineService;
public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {
- Response response;
+ HttpResponse response;
try {
response = engineService.deploy(correlationRule);
} catch (Exception e) {
throw new CorrelationException("Failed to call the rule deployment RESTful API.", e);
}
- if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
log.info("Succeeded in calling the rule deployment RESTful API from the engine management service.");
try {
- JSONObject json = JSONObject.fromObject(response.readEntity(String.class));
+ JSONObject json = JSONObject.fromObject(HttpsUtils.extractResponseEntity(response));
return json.get(RuleMgtConstant.PACKAGE).toString();
} catch (Exception e) {
throw new CorrelationException("Failed to parse the value returned by the engine management service.", e);
@@ -53,13 +54,13 @@ public class EngineWrapper { }
public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {
- Response response;
+ HttpResponse response;
try {
response = engineService.delete(packageName);
} catch (Exception e) {
throw new CorrelationException("Failed to call the rule deleting RESTful API.", e);
}
- if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
log.info("Succeeded in calling the rule deleting RESTful API from the engine management service.");
return true;
} else {
@@ -70,17 +71,17 @@ public class EngineWrapper { public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)
throws CorrelationException {
log.info("Rule Contents: " + correlationCheckRule4Engine.getContent());
- Response response;
+ HttpResponse response;
try {
response = engineService.check(correlationCheckRule4Engine);
} catch (Exception e) {
throw new CorrelationException("Failed to call the rule verification RESTful API.", e);
}
- if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ 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.getStatus() + " " + response.getStatusInfo() + " " + response.getEntity());
+ log.info(response.getStatusLine().getStatusCode() + " " + response.getEntity());
throw new CorrelationException("Failed to verify the rule. The contents of the rule are invalid.");
}
}
diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java index a5ec87c..5ac4fe6 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java @@ -23,7 +23,7 @@ public class RuleMgtConstant { public static final int STATUS_RULE_OPEN = 1;
public static final int STATUS_RULE_CLOSE = 0;
public static final int STATUS_RULE_ALL = 2;
- public static final String PACKAGE = "package";
+ public static final String PACKAGE = "packageName";
public static final String ENGINE_PATH = "/api/holmes-engine-mgmt/v1/rule";
public static final int RESPONSE_STATUS_OK = 200;
}
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 d0513ca..0e4f790 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 @@ -13,20 +13,29 @@ */ package org.onap.holmes.rulemgt.dcae; +import com.alibaba.fastjson.JSON; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Type; +import java.util.HashMap; import java.util.List; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import lombok.extern.slf4j.Slf4j; -import org.glassfish.jersey.client.ClientConfig; +import org.apache.http.HttpResponse; +import org.apache.http.entity.StringEntity; 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.Md5Util; import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; @@ -39,7 +48,7 @@ public class DcaeConfigurationPolling implements Runnable { private String hostname; - private String url = "http://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule"; + private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule"; public DcaeConfigurationPolling(String hostname) { this.hostname = hostname; @@ -55,7 +64,7 @@ public class DcaeConfigurationPolling implements Runnable { try { dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname); String md5 = Md5Util.md5(dcaeConfigurations); - if (prevResult && prevConfigMd5.equals(md5)) { + if (prevResult && prevConfigMd5.equals(md5)){ log.info("Operation aborted due to identical Configurations."); return; } @@ -63,9 +72,20 @@ public class DcaeConfigurationPolling implements Runnable { prevResult = false; } catch (CorrelationException e) { log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e); + } catch (Exception e) { + log.info("Failed to generate the MD5 information for new configurations.", e); } + RuleQueryListResponse ruleQueryListResponse = null; if (dcaeConfigurations != null) { - RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules(); + 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); + } + } + if (ruleQueryListResponse != null) { List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules(); deleteAllCorrelationRules(ruleResult4APIs); try { @@ -77,24 +97,39 @@ public class DcaeConfigurationPolling implements Runnable { } } - private RuleQueryListResponse getAllCorrelationRules() { - Client client = ClientBuilder.newClient(new ClientConfig()); - WebTarget webTarget = client.target(url); - return webTarget.request("application/json").get() - .readEntity(RuleQueryListResponse.class); + 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); } - private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) - throws CorrelationException { + private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException { boolean suc = false; for (Rule rule : dcaeConfigurations.getDefaultRules()) { RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule); - Client client = ClientBuilder.newClient(new ClientConfig()); - String content = GsonUtil.beanToJson(ruleCreateRequest); - WebTarget webTarget = client.target(url); - Response response = webTarget.request(MediaType.APPLICATION_JSON) - .put(Entity.entity(content, MediaType.APPLICATION_JSON)); - suc = response.getStatus() == 200; + 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<String, String> headers = new HashMap<>(); + headers.put("Content-Type", MediaType.APPLICATION_JSON); + headers.put("Accept", MediaType.APPLICATION_JSON); + HttpResponse httpResponse; + try { + httpResponse = HttpsUtils + .put(url, headers, new HashMap<>(), new StringEntity(content)); + } catch (UnsupportedEncodingException e) { + throw new CorrelationException("Failed to create https entity.", e); + } catch (Exception e) { + throw new CorrelationException(e.getMessage()); + } + if (httpResponse != null) { + suc = httpResponse.getStatusLine().getStatusCode() == 200; + } if (!suc) { break; } @@ -102,11 +137,16 @@ public class DcaeConfigurationPolling implements Runnable { return suc; } - private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs) { - ruleResult4APIs.forEach(correlationRule -> { - Client client = ClientBuilder.newClient(new ClientConfig()); - WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId()); - webTarget.request(MediaType.APPLICATION_JSON).delete(); + private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){ + ruleResult4APIs.forEach(correlationRule ->{ + HashMap<String, String> headers = new HashMap<>(); + headers.put("Content-Type", MediaType.APPLICATION_JSON); + try { + HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers); + } catch (Exception e) { + log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId() + + " exception messge is : " + e.getMessage(), e); + } }); } diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java index 041377f..d3c01ae 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,6 +20,9 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.SwaggerDefinition;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
@@ -47,6 +50,7 @@ import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition; import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;
import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;
@@ -67,10 +71,9 @@ public class RuleMgtResources { response = RuleAddAndUpdateResponse.class)
@Timed
public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,
- @ApiParam(value =
- "The request entity of the HTTP call, which comprises \"rulename\"(required), "
- + "\"loopcontrolname\"(required), \"content\"(required), \"enabled\"(required) "
- + "and \"description\"(optional)", required = true)
+ @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleName\"(required), "
+ + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) "
+ + "and \"description\"(optional)", required = true)
RuleCreateRequest ruleCreateRequest) {
Locale locale = LanguageUtil.getLocale(request);
RuleAddAndUpdateResponse ruleChangeResponse;
@@ -90,15 +93,13 @@ public class RuleMgtResources { @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class)
@Timed
public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,
- @ApiParam(value =
- "The request entity of the HTTP call, which comprises \"ruleid\"(required), "
- + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)
+ @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleId\"(required), "
+ + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)
RuleUpdateRequest ruleUpdateRequest) {
Locale locale = LanguageUtil.getLocale(request);
RuleAddAndUpdateResponse ruleChangeResponse;
try {
- ruleChangeResponse = ruleMgtWrapper
- .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);
+ ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);
log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");
return ruleChangeResponse;
} catch (CorrelationException e) {
@@ -130,19 +131,18 @@ public class RuleMgtResources { @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class)
@Timed
public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,
- @ApiParam(value =
- "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "
- + "\"rulename\"(optional), \"creator\"(optional), "
- + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}",
+ @ApiParam(value = "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "
+ + "\"rulename\"(optional), \"creator\"(optional), "
+ + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}",
required = false) @QueryParam("queryrequest") String ruleQueryRequest) {
Locale locale = LanguageUtil.getLocale(request);
RuleQueryListResponse ruleQueryListResponse;
+
RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);
try {
ruleQueryListResponse = ruleMgtWrapper
.getCorrelationRuleByCondition(ruleQueryCondition);
- log.info("query rule successful by condition:" + JSONObject
- .fromObject(ruleQueryCondition));
+ log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));
return ruleQueryListResponse;
} catch (CorrelationException e) {
log.error("query rule failed,cause query condition conversion failure", e);
@@ -153,7 +153,6 @@ public class RuleMgtResources { private RuleQueryCondition getRuleQueryCondition(String queryRequest,
HttpServletRequest request) {
Locale locale = LanguageUtil.getLocale(request);
-
RuleQueryCondition ruleQueryCondition = GsonUtil.jsonToBean(queryRequest, RuleQueryCondition.class);
if (queryRequest == null) {
if(ruleQueryCondition==null){
diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java index b63b106..cc8c047 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -20,32 +20,40 @@ package org.onap.holmes.rulemgt.bolt.enginebolt; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
-import javax.ws.rs.core.Response;
+import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+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.common.exception.CorrelationException;
import org.powermock.api.easymock.PowerMock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
+@PrepareForTest({EngineWrapper.class, EngineService.class, HttpsUtils.class, HttpResponse.class,
+ StatusLine.class})
+@RunWith(PowerMockRunner.class)
public class EngineWrapperTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private EngineWrapper engineWrapper = new EngineWrapper();
private EngineService engineServiceMock;
- private Response response;
+ private HttpResponse httpResponse;
private StatusLine statusLineMock;
@Before
public void setUp() throws Exception {
engineServiceMock = PowerMock.createMock(EngineService.class);
- response = PowerMock.createMock(Response.class);
+ httpResponse = PowerMock.createMock(HttpResponse.class);
statusLineMock = PowerMock.createMock(StatusLine.class);
Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);
}
@@ -55,8 +63,10 @@ public class EngineWrapperTest { thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to call the rule deployment RESTful API.");
- EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(
- new RuntimeException(""));
+ EasyMock.expect(
+ engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andThrow(
+ new RuntimeException(""));
PowerMock.replayAll();
engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -69,9 +79,11 @@ public class EngineWrapperTest { thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to deploy the rule!");
- EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(400);
+ EasyMock.expect(
+ engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn(httpResponse);
+ EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
PowerMock.replayAll();
engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -81,14 +93,18 @@ public class EngineWrapperTest { @Test
public void deployEngine_parse_content_exception() throws Exception {
+ PowerMock.resetAll();
String content = "";
-
+ PowerMockito.mockStatic(HttpsUtils.class);
thrown.expect(CorrelationException.class);
- thrown.expectMessage("Failed to parse the value returned by the engine management service.");
- EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(200);
- EasyMock.expect(response.readEntity(String.class)).andReturn(content);
+ thrown.expectMessage(
+ "Failed to parse the value returned by the engine management service.");
+ EasyMock.expect(
+ engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn(httpResponse);
+ EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+ PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);
PowerMock.replayAll();
engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -98,11 +114,15 @@ public class EngineWrapperTest { @Test
public void deployEngine_success() throws Exception {
- String content = "{\"package\":\"test\"}";
- EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
- .andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(200);
- EasyMock.expect(response.readEntity(String.class)).andReturn(content);
+ PowerMock.resetAll();
+ String content = "{\"packageName\":\"test\"}";
+ PowerMockito.mockStatic(HttpsUtils.class);
+ EasyMock.expect(
+ engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))
+ .andReturn(httpResponse);
+ EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
+ PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);
PowerMock.replayAll();
String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());
@@ -131,8 +151,9 @@ public class EngineWrapperTest { thrown.expectMessage("Failed to delete the rule!");
EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
- .andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(400);
+ .andReturn(httpResponse);
+ EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);
PowerMock.replayAll();
@@ -144,8 +165,9 @@ public class EngineWrapperTest { @Test
public void deleteRuleFromEngine_success() throws Exception {
EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))
- .andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(200);
+ .andReturn(httpResponse);
+ EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
PowerMock.replayAll();
@@ -159,8 +181,10 @@ public class EngineWrapperTest { thrown.expect(CorrelationException.class);
thrown.expectMessage("Failed to call the rule verification RESTful API.");
- EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(
- new RuntimeException(""));
+ EasyMock.expect(
+ engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
+ .andThrow(
+ new RuntimeException(""));
PowerMock.replayAll();
engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());
@@ -170,9 +194,11 @@ public class EngineWrapperTest { @Test
public void checkRuleFromEngine_success() throws Exception {
- EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
- .andReturn(response);
- EasyMock.expect(response.getStatus()).andReturn(200);
+ EasyMock.expect(
+ engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))
+ .andReturn(httpResponse);
+ EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);
+ EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);
PowerMock.replayAll();
diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java index 8d16198..1037495 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java @@ -15,15 +15,9 @@ */ package org.onap.holmes.rulemgt.dcae; -import static org.easymock.EasyMock.anyObject; -import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; import static org.powermock.api.mockito.PowerMockito.when; - -import com.alibaba.fastjson.JSONException; -import com.fasterxml.jackson.databind.ObjectMapper; -import javax.ws.rs.ProcessingException; import org.junit.Before; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -40,7 +34,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -@PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class, ObjectMapper.class}) +@PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class}) @RunWith(PowerMockRunner.class) public class DcaeConfigurationPollingTest { @@ -64,7 +58,7 @@ public class DcaeConfigurationPollingTest { .thenReturn("host"); PowerMock.createMock(DcaeConfigurationParser.class); PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host") - .andThrow(new CorrelationException("")).anyTimes(); + .andThrow(new CorrelationException("tests")).anyTimes(); PowerMock.replayAll(); Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations"); @@ -94,7 +88,7 @@ public class DcaeConfigurationPollingTest { public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception() throws Exception { PowerMock.resetAll(); - thrown.expect(ProcessingException.class); + thrown.expect(CorrelationException.class); DcaeConfigurations dcaeConfigurations = new DcaeConfigurations(); Rule rule = new Rule("test", "test", "tset",1); dcaeConfigurations.getDefaultRules().add(rule); diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java index 0c3bd89..a35f6b8 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java @@ -57,6 +57,7 @@ public class RuleMgtResourcesTest { @Test
public void addCorrelationRule_correlation_exception() throws Exception {
+ PowerMock.resetAll();
thrown.expect(WebApplicationException.class);
final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
|