HIGUCHI Yuta | ea60e5f | 2013-06-12 11:10:21 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.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 | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 5 | import net.onrc.onos.ofcontroller.bgproute.RibUpdate.Operation; |
| 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 | |
| 14 | public class BgpRouteResource extends ServerResource { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 15 | |
| 16 | protected static Logger log = LoggerFactory.getLogger(BgpRouteResource.class); |
| 17 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 18 | private String addrToString(byte [] addr) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 19 | String str = ""; |
| 20 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 21 | for (int i = 0; i < 4; i++) { |
| 22 | int val = (addr[i] & 0xff); |
| 23 | str += val; |
| 24 | if (i != 3) |
| 25 | str += "."; |
| 26 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 27 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 28 | return str; |
| 29 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 30 | |
| 31 | //@SuppressWarnings("unused") |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 32 | @Get |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 33 | public String get(String fmJson) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 34 | String dest = (String) getRequestAttributes().get("dest"); |
| 35 | String output = ""; |
| 36 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
| 37 | get(IBgpRouteService.class.getCanonicalName()); |
| 38 | |
| 39 | if (dest != null) { |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 40 | //TODO Needs to be changed to use the new RestClient.get(). |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 41 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 42 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 43 | //Prefix p; |
| 44 | //try { |
| 45 | // p = new Prefix(dest, 32); |
| 46 | //} catch (UnknownHostException e) { |
| 47 | //if (p == null) { |
| 48 | // return "[GET]: dest address format is wrong"; |
| 49 | //} |
| 50 | |
| 51 | // the dest here refers to router-id |
| 52 | //bgpdRestIp includes port number, such as 1.1.1.1:8080 |
| 53 | String BGPdRestIp = bgpRoute.getBGPdRestIp(); |
| 54 | String url="http://"+BGPdRestIp+"/wm/bgp/"+dest; |
| 55 | |
| 56 | RestClient.get(url); |
| 57 | |
| 58 | output="Get rib from bgpd finished!\n"; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 59 | return output; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 60 | } |
| 61 | else { |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 62 | //Ptree ptree = bgpRoute.getPtree(); |
| 63 | IPatriciaTrie ptree = bgpRoute.getPtree(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 64 | output += "{\n \"rib\": [\n"; |
| 65 | boolean printed = false; |
| 66 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 67 | /* |
| 68 | for (PtreeNode node = ptree.begin(); node!= null; node = ptree.next(node)) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 69 | if (node.rib == null) { |
| 70 | continue; |
| 71 | } |
| 72 | if (printed == true) { |
| 73 | output += ",\n"; |
| 74 | } |
| 75 | output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", "; |
| 76 | output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}"; |
| 77 | printed = true; |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 78 | }*/ |
| 79 | |
| 80 | synchronized(ptree) { |
| 81 | Iterator<IPatriciaTrie.Entry> it = ptree.iterator(); |
| 82 | while (it.hasNext()) { |
| 83 | IPatriciaTrie.Entry entry = it.next(); |
| 84 | |
| 85 | if (printed == true) { |
| 86 | output += ",\n"; |
| 87 | } |
| 88 | |
| 89 | output += " {\"prefix\": \"" + entry.getPrefix() +"\", "; |
| 90 | output += "\"nexthop\": \"" + entry.getRib().getNextHop().getHostAddress() +"\"}"; |
| 91 | //output += ",\n"; |
| 92 | |
| 93 | printed = true; |
| 94 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 95 | } |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 96 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 97 | //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n"; |
| 98 | output += "\n ]\n}\n"; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 99 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 100 | |
| 101 | return output; |
| 102 | } |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 103 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 104 | //unused? |
| 105 | /* |
| 106 | public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException { |
| 107 | return ByteBuffer.wrap(value.getBytes("UTF-8")); |
| 108 | } |
| 109 | */ |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 110 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 111 | //unused? |
| 112 | /* |
| 113 | public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException { |
| 114 | byte[] bytes = new byte[buffer.remaining()]; |
| 115 | buffer.get(bytes); |
| 116 | return new String(bytes, "UTF-8"); |
| 117 | } |
| 118 | */ |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 119 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 120 | @Post |
| 121 | public String store(String fmJson) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 122 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 123 | get(IBgpRouteService.class.getCanonicalName()); |
| 124 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 125 | //Ptree ptree = bgpRoute.getPtree(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 126 | |
| 127 | String routerId = (String) getRequestAttributes().get("routerid"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 128 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 129 | String mask = (String) getRequestAttributes().get("mask"); |
| 130 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 131 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 132 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 133 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 134 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 135 | if (capability == null) { |
| 136 | // this is a prefix add |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 137 | Prefix p; |
| 138 | try { |
| 139 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 140 | } catch (NumberFormatException e) { |
| 141 | reply = "[POST: mask format is wrong]"; |
| 142 | log.info(reply); |
| 143 | return reply + "\n"; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 144 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 145 | reply = "[POST: prefix format is wrong]"; |
| 146 | log.info(reply); |
| 147 | return reply + "\n"; |
| 148 | } |
| 149 | |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 150 | Rib rib = new Rib(routerId, nexthop, p.getPrefixLength()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 151 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 152 | bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib)); |
| 153 | |
| 154 | /* |
| 155 | PtreeNode node = ptree.acquire(p.getAddress(), p.getPrefixLength()); |
| 156 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 157 | if (node.rib != null) { |
| 158 | node.rib = null; |
| 159 | ptree.delReference(node); |
| 160 | } |
| 161 | node.rib = rib; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 162 | |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 163 | bgpRoute.prefixAdded(node); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 164 | */ |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 165 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 166 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]"; |
| 167 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 168 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 169 | else if(capability.equals("1")) { |
| 170 | reply = "[POST-capability: " + capability + "]\n"; |
| 171 | log.info(reply); |
| 172 | // to store the number in the top node of the Ptree |
| 173 | } |
| 174 | else { |
| 175 | reply = "[POST-capability: " + capability + "]\n"; |
| 176 | log.info(reply); |
| 177 | // to store the number in the top node of the Ptree |
| 178 | } |
| 179 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 180 | return reply + "\n"; |
| 181 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 182 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 183 | @Delete |
| 184 | public String delete(String fmJson) { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 185 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 186 | get(IBgpRouteService.class.getCanonicalName()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 187 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 188 | //Ptree ptree = bgpRoute.getPtree(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 189 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 190 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 191 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 192 | String mask = (String) getRequestAttributes().get("mask"); |
| 193 | String nextHop = (String) getRequestAttributes().get("nexthop"); |
| 194 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 195 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 196 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 197 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 198 | if (capability == null) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 199 | // this is a prefix delete |
| 200 | Prefix p; |
| 201 | try { |
| 202 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 203 | } catch (NumberFormatException e) { |
| 204 | reply = "[DELE: mask format is wrong]"; |
| 205 | log.info(reply); |
| 206 | return reply + "\n"; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 207 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 208 | reply = "[DELE: prefix format is wrong]"; |
| 209 | log.info(reply); |
| 210 | return reply + "\n"; |
| 211 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 212 | |
| 213 | Rib r = new Rib(routerId, nextHop, p.getPrefixLength()); |
| 214 | |
| 215 | bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r)); |
| 216 | |
| 217 | /* |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 218 | PtreeNode node = ptree.lookup(p.getAddress(), p.getPrefixLength()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 219 | |
| 220 | //Remove the flows from the switches before the rib is lost |
| 221 | //Theory: we could get a delete for a prefix not in the Ptree. |
| 222 | //This would result in a null node being returned. We could get a delete for |
| 223 | //a node that's not actually there, but is a aggregate node. This would result |
| 224 | //in a non-null node with a null rib. Only a non-null node with a non-null |
| 225 | //rib is an actual prefix in the Ptree. |
| 226 | if (node != null && node.rib != null){ |
| 227 | bgpRoute.prefixDeleted(node); |
| 228 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 229 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 230 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 231 | |
| 232 | if (node != null && node.rib != null) { |
| 233 | if (r.equals(node.rib)) { |
| 234 | node.rib = null; |
| 235 | ptree.delReference(node); |
| 236 | } |
| 237 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 238 | */ |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 239 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 240 | reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]"; |
| 241 | } |
| 242 | else { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 243 | // clear the local rib: Ptree |
| 244 | bgpRoute.clearPtree(); |
| 245 | reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n"; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 246 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 247 | // to store the number in the top node of the Ptree |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 248 | } |
| 249 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 250 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 251 | return reply + "\n"; |
| 252 | } |
| 253 | } |