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