pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1 | package net.floodlightcontroller.bgproute; |
| 2 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 3 | import java.net.UnknownHostException; |
| 4 | |
| 5 | import org.restlet.resource.Delete; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 6 | import org.restlet.resource.Get; |
| 7 | import org.restlet.resource.Post; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 8 | import org.restlet.resource.ServerResource; |
| 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 11 | |
| 12 | public class BgpRouteResource extends ServerResource { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 13 | |
| 14 | protected static Logger log = LoggerFactory.getLogger(BgpRouteResource.class); |
| 15 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 16 | private String addrToString(byte [] addr) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 17 | String str = ""; |
| 18 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 19 | for (int i = 0; i < 4; i++) { |
| 20 | int val = (addr[i] & 0xff); |
| 21 | str += val; |
| 22 | if (i != 3) |
| 23 | str += "."; |
| 24 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 25 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 26 | return str; |
| 27 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 28 | |
| 29 | //@SuppressWarnings("unused") |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 30 | @Get |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 31 | public String get(String fmJson) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 32 | String dest = (String) getRequestAttributes().get("dest"); |
| 33 | String output = ""; |
| 34 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
| 35 | get(IBgpRouteService.class.getCanonicalName()); |
| 36 | |
| 37 | if (dest != null) { |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 38 | //TODO Needs to be changed to use the new RestClient.get(). |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 39 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 40 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 41 | //Prefix p; |
| 42 | //try { |
| 43 | // p = new Prefix(dest, 32); |
| 44 | //} catch (UnknownHostException e) { |
| 45 | //if (p == null) { |
| 46 | // return "[GET]: dest address format is wrong"; |
| 47 | //} |
| 48 | |
| 49 | // the dest here refers to router-id |
| 50 | //bgpdRestIp includes port number, such as 1.1.1.1:8080 |
| 51 | String BGPdRestIp = bgpRoute.getBGPdRestIp(); |
| 52 | String url="http://"+BGPdRestIp+"/wm/bgp/"+dest; |
| 53 | |
| 54 | RestClient.get(url); |
| 55 | |
| 56 | output="Get rib from bgpd finished!\n"; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 57 | return output; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 58 | } |
| 59 | else { |
| 60 | Ptree ptree = bgpRoute.getPtree(); |
| 61 | output += "{\n \"rib\": [\n"; |
| 62 | boolean printed = false; |
| 63 | |
| 64 | for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 65 | if (node.rib == null) { |
| 66 | continue; |
| 67 | } |
| 68 | if (printed == true) { |
| 69 | output += ",\n"; |
| 70 | } |
| 71 | output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", "; |
| 72 | output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}"; |
| 73 | printed = true; |
| 74 | } |
| 75 | //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n"; |
| 76 | output += "\n ]\n}\n"; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 77 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 78 | |
| 79 | return output; |
| 80 | } |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 81 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 82 | //unused? |
| 83 | /* |
| 84 | public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException { |
| 85 | return ByteBuffer.wrap(value.getBytes("UTF-8")); |
| 86 | } |
| 87 | */ |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 88 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 89 | //unused? |
| 90 | /* |
| 91 | public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException { |
| 92 | byte[] bytes = new byte[buffer.remaining()]; |
| 93 | buffer.get(bytes); |
| 94 | return new String(bytes, "UTF-8"); |
| 95 | } |
| 96 | */ |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 97 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 98 | @Post |
| 99 | public String store(String fmJson) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 100 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 101 | get(IBgpRouteService.class.getCanonicalName()); |
| 102 | |
| 103 | Ptree ptree = bgpRoute.getPtree(); |
| 104 | |
| 105 | String routerId = (String) getRequestAttributes().get("routerid"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 106 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 107 | String mask = (String) getRequestAttributes().get("mask"); |
| 108 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 109 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 110 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 111 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 112 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 113 | if (capability == null) { |
| 114 | // this is a prefix add |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 115 | Prefix p; |
| 116 | try { |
| 117 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 118 | } catch (NumberFormatException e) { |
| 119 | reply = "[POST: mask format is wrong]"; |
| 120 | log.info(reply); |
| 121 | return reply + "\n"; |
| 122 | } catch (UnknownHostException e1) { |
| 123 | reply = "[POST: prefix format is wrong]"; |
| 124 | log.info(reply); |
| 125 | return reply + "\n"; |
| 126 | } |
| 127 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 128 | PtreeNode node = ptree.acquire(p.getAddress(), p.masklen); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 129 | Rib rib = new Rib(routerId, nexthop, p.masklen); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 130 | |
| 131 | if (node.rib != null) { |
| 132 | node.rib = null; |
| 133 | ptree.delReference(node); |
| 134 | } |
| 135 | node.rib = rib; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 136 | |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 137 | bgpRoute.prefixAdded(node); |
| 138 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 139 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]"; |
| 140 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 141 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 142 | else if(capability.equals("1")) { |
| 143 | reply = "[POST-capability: " + capability + "]\n"; |
| 144 | log.info(reply); |
| 145 | // to store the number in the top node of the Ptree |
| 146 | } |
| 147 | else { |
| 148 | reply = "[POST-capability: " + capability + "]\n"; |
| 149 | log.info(reply); |
| 150 | // to store the number in the top node of the Ptree |
| 151 | } |
| 152 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 153 | return reply + "\n"; |
| 154 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 155 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 156 | @Delete |
| 157 | public String delete(String fmJson) { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 158 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 159 | get(IBgpRouteService.class.getCanonicalName()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 160 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 161 | Ptree ptree = bgpRoute.getPtree(); |
| 162 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 163 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 164 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 165 | String mask = (String) getRequestAttributes().get("mask"); |
| 166 | String nextHop = (String) getRequestAttributes().get("nexthop"); |
| 167 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 168 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 169 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 170 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 171 | if (capability == null) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 172 | // this is a prefix delete |
| 173 | Prefix p; |
| 174 | try { |
| 175 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 176 | } catch (NumberFormatException e) { |
| 177 | reply = "[DELE: mask format is wrong]"; |
| 178 | log.info(reply); |
| 179 | return reply + "\n"; |
| 180 | } catch (UnknownHostException e1) { |
| 181 | reply = "[DELE: prefix format is wrong]"; |
| 182 | log.info(reply); |
| 183 | return reply + "\n"; |
| 184 | } |
| 185 | |
| 186 | PtreeNode node = ptree.lookup(p.getAddress(), p.masklen); |
| 187 | |
| 188 | Rib r = new Rib(routerId, nextHop, p.masklen); |
| 189 | |
| 190 | if (node != null && node.rib != null) { |
| 191 | if (r.equals(node.rib)) { |
| 192 | node.rib = null; |
| 193 | ptree.delReference(node); |
| 194 | } |
| 195 | } |
| 196 | |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 197 | bgpRoute.prefixDeleted(node); |
| 198 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 199 | reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]"; |
| 200 | } |
| 201 | else { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 202 | // clear the local rib: Ptree |
| 203 | bgpRoute.clearPtree(); |
| 204 | reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n"; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 205 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 206 | // to store the number in the top node of the Ptree |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 207 | } |
| 208 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 209 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 210 | return reply + "\n"; |
| 211 | } |
| 212 | } |