Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 2 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 3 | import java.util.Iterator; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 4 | |
Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 5 | import net.onrc.onos.apps.bgproute.RibUpdate.Operation; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 6 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 7 | import org.restlet.resource.Delete; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 8 | import org.restlet.resource.Get; |
| 9 | import org.restlet.resource.Post; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 10 | import org.restlet.resource.ServerResource; |
| 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 13 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 14 | /** |
| 15 | * REST resource that handles REST calls from BGPd. This is the interface BGPd |
| 16 | * uses to push RIB entries (routes) to SDN-IP. |
| 17 | */ |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 18 | public class BgpRouteResource extends ServerResource { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 19 | private static final Logger log = LoggerFactory.getLogger(BgpRouteResource.class); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 20 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Gets the contents of SDN-IP's route table. |
| 23 | * |
| 24 | * @return the contents of SDN-IP's route table formatted as JSON |
| 25 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 26 | @Get |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 27 | public String handleGetMethod() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | String dest = (String) getRequestAttributes().get("dest"); |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 29 | StringBuilder output = new StringBuilder(80); |
| 30 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext() |
| 31 | .getAttributes(). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | get(IBgpRouteService.class.getCanonicalName()); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 33 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 34 | if (dest == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree(); |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 36 | output.append("{\n \"rib\": [\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | boolean printed = false; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 38 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 39 | synchronized (ptree) { |
| 40 | Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator(); |
| 41 | while (it.hasNext()) { |
| 42 | IPatriciaTrie.Entry<RibEntry> entry = it.next(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 43 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 44 | if (printed) { |
| 45 | output.append(",\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 47 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 48 | output.append(" {\"prefix\": \""); |
| 49 | output.append(entry.getPrefix()); |
| 50 | output.append("\", \"nexthop\": \""); |
| 51 | output.append(entry.getValue().getNextHop().getHostAddress()); |
| 52 | output.append("\"}"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 53 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | printed = true; |
| 55 | } |
| 56 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 57 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 58 | output.append("\n ]\n}\n"); |
| 59 | } else { |
| 60 | // TODO Needs to be changed to use the new RestClient.get(). |
| 61 | |
| 62 | // the dest here refers to router-id |
| 63 | // bgpdRestIp includes port number, such as 1.1.1.1:8080 |
| 64 | String bgpdRestIp = bgpRoute.getBGPdRestIp(); |
| 65 | String url = "http://" + bgpdRestIp + "/wm/bgp/" + dest; |
| 66 | |
| 67 | // Doesn't actually do anything with the response |
| 68 | RestClient.get(url); |
| 69 | |
| 70 | output.append("Get rib from bgpd finished!\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 72 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 73 | return output.toString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 75 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Handler to receive a new RIB entry. The details of the RIB entry are |
| 78 | * given as part of the URL. |
| 79 | * |
| 80 | * @return a String describing the result of the action |
| 81 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | @Post |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 83 | public String handlePostMethod() { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 84 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext() |
| 85 | .getAttributes(). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | get(IBgpRouteService.class.getCanonicalName()); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 87 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | String strSysuptime = (String) getRequestAttributes().get("sysuptime"); |
| 89 | String strSequence = (String) getRequestAttributes().get("sequence"); |
| 90 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 91 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 92 | String mask = (String) getRequestAttributes().get("mask"); |
| 93 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 94 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | log.debug("sysuptime: {}", strSysuptime); |
| 97 | log.debug("sequence: {}", strSequence); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 98 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | String reply = ""; |
| 100 | |
| 101 | if (capability == null) { |
| 102 | // this is a prefix add |
| 103 | Prefix p; |
| 104 | long sysUpTime, sequenceNum; |
| 105 | try { |
| 106 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 107 | sysUpTime = Long.parseLong(strSysuptime); |
| 108 | sequenceNum = Long.parseLong(strSequence); |
| 109 | } catch (NumberFormatException e) { |
| 110 | reply = "[POST: mask format is wrong]"; |
| 111 | log.info(reply); |
| 112 | return reply + "\n"; |
| 113 | } catch (IllegalArgumentException e1) { |
| 114 | reply = "[POST: prefix format is wrong]"; |
| 115 | log.info(reply); |
| 116 | return reply + "\n"; |
| 117 | } |
| 118 | |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 119 | RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime, |
| 120 | sequenceNum); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | |
| 122 | bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib)); |
| 123 | |
| 124 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]"; |
| 125 | log.info(reply); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 126 | } else { |
| 127 | reply = "[POST-capability: " + capability + "]\n"; |
| 128 | log.info(reply); |
| 129 | // to store the number in the top node of the Ptree |
| 130 | } |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 131 | /*else if ("1".equals(capability)) { |
| 132 | reply = "[POST-capability: " + capability + "]\n"; |
| 133 | log.info(reply); |
| 134 | // to store the number in the top node of the Ptree |
| 135 | }*/ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | |
| 137 | return reply + "\n"; |
| 138 | } |
| 139 | |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Handler to remove a RIB entry from ONOS's route table. The details of |
| 142 | * the route to remove are passed as part of the URL. |
| 143 | * |
| 144 | * @return a String describing the result of the action |
| 145 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | @Delete |
Jonathan Hart | 4e7c22e | 2014-04-09 10:59:34 -0700 | [diff] [blame] | 147 | public String handleDeleteMethod() { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 148 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext() |
| 149 | .getAttributes(). |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | get(IBgpRouteService.class.getCanonicalName()); |
| 151 | |
| 152 | String strSysuptime = (String) getRequestAttributes().get("sysuptime"); |
| 153 | String strSequence = (String) getRequestAttributes().get("sequence"); |
| 154 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 155 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 156 | String mask = (String) getRequestAttributes().get("mask"); |
| 157 | String nextHop = (String) getRequestAttributes().get("nexthop"); |
| 158 | String capability = (String) getRequestAttributes().get("capability"); |
| 159 | |
| 160 | log.debug("sysuptime: {}", strSysuptime); |
| 161 | log.debug("sequence: {}", strSequence); |
| 162 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 163 | //String reply = ""; |
| 164 | StringBuilder replyStringBuilder = new StringBuilder(80); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 165 | |
| 166 | if (capability == null) { |
| 167 | // this is a prefix delete |
| 168 | Prefix p; |
| 169 | long sysUpTime, sequenceNum; |
| 170 | try { |
| 171 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 172 | sysUpTime = Long.parseLong(strSysuptime); |
| 173 | sequenceNum = Long.parseLong(strSequence); |
| 174 | } catch (NumberFormatException e) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 175 | String reply = "[DELE: mask format is wrong]"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | log.info(reply); |
| 177 | return reply + "\n"; |
| 178 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 179 | String reply = "[DELE: prefix format is wrong]"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 180 | log.info(reply); |
| 181 | return reply + "\n"; |
| 182 | } |
| 183 | |
| 184 | RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum); |
| 185 | |
| 186 | bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r)); |
| 187 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 188 | replyStringBuilder.append("[DELE: ") |
| 189 | .append(prefix) |
| 190 | .append('/') |
| 191 | .append(mask) |
| 192 | .append(':') |
| 193 | .append(nextHop) |
| 194 | .append(']'); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | } else { |
| 196 | // clear the local rib: Ptree |
| 197 | bgpRoute.clearPtree(); |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 198 | replyStringBuilder.append("[DELE-capability: ") |
| 199 | .append(capability) |
| 200 | .append("; The local RibEntry is cleared!]\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 201 | |
| 202 | // to store the number in the top node of the Ptree |
| 203 | } |
| 204 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 205 | log.info(replyStringBuilder.toString()); |
| 206 | replyStringBuilder.append('\n'); |
| 207 | return replyStringBuilder.toString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 208 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 209 | } |