1 package org.onap.sdc.dcae.composition.controller.health;
4 import java.util.Collections;
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;
26 import com.google.gson.Gson;
31 @ConfigurationProperties(prefix="blueprinter")
32 public class HealthPoller {
34 private String hcretrynum;
37 private OnapLoggerError errLogger = OnapLoggerError.getInstance();
38 private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
41 private ToscaLabHealthState toscaLabHealthState;
43 public HealthPoller() {
48 @Scheduled(fixedDelayString="${healthpoller.fixedDelay}")
49 public void pollToscaLabHealth() {
50 ComponentsInfo toscaLabHealthRes = null;
51 ResponseEntity<String> healthRes = null;
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);
62 } catch (Exception e) {
63 toscaLabHealthRes = getNegativeHealth(e.getMessage());
65 if(toscaLabHealthRes == null){
66 toscaLabHealthRes = getNegativeHealth(healthRes.getBody() + "-" + healthRes.getStatusCode());
68 toscaLabHealthState.setToscaLabHealthResponse(toscaLabHealthRes);
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;
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);
89 public void setHcuri(URI hcuri) {
93 public void setHcretrynum(String hcretrynum) {
94 this.hcretrynum = hcretrynum;