Fix the DCAE integration Bugs
[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 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.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 = 10 * 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     @Override
50     public void run() {
51         DcaeConfigurations dcaeConfigurations = null;
52         try {
53             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
54         } catch (CorrelationException e) {
55             log.error("Failed to fetch DCAE configurations. " + e.getMessage());
56         }
57         if (dcaeConfigurations != null) {
58             RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules();
59             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
60             deleteAllCorrelationRules(ruleResult4APIs);
61             try {
62                 addAllCorrelationRules(dcaeConfigurations);
63             } catch (CorrelationException e) {
64                 log.error("Failed to add rules. " + e.getMessage());
65             }
66         }
67     }
68
69     private RuleQueryListResponse getAllCorrelationRules() {
70         Client client = ClientBuilder.newClient(new ClientConfig());
71         WebTarget webTarget = client.target(url);
72         return webTarget.request("application/json").get()
73                 .readEntity(RuleQueryListResponse.class);
74     }
75
76     private void addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
77         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
78             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
79             Client client = ClientBuilder.newClient(new ClientConfig());
80             ObjectMapper mapper = new ObjectMapper();
81             String content = null;
82             try {
83                 content = mapper.writeValueAsString(ruleCreateRequest);
84             } catch (JsonProcessingException e) {
85                 throw new CorrelationException("Failed to convert the message object to a json string.", e);
86             }
87             WebTarget webTarget = client.target(url);
88             webTarget.request(MediaType.APPLICATION_JSON)
89                     .put(Entity.entity(content, MediaType.APPLICATION_JSON));
90         }
91     }
92
93     private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
94         ruleResult4APIs.forEach(correlationRule ->{
95             Client client = ClientBuilder.newClient(new ClientConfig());
96             WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId());
97             webTarget.request(MediaType.APPLICATION_JSON).delete();
98         });
99     }
100
101     private RuleCreateRequest getRuleCreateRequest(Rule rule) {
102         RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
103         ruleCreateRequest.setLoopControlName(rule.getLoopControlName());
104         ruleCreateRequest.setRuleName(rule.getName());
105         ruleCreateRequest.setContent(rule.getContents());
106         ruleCreateRequest.setDescription("");
107         ruleCreateRequest.setEnabled(1);
108         return ruleCreateRequest;
109     }
110 }