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.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;
45 public class DcaeConfigurationPolling implements Runnable {
47 public static long POLLING_PERIOD = 30 * 1000L;
49 private String hostname;
51 private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
53 public DcaeConfigurationPolling(String hostname) {
54 this.hostname = hostname;
57 private String prevConfigMd5 = Md5Util.md5(null);
59 private boolean prevResult = false;
63 DcaeConfigurations dcaeConfigurations = null;
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.");
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);
78 RuleQueryListResponse ruleQueryListResponse = null;
79 if (dcaeConfigurations != null) {
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);
88 if (ruleQueryListResponse != null) {
89 List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
90 deleteAllCorrelationRules(ruleResult4APIs);
92 prevResult = addAllCorrelationRules(dcaeConfigurations);
93 } catch (CorrelationException e) {
94 log.error("Failed to add rules. " + e.getMessage(), e);
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);
108 private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
110 for (Rule rule : dcaeConfigurations.getDefaultRules()) {
111 RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
114 content = GsonUtil.beanToJson(ruleCreateRequest);
115 } catch (Exception e) {
116 throw new CorrelationException("Failed to convert the message object to a json string.", e);
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;
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());
130 if (httpResponse != null) {
131 suc = httpResponse.getStatusLine().getStatusCode() == 200;
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);
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);
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;