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 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -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 | |
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 | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 71 | String strSysuptime = (String) getRequestAttributes().get("sysuptime"); |
| 72 | String strSequence = (String) getRequestAttributes().get("sequence"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 73 | String routerId = (String) getRequestAttributes().get("routerid"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 74 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 75 | String mask = (String) getRequestAttributes().get("mask"); |
| 76 | String nexthop = (String) getRequestAttributes().get("nexthop"); |
| 77 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 78 | |
| 79 | log.debug("sysuptime: {}", strSysuptime); |
| 80 | log.debug("sequence: {}", strSequence); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 81 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 82 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 83 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 84 | if (capability == null) { |
| 85 | // this is a prefix add |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 86 | Prefix p; |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 87 | long sysUpTime, sequenceNum; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 88 | try { |
| 89 | p = new Prefix(prefix, Integer.valueOf(mask)); |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 90 | sysUpTime = Long.parseLong(strSysuptime); |
| 91 | sequenceNum = Long.parseLong(strSequence); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 92 | } catch (NumberFormatException e) { |
| 93 | reply = "[POST: mask format is wrong]"; |
| 94 | log.info(reply); |
| 95 | return reply + "\n"; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 96 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 97 | reply = "[POST: prefix format is wrong]"; |
| 98 | log.info(reply); |
| 99 | return reply + "\n"; |
| 100 | } |
| 101 | |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 102 | RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime, sequenceNum); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 103 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 104 | bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib)); |
| 105 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 106 | reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]"; |
| 107 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 108 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 109 | else if(capability.equals("1")) { |
| 110 | reply = "[POST-capability: " + capability + "]\n"; |
| 111 | log.info(reply); |
| 112 | // to store the number in the top node of the Ptree |
| 113 | } |
| 114 | else { |
| 115 | reply = "[POST-capability: " + capability + "]\n"; |
| 116 | log.info(reply); |
| 117 | // to store the number in the top node of the Ptree |
| 118 | } |
| 119 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 120 | return reply + "\n"; |
| 121 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 122 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 123 | @Delete |
| 124 | public String delete(String fmJson) { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 125 | IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes(). |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 126 | get(IBgpRouteService.class.getCanonicalName()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 127 | |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 128 | String strSysuptime = (String) getRequestAttributes().get("sysuptime"); |
| 129 | String strSequence = (String) getRequestAttributes().get("sequence"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 130 | String routerId = (String) getRequestAttributes().get("routerid"); |
| 131 | String prefix = (String) getRequestAttributes().get("prefix"); |
| 132 | String mask = (String) getRequestAttributes().get("mask"); |
| 133 | String nextHop = (String) getRequestAttributes().get("nexthop"); |
| 134 | String capability = (String) getRequestAttributes().get("capability"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 135 | |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 136 | log.debug("sysuptime: {}", strSysuptime); |
| 137 | log.debug("sequence: {}", strSequence); |
| 138 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 139 | String reply = ""; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 140 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 141 | if (capability == null) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 142 | // this is a prefix delete |
| 143 | Prefix p; |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 144 | long sysUpTime, sequenceNum; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 145 | try { |
| 146 | p = new Prefix(prefix, Integer.valueOf(mask)); |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 147 | sysUpTime = Long.parseLong(strSysuptime); |
| 148 | sequenceNum = Long.parseLong(strSequence); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 149 | } catch (NumberFormatException e) { |
| 150 | reply = "[DELE: mask format is wrong]"; |
| 151 | log.info(reply); |
| 152 | return reply + "\n"; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 153 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 154 | reply = "[DELE: prefix format is wrong]"; |
| 155 | log.info(reply); |
| 156 | return reply + "\n"; |
| 157 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 158 | |
Jonathan Hart | e4c9869 | 2013-10-18 17:40:03 -0700 | [diff] [blame] | 159 | RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 160 | |
| 161 | bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r)); |
| 162 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 163 | reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]"; |
| 164 | } |
| 165 | else { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 166 | // clear the local rib: Ptree |
| 167 | bgpRoute.clearPtree(); |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 168 | reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n"; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 169 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 170 | // to store the number in the top node of the Ptree |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 171 | } |
| 172 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 173 | log.info(reply); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 174 | return reply + "\n"; |
| 175 | } |
| 176 | } |