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