Replace Jackson with GSON
[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 java.util.List;
17 import javax.ws.rs.client.Client;
18 import javax.ws.rs.client.ClientBuilder;
19 import javax.ws.rs.client.Entity;
20 import javax.ws.rs.client.WebTarget;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23 import lombok.extern.slf4j.Slf4j;
24 import org.glassfish.jersey.client.ClientConfig;
25 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
26 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
27 import org.onap.holmes.common.dcae.entity.Rule;
28 import org.onap.holmes.common.exception.CorrelationException;
29 import org.onap.holmes.common.utils.GsonUtil;
30 import org.onap.holmes.common.utils.Md5Util;
31 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
32 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
33 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
34
35 @Slf4j
36 public class DcaeConfigurationPolling implements Runnable {
37
38     public static long POLLING_PERIOD = 30 * 1000L;
39
40     private String hostname;
41
42     private String url = "http://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
43
44     public DcaeConfigurationPolling(String hostname) {
45         this.hostname = hostname;
46     }
47
48     private String prevConfigMd5 = Md5Util.md5(null);
49
50     private boolean prevResult = false;
51
52     @Override
53     public void run() {
54         DcaeConfigurations dcaeConfigurations = null;
55         try {
56             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
57             String md5 = Md5Util.md5(dcaeConfigurations);
58             if (prevResult && prevConfigMd5.equals(md5)) {
59                 log.info("Operation aborted due to identical Configurations.");
60                 return;
61             }
62             prevConfigMd5 = md5;
63             prevResult = false;
64         } catch (CorrelationException e) {
65             log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
66         }
67         if (dcaeConfigurations != null) {
68             RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules();
69             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
70             deleteAllCorrelationRules(ruleResult4APIs);
71             try {
72                 prevResult = addAllCorrelationRules(dcaeConfigurations);
73             } catch (CorrelationException e) {
74                 log.error("Failed to add rules. " + e.getMessage(), e);
75                 prevResult = false;
76             }
77         }
78     }
79
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);
85     }
86
87     private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations)
88             throws CorrelationException {
89         boolean suc = false;
90         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
91             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
92             Client client = ClientBuilder.newClient(new ClientConfig());
93             String content = GsonUtil.beanToJson(ruleCreateRequest);
94             WebTarget webTarget = client.target(url);
95             Response response = webTarget.request(MediaType.APPLICATION_JSON)
96                     .put(Entity.entity(content, MediaType.APPLICATION_JSON));
97             suc = response.getStatus() == 200;
98             if (!suc) {
99                 break;
100             }
101         }
102         return suc;
103     }
104
105     private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs) {
106         ruleResult4APIs.forEach(correlationRule -> {
107             Client client = ClientBuilder.newClient(new ClientConfig());
108             WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId());
109             webTarget.request(MediaType.APPLICATION_JSON).delete();
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 }