2 * Copyright 2017-2021 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.onap.holmes.rulemgt.db;
\r
18 import lombok.extern.slf4j.Slf4j;
\r
19 import org.jdbi.v3.core.Handle;
\r
20 import org.jdbi.v3.core.Jdbi;
\r
21 import org.jdbi.v3.core.statement.Query;
\r
22 import org.onap.holmes.common.api.entity.CorrelationRule;
\r
23 import org.onap.holmes.common.exception.CorrelationException;
\r
24 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
\r
25 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;
\r
26 import org.springframework.beans.factory.annotation.Autowired;
\r
27 import org.springframework.stereotype.Service;
\r
29 import java.beans.PropertyDescriptor;
\r
30 import java.lang.reflect.Field;
\r
31 import java.lang.reflect.Method;
\r
36 public class CorrelationRuleQueryService {
\r
41 public List<CorrelationRule> getCorrelationRulesByCondition(RuleQueryCondition ruleQueryCondition)
\r
42 throws CorrelationException {
\r
43 List<CorrelationRule> correlationRules = new ArrayList<CorrelationRule>();
\r
44 Handle handle = null;
\r
45 String whereStr = getWhereStrByRequestEntity(ruleQueryCondition);
\r
47 StringBuilder querySql = new StringBuilder("SELECT * FROM APLUS_RULE ").append(whereStr);
\r
48 handle = jdbi.open();
\r
49 Query query = handle.createQuery(querySql.toString());
\r
50 for (Object value : query.mapToMap().list()) {
\r
51 CorrelationRule correlationRule = getCorrelationRule((Map) value);
\r
52 correlationRules.add(correlationRule);
\r
54 return correlationRules;
\r
55 } catch (Exception e) {
\r
56 log.warn("Failed to query the rule: id =" + ruleQueryCondition.getRid() + ".");
\r
57 throw new CorrelationException("Failed to query the rule.", e);
\r
63 private CorrelationRule getCorrelationRule(Map value) {
\r
64 CorrelationRule correlationRule = new CorrelationRule();
\r
65 correlationRule.setName((String) value.get("name"));
\r
66 correlationRule.setRid((String) value.get("rid"));
\r
67 correlationRule.setDescription((String) value.get("description"));
\r
68 correlationRule.setEnabled((Integer) value.get("enable"));
\r
69 correlationRule.setTemplateID((Long) value.get("templateid"));
\r
70 correlationRule.setEngineID((String) value.get("engineid"));
\r
71 correlationRule.setEngineType((String) value.get("enginetype"));
\r
72 correlationRule.setCreator((String) value.get("creator"));
\r
73 correlationRule.setCreateTime((Date) value.get("createtime"));
\r
74 correlationRule.setModifier((String) value.get("updator"));
\r
75 correlationRule.setUpdateTime((Date) value.get("updatetime"));
\r
76 correlationRule.setParams((Properties) value.get("params"));
\r
77 correlationRule.setContent((String) value.get("content"));
\r
78 correlationRule.setVendor((String) value.get("vendor"));
\r
79 correlationRule.setPackageName((String) value.get("package"));
\r
80 correlationRule.setClosedControlLoopName((String) value.get("ctrlloop"));
\r
81 correlationRule.setEngineInstance((String) value.get("engineinstance"));
\r
82 return correlationRule;
\r
85 private String getWhereStrByRequestEntity(RuleQueryCondition ruleQueryCondition) throws CorrelationException {
\r
87 Class clazz = ruleQueryCondition.getClass();
\r
88 Field[] fields = clazz.getDeclaredFields();
\r
89 String whereSql = " WHERE ";
\r
91 for (Field field : fields) {
\r
92 // Jacoco will cause an exception when calculating the coverage of the UT
\r
93 // Remove this if jacoco solves this problem in the future
\r
94 if (field.getName().contains("jacoco")) {
\r
97 PropertyDescriptor pd = new PropertyDescriptor((String)field.getName(),
\r
99 Method getMethod = pd.getReadMethod();
\r
100 Object o = getMethod.invoke(ruleQueryCondition);
\r
102 String tempName = field.getName();
\r
103 if ("enabled".equals(tempName)) {
\r
104 int enabled = (int) o;
\r
105 if (enabled != RuleMgtConstant.STATUS_RULE_ALL) {
\r
106 whereSql = whereSql + "enable =" + enabled;
\r
107 whereSql += " AND ";
\r
109 } else if ("name".equals(tempName)) {
\r
110 if (!"".equals(o.toString().trim())) {
\r
111 whereSql = whereSql + field.getName() + " like '%" + o + "%' AND ";
\r
113 } else if (!"".equals(o.toString().trim())) {
\r
114 whereSql = whereSql + field.getName() + "='" + o + "' AND ";
\r
118 whereSql = whereSql.trim();
\r
119 if(!"WHERE".equals(whereSql)){
\r
120 return whereSql.substring(0, whereSql.length() - "AND".length());
\r
123 } catch (Exception e) {
\r
124 throw new CorrelationException("An error occurred while building the query SQL.", e);
\r