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