196b21a2f8ba0b34e520591ec45216904d0d8552
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / 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.onap.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.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
27 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
28 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
29 import org.onap.holmes.rulemgt.db.CorrelationRuleDao;\r
30 import org.onap.holmes.common.api.entity.CorrelationRule;\r
31 import org.onap.holmes.common.exception.CorrelationException;\r
32 import org.onap.holmes.common.utils.DbDaoUtil;\r
33 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
34 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;\r
35 import org.onap.holmes.rulemgt.bean.request.RuleDeleteRequest;\r
36 import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;\r
37 import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
38 import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
39 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
40 import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
41 import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
42 import org.onap.holmes.rulemgt.send.Ip4AddingRule;\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 Ip4AddingRule ip4AddingRule;\r
52 \r
53     @Inject\r
54     private RuleQueryWrapper ruleQueryWrapper;\r
55 \r
56     @Inject\r
57     private CorrelationRuleQueryDao correlationRuleQueryDao;\r
58     @Inject\r
59     private EngineWrapper engineWarpper;\r
60     @Inject\r
61     private DbDaoUtil daoUtil;\r
62 \r
63     private CorrelationRuleDao correlationRuleDao;\r
64 \r
65     @PostConstruct\r
66     public void initDaoUtil() {\r
67         correlationRuleDao = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class);\r
68     }\r
69 \r
70     public RuleAddAndUpdateResponse addCorrelationRule(String creator, RuleCreateRequest ruleCreateRequest)\r
71             throws CorrelationException {\r
72         if (ruleCreateRequest == null) {\r
73             throw new CorrelationException("The request object can not be empty!");\r
74         }\r
75         CorrelationRule correlationRule = convertCreateRequest2Rule(creator,\r
76                 ruleCreateRequest);\r
77         checkCorrelation(correlationRule);\r
78         CorrelationRule ruleTemp = correlationRuleDao.queryRuleByRuleName(correlationRule.getName());\r
79         if (ruleTemp != null) {\r
80             throw new CorrelationException("A rule with the same name already exists.");\r
81         }\r
82         String ip ="";\r
83         try{\r
84             ip = ip4AddingRule.getEngineIp4AddRule();\r
85         }catch(Exception e){\r
86             log.error("When adding rules, can not get engine instance ip");\r
87         }\r
88         String packageName = deployRule2Engine(correlationRule, ip);\r
89         correlationRule.setPackageName(packageName);\r
90         correlationRule.setEngineInstance(ip);\r
91         CorrelationRule result = null;\r
92         try {\r
93             result = correlationRuleDao.saveRule(correlationRule);\r
94         } catch (CorrelationException e) {\r
95             engineWarpper.deleteRuleFromEngine(packageName, ip);\r
96             throw new CorrelationException(e.getMessage(), e);\r
97         }\r
98         RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();\r
99         ruleAddAndUpdateResponse.setRuleId(result.getRid());\r
100         return ruleAddAndUpdateResponse;\r
101     }\r
102 \r
103     public RuleAddAndUpdateResponse updateCorrelationRule(String modifier, RuleUpdateRequest ruleUpdateRequest)\r
104             throws CorrelationException {\r
105         if (ruleUpdateRequest == null) {\r
106             throw new CorrelationException("The request object can not be empty!");\r
107         }\r
108         CorrelationRule oldCorrelationRule = correlationRuleDao.queryRuleByRid(ruleUpdateRequest.getRuleId());\r
109         if (oldCorrelationRule == null) {\r
110             throw new CorrelationException("You're trying to update a rule which does not exist in the system.");\r
111         }\r
112         String updateIp = "";\r
113         updateIp = oldCorrelationRule.getEngineInstance();\r
114         CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,\r
115                 ruleUpdateRequest, oldCorrelationRule.getName());\r
116         newCorrelationRule.setEngineInstance(updateIp);\r
117         checkCorrelation(newCorrelationRule);\r
118         RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();\r
119         ruleChangeResponse.setRuleId(newCorrelationRule.getRid());\r
120 \r
121         if (!haveChange(newCorrelationRule, oldCorrelationRule)) {\r
122             return ruleChangeResponse;\r
123         }\r
124         if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
125             String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance();\r
126             engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance);\r
127         }\r
128         newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));\r
129         correlationRuleDao.updateRule(newCorrelationRule);\r
130         return ruleChangeResponse;\r
131     }\r
132 \r
133     private void checkCorrelation(CorrelationRule correlationRule) throws CorrelationException {\r
134         int enabled = correlationRule.getEnabled();\r
135         String ruleName = correlationRule.getName() == null ? "" : correlationRule.getName().trim();\r
136         String content = correlationRule.getContent() == null ? "" : correlationRule.getContent().trim();\r
137         if ("".equals(content)) {\r
138             throw new CorrelationException("The contents of the rule can not be empty!");\r
139         }\r
140         if (enabled != RuleMgtConstant.STATUS_RULE_CLOSE\r
141                 && enabled != RuleMgtConstant.STATUS_RULE_OPEN) {\r
142             throw new CorrelationException("Invalid rule status. Only 0 (disabled) and 1 (enabled) are allowed.");\r
143         }\r
144         if ("".equals(ruleName)) {\r
145             throw new CorrelationException("The name of the rule can not be empty.");\r
146         }\r
147     }\r
148 \r
149     private boolean haveChange(CorrelationRule newCorrelationRule, CorrelationRule oldCorrelationRule) {\r
150         String newContent = newCorrelationRule.getContent();\r
151         String oldContent = oldCorrelationRule.getContent();\r
152         int newEnabled = newCorrelationRule.getEnabled();\r
153         int oldEnabled = oldCorrelationRule.getEnabled();\r
154         String newDes = newCorrelationRule.getDescription();\r
155         String oldDes = oldCorrelationRule.getDescription();\r
156         String oldControlLoop = oldCorrelationRule.getClosedControlLoopName();\r
157         String newControlLoop = newCorrelationRule.getClosedControlLoopName();\r
158         if (newContent.equals(oldContent) && newEnabled == oldEnabled\r
159                 && newDes.equals(oldDes) && newControlLoop.equals(oldControlLoop)) {\r
160             return false;\r
161         }\r
162         return true;\r
163     }\r
164 \r
165     public void deleteCorrelationRule(RuleDeleteRequest ruleDeleteRequest)\r
166             throws CorrelationException {\r
167         if (ruleDeleteRequest == null) {\r
168             throw new CorrelationException("The request object can not be empty!");\r
169         }\r
170         CorrelationRule correlationRule = correlationRuleDao.queryRuleByRid(ruleDeleteRequest.getRuleId());\r
171         if (correlationRule == null) {\r
172             log.warn("the rule:rule id=" + ruleDeleteRequest.getRuleId() + " does not exist the database.");\r
173             throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");\r
174         }\r
175         if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
176             String ip = correlationRule.getEngineInstance();\r
177             engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);\r
178         }\r
179         correlationRuleDao.deleteRule(correlationRule);\r
180     }\r
181 \r
182     private CorrelationRule convertCreateRequest2Rule(String userName,\r
183             RuleCreateRequest ruleCreateRequest) throws CorrelationException {\r
184         String tempContent = ruleCreateRequest.getContent();\r
185         CorrelationRule correlationRule = new CorrelationRule();\r
186         String ruleId = "rule_" + System.currentTimeMillis();\r
187         String description = ruleCreateRequest.getDescription() == null ? "" : ruleCreateRequest.getDescription();\r
188         correlationRule.setRid(ruleId);\r
189         if (tempContent != null) {\r
190             correlationRule.setContent(tempContent.trim());\r
191         }\r
192         correlationRule.setDescription(description);\r
193         correlationRule.setCreateTime(new Date());\r
194         correlationRule.setUpdateTime(new Date());\r
195         correlationRule.setName(ruleCreateRequest.getRuleName());\r
196         correlationRule.setEngineID("correlation-d");\r
197         correlationRule.setEngineType("");\r
198         correlationRule.setTemplateID(0);\r
199         correlationRule.setVendor("");\r
200         correlationRule.setCreator(userName);\r
201         correlationRule.setModifier(userName);\r
202         correlationRule.setEnabled(ruleCreateRequest.getEnabled());\r
203         correlationRule.setClosedControlLoopName(ruleCreateRequest.getLoopControlName());\r
204         return correlationRule;\r
205     }\r
206 \r
207     private CorrelationRule convertRuleUpdateRequest2CorrelationRule(String modifier,\r
208             RuleUpdateRequest ruleUpdateRequest, String ruleName) throws CorrelationException {\r
209         CorrelationRule correlationRule = new CorrelationRule();\r
210         String description = ruleUpdateRequest.getDescription() == null ? "" : ruleUpdateRequest.getDescription();\r
211         correlationRule.setRid(ruleUpdateRequest.getRuleId());\r
212         correlationRule.setContent(ruleUpdateRequest.getContent());\r
213         correlationRule.setDescription(description);\r
214         correlationRule.setEnabled(ruleUpdateRequest.getEnabled());\r
215         correlationRule.setUpdateTime(new Date());\r
216         correlationRule.setModifier(modifier);\r
217         correlationRule.setName(ruleName);\r
218         correlationRule.setClosedControlLoopName(ruleUpdateRequest.getLoopControlName());\r
219         return correlationRule;\r
220     }\r
221 \r
222     public String deployRule2Engine(CorrelationRule correlationRule, String ip)\r
223             throws CorrelationException {\r
224         if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule), ip) && (\r
225                 correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {\r
226             return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);\r
227         }\r
228         return "";\r
229     }\r
230 \r
231     public RuleQueryListResponse getCorrelationRuleByCondition(\r
232             RuleQueryCondition ruleQueryCondition) throws CorrelationException {\r
233         List<CorrelationRule> correlationRule = correlationRuleQueryDao\r
234                 .getCorrelationRulesByCondition(ruleQueryCondition);\r
235         RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();\r
236         ruleQueryListResponse.setTotalCount(correlationRule.size());\r
237         ruleQueryListResponse\r
238                 .setCorrelationRules(correlationRules2RuleResult4APIs(correlationRule));\r
239         return ruleQueryListResponse;\r
240     }\r
241 \r
242     private List<RuleResult4API> correlationRules2RuleResult4APIs(\r
243             List<CorrelationRule> correlationRules) {\r
244         List<RuleResult4API> ruleResult4APIs = new ArrayList<RuleResult4API>();\r
245         for (CorrelationRule correlationRule : correlationRules) {\r
246             RuleResult4API ruleResult4API = new RuleResult4API();\r
247             String description = correlationRule.getDescription() == null ? "" : correlationRule.getDescription();\r
248             ruleResult4API.setRuleId(correlationRule.getRid());\r
249             ruleResult4API.setRuleName(correlationRule.getName());\r
250             ruleResult4API.setDescription(description);\r
251             ruleResult4API.setContent(correlationRule.getContent());\r
252             ruleResult4API.setCreateTime(correlationRule.getCreateTime());\r
253             ruleResult4API.setCreator(correlationRule.getCreator());\r
254             ruleResult4API.setUpdateTime(correlationRule.getUpdateTime());\r
255             ruleResult4API.setModifier(correlationRule.getModifier());\r
256             ruleResult4API.setEnabled(correlationRule.getEnabled());\r
257             ruleResult4API.setLoopControlName(correlationRule.getClosedControlLoopName());\r
258             ruleResult4APIs.add(ruleResult4API);\r
259         }\r
260         return ruleResult4APIs;\r
261     }\r
262 \r
263     private CorrelationDeployRule4Engine correlationRules2DeployRule(\r
264             CorrelationRule correlationRule) {\r
265         CorrelationDeployRule4Engine correlationDeployRule4Engine = new CorrelationDeployRule4Engine();\r
266         correlationDeployRule4Engine.setContent(correlationRule.getContent());\r
267         correlationDeployRule4Engine.setEngineId(correlationRule.getEngineID());\r
268         correlationDeployRule4Engine.setLoopControlName(correlationRule.getClosedControlLoopName());\r
269         return correlationDeployRule4Engine;\r
270     }\r
271 \r
272     private CorrelationCheckRule4Engine correlationRules2CheckRule(\r
273             CorrelationRule correlationRule) {\r
274         CorrelationCheckRule4Engine correlationCheckRule4Engine = new CorrelationCheckRule4Engine();\r
275         correlationCheckRule4Engine.setContent(correlationRule.getContent());\r
276         return correlationCheckRule4Engine;\r
277     }\r
278 }\r