2 * Copyright 2017-2021 ZTE Corporation.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
14 package org.onap.holmes.rulemgt.dcae;
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;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import java.util.List;
34 public class DcaeConfigurationPolling implements Runnable {
36 public static final long POLLING_PERIOD = 30 * 1000L;
38 private String hostname;
40 private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
42 public DcaeConfigurationPolling(String hostname) {
43 this.hostname = hostname;
46 private String prevConfigMd5 = Md5Util.md5(null);
48 private boolean prevResult = false;
52 DcaeConfigurations dcaeConfigurations = null;
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.");
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);
67 RuleQueryListResponse ruleQueryListResponse = null;
68 if (dcaeConfigurations != null) {
70 ruleQueryListResponse = getAllCorrelationRules();
71 } catch (Exception e) {
72 log.error("Failed to get deployed rules from the rule management module: " + e.getMessage(), e);
75 if (ruleQueryListResponse != null) {
76 List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
77 deleteAllCorrelationRules(ruleResult4APIs);
79 prevResult = addAllCorrelationRules(dcaeConfigurations);
80 } catch (CorrelationException e) {
81 log.error("Failed to add rules. " + e.getMessage(), e);
87 private RuleQueryListResponse getAllCorrelationRules() {
88 return JerseyClient.newInstance().get(url, RuleQueryListResponse.class);
91 private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
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;
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());
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;