2 * Copyright 2017 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;
16 import com.fasterxml.jackson.core.JsonProcessingException;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import java.util.List;
19 import javax.ws.rs.client.Client;
20 import javax.ws.rs.client.ClientBuilder;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.client.WebTarget;
23 import javax.ws.rs.core.MediaType;
24 import lombok.extern.slf4j.Slf4j;
25 import org.glassfish.jersey.client.ClientConfig;
26 import org.onap.holmes.common.config.MicroServiceConfig;
27 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
28 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
29 import org.onap.holmes.common.dcae.entity.Rule;
30 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
31 import org.onap.holmes.common.exception.CorrelationException;
32 import org.onap.holmes.common.utils.Md5Util;
33 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
34 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
35 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
38 public class DcaeConfigurationPolling implements Runnable {
40 public static long POLLING_PERIOD = 30 * 1000L;
42 private String hostname;
44 private String url = "http://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
46 public DcaeConfigurationPolling(String hostname) {
47 this.hostname = hostname;
50 private String prevConfigMd5 = Md5Util.md5(null);
54 DcaeConfigurations dcaeConfigurations = null;
56 dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
57 String md5 = Md5Util.md5(dcaeConfigurations);
58 if (prevConfigMd5.equals(md5)){
59 log.info("Operation aborted due to identical Configurations.");
63 } catch (CorrelationException e) {
64 log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
65 } catch (JsonProcessingException e) {
66 log.info("Failed to generate the MD5 information for new configurations.", e);
68 if (dcaeConfigurations != null) {
69 RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules();
70 List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
71 deleteAllCorrelationRules(ruleResult4APIs);
73 addAllCorrelationRules(dcaeConfigurations);
74 } catch (CorrelationException e) {
75 log.error("Failed to add rules. " + e.getMessage(), e);
80 private RuleQueryListResponse getAllCorrelationRules() {
81 Client client = ClientBuilder.newClient(new ClientConfig());
82 WebTarget webTarget = client.target(url);
83 return webTarget.request("application/json").get()
84 .readEntity(RuleQueryListResponse.class);
87 private void addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
88 for (Rule rule : dcaeConfigurations.getDefaultRules()) {
89 RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
90 Client client = ClientBuilder.newClient(new ClientConfig());
91 ObjectMapper mapper = new ObjectMapper();
92 String content = null;
94 content = mapper.writeValueAsString(ruleCreateRequest);
95 } catch (JsonProcessingException e) {
96 throw new CorrelationException("Failed to convert the message object to a json string.", e);
98 WebTarget webTarget = client.target(url);
99 webTarget.request(MediaType.APPLICATION_JSON)
100 .put(Entity.entity(content, MediaType.APPLICATION_JSON));
104 private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
105 ruleResult4APIs.forEach(correlationRule ->{
106 Client client = ClientBuilder.newClient(new ClientConfig());
107 WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId());
108 webTarget.request(MediaType.APPLICATION_JSON).delete();
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;