Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 1 | package net.onrc.onos.registry.controller; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | |
Jonathan Hart | bc4893a | 2013-03-14 22:21:37 -0700 | [diff] [blame^] | 6 | import org.restlet.Response; |
| 7 | import org.restlet.data.Status; |
| 8 | import org.restlet.representation.Representation; |
| 9 | import org.restlet.representation.StringRepresentation; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 10 | import org.restlet.resource.Get; |
| 11 | import org.restlet.resource.ServerResource; |
| 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
| 15 | public class ControllerRegistryResource extends ServerResource { |
| 16 | |
| 17 | protected static Logger log = LoggerFactory.getLogger(ControllerRegistryResource.class); |
| 18 | |
| 19 | @Get("json") |
| 20 | public Collection<String> getControllers() { |
| 21 | IControllerRegistryService registry = |
| 22 | (IControllerRegistryService) getContext().getAttributes(). |
| 23 | get(IControllerRegistryService.class.getCanonicalName()); |
| 24 | |
| 25 | Collection<String> controllers = null; |
| 26 | try { |
| 27 | controllers = registry.getAllControllers(); |
| 28 | } catch (RegistryException e) { |
| 29 | log.warn("Error retrieving controller list: {}", e.getMessage()); |
| 30 | } |
| 31 | |
| 32 | if (controllers == null){ |
| 33 | controllers = new ArrayList<String>(); |
Jonathan Hart | bc4893a | 2013-03-14 22:21:37 -0700 | [diff] [blame^] | 34 | Response response = getResponse(); |
| 35 | response.setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
| 36 | Representation error = new StringRepresentation("Null data returned. Zookeeper connection may be down"); |
| 37 | response.setEntity(error); |
| 38 | return null; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 39 | } |
Jonathan Hart | bc4893a | 2013-03-14 22:21:37 -0700 | [diff] [blame^] | 40 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 41 | return controllers; |
| 42 | } |
| 43 | |
| 44 | } |