blob: f83ad8bab0ebfd1e96b49f8e5b3ddfad2991b4d9 [file] [log] [blame]
Jonathan Hart382623d2014-04-03 09:48:11 -07001package net.onrc.onos.apps.bgproute;
pingping-line2a09ca2013-03-23 09:33:58 +08002
pingping-line2a09ca2013-03-23 09:33:58 +08003import org.restlet.resource.Delete;
Jonathan Harta99ec672014-04-03 11:30:34 -07004import org.restlet.resource.Post;
pingping-line2a09ca2013-03-23 09:33:58 +08005import org.restlet.resource.ServerResource;
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
pingping-line2a09ca2013-03-23 09:33:58 +08008
Jonathan Hart4e7c22e2014-04-09 10:59:34 -07009/**
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-line2a09ca2013-03-23 09:33:58 +080016public class BgpRouteResourceSynch extends ServerResource {
Ray Milkeyec838942014-04-09 11:28:43 -070017 private static final Logger log = LoggerFactory.getLogger(BgpRouteResourceSynch.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070018
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070019 /**
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 Milkey269ffb92014-04-03 14:43:30 -070025 @Post
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070026 public String handlePostMethod() {
Ray Milkey269ffb92014-04-03 14:43:30 -070027
28 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
pingping-line2a09ca2013-03-23 09:33:58 +080029 get(IBgpRouteService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -070030
Jonathan Hart738980f2014-04-04 10:11:15 -070031 String routerId = (String) getRequestAttributes().get("routerid");
Ray Milkey269ffb92014-04-03 14:43:30 -070032 String prefix = (String) getRequestAttributes().get("prefix");
33 String mask = (String) getRequestAttributes().get("mask");
34 String nexthop = (String) getRequestAttributes().get("nexthop");
35
Jonathan Hart738980f2014-04-04 10:11:15 -070036 String bgpdRestIp = bgpRoute.getBGPdRestIp();
Ray Milkey269ffb92014-04-03 14:43:30 -070037
Jonathan Hart738980f2014-04-04 10:11:15 -070038 // bgpdRestIp includes port number, e.g. 1.1.1.1:8080
39 RestClient.post("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/"
40 + mask + "/" + nexthop);
Ray Milkey269ffb92014-04-03 14:43:30 -070041
42 String reply = "";
43 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "/synch]";
44 log.info(reply);
45
46 return reply + "\n";
pingping-line2a09ca2013-03-23 09:33:58 +080047
Ray Milkey269ffb92014-04-03 14:43:30 -070048 }
49
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070050 /**
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 Milkey269ffb92014-04-03 14:43:30 -070056 @Delete
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070057 public String handleDeleteMethod() {
Ray Milkey269ffb92014-04-03 14:43:30 -070058 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 Hart938a0152014-04-07 18:27:31 -070066 StringBuilder reply = new StringBuilder();
Ray Milkey269ffb92014-04-03 14:43:30 -070067
Jonathan Hart738980f2014-04-04 10:11:15 -070068 String bgpdRestIp = bgpRoute.getBGPdRestIp();
Ray Milkey269ffb92014-04-03 14:43:30 -070069
Jonathan Hart738980f2014-04-04 10:11:15 -070070 RestClient.delete("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/"
71 + mask + "/" + nextHop);
Ray Milkey269ffb92014-04-03 14:43:30 -070072
Jonathan Hart938a0152014-04-07 18:27:31 -070073 reply.append("[DELE: ")
74 .append(prefix)
75 .append('/')
76 .append(mask)
77 .append(':')
78 .append(nextHop)
79 .append("/synch]");
Ray Milkey269ffb92014-04-03 14:43:30 -070080
Jonathan Hart938a0152014-04-07 18:27:31 -070081 log.info(reply.toString());
Ray Milkey269ffb92014-04-03 14:43:30 -070082
Jonathan Hart938a0152014-04-07 18:27:31 -070083 return reply.append("\n").toString();
Ray Milkey269ffb92014-04-03 14:43:30 -070084 }
pingping-line2a09ca2013-03-23 09:33:58 +080085}