inherit from oparent
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / health / ConsulLinkHealthCheck.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.msb.sdclient.health;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.onap.msb.sdclient.wrapper.util.HttpClientUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.codahale.metrics.health.HealthCheck;
24
25 public class ConsulLinkHealthCheck extends HealthCheck {
26
27
28         private static final Logger LOGGER = LoggerFactory
29                         .getLogger(ConsulLinkHealthCheck.class);
30         
31         private String CHECK_IP="127.0.0.1";
32         private String CHECK_PORT="8500";
33         private String CHECK_URL = "http://"+CHECK_IP+":"+CHECK_PORT+"/v1/status/leader";
34
35
36         @Override
37         protected Result check() throws Exception {
38                 // TODO Auto-generated method stub
39                 
40                 if(!StringUtils.isBlank(System.getenv("CONSUL_IP")))
41                 {
42                         CHECK_IP=System.getenv("CONSUL_IP");
43                         CHECK_URL = "http://"+CHECK_IP+":"+CHECK_PORT+"/v1/status/leader";
44                         LOGGER.info("check consul URL:"+CHECK_URL);
45                 }
46                 
47                 int resultStatus = HttpClientUtil.httpGetStatus(CHECK_URL);
48
49                 if (resultStatus == 200) {
50                         return Result.healthy();
51                 } else {
52                         return Result
53                                         .unhealthy("check consul link {} fail:{}",CHECK_URL,resultStatus);
54                 }
55
56         }
57
58 }