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 | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 18 | @Get |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 19 | public String get(String fmJson) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 20 | String dest = (String) getRequestAttributes().get("dest"); |
| 21 | String output = ""; |
| 22 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
| 23 | get(IBgpRouteService.class.getCanonicalName()); |
| 24 | |
| 25 | if (dest != null) { |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 26 | //TODO Needs to be changed to use the new RestClient.get(). |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 27 | |
| 28 | // the dest here refers to router-id |
| 29 | //bgpdRestIp includes port number, such as 1.1.1.1:8080 |
| 30 | String BGPdRestIp = bgpRoute.getBGPdRestIp(); |
| 31 | String url="http://"+BGPdRestIp+"/wm/bgp/"+dest; |
| 32 | |
Jonathan Hart | 5b803bc | 2013-09-23 14:46:11 +1200 | [diff] [blame] | 33 | //Doesn't actually do anything with the response |
| 34 | RestClient.get(url); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 35 | |
| 36 | output="Get rib from bgpd finished!\n"; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 37 | return output; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 38 | } |
| 39 | else { |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 40 | IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 41 | output += "{\n \"rib\": [\n"; |
| 42 | boolean printed = false; |
| 43 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 44 | synchronized(ptree) { |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 45 | Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator(); |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 46 | while (it.hasNext()) { |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 47 | IPatriciaTrie.Entry<RibEntry> entry = it.next(); |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 48 | |
| 49 | if (printed == true) { |
| 50 | output += ",\n"; |
| 51 | } |
| 52 | |
| 53 | output += " {\"prefix\": \"" + entry.getPrefix() +"\", "; |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 54 | output += "\"nexthop\": \"" + entry.getValue().getNextHop().getHostAddress() +"\"}"; |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 55 | |
| 56 | printed = true; |
| 57 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 58 | } |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 59 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 60 | output += "\n ]\n}\n"; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 61 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 62 | |
| 63 | return output; |
| 64 | } |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 65 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 66 | @Post |
| 67 | public String store(String fmJson) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 68 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 69 | get(IBgpRouteService.class.getCanonicalName()); |
| 70 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 71 | String routerId = (String) getRequestAttributes().get("routerid"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 72 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 73 | String mask = (String) getRequestAttributes().get("mask"); |
| 74 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 75 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 76 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 77 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 78 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 79 | if (capability == null) { |
| 80 | // this is a prefix add |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 81 | Prefix p; |
| 82 | try { |
| 83 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 84 | } catch (NumberFormatException e) { |
| 85 | reply = "[POST: mask format is wrong]"; |
| 86 | log.info(reply); |
| 87 | return reply + "\n"; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 88 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 89 | reply = "[POST: prefix format is wrong]"; |
| 90 | log.info(reply); |
| 91 | return reply + "\n"; |
| 92 | } |
| 93 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 94 | RibEntry rib = new RibEntry(routerId, nexthop); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 95 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 96 | bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib)); |
| 97 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 98 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]"; |
| 99 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 100 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 101 | else if(capability.equals("1")) { |
| 102 | reply = "[POST-capability: " + capability + "]\n"; |
| 103 | log.info(reply); |
| 104 | // to store the number in the top node of the Ptree |
| 105 | } |
| 106 | else { |
| 107 | reply = "[POST-capability: " + capability + "]\n"; |
| 108 | log.info(reply); |
| 109 | // to store the number in the top node of the Ptree |
| 110 | } |
| 111 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 112 | return reply + "\n"; |
| 113 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 114 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 115 | @Delete |
| 116 | public String delete(String fmJson) { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 117 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 118 | get(IBgpRouteService.class.getCanonicalName()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 119 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 120 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 121 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 122 | String mask = (String) getRequestAttributes().get("mask"); |
| 123 | String nextHop = (String) getRequestAttributes().get("nexthop"); |
| 124 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 125 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 126 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 127 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 128 | if (capability == null) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 129 | // this is a prefix delete |
| 130 | Prefix p; |
| 131 | try { |
| 132 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 133 | } catch (NumberFormatException e) { |
| 134 | reply = "[DELE: mask format is wrong]"; |
| 135 | log.info(reply); |
| 136 | return reply + "\n"; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 137 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 138 | reply = "[DELE: prefix format is wrong]"; |
| 139 | log.info(reply); |
| 140 | return reply + "\n"; |
| 141 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 142 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 143 | RibEntry r = new RibEntry(routerId, nextHop); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 144 | |
| 145 | bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r)); |
| 146 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 147 | reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]"; |
| 148 | } |
| 149 | else { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 150 | // clear the local rib: Ptree |
| 151 | bgpRoute.clearPtree(); |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 152 | reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n"; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 153 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 154 | // to store the number in the top node of the Ptree |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 155 | } |
| 156 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 157 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 158 | return reply + "\n"; |
| 159 | } |
| 160 | } |