blob: f05884365a9a0bdfc43f8042a5ac4549ac661c1a [file] [log] [blame]
HIGUCHI Yutaea60e5f2013-06-12 11:10:21 -07001package net.onrc.onos.ofcontroller.bgproute;
pingping-lina2cbfad2013-03-07 08:39:21 +08002
Jonathan Hartd7e158d2013-08-07 23:04:48 +12003import java.util.Iterator;
Jonathan Hart61ba9372013-05-19 20:10:29 -07004
Jonathan Hart8b9349e2013-07-26 15:55:28 +12005import net.onrc.onos.ofcontroller.bgproute.RibUpdate.Operation;
6
Jonathan Hart61ba9372013-05-19 20:10:29 -07007import org.restlet.resource.Delete;
pingping-lina2cbfad2013-03-07 08:39:21 +08008import org.restlet.resource.Get;
9import org.restlet.resource.Post;
pingping-lina2cbfad2013-03-07 08:39:21 +080010import org.restlet.resource.ServerResource;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
pingping-lina2cbfad2013-03-07 08:39:21 +080013
14public class BgpRouteResource extends ServerResource {
Jonathan Hart61ba9372013-05-19 20:10:29 -070015
16 protected static Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
17
pingping-lina2cbfad2013-03-07 08:39:21 +080018 private String addrToString(byte [] addr) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070019 String str = "";
20
pingping-lina2cbfad2013-03-07 08:39:21 +080021 for (int i = 0; i < 4; i++) {
22 int val = (addr[i] & 0xff);
23 str += val;
24 if (i != 3)
25 str += ".";
26 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070027
pingping-lina2cbfad2013-03-07 08:39:21 +080028 return str;
29 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070030
31 //@SuppressWarnings("unused")
pingping-line2a09ca2013-03-23 09:33:58 +080032 @Get
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070033 public String get(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070034 String dest = (String) getRequestAttributes().get("dest");
35 String output = "";
36 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
37 get(IBgpRouteService.class.getCanonicalName());
38
39 if (dest != null) {
Jonathan Hart50a8d1e2013-06-06 16:00:47 +120040 //TODO 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 {
Jonathan Hartd7e158d2013-08-07 23:04:48 +120062 //Ptree ptree = bgpRoute.getPtree();
Jonathan Hart29b972d2013-08-12 23:43:51 +120063 IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree();
Jonathan Hart61ba9372013-05-19 20:10:29 -070064 output += "{\n \"rib\": [\n";
65 boolean printed = false;
66
Jonathan Hartd7e158d2013-08-07 23:04:48 +120067 /*
68 for (PtreeNode node = ptree.begin(); node!= null; node = ptree.next(node)) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070069 if (node.rib == null) {
70 continue;
71 }
72 if (printed == true) {
73 output += ",\n";
74 }
75 output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
76 output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
77 printed = true;
Jonathan Hartd7e158d2013-08-07 23:04:48 +120078 }*/
79
80 synchronized(ptree) {
Jonathan Hart29b972d2013-08-12 23:43:51 +120081 Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator();
Jonathan Hartd7e158d2013-08-07 23:04:48 +120082 while (it.hasNext()) {
Jonathan Hart29b972d2013-08-12 23:43:51 +120083 IPatriciaTrie.Entry<RibEntry> entry = it.next();
Jonathan Hartd7e158d2013-08-07 23:04:48 +120084
85 if (printed == true) {
86 output += ",\n";
87 }
88
89 output += " {\"prefix\": \"" + entry.getPrefix() +"\", ";
Jonathan Hart29b972d2013-08-12 23:43:51 +120090 output += "\"nexthop\": \"" + entry.getValue().getNextHop().getHostAddress() +"\"}";
Jonathan Hartd7e158d2013-08-07 23:04:48 +120091 //output += ",\n";
92
93 printed = true;
94 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070095 }
Jonathan Hartd7e158d2013-08-07 23:04:48 +120096
Jonathan Hart61ba9372013-05-19 20:10:29 -070097 //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
98 output += "\n ]\n}\n";
pingping-lina2cbfad2013-03-07 08:39:21 +080099 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700100
101 return output;
102 }
pingping-line2a09ca2013-03-23 09:33:58 +0800103
Jonathan Hart61ba9372013-05-19 20:10:29 -0700104 //unused?
105 /*
106 public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException {
107 return ByteBuffer.wrap(value.getBytes("UTF-8"));
108 }
109 */
pingping-line2a09ca2013-03-23 09:33:58 +0800110
Jonathan Hart61ba9372013-05-19 20:10:29 -0700111 //unused?
112 /*
113 public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException {
114 byte[] bytes = new byte[buffer.remaining()];
115 buffer.get(bytes);
116 return new String(bytes, "UTF-8");
117 }
118 */
pingping-line2a09ca2013-03-23 09:33:58 +0800119
pingping-lina2cbfad2013-03-07 08:39:21 +0800120 @Post
121 public String store(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700122 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
123 get(IBgpRouteService.class.getCanonicalName());
124
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200125 //Ptree ptree = bgpRoute.getPtree();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700126
127 String routerId = (String) getRequestAttributes().get("routerid");
pingping-lina2cbfad2013-03-07 08:39:21 +0800128 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");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700132
pingping-line2a09ca2013-03-23 09:33:58 +0800133 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700134
pingping-lina2cbfad2013-03-07 08:39:21 +0800135 if (capability == null) {
136 // this is a prefix add
Jonathan Hart61ba9372013-05-19 20:10:29 -0700137 Prefix p;
138 try {
139 p = new Prefix(prefix, Integer.valueOf(mask));
140 } catch (NumberFormatException e) {
141 reply = "[POST: mask format is wrong]";
142 log.info(reply);
143 return reply + "\n";
Jonathan Hart32e18222013-08-07 22:05:42 +1200144 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700145 reply = "[POST: prefix format is wrong]";
146 log.info(reply);
147 return reply + "\n";
148 }
149
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200150 RibEntry rib = new RibEntry(routerId, nexthop);
pingping-lina2cbfad2013-03-07 08:39:21 +0800151
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200152 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
153
154 /*
155 PtreeNode node = ptree.acquire(p.getAddress(), p.getPrefixLength());
156
pingping-lina2cbfad2013-03-07 08:39:21 +0800157 if (node.rib != null) {
158 node.rib = null;
159 ptree.delReference(node);
160 }
161 node.rib = rib;
Jonathan Hart61ba9372013-05-19 20:10:29 -0700162
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200163 bgpRoute.prefixAdded(node);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200164 */
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200165
pingping-lina2cbfad2013-03-07 08:39:21 +0800166 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
167 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800168 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700169 else if(capability.equals("1")) {
170 reply = "[POST-capability: " + capability + "]\n";
171 log.info(reply);
172 // to store the number in the top node of the Ptree
173 }
174 else {
175 reply = "[POST-capability: " + capability + "]\n";
176 log.info(reply);
177 // to store the number in the top node of the Ptree
178 }
179
pingping-lina2cbfad2013-03-07 08:39:21 +0800180 return reply + "\n";
181 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700182
pingping-lina2cbfad2013-03-07 08:39:21 +0800183 @Delete
184 public String delete(String fmJson) {
pingping-line2a09ca2013-03-23 09:33:58 +0800185 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
Jonathan Hart61ba9372013-05-19 20:10:29 -0700186 get(IBgpRouteService.class.getCanonicalName());
pingping-lina2cbfad2013-03-07 08:39:21 +0800187
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200188 //Ptree ptree = bgpRoute.getPtree();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700189
pingping-lina2cbfad2013-03-07 08:39:21 +0800190 String routerId = (String) getRequestAttributes().get("routerid");
191 String prefix = (String) getRequestAttributes().get("prefix");
192 String mask = (String) getRequestAttributes().get("mask");
193 String nextHop = (String) getRequestAttributes().get("nexthop");
194 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700195
pingping-line2a09ca2013-03-23 09:33:58 +0800196 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700197
pingping-lina2cbfad2013-03-07 08:39:21 +0800198 if (capability == null) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700199 // this is a prefix delete
200 Prefix p;
201 try {
202 p = new Prefix(prefix, Integer.valueOf(mask));
203 } catch (NumberFormatException e) {
204 reply = "[DELE: mask format is wrong]";
205 log.info(reply);
206 return reply + "\n";
Jonathan Hart32e18222013-08-07 22:05:42 +1200207 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700208 reply = "[DELE: prefix format is wrong]";
209 log.info(reply);
210 return reply + "\n";
211 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200212
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200213 RibEntry r = new RibEntry(routerId, nextHop);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200214
215 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
216
217 /*
Jonathan Hartd1b9d872013-07-23 12:17:21 +1200218 PtreeNode node = ptree.lookup(p.getAddress(), p.getPrefixLength());
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200219
220 //Remove the flows from the switches before the rib is lost
221 //Theory: we could get a delete for a prefix not in the Ptree.
222 //This would result in a null node being returned. We could get a delete for
223 //a node that's not actually there, but is a aggregate node. This would result
224 //in a non-null node with a null rib. Only a non-null node with a non-null
225 //rib is an actual prefix in the Ptree.
226 if (node != null && node.rib != null){
227 bgpRoute.prefixDeleted(node);
228 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700229
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200230
Jonathan Hart61ba9372013-05-19 20:10:29 -0700231
232 if (node != null && node.rib != null) {
233 if (r.equals(node.rib)) {
234 node.rib = null;
235 ptree.delReference(node);
236 }
237 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200238 */
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200239
Jonathan Hart61ba9372013-05-19 20:10:29 -0700240 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
241 }
242 else {
pingping-line2a09ca2013-03-23 09:33:58 +0800243 // clear the local rib: Ptree
244 bgpRoute.clearPtree();
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200245 reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700246
pingping-line2a09ca2013-03-23 09:33:58 +0800247 // to store the number in the top node of the Ptree
Jonathan Hart61ba9372013-05-19 20:10:29 -0700248 }
249
pingping-line2a09ca2013-03-23 09:33:58 +0800250 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800251 return reply + "\n";
252 }
253}