Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / health / ConsulLinkHealthCheck.java
1 package org.onap.msb.apiroute.health;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.onap.msb.apiroute.ApiRouteApp;
5 import org.onap.msb.apiroute.wrapper.InitRouteServiceWrapper;
6 import org.onap.msb.apiroute.wrapper.util.ConfigUtil;
7 import org.onap.msb.apiroute.wrapper.util.HttpClientUtil;
8 import org.onap.msb.apiroute.wrapper.util.HttpGetResult;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.codahale.metrics.health.HealthCheck;
13
14 public class ConsulLinkHealthCheck extends HealthCheck implements Runnable {
15
16         private static final Logger LOGGER = LoggerFactory
17                         .getLogger(ConsulLinkHealthCheck.class);
18
19         private final static String CONSUL_IP_ENV = ConfigUtil.getInstance().getConsul_ip();
20
21         private static int failedLoopCheckNum = 12;
22         private static int failedTimer = 5 * 1000;
23
24         private static int normalTimer = 20 * 1000;
25         private static Result result = Result.healthy();
26
27         private String CHECK_IP = "127.0.0.1";
28         private String CHECK_PORT = "8500";
29         private String CHECK_URL = "http://" + CHECK_IP + ":" + CHECK_PORT
30                         + "/v1/status/leader";
31
32         public static Result getResult() {
33                 return result;
34         }
35
36         @Override
37         protected Result check() {
38                 // TODO Auto-generated method stub
39
40                 if (!StringUtils.isBlank(CONSUL_IP_ENV)) {
41                         CHECK_IP = CONSUL_IP_ENV;
42                         CHECK_URL = "http://" + CHECK_IP + ":" + CHECK_PORT
43                                         + "/v1/status/leader";
44
45                         if (LOGGER.isDebugEnabled()) {
46                                 LOGGER.debug("check consul URL:" + CHECK_URL);
47                         }
48
49                         try {
50
51                                 HttpGetResult result = HttpClientUtil
52                                                 .httpGetStatusAndBody(CHECK_URL);
53
54                                 //response format:"127.0.0.1:8300"
55                                 if (result.getStatusCode() == 200 && result.getBody() != null
56                                                 && result.getBody().contains(":8300")) {
57                                         return Result.healthy();
58                                 } else {
59                                         return Result.unhealthy("check consul link " + CHECK_URL
60                                                         + " fail:" + result.getStatusCode()+":"+result.getBody());
61                                 }
62
63                         } catch (Exception e) {
64                                 LOGGER.warn(
65                                                 "ConsulLinkHealthCheck:" + CHECK_URL + " execption", e);
66                                 return Result.unhealthy("check consul link " + CHECK_URL
67                                                 + " exception:{}");
68                         }
69
70                 }
71
72                 return Result.healthy();
73         }
74
75         @Override
76         public void run() {
77                 // TODO Auto-generated method stub
78                 while (true) {
79
80                         if (LOGGER.isDebugEnabled()) {
81                                 LOGGER.debug("consul link check starttime:"
82                                                 + System.currentTimeMillis());
83                         }
84
85                         result = checkWithPolicy();
86
87                         if (LOGGER.isDebugEnabled()) {
88                                 LOGGER.debug("consul link check result:" + result.isHealthy()
89                                                 + " message:" + result.getMessage());
90
91                                 LOGGER.debug("consul link check endtime:"
92                                                 + System.currentTimeMillis());
93                         }
94
95                         try {
96                                 Thread.sleep(normalTimer);
97                         } catch (InterruptedException e) {
98                                 // TODO Auto-generated catch block
99                                 LOGGER.warn("loop check consul,thread sleep excepiton", e);
100                         }
101                 }
102         }
103
104         private Result checkWithPolicy() {
105                 int failedNum = 0;
106                 Result temp = Result.healthy();
107
108                 do {
109                         // check again
110                         temp = check();
111
112                         // healthy break;
113                         if (temp.isHealthy()) {
114                                 break;
115                         }
116
117                         // unhealthy go on
118                         failedNum++;
119                         
120                         try {
121                                 Thread.sleep(failedTimer);
122                         } catch (InterruptedException e) {
123                                 // TODO Auto-generated catch block
124                                 LOGGER.warn("loop check consul,thread sleep excepiton", e);
125                         }
126                         
127                 } while (failedNum <= failedLoopCheckNum);
128
129                 return temp;
130         }
131         
132 }