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