Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.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 | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 5 | import net.onrc.onos.apps.bgproute.RibUpdate.Operation; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 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 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 16 | protected final static Logger log = LoggerFactory.getLogger(BgpRouteResource.class); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 18 | @Get |
| 19 | public String get(String fmJson) { |
| 20 | String dest = (String) getRequestAttributes().get("dest"); |
| 21 | String output = ""; |
| 22 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 23 | get(IBgpRouteService.class.getCanonicalName()); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 24 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 25 | if (dest != null) { |
| 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 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 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; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 32 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 33 | //Doesn't actually do anything with the response |
| 34 | RestClient.get(url); |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 35 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 36 | output = "Get rib from bgpd finished!\n"; |
| 37 | return output; |
| 38 | } else { |
| 39 | IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree(); |
| 40 | output += "{\n \"rib\": [\n"; |
| 41 | boolean printed = false; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 42 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 43 | synchronized (ptree) { |
| 44 | Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator(); |
| 45 | while (it.hasNext()) { |
| 46 | IPatriciaTrie.Entry<RibEntry> entry = it.next(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 47 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 48 | if (printed == true) { |
| 49 | output += ",\n"; |
| 50 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 52 | output += " {\"prefix\": \"" + entry.getPrefix() + "\", "; |
| 53 | output += "\"nexthop\": \"" + entry.getValue().getNextHop().getHostAddress() + "\"}"; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 54 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 55 | printed = true; |
| 56 | } |
| 57 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 58 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 59 | output += "\n ]\n}\n"; |
| 60 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 62 | return output; |
| 63 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 64 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 65 | @Post |
| 66 | public String store(String fmJson) { |
| 67 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 68 | get(IBgpRouteService.class.getCanonicalName()); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 69 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 70 | String strSysuptime = (String) getRequestAttributes().get("sysuptime"); |
| 71 | String strSequence = (String) getRequestAttributes().get("sequence"); |
| 72 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 73 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 74 | String mask = (String) getRequestAttributes().get("mask"); |
| 75 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 76 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 77 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 78 | log.debug("sysuptime: {}", strSysuptime); |
| 79 | log.debug("sequence: {}", strSequence); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 80 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame^] | 81 | String reply = ""; |
| 82 | |
| 83 | if (capability == null) { |
| 84 | // this is a prefix add |
| 85 | Prefix p; |
| 86 | long sysUpTime, sequenceNum; |
| 87 | try { |
| 88 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 89 | sysUpTime = Long.parseLong(strSysuptime); |
| 90 | sequenceNum = Long.parseLong(strSequence); |
| 91 | } catch (NumberFormatException e) { |
| 92 | reply = "[POST: mask format is wrong]"; |
| 93 | log.info(reply); |
| 94 | return reply + "\n"; |
| 95 | } catch (IllegalArgumentException e1) { |
| 96 | reply = "[POST: prefix format is wrong]"; |
| 97 | log.info(reply); |
| 98 | return reply + "\n"; |
| 99 | } |
| 100 | |
| 101 | RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime, sequenceNum); |
| 102 | |
| 103 | bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib)); |
| 104 | |
| 105 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]"; |
| 106 | log.info(reply); |
| 107 | } else if (capability.equals("1")) { |
| 108 | reply = "[POST-capability: " + capability + "]\n"; |
| 109 | log.info(reply); |
| 110 | // to store the number in the top node of the Ptree |
| 111 | } else { |
| 112 | reply = "[POST-capability: " + capability + "]\n"; |
| 113 | log.info(reply); |
| 114 | // to store the number in the top node of the Ptree |
| 115 | } |
| 116 | |
| 117 | return reply + "\n"; |
| 118 | } |
| 119 | |
| 120 | @Delete |
| 121 | public String delete(String fmJson) { |
| 122 | IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes(). |
| 123 | get(IBgpRouteService.class.getCanonicalName()); |
| 124 | |
| 125 | String strSysuptime = (String) getRequestAttributes().get("sysuptime"); |
| 126 | String strSequence = (String) getRequestAttributes().get("sequence"); |
| 127 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 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"); |
| 132 | |
| 133 | log.debug("sysuptime: {}", strSysuptime); |
| 134 | log.debug("sequence: {}", strSequence); |
| 135 | |
| 136 | String reply = ""; |
| 137 | |
| 138 | if (capability == null) { |
| 139 | // this is a prefix delete |
| 140 | Prefix p; |
| 141 | long sysUpTime, sequenceNum; |
| 142 | try { |
| 143 | p = new Prefix(prefix, Integer.valueOf(mask)); |
| 144 | sysUpTime = Long.parseLong(strSysuptime); |
| 145 | sequenceNum = Long.parseLong(strSequence); |
| 146 | } catch (NumberFormatException e) { |
| 147 | reply = "[DELE: mask format is wrong]"; |
| 148 | log.info(reply); |
| 149 | return reply + "\n"; |
| 150 | } catch (IllegalArgumentException e1) { |
| 151 | reply = "[DELE: prefix format is wrong]"; |
| 152 | log.info(reply); |
| 153 | return reply + "\n"; |
| 154 | } |
| 155 | |
| 156 | RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum); |
| 157 | |
| 158 | bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r)); |
| 159 | |
| 160 | reply = reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]"; |
| 161 | } else { |
| 162 | // clear the local rib: Ptree |
| 163 | bgpRoute.clearPtree(); |
| 164 | reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n"; |
| 165 | |
| 166 | // to store the number in the top node of the Ptree |
| 167 | } |
| 168 | |
| 169 | log.info(reply); |
| 170 | return reply + "\n"; |
| 171 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 172 | } |