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