blob: fa9d57769f191ecc8319a9be49bc3fccb751a57c [file] [log] [blame]
pingping-lina2cbfad2013-03-07 08:39:21 +08001package net.floodlightcontroller.bgproute;
2
Jonathan Hart61ba9372013-05-19 20:10:29 -07003import java.net.UnknownHostException;
4
5import org.restlet.resource.Delete;
pingping-lina2cbfad2013-03-07 08:39:21 +08006import org.restlet.resource.Get;
7import org.restlet.resource.Post;
pingping-lina2cbfad2013-03-07 08:39:21 +08008import org.restlet.resource.ServerResource;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
pingping-lina2cbfad2013-03-07 08:39:21 +080011
12public class BgpRouteResource extends ServerResource {
Jonathan Hart61ba9372013-05-19 20:10:29 -070013
14 protected static Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
15
pingping-lina2cbfad2013-03-07 08:39:21 +080016 private String addrToString(byte [] addr) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070017 String str = "";
18
pingping-lina2cbfad2013-03-07 08:39:21 +080019 for (int i = 0; i < 4; i++) {
20 int val = (addr[i] & 0xff);
21 str += val;
22 if (i != 3)
23 str += ".";
24 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070025
pingping-lina2cbfad2013-03-07 08:39:21 +080026 return str;
27 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070028
29 //@SuppressWarnings("unused")
pingping-line2a09ca2013-03-23 09:33:58 +080030 @Get
Jonathan Hart61ba9372013-05-19 20:10:29 -070031 public String get(String fmJson) {
32 //String linpp=fmJson;
33 String dest = (String) getRequestAttributes().get("dest");
34 String output = "";
35 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
36 get(IBgpRouteService.class.getCanonicalName());
37
38 if (dest != null) {
39 //TODO this code path looks like its never used (except maybe for debugging)
40 //Needs to be changed to use the new RestClient.get().
pingping-line2a09ca2013-03-23 09:33:58 +080041
pingping-line2a09ca2013-03-23 09:33:58 +080042
Jonathan Hart61ba9372013-05-19 20:10:29 -070043 //Prefix p;
44 //try {
45 // p = new Prefix(dest, 32);
46 //} catch (UnknownHostException e) {
47 //if (p == null) {
48 // return "[GET]: dest address format is wrong";
49 //}
50
51 // the dest here refers to router-id
52 //bgpdRestIp includes port number, such as 1.1.1.1:8080
53 String BGPdRestIp = bgpRoute.getBGPdRestIp();
54 String url="http://"+BGPdRestIp+"/wm/bgp/"+dest;
55
56 RestClient.get(url);
57
58 output="Get rib from bgpd finished!\n";
pingping-line2a09ca2013-03-23 09:33:58 +080059 return output;
Jonathan Hart61ba9372013-05-19 20:10:29 -070060 }
61 else {
62 Ptree ptree = bgpRoute.getPtree();
63 output += "{\n \"rib\": [\n";
64 boolean printed = false;
65
66 for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
67 if (node.rib == null) {
68 continue;
69 }
70 if (printed == true) {
71 output += ",\n";
72 }
73 output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
74 output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
75 printed = true;
76 }
77 //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
78 output += "\n ]\n}\n";
pingping-lina2cbfad2013-03-07 08:39:21 +080079 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070080
81 return output;
82 }
pingping-line2a09ca2013-03-23 09:33:58 +080083
Jonathan Hart61ba9372013-05-19 20:10:29 -070084 //unused?
85 /*
86 public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException {
87 return ByteBuffer.wrap(value.getBytes("UTF-8"));
88 }
89 */
pingping-line2a09ca2013-03-23 09:33:58 +080090
Jonathan Hart61ba9372013-05-19 20:10:29 -070091 //unused?
92 /*
93 public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException {
94 byte[] bytes = new byte[buffer.remaining()];
95 buffer.get(bytes);
96 return new String(bytes, "UTF-8");
97 }
98 */
pingping-line2a09ca2013-03-23 09:33:58 +080099
pingping-lina2cbfad2013-03-07 08:39:21 +0800100 @Post
101 public String store(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700102 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
103 get(IBgpRouteService.class.getCanonicalName());
104
105 Ptree ptree = bgpRoute.getPtree();
106
107 String routerId = (String) getRequestAttributes().get("routerid");
pingping-lina2cbfad2013-03-07 08:39:21 +0800108 String prefix = (String) getRequestAttributes().get("prefix");
109 String mask = (String) getRequestAttributes().get("mask");
110 String nexthop = (String) getRequestAttributes().get("nexthop");
111 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700112
pingping-line2a09ca2013-03-23 09:33:58 +0800113 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700114
pingping-lina2cbfad2013-03-07 08:39:21 +0800115 if (capability == null) {
116 // this is a prefix add
Jonathan Hart61ba9372013-05-19 20:10:29 -0700117 Prefix p;
118 try {
119 p = new Prefix(prefix, Integer.valueOf(mask));
120 } catch (NumberFormatException e) {
121 reply = "[POST: mask format is wrong]";
122 log.info(reply);
123 return reply + "\n";
124 } catch (UnknownHostException e1) {
125 reply = "[POST: prefix format is wrong]";
126 log.info(reply);
127 return reply + "\n";
128 }
129
pingping-lina2cbfad2013-03-07 08:39:21 +0800130 PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
Jonathan Hart61ba9372013-05-19 20:10:29 -0700131 Rib rib = new Rib(routerId, nexthop, p.masklen);
pingping-lina2cbfad2013-03-07 08:39:21 +0800132
133 if (node.rib != null) {
134 node.rib = null;
135 ptree.delReference(node);
136 }
137 node.rib = rib;
Jonathan Hart61ba9372013-05-19 20:10:29 -0700138
pingping-lina2cbfad2013-03-07 08:39:21 +0800139 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
140 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800141 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700142 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-lina2cbfad2013-03-07 08:39:21 +0800153 return reply + "\n";
154 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700155
pingping-lina2cbfad2013-03-07 08:39:21 +0800156 @Delete
157 public String delete(String fmJson) {
pingping-line2a09ca2013-03-23 09:33:58 +0800158 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
Jonathan Hart61ba9372013-05-19 20:10:29 -0700159 get(IBgpRouteService.class.getCanonicalName());
pingping-lina2cbfad2013-03-07 08:39:21 +0800160
Jonathan Hart61ba9372013-05-19 20:10:29 -0700161 Ptree ptree = bgpRoute.getPtree();
162
pingping-lina2cbfad2013-03-07 08:39:21 +0800163 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 Hart61ba9372013-05-19 20:10:29 -0700168
pingping-line2a09ca2013-03-23 09:33:58 +0800169 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700170
pingping-lina2cbfad2013-03-07 08:39:21 +0800171 if (capability == null) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700172 // 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
197 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
198 }
199 else {
pingping-line2a09ca2013-03-23 09:33:58 +0800200 // clear the local rib: Ptree
201 bgpRoute.clearPtree();
202 reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700203
pingping-line2a09ca2013-03-23 09:33:58 +0800204 // to store the number in the top node of the Ptree
Jonathan Hart61ba9372013-05-19 20:10:29 -0700205 }
206
pingping-line2a09ca2013-03-23 09:33:58 +0800207 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800208 return reply + "\n";
209 }
210}