Add rule management module unit tests
[holmes/rule-management.git] / rulemgt / src / main / java / org / openo / holmes / rulemgt / wrapper / RuleMgtWrapper.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 package org.openo.holmes.rulemgt.wrapper;\r
17 \r
18 import java.util.ArrayList;\r
19 import java.util.Date;\r
20 import java.util.List;\r
21 import javax.annotation.PostConstruct;\r
22 import javax.inject.Inject;\r
23 import javax.inject.Singleton;\r
24 import lombok.extern.slf4j.Slf4j;\r
25 import org.jvnet.hk2.annotations.Service;\r
26 import org.openo.holmes.common.api.entity.CorrelationRule;\r
27 import org.openo.holmes.common.exception.CorrelationException;\r
28 import org.openo.holmes.common.utils.DbDaoUtil;\r
29 import org.openo.holmes.common.utils.I18nProxy;\r
30 import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
31 import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
32 import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest;\r
33 import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
34 import org.openo.holmes.rulemgt.bean.request.RuleQueryCondition;\r
35 import org.openo.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
36 import org.openo.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
37 import org.openo.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
38 import org.openo.holmes.rulemgt.bean.response.RuleResult4API;\r
39 import org.openo.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
40 import org.openo.holmes.rulemgt.constant.RuleMgtConstant;\r
41 import org.openo.holmes.rulemgt.db.CorrelationRuleDao;\r
42 import org.openo.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
43 \r
44 \r
45 @Service\r
46 @Singleton\r
47 @Slf4j\r
48 public class RuleMgtWrapper {\r
49 \r
50     @Inject\r
51     private CorrelationRuleQueryDao correlationRuleQueryDao;\r
52     @Inject\r
53     private EngineWrapper engineWarpper;\r
54     @Inject\r
55     private DbDaoUtil daoUtil;\r
56 \r
57     private CorrelationRuleDao correlationRuleDao;\r
58 \r
59     @PostConstruct\r
60     public void initDaoUtil() {\r
61         correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);\r
62     }\r
63 \r
64     public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)\r
65             throws CorrelationException {\r
66         if (ruleCreateRequest == null) {\r
67             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);\r
68         }\r
69         CorrelationRule correlationRule = convertCreateRequest2Rule(creator,\r
70                 ruleCreateRequest);\r
71         checkCorrelation(correlationRule);\r
72         CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());\r
73         if (ruleTemp != null) {\r
74             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);\r
75         }\r
76         correlationRule.setPackageName(deployRule2Engine(correlationRule));\r
77         CorrelationRule result = correlationRuleDao.saveRule(correlationRule);\r
78         RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();\r
79         ruleAddAndUpdateResponse.setRuleId(result.getRid());\r
80         return ruleAddAndUpdateResponse;\r
81     }\r
82 \r
83     public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)\r
84             throws CorrelationException {\r
85         if (ruleUpdateRequest == null) {\r
86             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);\r
87         }\r
88         CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());\r
89         if (oldCorrelationRule == null) {\r
90             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);\r
91         }\r
92         CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,\r
93                 ruleUpdateRequest, oldCorrelationRule.getName());\r
94         checkCorrelation(newCorrelationRule);\r
95         RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();\r
96         ruleChangeResponse.setRuleId(newCorrelationRule.getRid());\r
97         if (!haveChange(newCorrelationRule, oldCorrelationRule)) {\r
98             return ruleChangeResponse;\r
99         }\r
100         if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
101             engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());\r
102         }\r
103         correlationRuleDao.updateRule(newCorrelationRule);\r
104         deployRule2Engine(newCorrelationRule);\r
105         return ruleChangeResponse;\r
106     }\r
107 \r
108     private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException {\r
109         int enabled = correlationRule.getEnabled();\r
110         String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();\r
111         String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();\r
112         if ("".equals(content)) {\r
113             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_CONTENT_CANNOT_BE_EMPTY);\r
114         }\r
115         if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE\r
116                 && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {\r
117             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_PARAMETER_ENABLED_ERROR);\r
118         }\r
119         if ("".equals(ruleName)) {\r
120             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NAME_CANNOT_BE_EMPTY);\r
121         }\r
122     }\r
123 \r
124     private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {\r
125         String newContent = newCorrelationRule.getContent();\r
126         String oldContent = oldCorrelationRule.getContent();\r
127         int newEnabled = newCorrelationRule.getEnabled();\r
128         int oldEnabled = oldCorrelationRule.getEnabled();\r
129         String newDes = newCorrelationRule.getDescription();\r
130         String oldDes = oldCorrelationRule.getDescription();\r
131         if (newContent.equals(oldContent) && newEnabled == oldEnabled && newDes.equals(oldDes)) {\r
132             return false;\r
133         }\r
134         return true;\r
135     }\r
136 \r
137     public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)\r
138             throws CorrelationException {\r
139         if (ruleDeleteRequest == null) {\r
140             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_REQUEST_OBJECT_IS_EMPTY);\r
141         }\r
142         CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());\r
143         if (correlationRule == null) {\r
144             log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");\r
145             throw new CorrelationException(I18nProxy.RULE_MANAGEMENT_RULE_NOT_EXIST_DATABASE);\r
146         }\r
147         if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
148             engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());\r
149         }\r
150         correlationRuleDao.deleteRule(correlationRule);\r
151     }\r
152 \r
153     private CorrelationRule convertCreateRequest2Rule(String userName,\r
154             RuleCreateRequest ruleCreateRequest) throws CorrelationException {\r
155         String tempContent = ruleCreateRequest.getContent();\r
156         CorrelationRule correlationRule = new CorrelationRule();\r
157         String ruleId = "rule_" + System.currentTimeMillis();\r
158         correlationRule.setRid(ruleId);\r
159         if (tempContent != null) {\r
160             correlationRule.setContent(tempContent.trim());\r
161         }\r
162         correlationRule.setDescription(ruleCreateRequest.getDescription());\r
163         correlationRule.setCreateTime(new Date());\r
164         correlationRule.setUpdateTime(new Date());\r
165         correlationRule.setName(ruleCreateRequest.getRuleName());\r
166         correlationRule.setEngineID("correlation-d");\r
167         correlationRule.setEngineType("");\r
168         correlationRule.setTemplateID(0);\r
169         correlationRule.setVendor("");\r
170         correlationRule.setCreator(userName);\r
171         correlationRule.setModifier(userName);\r
172         correlationRule.setEnabled(ruleCreateRequest.getEnabled());\r
173         return correlationRule;\r
174     }\r
175 \r
176     private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,\r
177             RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {\r
178         CorrelationRule correlationRule = new CorrelationRule();\r
179         correlationRule.setRid(ruleUpdateRequest.getRuleId());\r
180         correlationRule.setContent(ruleUpdateRequest.getContent());\r
181         correlationRule.setDescription(ruleUpdateRequest.getDescription());\r
182         correlationRule.setEnabled(ruleUpdateRequest.getEnabled());\r
183         correlationRule.setUpdateTime(new Date());\r
184         correlationRule.setModifier(modifier);\r
185         correlationRule.setName(ruleName);\r
186         return correlationRule;\r
187     }\r
188 \r
189     private String deployRule2Engine(CorrelationRule correlationRule)\r
190             throws CorrelationException {\r
191         if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && (\r
192                 correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {\r
193             return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));\r
194         }\r
195         return "";\r
196     }\r
197 \r
198     public RuleQueryListResponse getCorrelationRuleByCondition(\r
199             RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
200         List<CorrelationRule> correlationRule = correlationRuleQueryDao\r
201                 .getCorrelationRulesByCondition(ruleQueryCondition);\r
202         RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();\r
203         ruleQueryListResponse.setTotalCount(correlationRule.size());\r
204         ruleQueryListResponse\r
205                 .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));\r
206         return ruleQueryListResponse;\r
207     }\r
208 \r
209     private List<RuleResult4API> correlationRules2RuleResult4APIs(\r
210             List<CorrelationRule> correlationRules) {\r
211         List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();\r
212         for (CorrelationRule correlationRule : correlationRules) {\r
213             RuleResult4API ruleResult4API = new RuleResult4API();\r
214             ruleResult4API.setRuleId(correlationRule.getRid());\r
215             ruleResult4API.setRuleName(correlationRule.getName());\r
216             ruleResult4API.setDescription(correlationRule.getDescription());\r
217             ruleResult4API.setContent(correlationRule.getContent());\r
218             ruleResult4API.setCreateTime(correlationRule.getCreateTime());\r
219             ruleResult4API.setCreator(correlationRule.getCreator());\r
220             ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());\r
221             ruleResult4API.setModifier(correlationRule.getModifier());\r
222             ruleResult4API.setEnabled(correlationRule.getEnabled());\r
223             ruleResult4APIs.add(ruleResult4API);\r
224         }\r
225         return ruleResult4APIs;\r
226     }\r
227 \r
228     private CorrelationDeployRule4Engine correlationRules2DeployRule(\r
229             CorrelationRule correlationRule) {\r
230         CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();\r
231         correlationDeployRule4Engine.setContent(correlationRule.getContent());\r
232         correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());\r
233         return correlationDeployRule4Engine;\r
234     }\r
235 \r
236     private CorrelationCheckRule4Engine correlationRules2CheckRule(\r
237             CorrelationRule correlationRule) {\r
238         CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();\r
239         correlationCheckRule4Engine.setContent(correlationRule.getContent());\r
240         return correlationCheckRule4Engine;\r
241     }\r
242 }\r