blob: 19b44c8e52627cc27c9948d57ec86700be65338d [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 Hart61ba9372013-05-19 20:10:29 -07003import java.net.UnknownHostException;
4
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 {
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
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200105 //Ptree ptree = bgpRoute.getPtree();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700106
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
Jonathan Hartd1b9d872013-07-23 12:17:21 +1200130 Rib rib = new Rib(routerId, nexthop, p.getPrefixLength());
pingping-lina2cbfad2013-03-07 08:39:21 +0800131
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200132 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
133
134 /*
135 PtreeNode node = ptree.acquire(p.getAddress(), p.getPrefixLength());
136
pingping-lina2cbfad2013-03-07 08:39:21 +0800137 if (node.rib != null) {
138 node.rib = null;
139 ptree.delReference(node);
140 }
141 node.rib = rib;
Jonathan Hart61ba9372013-05-19 20:10:29 -0700142
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200143 bgpRoute.prefixAdded(node);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200144 */
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200145
pingping-lina2cbfad2013-03-07 08:39:21 +0800146 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
147 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800148 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700149 else if(capability.equals("1")) {
150 reply = "[POST-capability: " + capability + "]\n";
151 log.info(reply);
152 // to store the number in the top node of the Ptree
153 }
154 else {
155 reply = "[POST-capability: " + capability + "]\n";
156 log.info(reply);
157 // to store the number in the top node of the Ptree
158 }
159
pingping-lina2cbfad2013-03-07 08:39:21 +0800160 return reply + "\n";
161 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700162
pingping-lina2cbfad2013-03-07 08:39:21 +0800163 @Delete
164 public String delete(String fmJson) {
pingping-line2a09ca2013-03-23 09:33:58 +0800165 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
Jonathan Hart61ba9372013-05-19 20:10:29 -0700166 get(IBgpRouteService.class.getCanonicalName());
pingping-lina2cbfad2013-03-07 08:39:21 +0800167
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200168 //Ptree ptree = bgpRoute.getPtree();
Jonathan Hart61ba9372013-05-19 20:10:29 -0700169
pingping-lina2cbfad2013-03-07 08:39:21 +0800170 String routerId = (String) getRequestAttributes().get("routerid");
171 String prefix = (String) getRequestAttributes().get("prefix");
172 String mask = (String) getRequestAttributes().get("mask");
173 String nextHop = (String) getRequestAttributes().get("nexthop");
174 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700175
pingping-line2a09ca2013-03-23 09:33:58 +0800176 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700177
pingping-lina2cbfad2013-03-07 08:39:21 +0800178 if (capability == null) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700179 // this is a prefix delete
180 Prefix p;
181 try {
182 p = new Prefix(prefix, Integer.valueOf(mask));
183 } catch (NumberFormatException e) {
184 reply = "[DELE: mask format is wrong]";
185 log.info(reply);
186 return reply + "\n";
187 } catch (UnknownHostException e1) {
188 reply = "[DELE: prefix format is wrong]";
189 log.info(reply);
190 return reply + "\n";
191 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200192
193 Rib r = new Rib(routerId, nextHop, p.getPrefixLength());
194
195 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
196
197 /*
Jonathan Hartd1b9d872013-07-23 12:17:21 +1200198 PtreeNode node = ptree.lookup(p.getAddress(), p.getPrefixLength());
Jonathan Hart1236a9b2013-06-18 22:10:05 +1200199
200 //Remove the flows from the switches before the rib is lost
201 //Theory: we could get a delete for a prefix not in the Ptree.
202 //This would result in a null node being returned. We could get a delete for
203 //a node that's not actually there, but is a aggregate node. This would result
204 //in a non-null node with a null rib. Only a non-null node with a non-null
205 //rib is an actual prefix in the Ptree.
206 if (node != null && node.rib != null){
207 bgpRoute.prefixDeleted(node);
208 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700209
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200210
Jonathan Hart61ba9372013-05-19 20:10:29 -0700211
212 if (node != null && node.rib != null) {
213 if (r.equals(node.rib)) {
214 node.rib = null;
215 ptree.delReference(node);
216 }
217 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200218 */
Jonathan Hart50a8d1e2013-06-06 16:00:47 +1200219
Jonathan Hart61ba9372013-05-19 20:10:29 -0700220 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
221 }
222 else {
pingping-line2a09ca2013-03-23 09:33:58 +0800223 // clear the local rib: Ptree
224 bgpRoute.clearPtree();
225 reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700226
pingping-line2a09ca2013-03-23 09:33:58 +0800227 // to store the number in the top node of the Ptree
Jonathan Hart61ba9372013-05-19 20:10:29 -0700228 }
229
pingping-line2a09ca2013-03-23 09:33:58 +0800230 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800231 return reply + "\n";
232 }
233}