Add Defensive Codes in Case Errors Occur
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / dcae / DcaeConfigurationPolling.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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 import com.fasterxml.jackson.core.JsonProcessingException;
17 import java.util.List;
18 import javax.ws.rs.client.Client;
19 import javax.ws.rs.client.ClientBuilder;
20 import javax.ws.rs.client.Entity;
21 import javax.ws.rs.client.WebTarget;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import lombok.extern.slf4j.Slf4j;
25 import org.glassfish.jersey.client.ClientConfig;
26 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
27 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
28 import org.onap.holmes.common.dcae.entity.Rule;
29 import org.onap.holmes.common.exception.CorrelationException;
30 import org.onap.holmes.common.utils.JacksonUtil;
31 import org.onap.holmes.common.utils.Md5Util;
32 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
33 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
34 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
35
36 @Slf4j
37 public class DcaeConfigurationPolling implements Runnable {
38
39     public static long POLLING_PERIOD = 30 * 1000L;
40
41     private String hostname;
42
43     private String url = "http://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
44
45     public DcaeConfigurationPolling(String hostname) {
46         this.hostname = hostname;
47     }
48
49     private String prevConfigMd5 = Md5Util.md5(null);
50
51     private boolean prevResult = false;
52
53     @Override
54     public void run() {
55         DcaeConfigurations dcaeConfigurations = null;
56         try {
57             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
58             String md5 = Md5Util.md5(dcaeConfigurations);
59             if (prevResult && prevConfigMd5.equals(md5)){
60                 log.info("Operation aborted due to identical Configurations.");
61                 return;
62             }
63             prevConfigMd5 = md5;
64             prevResult = false;
65         } catch (CorrelationException e) {
66             log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
67         } catch (JsonProcessingException e) {
68             log.info("Failed to generate the MD5 information for new configurations.", e);
69         }
70         if (dcaeConfigurations != null) {
71             RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules();
72             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
73             deleteAllCorrelationRules(ruleResult4APIs);
74             try {
75                 prevResult = addAllCorrelationRules(dcaeConfigurations);
76             } catch (CorrelationException e) {
77                 log.error("Failed to add rules. " + e.getMessage(), e);
78                 prevResult = false;
79             }
80         }
81     }
82
83     private RuleQueryListResponse getAllCorrelationRules() {
84         Client client = ClientBuilder.newClient(new ClientConfig());
85         WebTarget webTarget = client.target(url);
86         return webTarget.request("application/json").get()
87                 .readEntity(RuleQueryListResponse.class);
88     }
89
90     private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
91         boolean suc = false;
92         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
93             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
94             Client client = ClientBuilder.newClient(new ClientConfig());
95             String content = null;
96             try {
97                 content = JacksonUtil.beanToJson(ruleCreateRequest);
98             } catch (JsonProcessingException e) {
99                 throw new CorrelationException("Failed to convert the message object to a json string.", e);
100             }
101             WebTarget webTarget = client.target(url);
102             Response response = webTarget.request(MediaType.APPLICATION_JSON)
103                     .put(Entity.entity(content, MediaType.APPLICATION_JSON));
104             suc = response.getStatus() == 200;
105             if (!suc) {
106                 break;
107             }
108         }
109         return suc;
110     }
111
112     private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
113         ruleResult4APIs.forEach(correlationRule ->{
114             Client client = ClientBuilder.newClient(new ClientConfig());
115             WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId());
116             webTarget.request(MediaType.APPLICATION_JSON).delete();
117         });
118     }
119
120     private RuleCreateRequest getRuleCreateRequest(Rule rule) {
121         RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
122         ruleCreateRequest.setLoopControlName(rule.getLoopControlName());
123         ruleCreateRequest.setRuleName(rule.getName());
124         ruleCreateRequest.setContent(rule.getContents());
125         ruleCreateRequest.setDescription("");
126         ruleCreateRequest.setEnabled(1);
127         return ruleCreateRequest;
128     }
129 }