Improve the API Descriptions
[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.entity.DcaeConfigurations;
28 import org.onap.holmes.common.dcae.entity.Rule;
29 import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
30 import org.onap.holmes.common.exception.CorrelationException;
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 = 10 * 1000L;
39
40     private String hostname;
41
42     private String url = "http://127.0.0.1/api/holmes-rule-mgmt/v1/rule";
43
44     public DcaeConfigurationPolling(String hostname) {
45         this.hostname = hostname;
46     }
47
48     @Override
49     public void run() {
50         DcaeConfigurations dcaeConfigurations = null;
51         try {
52             dcaeConfigurations = getDcaeConfigurations();
53         } catch (CorrelationException e) {
54             log.error("Failed to fetch DCAE configurations" + e.getMessage());
55         }
56         if (dcaeConfigurations != null) {
57             RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules();
58             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
59             deleteAllCorrelationRules(ruleResult4APIs);
60             try {
61                 addAllCorrelationRules(dcaeConfigurations);
62             } catch (CorrelationException e) {
63                 log.error("Failed to add rules" + e.getMessage());
64             }
65         }
66     }
67
68     private DcaeConfigurations getDcaeConfigurations() throws CorrelationException {
69         String serviceAddrInfo = MicroServiceConfig.getServiceAddrInfoFromCBS(hostname);
70         String response = getDcaeResponse(serviceAddrInfo);
71         DcaeConfigurations dcaeConfigurations = null;
72         dcaeConfigurations = DcaeConfigurationParser.parse(response);
73         return dcaeConfigurations;
74     }
75
76     private String getDcaeResponse(String serviceAddrInfo) {
77         Client client = ClientBuilder.newClient(new ClientConfig());
78         WebTarget webTarget = client.target(serviceAddrInfo);
79         return webTarget.request("application/json").get()
80                 .readEntity(String.class);
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 void addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
91         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
92             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
93             Client client = ClientBuilder.newClient(new ClientConfig());
94             ObjectMapper mapper = new ObjectMapper();
95             String content = null;
96             try {
97                 content = mapper.writeValueAsString(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             webTarget.request(MediaType.APPLICATION_JSON)
103                     .put(Entity.entity(content, MediaType.APPLICATION_JSON));
104         }
105     }
106
107     private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
108         ruleResult4APIs.forEach(correlationRule ->{
109             Client client = ClientBuilder.newClient(new ClientConfig());
110             WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId());
111             webTarget.request(MediaType.APPLICATION_JSON).delete();
112         });
113     }
114
115     private RuleCreateRequest getRuleCreateRequest(Rule rule) {
116         RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
117         ruleCreateRequest.setLoopControlName(rule.getLoopControlName());
118         ruleCreateRequest.setRuleName(rule.getName());
119         ruleCreateRequest.setContent(rule.getContents());
120         ruleCreateRequest.setDescription("");
121         ruleCreateRequest.setEnabled(1);
122         return ruleCreateRequest;
123     }
124 }