summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/main
diff options
context:
space:
mode:
authoryoubowu <wu.youbo@zte.com.cn>2017-02-27 09:21:05 +0800
committer6092002067 <wu.youbo@zte.com.cn>2017-02-27 10:15:46 +0800
commite87f8243439fb68d1308a5132ae624942d77776d (patch)
tree2ca350893d9f1bb2a69827fa1aa1deac15038801 /rulemgt/src/main
parent27e71c9c0f9ec16e377a5f21ec4f72bbd5c1efc0 (diff)
Add rule management module unit tests
Issue-ID:HOLMES-50 Change-Id: Ie21a9046cb3d237aa0c52c9c8ccb77123da2de1d Signed-off-by: youbowu <wu.youbo@zte.com.cn>
Diffstat (limited to 'rulemgt/src/main')
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java20
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java27
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java129
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java52
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java11
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java34
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java3
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java16
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java236
9 files changed, 355 insertions, 173 deletions
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java
index 3433678..57ae0c5 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java
@@ -15,14 +15,15 @@
*/
package org.openo.holmes.rulemgt;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
-
import org.hibernate.validator.constraints.NotEmpty;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import io.dropwizard.Configuration;
+import org.jvnet.hk2.annotations.Service;
+@Service
public class RuleAppConfig extends Configuration {
@NotEmpty
@@ -31,6 +32,8 @@ public class RuleAppConfig extends Configuration {
@NotEmpty
private String apidescription = "Holmes rule management rest API";
+ @NotEmpty
+ private String msbServerAddr;
@Valid
@NotNull
@@ -51,8 +54,17 @@ public class RuleAppConfig extends Configuration {
return apidescription;
}
- public void setApidescription( String apidescription ) {
+ public void setApidescription(String apidescription) {
this.apidescription = apidescription;
}
+ @JsonProperty
+ public String getMsbServerAddr() {
+ return msbServerAddr;
+ }
+
+ @JsonProperty
+ public void setMsbServerAddr(String msbServerAddr) {
+ this.msbServerAddr = msbServerAddr;
+ }
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java
new file mode 100644
index 0000000..463eb2a
--- /dev/null
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bean/request/CorrelationRestRequest.java
@@ -0,0 +1,27 @@
+/**
+ * Copyright 2017 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
+ *
+ * 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,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.openo.holmes.rulemgt.bean.request;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class CorrelationRestRequest {
+
+ private String rootURL;
+} \ No newline at end of file
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java
new file mode 100644
index 0000000..6267cff
--- /dev/null
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java
@@ -0,0 +1,129 @@
+/**
+ * Copyright 2017 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
+ *
+ * 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,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openo.holmes.rulemgt.bolt.enginebolt;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+import javax.inject.Inject;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+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.client.methods.HttpRequestBase;
+import org.apache.http.entity.BufferedHttpEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.client.HttpClients;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.exception.CorrelationException;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.rulemgt.RuleAppConfig;
+import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
+import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
+
+@Service
+public class EngineService {
+
+ @Inject
+ private RuleAppConfig ruleAppConfig;
+
+ protected HttpResponse delete(String packageName) throws Exception {
+ return deleteRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
+ }
+
+ protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine) throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ String content = mapper.writeValueAsString(correlationCheckRule4Engine);
+ return postRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);
+ }
+
+ protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ String content = mapper.writeValueAsString(correlationDeployRule4Engine);
+ return putRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);
+ }
+
+ private HttpResponse postRequest(String url, String content) throws Exception {
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(url);
+ setHeader(httpPost);
+ if (StringUtils.isNotEmpty(content)) {
+ httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
+ }
+ return httpClient.execute(httpPost);
+ }
+
+ private HttpResponse putRequest(String url, String content)
+ throws Exception {
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPut httpPut = new HttpPut(url);
+ setHeader(httpPut);
+ if (StringUtils.isNotEmpty(content)) {
+ httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
+ }
+ return httpClient.execute(httpPut);
+ }
+
+ private HttpResponse deleteRequest(String url) throws Exception {
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpDelete httpDelete = new HttpDelete(url);
+ setHeader(httpDelete);
+ return httpClient.execute(httpDelete);
+ }
+
+ private void setHeader(HttpRequestBase httpRequestBase) {
+ httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");
+ httpRequestBase.setHeader("Accept", "application/json");
+ httpRequestBase.setHeader("Content-Type", "application/json");
+ }
+
+ public byte[] getData(HttpEntity httpEntity) throws Exception {
+ BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ bufferedHttpEntity.writeTo(byteArrayOutputStream);
+ byte[] responseBytes = byteArrayOutputStream.toByteArray();
+ return responseBytes;
+ }
+
+ public String getResponseContent(HttpResponse httpResponse) throws CorrelationException {
+ byte[] dataByte;
+ String result = null;
+ try {
+ HttpEntity httpEntity = httpResponse.getEntity();
+ if (httpEntity != null) {
+ byte[] responseBytes = getData(httpEntity);
+ dataByte = responseBytes;
+ result = bytesToString(dataByte);
+ }
+ return result;
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR, e);
+ }
+ }
+
+ private String bytesToString(byte[] bytes) throws UnsupportedEncodingException {
+ if (bytes != null) {
+ String returnStr = new String(bytes, "utf-8");
+ returnStr = StringUtils.trim(returnStr);
+ return returnStr;
+ }
+ return null;
+ }
+}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java
index 329b9c9..b69e66b 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java
@@ -15,27 +15,73 @@
*/
package org.openo.holmes.rulemgt.bolt.enginebolt;
+import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
+import net.sf.json.JSONObject;
+import org.apache.http.HttpResponse;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.exception.CorrelationException;
+import org.openo.holmes.common.utils.I18nProxy;
import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
@Service
@Slf4j
public class EngineWrapper {
+ @Inject
+ private EngineService engineService;
public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {
- return "";
+ HttpResponse httpResponse;
+ try {
+ httpResponse = engineService.deploy(correlationRule);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DEPLOY_RULE_REST_FAILED,e);
+ }
+ if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ log.info("Call deploy rule rest interface in engine successfully.");
+ String content = engineService.getResponseContent(httpResponse);
+ try {
+ JSONObject json = JSONObject.fromObject(content);
+ return json.get(RuleMgtConstant.PACKAGE).toString();
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARSE_DEPLOY_RESULT_ERROR,e);
+ }
+ } else {
+ throw new CorrelationException(I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
+ }
}
public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {
- return true;
+ HttpResponse httpResponse;
+ try {
+ httpResponse = engineService.delete(packageName);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CALL_DELETE_RULE_REST_FAILED,e);
+ }
+ if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ log.info("Call delete rule rest interface in engine successfully.");
+ return true;
+ } else {
+ throw new CorrelationException(I18nProxy.ENGINE_DELETE_RULE_FAILED);
+ }
}
public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)
throws CorrelationException {
- return true;
+ HttpResponse httpResponse;
+ try {
+ httpResponse = engineService.check(correlationCheckRule4Engine);
+ } catch (Exception e) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT__CALL_CHECK_RULE_REST_FAILED,e);
+ }
+ if (httpResponse.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {
+ log.info("Call check rule rest interface in engine successfully.");
+ return true;
+ } else {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS);
+ }
}
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java
index e086829..348056d 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/constant/RuleMgtConstant.java
@@ -17,12 +17,13 @@ package org.openo.holmes.rulemgt.constant;
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_NAME = "packagename";
-
private 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 ENGINE_PATH = "/api/correlation-engine/v1/rule";
+ public static final int RESPONSE_STATUS_OK = 200;
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java
index 5096db3..224f861 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/CorrelationRuleQueryDao.java
@@ -71,7 +71,7 @@ public class CorrelationRuleQueryDao {
correlationRule.setDescription((String) value.get("description"));
correlationRule.setEnabled((Integer) value.get("enable"));
correlationRule.setTemplateID((Integer) value.get("templateID"));
- correlationRule.setEngineId((String) value.get("engineID"));
+ correlationRule.setEngineID((String) value.get("engineID"));
correlationRule.setEngineType((String) value.get("engineType"));
correlationRule.setCreator((String) value.get("creator"));
correlationRule.setCreateTime((Date) value.get("createTime"));
@@ -80,7 +80,6 @@ public class CorrelationRuleQueryDao {
correlationRule.setParams((Properties) value.get("params"));
correlationRule.setDomain((String) value.get("domain"));
correlationRule.setContent((String) value.get("content"));
- correlationRule.setIsManual((Integer) value.get("isManual"));
correlationRule.setVendor((String) value.get("vendor"));
correlationRule.setPackageName((String) value.get("package"));
return correlationRule;
@@ -95,21 +94,25 @@ public class CorrelationRuleQueryDao {
for (Field field : fields) {
// Jacoco will cause an exception when calculating the coverage of the UT
// Remove this if jacoco solves this problem in the future
- if (field.getName().contains("jacoco")){
+ if (field.getName().contains("jacoco")) {
continue;
}
-
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
clazz);
Method getMethod = pd.getReadMethod();
Object o = getMethod.invoke(ruleQueryCondition);
if (o != null) {
String tempName = field.getName();
- if ("enabled".equals(tempName) && (int) o != RuleMgtConstant.STATUS_RULE_ALL) {
- whereSql = whereSql + "enable =" + (int) o;
- whereSql += " AND ";
- } else if ("name".equals(tempName) && !"".equals(o.toString().trim())) {
- whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
+ if ("enabled".equals(tempName)) {
+ int enabled = (int) o;
+ if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {
+ whereSql = whereSql + "enable =" + enabled;
+ whereSql += " AND ";
+ }
+ } else if ("name".equals(tempName)) {
+ if (!"".equals(o.toString().trim())) {
+ whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
+ }
} else if (!"".equals(o.toString().trim())) {
whereSql = whereSql + field.getName() + "='" + o + "' AND ";
}
@@ -124,17 +127,4 @@ public class CorrelationRuleQueryDao {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_QUERY_SQL_FAILED, e);
}
}
-
- private String getMethodName(Field field){
- String ret = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);;
- Class clazz = field.getDeclaringClass();
-
- if (clazz.equals(Boolean.class)){
- ret = "is" + ret;
- }else{
- ret = "get" + ret;
- }
-
- return ret;
- }
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java
index d318de9..a8b114d 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/db/mapper/CorrelationRuleMapper.java
@@ -32,7 +32,7 @@ public class CorrelationRuleMapper implements ResultSetMapper<CorrelationRule> {
correlationRule.setDescription(resultSet.getString("description"));
correlationRule.setEnabled(resultSet.getInt("enable"));
correlationRule.setTemplateID(resultSet.getInt("templateID"));
- correlationRule.setEngineId(resultSet.getString("engineID"));
+ correlationRule.setEngineID(resultSet.getString("engineID"));
correlationRule.setEngineType(resultSet.getString("engineType"));
correlationRule.setCreator(resultSet.getString("creator"));
correlationRule.setCreateTime(resultSet.getDate("createTime"));
@@ -41,7 +41,6 @@ public class CorrelationRuleMapper implements ResultSetMapper<CorrelationRule> {
correlationRule.setParams((Properties)resultSet.getObject("params"));
correlationRule.setDomain(resultSet.getString("domain"));
correlationRule.setContent(resultSet.getString("content"));
- correlationRule.setIsManual(resultSet.getInt("isManual"));
correlationRule.setVendor(resultSet.getString("vendor"));
correlationRule.setPackageName(resultSet.getString("package"));
return correlationRule;
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java
index c2d95f9..682faf0 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java
@@ -34,6 +34,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j;
+import net.sf.json.JSONObject;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.exception.CorrelationException;
import org.openo.holmes.common.utils.ExceptionUtil;
@@ -47,10 +48,11 @@ import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;
import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest;
import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.openo.holmes.rulemgt.constant.RuleMgtConstant;
import org.openo.holmes.rulemgt.wrapper.RuleMgtWrapper;
-@SwaggerDefinition
@Service
+@SwaggerDefinition
@Path("/rule")
@Api(tags = {"CorrelationRules"})
@Produces(MediaType.APPLICATION_JSON)
@@ -89,11 +91,11 @@ public class RuleMgtResources {
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) {
- log.error("create rule:" + ruleUpdateRequest.getContent() + " failed", e);
+ log.error("update rule:" + ruleUpdateRequest.getContent() + " failed", e);
throw ExceptionUtil.buildExceptionResponse(I18nProxy.getInstance().getValue(locale,
e.getMessage()));
}
@@ -132,6 +134,7 @@ public class RuleMgtResources {
try {
ruleQueryListResponse = ruleMgtWrapper
.getCorrelationRuleByCondition(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);
@@ -147,9 +150,9 @@ public class RuleMgtResources {
RuleQueryCondition ruleQueryCondition = JacksonUtil
.jsonToBean(queryRequest, RuleQueryCondition.class);
if (queryRequest == null) {
- ruleQueryCondition.setEnabled(2);
+ ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);
} else if (queryRequest.indexOf("enabled") == -1) {
- ruleQueryCondition.setEnabled(2);
+ ruleQueryCondition.setEnabled(RuleMgtConstant.STATUS_RULE_ALL);
}
return ruleQueryCondition;
} catch (IOException e) {
@@ -158,5 +161,4 @@ public class RuleMgtResources {
I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR));
}
}
-
}
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
index b1ed44e..afd80b1 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/wrapper/RuleMgtWrapper.java
@@ -18,6 +18,7 @@ package org.openo.holmes.rulemgt.wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
+import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
@@ -53,168 +54,143 @@ public class RuleMgtWrapper {
@Inject
private DbDaoUtil daoUtil;
- public RuleAddAndUpdateResponse addCorrelationRule(String creator,
- RuleCreateRequest ruleCreateRequest)
+ private CorrelationRuleDao correlationRuleDao;
+
+ @PostConstruct
+ public void initDaoUtil() {
+ correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);
+ }
+
+ public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)
throws CorrelationException {
- CorrelationRule correlationRule = convertRuleCreateRequest2CorrelationRule(creator,
- ruleCreateRequest);
- if (correlationRule.getName() == null || "".equals(correlationRule.getName().trim())) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_IS_EMPTY);
- }
- CorrelationRule ruleTemp;
- try {
- ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .queryRuleByRuleName(correlationRule.getName());
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR, e);
+ if (ruleCreateRequest == null) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
+ CorrelationRule correlationRule = convertCreateRequest2Rule(creator,
+ ruleCreateRequest);
+ checkCorrelation(correlationRule);
+ CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());
if (ruleTemp != null) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
}
correlationRule.setPackageName(deployRule2Engine(correlationRule));
- try {
- correlationRule = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .saveRule(correlationRule);
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CREATE_RULE_FAILED, e);
- }
+ CorrelationRule result = correlationRuleDao.saveRule(correlationRule);
RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();
- ruleAddAndUpdateResponse.setRuleId(correlationRule.getRid());
+ ruleAddAndUpdateResponse.setRuleId(result.getRid());
return ruleAddAndUpdateResponse;
}
- public RuleAddAndUpdateResponse updateCorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest)
+ public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)
throws CorrelationException {
- if (ruleUpdateRequest != null) {
- CorrelationRule oldCorrelationRule;
- try {
- oldCorrelationRule = daoUtil
- .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .getRuleByRid(ruleUpdateRequest.getRuleId());
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR, e);
- }
- if (oldCorrelationRule == null) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
- }
- CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
- ruleUpdateRequest);
- checkCorrelation(newCorrelationRule, oldCorrelationRule);
- RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
- try {
- if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());
- }
- daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .updateRule(newCorrelationRule);
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_UPDATE_RULE_FAILED, e);
- }
- ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
- deployRule2Engine(newCorrelationRule);
- return ruleChangeResponse;
- } else {
+ if (ruleUpdateRequest == null) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
-
+ CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());
+ if (oldCorrelationRule == null) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ }
+ CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,
+ ruleUpdateRequest, oldCorrelationRule.getName());
+ checkCorrelation(newCorrelationRule);
+ RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();
+ ruleChangeResponse.setRuleId(newCorrelationRule.getRid());
+ if (!haveChange(newCorrelationRule, oldCorrelationRule)) {
+ return ruleChangeResponse;
+ }
+ if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());
+ }
+ correlationRuleDao.updateRule(newCorrelationRule);
+ deployRule2Engine(newCorrelationRule);
+ return ruleChangeResponse;
}
- public void checkCorrelation(CorrelationRule newCorrelationRule,
- CorrelationRule oldCorrelationRule) throws CorrelationException {
- int newEnabled = newCorrelationRule.getEnabled();
- if (newCorrelationRule.getContent() == null) {
- newCorrelationRule.setContent(oldCorrelationRule.getContent());
+ private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException {
+ int enabled = correlationRule.getEnabled();
+ String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();
+ String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();
+ if ("".equals(content)) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CONTENT_CANNOT_BE_EMPTY);
}
- if (newEnabled != RuleMgtConstant.STATUS_RULE_CLOSE
- && newEnabled != RuleMgtConstant.STATUS_RULE_OPEN) {
+ if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE
+ && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);
}
+ if ("".equals(ruleName)) {
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY);
+ }
+ }
+
+ private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {
+ String newContent = newCorrelationRule.getContent();
+ String oldContent = oldCorrelationRule.getContent();
+ int newEnabled = newCorrelationRule.getEnabled();
+ int oldEnabled = oldCorrelationRule.getEnabled();
+ String newDes = newCorrelationRule.getDescription();
+ String oldDes = oldCorrelationRule.getDescription();
+ if (newContent.equals(oldContent) && newEnabled == oldEnabled && newDes.equals(oldDes)) {
+ return false;
+ }
+ return true;
}
public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)
throws CorrelationException {
- if (ruleDeleteRequest != null) {
- CorrelationRule correlationRule;
- try {
- correlationRule = daoUtil
- .getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .getRuleByRid(ruleDeleteRequest.getRuleId());
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DB_ERROR, e);
- }
- if (correlationRule == null) {
- log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId()
- + " does not exist the database.");
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
- }
- try {
- if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());
- }
- daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
- .deleteRule(correlationRule);
- } catch (Exception e) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_DELETE_RULE_FAILED, e);
- }
- } else {
+ if (ruleDeleteRequest == null) {
throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
}
-
+ CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());
+ if (correlationRule == null) {
+ log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");
+ throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);
+ }
+ if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
+ engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());
+ }
+ correlationRuleDao.deleteRule(correlationRule);
}
- public CorrelationRule convertRuleCreateRequest2CorrelationRule(String userName,
+ private CorrelationRule convertCreateRequest2Rule(String userName,
RuleCreateRequest ruleCreateRequest) throws CorrelationException {
- if (ruleCreateRequest != null) {
- if (ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_OPEN
- && ruleCreateRequest.getEnabled() != RuleMgtConstant.STATUS_RULE_CLOSE) {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
- }
- CorrelationRule correlationRule = new CorrelationRule();
- String ruleId = "rule_" + System.currentTimeMillis();
- correlationRule.setRid(ruleId);
- correlationRule.setContent(ruleCreateRequest.getContent());
- correlationRule.setDescription(ruleCreateRequest.getDescription());
- correlationRule.setCreateTime(new Date());
- correlationRule.setUpdateTime(new Date());
- correlationRule.setName(ruleCreateRequest.getRuleName());
- correlationRule.setEngineId("correlation-d");
- correlationRule.setEngineType("");
- correlationRule.setIsManual(0);
- correlationRule.setTemplateID(0);
- correlationRule.setVendor("");
- correlationRule.setCreator(userName);
- correlationRule.setModifier(userName);
- correlationRule.setEnabled(ruleCreateRequest.getEnabled());
- return correlationRule;
- } else {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
+ String tempContent = ruleCreateRequest.getContent();
+ CorrelationRule correlationRule = new CorrelationRule();
+ String ruleId = "rule_" + System.currentTimeMillis();
+ correlationRule.setRid(ruleId);
+ if (tempContent != null) {
+ correlationRule.setContent(tempContent.trim());
}
-
+ correlationRule.setDescription(ruleCreateRequest.getDescription());
+ correlationRule.setCreateTime(new Date());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setName(ruleCreateRequest.getRuleName());
+ correlationRule.setEngineID("correlation-d");
+ correlationRule.setEngineType("");
+ correlationRule.setTemplateID(0);
+ correlationRule.setVendor("");
+ correlationRule.setCreator(userName);
+ correlationRule.setModifier(userName);
+ correlationRule.setEnabled(ruleCreateRequest.getEnabled());
+ return correlationRule;
}
private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,
- RuleUpdateRequest ruleUpdateRequest) throws CorrelationException {
- if (ruleUpdateRequest != null) {
- CorrelationRule correlationRule = new CorrelationRule();
- correlationRule.setRid(ruleUpdateRequest.getRuleId());
- correlationRule.setContent(ruleUpdateRequest.getContent());
- correlationRule.setDescription(ruleUpdateRequest.getDescription());
- correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
- correlationRule.setUpdateTime(new Date());
- correlationRule.setModifier(modifier);
- return correlationRule;
- } else {
- throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);
- }
-
+ RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setRid(ruleUpdateRequest.getRuleId());
+ correlationRule.setContent(ruleUpdateRequest.getContent());
+ correlationRule.setDescription(ruleUpdateRequest.getDescription());
+ correlationRule.setEnabled(ruleUpdateRequest.getEnabled());
+ correlationRule.setUpdateTime(new Date());
+ correlationRule.setModifier(modifier);
+ correlationRule.setName(ruleName);
+ return correlationRule;
}
- private String deployRule2Engine(CorrelationRule correlationRule) throws CorrelationException {
- if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule))) {
- if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {
- return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));
- }
+ private String deployRule2Engine(CorrelationRule correlationRule)
+ throws CorrelationException {
+ if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && (
+ correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {
+ return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));
}
return "";
}
@@ -253,7 +229,7 @@ public class RuleMgtWrapper {
CorrelationRule correlationRule) {
CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
correlationDeployRule4Engine.setContent(correlationRule.getContent());
- correlationDeployRule4Engine.setEngineId(correlationRule.getEngineId());
+ correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());
return correlationDeployRule4Engine;
}