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