Fixed Some Sonar Findings
[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.alibaba.fastjson.JSON;
17 import com.google.gson.Gson;
18 import com.google.gson.GsonBuilder;
19 import com.google.gson.JsonDeserializationContext;
20 import com.google.gson.JsonDeserializer;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParseException;
23 import com.google.gson.reflect.TypeToken;
24 import java.io.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.lang.reflect.Type;
27 import java.util.HashMap;
28 import java.util.List;
29 import javax.ws.rs.core.MediaType;
30 import lombok.extern.slf4j.Slf4j;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.entity.StringEntity;
33 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
34 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
35 import org.onap.holmes.common.dcae.entity.Rule;
36 import org.onap.holmes.common.exception.CorrelationException;
37 import org.onap.holmes.common.utils.GsonUtil;
38 import org.onap.holmes.common.utils.HttpsUtils;
39 import org.onap.holmes.common.utils.Md5Util;
40 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
41 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
42 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
43
44 @Slf4j
45 public class DcaeConfigurationPolling implements Runnable {
46
47     public static final long POLLING_PERIOD = 30 * 1000L;
48
49     private String hostname;
50
51     private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
52
53     public DcaeConfigurationPolling(String hostname) {
54         this.hostname = hostname;
55     }
56
57     private String prevConfigMd5 = Md5Util.md5(null);
58
59     private boolean prevResult = false;
60
61     @Override
62     public void run() {
63         DcaeConfigurations dcaeConfigurations = null;
64         try {
65             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
66             String md5 = Md5Util.md5(dcaeConfigurations);
67             if (prevResult && prevConfigMd5.equals(md5)){
68                 log.info("Operation aborted due to identical Configurations.");
69                 return;
70             }
71             prevConfigMd5 = md5;
72             prevResult = false;
73         } catch (CorrelationException e) {
74             log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
75         } catch (Exception e) {
76             log.info("Failed to generate the MD5 information for new configurations.", e);
77         }
78         RuleQueryListResponse ruleQueryListResponse = null;
79         if (dcaeConfigurations != null) {
80             try {
81                 ruleQueryListResponse = getAllCorrelationRules();
82             } catch (CorrelationException e) {
83                 log.error("Failed to get right response!" + e.getMessage(), e);
84             } catch (IOException e) {
85                 log.error("Failed to extract response entity. " + e.getMessage(), e);
86             }
87         }
88         if (ruleQueryListResponse != null) {
89             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
90             deleteAllCorrelationRules(ruleResult4APIs);
91             try {
92                 prevResult = addAllCorrelationRules(dcaeConfigurations);
93             } catch (CorrelationException e) {
94                 log.error("Failed to add rules. " + e.getMessage(), e);
95                 prevResult = false;
96             }
97         }
98     }
99
100     public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException {
101               HashMap<String, String> headers = new HashMap<>();
102         headers.put("Content-Type", MediaType.APPLICATION_JSON);
103         HttpResponse httpResponse = HttpsUtils.get(url, headers);
104         String response = HttpsUtils.extractResponseEntity(httpResponse);
105         return JSON.parseObject(response,RuleQueryListResponse.class);
106     }
107
108     private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
109         boolean suc = false;
110         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
111             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
112             String content = "";
113             try {
114                 content = GsonUtil.beanToJson(ruleCreateRequest);
115             } catch (Exception e) {
116                 throw new CorrelationException("Failed to convert the message object to a json string.", e);
117             }
118             HashMap<String, String> headers = new HashMap<>();
119             headers.put("Content-Type", MediaType.APPLICATION_JSON);
120             headers.put("Accept", MediaType.APPLICATION_JSON);
121             HttpResponse httpResponse;
122             try {
123                 httpResponse = HttpsUtils
124                         .put(url, headers, new HashMap<>(), new StringEntity(content));
125             } catch (UnsupportedEncodingException e) {
126                 throw new CorrelationException("Failed to create https entity.", e);
127             } catch (Exception e) {
128                 throw new CorrelationException(e.getMessage());
129             }
130             if (httpResponse != null) {
131                 suc = httpResponse.getStatusLine().getStatusCode() == 200;
132             }
133             if (!suc) {
134                 break;
135             }
136         }
137         return suc;
138     }
139
140     private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
141         ruleResult4APIs.forEach(correlationRule ->{
142             HashMap<String, String> headers = new HashMap<>();
143             headers.put("Content-Type", MediaType.APPLICATION_JSON);
144             try {
145                 HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers);
146             } catch (Exception e) {
147                 log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId()
148                         + " exception messge is : " + e.getMessage(), e);
149             }
150         });
151     }
152
153     private RuleCreateRequest getRuleCreateRequest(Rule rule) {
154         RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
155         ruleCreateRequest.setLoopControlName(rule.getLoopControlName());
156         ruleCreateRequest.setRuleName(rule.getName());
157         ruleCreateRequest.setContent(rule.getContents());
158         ruleCreateRequest.setDescription("");
159         ruleCreateRequest.setEnabled(1);
160         return ruleCreateRequest;
161     }
162 }