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