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