blob: 4ad1e9531ab41437ae7899fc7301b207970be3a5 [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 Harte7e1c6e2013-06-04 20:50:23 -070031 public String get(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070032 String dest = (String) getRequestAttributes().get("dest");
33 String output = "";
34 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
35 get(IBgpRouteService.class.getCanonicalName());
36
37 if (dest != null) {
Jonathan Hart50a8d1e2013-06-06 16:00:47 +120038 //TODO Needs to be changed to use the new RestClient.get().
pingping-line2a09ca2013-03-23 09:33:58 +080039
pingping-line2a09ca2013-03-23 09:33:58 +080040
Jonathan Hart61ba9372013-05-19 20:10:29 -070041 //Prefix p;
42 //try {
43 // p = new Prefix(dest, 32);
44 //} catch (UnknownHostException e) {
45 //if (p == null) {
46 // return "[GET]: dest address format is wrong";
47 //}
48
49 // the dest here refers to router-id
50 //bgpdRestIp includes port number, such as 1.1.1.1:8080
51 String BGPdRestIp = bgpRoute.getBGPdRestIp();
52 String url="http://"+BGPdRestIp+"/wm/bgp/"+dest;
53
54 RestClient.get(url);
55
56 output="Get rib from bgpd finished!\n";
pingping-line2a09ca2013-03-23 09:33:58 +080057 return output;
Jonathan Hart61ba9372013-05-19 20:10:29 -070058 }
59 else {
60 Ptree ptree = bgpRoute.getPtree();
61 output += "{\n \"rib\": [\n";
62 boolean printed = false;
63
64 for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
65 if (node.rib == null) {
66 continue;
67 }
68 if (printed == true) {
69 output += ",\n";
70 }
71 output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
72 output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
73 printed = true;
74 }
75 //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
76 output += "\n ]\n}\n";
pingping-lina2cbfad2013-03-07 08:39:21 +080077 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070078
79 return output;
80 }
pingping-line2a09ca2013-03-23 09:33:58 +080081
Jonathan Hart61ba9372013-05-19 20:10:29 -070082 //unused?
83 /*
84 public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException {
85 return ByteBuffer.wrap(value.getBytes("UTF-8"));
86 }
87 */
pingping-line2a09ca2013-03-23 09:33:58 +080088
Jonathan Hart61ba9372013-05-19 20:10:29 -070089 //unused?
90 /*
91 public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException {
92 byte[] bytes = new byte[buffer.remaining()];
93 buffer.get(bytes);
94 return new String(bytes, "UTF-8");
95 }
96 */
pingping-line2a09ca2013-03-23 09:33:58 +080097
pingping-lina2cbfad2013-03-07 08:39:21 +080098 @Post
99 public String store(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700100 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
101 get(IBgpRouteService.class.getCanonicalName());
102
103 Ptree ptree = bgpRoute.getPtree();
104
105 String routerId = (String) getRequestAttributes().get("routerid");
pingping-lina2cbfad2013-03-07 08:39:21 +0800106 String prefix = (String) getRequestAttributes().get("prefix");
107 String mask = (String) getRequestAttributes().get("mask");
108 String nexthop = (String) getRequestAttributes().get("nexthop");
109 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700110
pingping-line2a09ca2013-03-23 09:33:58 +0800111 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700112
pingping-lina2cbfad2013-03-07 08:39:21 +0800113 if (capability == null) {
114 // this is a prefix add
Jonathan Hart61ba9372013-05-19 20:10:29 -0700115 Prefix p;
116 try {
117 p = new Prefix(prefix, Integer.valueOf(mask));
118 } catch (NumberFormatException e) {
119 reply = "[POST: mask format is wrong]";
120 log.info(reply);
121 return reply + "\n";
122 } catch (UnknownHostException e1) {
123 reply = "[POST: prefix format is wrong]";
124 log.info(reply);
125 return reply + "\n";
126 }
127
pingping-lina2cbfad2013-03-07 08:39:21 +0800128 PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
Jonathan Hart61ba9372013-05-19 20:10:29 -0700129 Rib rib = new Rib(routerId, nexthop, p.masklen);
pingping-lina2cbfad2013-03-07 08:39:21 +0800130
131 if (node.rib != null) {
132 node.rib = null;
133 ptree.delReference(node);
134 }
135 node.rib = rib;
Jonathan Hart61ba9372013-05-19 20:10:29 -0700136
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200137 bgpRoute.prefixAdded(node);
138
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
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200197 bgpRoute.prefixDeleted(node);
198
Jonathan Hart61ba9372013-05-19 20:10:29 -0700199 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
200 }
201 else {
pingping-line2a09ca2013-03-23 09:33:58 +0800202 // clear the local rib: Ptree
203 bgpRoute.clearPtree();
204 reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700205
pingping-line2a09ca2013-03-23 09:33:58 +0800206 // to store the number in the top node of the Ptree
Jonathan Hart61ba9372013-05-19 20:10:29 -0700207 }
208
pingping-line2a09ca2013-03-23 09:33:58 +0800209 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800210 return reply + "\n";
211 }
212}