DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / composition / controller / health / HealthPoller.java
1 package org.onap.sdc.dcae.composition.controller.health;
2
3 import java.net.URI;
4 import java.util.Collections;
5
6 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
7 import org.onap.sdc.common.onaplog.OnapLoggerError;
8 import org.onap.sdc.common.onaplog.Enums.LogLevel;
9 import org.onap.sdc.dcae.composition.restmodels.health.ComponentsInfo;
10 import org.onap.sdc.dcae.catalog.commons.Future;
11 import org.onap.sdc.dcae.catalog.commons.Http;
12 import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.context.properties.ConfigurationProperties;
15 import org.springframework.context.annotation.Configuration;
16 import org.springframework.http.HttpEntity;
17 import org.springframework.http.HttpHeaders;
18 import org.springframework.http.HttpMethod;
19 import org.springframework.http.HttpStatus;
20 import org.springframework.http.MediaType;
21 import org.springframework.http.ResponseEntity;
22 import org.springframework.scheduling.annotation.EnableAsync;
23 import org.springframework.scheduling.annotation.EnableScheduling;
24 import org.springframework.scheduling.annotation.Scheduled;
25
26 import com.google.gson.Gson;
27
28 @Configuration
29 @EnableAsync
30 @EnableScheduling
31 @ConfigurationProperties(prefix="blueprinter")
32 public class HealthPoller {
33         private URI     hcuri;
34         private String hcretrynum;
35         private Gson gson;
36         
37         private OnapLoggerError errLogger = OnapLoggerError.getInstance();
38         private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
39
40         @Autowired
41         private ToscaLabHealthState toscaLabHealthState;
42         
43         public HealthPoller() {
44                 super();
45                 gson = new Gson();
46         }
47
48         @Scheduled(fixedDelayString="${healthpoller.fixedDelay}")
49         public void pollToscaLabHealth() {
50                 ComponentsInfo toscaLabHealthRes = null;
51                 ResponseEntity<String> healthRes = null;
52                 try {
53                         for(int i=0; i<Integer.valueOf(hcretrynum); i++){ // 3 tries
54                                 healthRes = sendHealthCheck();
55                                 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Try #{}: {}", i, healthRes);
56                                 if(healthRes.getStatusCode()==HttpStatus.OK){
57                                         String result = (String) healthRes.getBody();
58                                         toscaLabHealthRes = gson.fromJson(result, ComponentsInfo.class);
59                                         break;
60                                 }
61                         }
62                 } catch (Exception e) {
63                         toscaLabHealthRes = getNegativeHealth(e.getMessage());
64                 }
65                 if(toscaLabHealthRes == null){
66                         toscaLabHealthRes = getNegativeHealth(healthRes.getBody() + "-" + healthRes.getStatusCode());
67                 }
68                 toscaLabHealthState.setToscaLabHealthResponse(toscaLabHealthRes);
69         }
70
71         private ComponentsInfo getNegativeHealth(String msg) {
72                 ComponentsInfo toscaLabHealthRes = new ComponentsInfo();
73                 String description = "DCAE-D BE failed while trying to fetch Tosca_Lab healthcheck. Exception: " +msg;
74                 toscaLabHealthRes.setDescription(description);
75                 toscaLabHealthRes.setHealthCheckComponent(DcaeBeConstants.Health.TOSCA_LAB);
76                 toscaLabHealthRes.setHealthCheckStatus(DcaeBeConstants.Health.DOWN);
77                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), description);
78                 return toscaLabHealthRes;
79         }
80         
81         public ResponseEntity<String> sendHealthCheck() {
82                 HttpHeaders headers = new HttpHeaders();
83                 headers.setContentType(MediaType.APPLICATION_JSON);
84                 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
85                 HttpEntity<String> entity = new HttpEntity<String>(headers);
86                 return Http.exchangeSync(hcuri.toString(), HttpMethod.GET, entity, String.class, 5000);
87         }
88
89         public void setHcuri(URI hcuri) {
90                 this.hcuri = hcuri;
91         }
92
93         public void setHcretrynum(String hcretrynum) {
94                 this.hcretrynum = hcretrynum;
95         }
96         
97 }