Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 2 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 3 | import org.restlet.resource.Delete; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 4 | import org.restlet.resource.Post; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 5 | import org.restlet.resource.ServerResource; |
| 6 | import org.slf4j.Logger; |
| 7 | import org.slf4j.LoggerFactory; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 8 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame^] | 9 | /** |
| 10 | * REST handler for sending commands to SDN-IP. This interface is intended for |
| 11 | * operators or developers to change the route table of BGPd. There are |
| 12 | * interfaces for both sending new routes and deleting routes. This is |
| 13 | * not intended to be used during general operation. It is to have a way to |
| 14 | * influence BGPd's behavior for debugging. |
| 15 | */ |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 16 | public class BgpRouteResourceSynch extends ServerResource { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 17 | private static final Logger log = LoggerFactory.getLogger(BgpRouteResourceSynch.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame^] | 19 | /** |
| 20 | * Handles a REST call to SDN-IP which gives a command to send a new route |
| 21 | * to BGPd. |
| 22 | * |
| 23 | * @return a String describing the result of the operation |
| 24 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 25 | @Post |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame^] | 26 | public String handlePostMethod() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 27 | |
| 28 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 29 | get(IBgpRouteService.class.getCanonicalName()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 31 | String routerId = (String) getRequestAttributes().get("routerid"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 33 | String mask = (String) getRequestAttributes().get("mask"); |
| 34 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 35 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 36 | String bgpdRestIp = bgpRoute.getBGPdRestIp(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 38 | // bgpdRestIp includes port number, e.g. 1.1.1.1:8080 |
| 39 | RestClient.post("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/" |
| 40 | + mask + "/" + nexthop); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | |
| 42 | String reply = ""; |
| 43 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "/synch]"; |
| 44 | log.info(reply); |
| 45 | |
| 46 | return reply + "\n"; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame^] | 50 | /** |
| 51 | * Handles a REST call to SDN-IP which gives a command to BGPd to delete a |
| 52 | * route from its route table. |
| 53 | * |
| 54 | * @return a String description of the result of the operation |
| 55 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | @Delete |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame^] | 57 | public String handleDeleteMethod() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 59 | get(IBgpRouteService.class.getCanonicalName()); |
| 60 | |
| 61 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 62 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 63 | String mask = (String) getRequestAttributes().get("mask"); |
| 64 | String nextHop = (String) getRequestAttributes().get("nexthop"); |
| 65 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 66 | StringBuilder reply = new StringBuilder(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 68 | String bgpdRestIp = bgpRoute.getBGPdRestIp(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 70 | RestClient.delete("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/" |
| 71 | + mask + "/" + nextHop); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 73 | reply.append("[DELE: ") |
| 74 | .append(prefix) |
| 75 | .append('/') |
| 76 | .append(mask) |
| 77 | .append(':') |
| 78 | .append(nextHop) |
| 79 | .append("/synch]"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 81 | log.info(reply.toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 83 | return reply.append("\n").toString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | } |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 85 | } |