Change rule retrieval from CBS to ConfigMap
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / dcae / DcaeConfigurationPolling.java
1 /**
2  * Copyright 2017-2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.holmes.rulemgt.dcae;
15
16
17 import lombok.extern.slf4j.Slf4j;
18 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
19 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
20 import org.onap.holmes.common.dcae.entity.Rule;
21 import org.onap.holmes.common.exception.CorrelationException;
22 import org.onap.holmes.common.utils.JerseyClient;
23 import org.onap.holmes.common.utils.Md5Util;
24 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
25 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
26 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
27
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import java.util.List;
31
32 @Slf4j
33 @Deprecated
34 public class DcaeConfigurationPolling implements Runnable {
35
36     public static final long POLLING_PERIOD = 30 * 1000L;
37
38     private String hostname;
39
40     private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
41
42     public DcaeConfigurationPolling(String hostname) {
43         this.hostname = hostname;
44     }
45
46     private String prevConfigMd5 = Md5Util.md5(null);
47
48     private boolean prevResult = false;
49
50     @Override
51     public void run() {
52         DcaeConfigurations dcaeConfigurations = null;
53         try {
54             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
55             String md5 = Md5Util.md5(dcaeConfigurations);
56             if (prevResult && prevConfigMd5.equals(md5)) {
57                 log.info("Operation aborted due to identical configurations.");
58                 return;
59             }
60             prevConfigMd5 = md5;
61             prevResult = false;
62         } catch (CorrelationException e) {
63             log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
64         } catch (Exception e) {
65             log.info("Failed to generate the MD5 information for new configurations.", e);
66         }
67         RuleQueryListResponse ruleQueryListResponse = null;
68         if (dcaeConfigurations != null) {
69             try {
70                 ruleQueryListResponse = getAllCorrelationRules();
71             } catch (Exception e) {
72                 log.error("Failed to get deployed rules from the rule management module: " + e.getMessage(), e);
73             }
74         }
75         if (ruleQueryListResponse != null) {
76             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
77             deleteAllCorrelationRules(ruleResult4APIs);
78             try {
79                 prevResult = addAllCorrelationRules(dcaeConfigurations);
80             } catch (CorrelationException e) {
81                 log.error("Failed to add rules. " + e.getMessage(), e);
82                 prevResult = false;
83             }
84         }
85     }
86
87     private RuleQueryListResponse getAllCorrelationRules() {
88         return JerseyClient.newInstance().get(url, RuleQueryListResponse.class);
89     }
90
91     private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
92         boolean suc = false;
93         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
94             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
95             suc = JerseyClient.newInstance().header("Accept", MediaType.APPLICATION_JSON)
96                     .put(url, Entity.json(ruleCreateRequest)) != null;
97
98             if (!suc) {
99                 break;
100             }
101         }
102         return suc;
103     }
104
105     private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs) {
106         ruleResult4APIs.forEach(correlationRule -> {
107             if (null == JerseyClient.newInstance().delete(url + "/" + correlationRule.getRuleId())) {
108                 log.warn("Failed to delete rule, the rule id is: {}", correlationRule.getRuleId());
109             }
110         });
111     }
112
113     private RuleCreateRequest getRuleCreateRequest(Rule rule) {
114         RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
115         ruleCreateRequest.setLoopControlName(rule.getLoopControlName());
116         ruleCreateRequest.setRuleName(rule.getName());
117         ruleCreateRequest.setContent(rule.getContents());
118         ruleCreateRequest.setDescription("");
119         ruleCreateRequest.setEnabled(1);
120         return ruleCreateRequest;
121     }
122 }