blob: 772bf7d6c4575815cbd4b36a89ac53725549a5e5 [file] [log] [blame]
Jonathan Hart8f6dc092014-04-18 15:56:43 -07001package net.onrc.onos.apps.sdnip.web;
2
3import net.onrc.onos.apps.sdnip.ISdnIpService;
4import net.onrc.onos.apps.sdnip.RestClient;
pingping-line2a09ca2013-03-23 09:33:58 +08005
pingping-line2a09ca2013-03-23 09:33:58 +08006import org.restlet.resource.Delete;
Jonathan Harta99ec672014-04-03 11:30:34 -07007import org.restlet.resource.Post;
pingping-line2a09ca2013-03-23 09:33:58 +08008import org.restlet.resource.ServerResource;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
pingping-line2a09ca2013-03-23 09:33:58 +080011
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070012/**
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 Hart8f6dc092014-04-18 15:56:43 -070019public class OutgoingRequestResource extends ServerResource {
20 private static final Logger log = LoggerFactory.getLogger(OutgoingRequestResource.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070021
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070022 /**
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 Milkey269ffb92014-04-03 14:43:30 -070028 @Post
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070029 public String handlePostMethod() {
Ray Milkey269ffb92014-04-03 14:43:30 -070030
Jonathan Hart8f6dc092014-04-18 15:56:43 -070031 ISdnIpService sdnIp = (ISdnIpService) getContext().getAttributes().
32 get(ISdnIpService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -070033
Jonathan Hart738980f2014-04-04 10:11:15 -070034 String routerId = (String) getRequestAttributes().get("routerid");
Ray Milkey269ffb92014-04-03 14:43:30 -070035 String prefix = (String) getRequestAttributes().get("prefix");
36 String mask = (String) getRequestAttributes().get("mask");
37 String nexthop = (String) getRequestAttributes().get("nexthop");
38
Jonathan Hart8f6dc092014-04-18 15:56:43 -070039 String bgpdRestIp = sdnIp.getBgpdRestIp();
Ray Milkey269ffb92014-04-03 14:43:30 -070040
Jonathan Hart738980f2014-04-04 10:11:15 -070041 // bgpdRestIp includes port number, e.g. 1.1.1.1:8080
42 RestClient.post("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/"
43 + mask + "/" + nexthop);
Ray Milkey269ffb92014-04-03 14:43:30 -070044
45 String reply = "";
46 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "/synch]";
47 log.info(reply);
48
49 return reply + "\n";
pingping-line2a09ca2013-03-23 09:33:58 +080050
Ray Milkey269ffb92014-04-03 14:43:30 -070051 }
52
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070053 /**
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 Milkey269ffb92014-04-03 14:43:30 -070059 @Delete
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070060 public String handleDeleteMethod() {
Jonathan Hart8f6dc092014-04-18 15:56:43 -070061 ISdnIpService sdnIp = (ISdnIpService) getContext().getAttributes().
62 get(ISdnIpService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -070063
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 Hart938a0152014-04-07 18:27:31 -070069 StringBuilder reply = new StringBuilder();
Ray Milkey269ffb92014-04-03 14:43:30 -070070
Jonathan Hart8f6dc092014-04-18 15:56:43 -070071 String bgpdRestIp = sdnIp.getBgpdRestIp();
Ray Milkey269ffb92014-04-03 14:43:30 -070072
Jonathan Hart738980f2014-04-04 10:11:15 -070073 RestClient.delete("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/"
74 + mask + "/" + nextHop);
Ray Milkey269ffb92014-04-03 14:43:30 -070075
Jonathan Hart938a0152014-04-07 18:27:31 -070076 reply.append("[DELE: ")
77 .append(prefix)
78 .append('/')
79 .append(mask)
80 .append(':')
81 .append(nextHop)
82 .append("/synch]");
Ray Milkey269ffb92014-04-03 14:43:30 -070083
Jonathan Hart938a0152014-04-07 18:27:31 -070084 log.info(reply.toString());
Ray Milkey269ffb92014-04-03 14:43:30 -070085
Jonathan Hart938a0152014-04-07 18:27:31 -070086 return reply.append("\n").toString();
Ray Milkey269ffb92014-04-03 14:43:30 -070087 }
pingping-line2a09ca2013-03-23 09:33:58 +080088}