[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / ConsulClientController.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.controller;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.List;\r
24 \r
25 import javax.servlet.http.HttpServletRequest;\r
26 import javax.servlet.http.HttpServletResponse;\r
27 \r
28 import org.springframework.beans.factory.annotation.Autowired;\r
29 import org.springframework.web.bind.annotation.PathVariable;\r
30 import org.springframework.web.bind.annotation.RequestMapping;\r
31 import org.springframework.web.bind.annotation.RequestMethod;\r
32 import org.springframework.web.bind.annotation.RestController;\r
33 \r
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
35 import org.openecomp.portalapp.controller.EPRestrictedBaseController;\r
36 import org.openecomp.portalapp.portal.service.ConsulHealthService;\r
37 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;\r
38 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;\r
39 import com.orbitz.consul.ConsulException;\r
40 import com.orbitz.consul.model.health.ServiceHealth;\r
41 \r
42 import io.searchbox.client.config.exception.NoServerConfiguredException;\r
43 \r
44 @RestController\r
45 @RequestMapping("/portalApi/consul")\r
46 public class ConsulClientController extends EPRestrictedBaseController {\r
47         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulClientController.class);\r
48 \r
49         @Autowired\r
50         private ConsulHealthService consulHealthService;\r
51         //Get location of a healthy node running our service\r
52         @RequestMapping(value = { "/service/{service}" }, method = RequestMethod.GET, produces = "application/json")\r
53         public PortalRestResponse<String> getServiceLocation(HttpServletRequest request, HttpServletResponse response, @PathVariable("service") String service) {\r
54 \r
55                 try{\r
56                         return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "Success!", consulHealthService.getServiceLocation(service));\r
57                 }\r
58                 catch(NoServerConfiguredException e){\r
59                         logger.error(logger.errorLogger, "No healthy service exception!");\r
60                         return new PortalRestResponse<String>(PortalRestStatusEnum.WARN, "Warning!", "No healthy service exception!");\r
61                 }\r
62                 catch(ConsulException e){\r
63                         logger.error(logger.errorLogger, "Couldn't connect ot consul - Is consul running?");\r
64                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "Error!", "Couldn't connect ot consul - Is consul running?");\r
65                 }\r
66         }\r
67 \r
68         @RequestMapping(value = { "/service/healthy/{service}" }, method = RequestMethod.GET, produces = "application/json")\r
69         public PortalRestResponse<List<ServiceHealth>> getAllHealthyNodes(HttpServletRequest request, HttpServletResponse response, @PathVariable("service") String service) {\r
70                 try{\r
71                         return new PortalRestResponse<List<ServiceHealth>>(PortalRestStatusEnum.OK, "Success!", consulHealthService.getAllHealthyNodes(service));\r
72                 }\r
73                 catch(ConsulException e){\r
74                         logger.error(logger.errorLogger, "Couldn't connect to consul - shouldn't break anything.");\r
75                         return new PortalRestResponse<List<ServiceHealth>>(PortalRestStatusEnum.ERROR,"Error!", new ArrayList<>());\r
76                 }\r
77         }\r
78         \r
79         @RequestMapping(value = { "/service/all/{service}" }, method = RequestMethod.GET, produces = "application/json")\r
80         public PortalRestResponse<List<ServiceHealth>> getAllNodes(HttpServletRequest request, HttpServletResponse response, @PathVariable("service") String service) {\r
81                 try{\r
82                         return new PortalRestResponse<List<ServiceHealth>>(PortalRestStatusEnum.OK, "Success!", consulHealthService.getAllNodes(service));\r
83                 }\r
84                 catch(ConsulException e){\r
85                         logger.error(logger.errorLogger, "Couldn't connect to consul - shouldn't break anything.");\r
86                         return new PortalRestResponse<List<ServiceHealth>>(PortalRestStatusEnum.ERROR,"Error!", new ArrayList<>());\r
87                 }\r
88         }\r
89 \r
90 }\r