[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / ConsulHealthServiceImpl.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.service;\r
21 \r
22 import java.util.List;\r
23 \r
24 import org.springframework.stereotype.Component;\r
25 \r
26 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
27 import org.openecomp.portalsdk.core.util.SystemProperties;\r
28 import com.ecwid.consul.ConsulException;\r
29 import com.orbitz.consul.Consul;\r
30 import com.orbitz.consul.HealthClient;\r
31 import com.orbitz.consul.model.health.ServiceHealth;\r
32 \r
33 @Component\r
34 public class ConsulHealthServiceImpl implements ConsulHealthService {\r
35         \r
36         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulHealthServiceImpl.class);\r
37 \r
38         @Override\r
39         public String getServiceLocation(String service){\r
40 \r
41                 List<ServiceHealth> nodes = null;\r
42                 \r
43                 try{\r
44                         Consul consul = Consul.builder().build();\r
45                         HealthClient healthClient = consul.healthClient();\r
46                         nodes = healthClient.getHealthyServiceInstances(service).getResponse();\r
47                 }\r
48                 catch(Exception e){\r
49                         //using both loggers.\r
50                         logger.debug(logger.debugLogger, " problem getting nodes for service - " + service + e.getMessage() + " - Defaulting to localhost", e);\r
51                         logger.error(logger.errorLogger, " problem getting nodes for service - " + service + e.getMessage() + " - Defaulting to localhost", e);\r
52 \r
53                         return "localhost:" + SystemProperties.getProperty("microservices.widget.local.port");\r
54                 }\r
55                 \r
56                 if(nodes == null || nodes.size() == 0){\r
57                         logger.debug(logger.debugLogger, "No healthy node found in the consul cluster running service " + service + ". Defaulting to localhost");\r
58                         return "localhost:" + SystemProperties.getProperty("microservices.widget.local.port");\r
59                 }\r
60                 else{\r
61                         String locationFromConsul;\r
62                         ServiceHealth node = nodes.get(0);\r
63                         locationFromConsul = node.getNode().getNode()  + ":" + node.getService().getPort();\r
64                         logger.debug(logger.debugLogger, "Found healthy service location using consul - returning location " + locationFromConsul);\r
65                         \r
66                         //if locationFromConsul is null for some reason (very unlikely at this point), default to localhost             \r
67                         if(null == locationFromConsul || "".equals(locationFromConsul)){\r
68                                 logger.debug(logger.debugLogger, "Couldn't get location from consul for service " + service + ". Defaulting to localhost");\r
69                                 return "localhost:" + SystemProperties.getProperty("microservices.widget.local.port");\r
70                         }\r
71                         else{\r
72                                 logger.debug(logger.debugLogger, "Found service location from consul for service " + service + ". Location is " + locationFromConsul);\r
73                                 return locationFromConsul;\r
74                         }                       \r
75                 }               \r
76         }\r
77 \r
78         @Override\r
79         public List<ServiceHealth> getAllHealthyNodes(String service) throws ConsulException{\r
80                 Consul consul = Consul.builder().build();\r
81                 HealthClient healthClient = consul.healthClient();\r
82                 return healthClient.getHealthyServiceInstances(service).getResponse();\r
83         }\r
84 \r
85         @Override\r
86         public List<ServiceHealth> getAllNodes(String service){\r
87                 Consul consul = Consul.builder().build();\r
88                 HealthClient healthClient = consul.healthClient();\r
89                 return healthClient.getAllServiceInstances(service).getResponse();\r
90         }\r
91 }\r