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