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;
33 public class DcaeConfigurationPolling implements Runnable {
35 public static final long POLLING_PERIOD = 30 * 1000L;
37 private String hostname;
39 private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
41 public DcaeConfigurationPolling(String hostname) {
42 this.hostname = hostname;
45 private String prevConfigMd5 = Md5Util.md5(null);
47 private boolean prevResult = false;
51 DcaeConfigurations dcaeConfigurations = null;
53 dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
54 String md5 = Md5Util.md5(dcaeConfigurations);
55 if (prevResult && prevConfigMd5.equals(md5)) {
56 log.info("Operation aborted due to identical configurations.");
61 } catch (CorrelationException e) {
62 log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
63 } catch (Exception e) {
64 log.info("Failed to generate the MD5 information for new configurations.", e);
66 RuleQueryListResponse ruleQueryListResponse = null;
67 if (dcaeConfigurations != null) {
69 ruleQueryListResponse = getAllCorrelationRules();
70 } catch (Exception e) {
71 log.error("Failed to get deployed rules from the rule management module: " + e.getMessage(), e);
74 if (ruleQueryListResponse != null) {
75 List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
76 deleteAllCorrelationRules(ruleResult4APIs);
78 prevResult = addAllCorrelationRules(dcaeConfigurations);
79 } catch (CorrelationException e) {
80 log.error("Failed to add rules. " + e.getMessage(), e);
86 private RuleQueryListResponse getAllCorrelationRules() {
87 return JerseyClient.newInstance().get(url, RuleQueryListResponse.class);
90 private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
92 for (Rule rule : dcaeConfigurations.getDefaultRules()) {
93 RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
94 suc = JerseyClient.newInstance().header("Accept", MediaType.APPLICATION_JSON)
95 .put(url, Entity.json(ruleCreateRequest)) != null;
104 private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs) {
105 ruleResult4APIs.forEach(correlationRule -> {
106 if (null == JerseyClient.newInstance().delete(url + "/" + correlationRule.getRuleId())) {
107 log.warn("Failed to delete rule, the rule id is: {}", correlationRule.getRuleId());
112 private RuleCreateRequest getRuleCreateRequest(Rule rule) {
113 RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
114 ruleCreateRequest.setLoopControlName(rule.getLoopControlName());
115 ruleCreateRequest.setRuleName(rule.getName());
116 ruleCreateRequest.setContent(rule.getContents());
117 ruleCreateRequest.setDescription("");
118 ruleCreateRequest.setEnabled(1);
119 return ruleCreateRequest;