blob: c1f0cd6959dc2c01d822db93110317d22d91396c [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-line2a09ca2013-03-23 09:33:58 +080018 @Get
Jonathan Harte7e1c6e2013-06-04 20:50:23 -070019 public String get(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070020 String dest = (String) getRequestAttributes().get("dest");
21 String output = "";
22 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
23 get(IBgpRouteService.class.getCanonicalName());
24
25 if (dest != null) {
Jonathan Hart50a8d1e2013-06-06 16:00:47 +120026 //TODO Needs to be changed to use the new RestClient.get().
Jonathan Hart61ba9372013-05-19 20:10:29 -070027
28 // the dest here refers to router-id
29 //bgpdRestIp includes port number, such as 1.1.1.1:8080
30 String BGPdRestIp = bgpRoute.getBGPdRestIp();
31 String url="http://"+BGPdRestIp+"/wm/bgp/"+dest;
32
Jonathan Hart5b803bc2013-09-23 14:46:11 +120033 //Doesn't actually do anything with the response
34 RestClient.get(url);
Jonathan Hart61ba9372013-05-19 20:10:29 -070035
36 output="Get rib from bgpd finished!\n";
pingping-line2a09ca2013-03-23 09:33:58 +080037 return output;
Jonathan Hart61ba9372013-05-19 20:10:29 -070038 }
39 else {
Jonathan Hart29b972d2013-08-12 23:43:51 +120040 IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree();
Jonathan Hart61ba9372013-05-19 20:10:29 -070041 output += "{\n \"rib\": [\n";
42 boolean printed = false;
43
Jonathan Hartd7e158d2013-08-07 23:04:48 +120044 synchronized(ptree) {
Jonathan Hart29b972d2013-08-12 23:43:51 +120045 Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator();
Jonathan Hartd7e158d2013-08-07 23:04:48 +120046 while (it.hasNext()) {
Jonathan Hart29b972d2013-08-12 23:43:51 +120047 IPatriciaTrie.Entry<RibEntry> entry = it.next();
Jonathan Hartd7e158d2013-08-07 23:04:48 +120048
49 if (printed == true) {
50 output += ",\n";
51 }
52
53 output += " {\"prefix\": \"" + entry.getPrefix() +"\", ";
Jonathan Hart29b972d2013-08-12 23:43:51 +120054 output += "\"nexthop\": \"" + entry.getValue().getNextHop().getHostAddress() +"\"}";
Jonathan Hartd7e158d2013-08-07 23:04:48 +120055
56 printed = true;
57 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070058 }
Jonathan Hartd7e158d2013-08-07 23:04:48 +120059
Jonathan Hart61ba9372013-05-19 20:10:29 -070060 output += "\n ]\n}\n";
pingping-lina2cbfad2013-03-07 08:39:21 +080061 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070062
63 return output;
64 }
pingping-line2a09ca2013-03-23 09:33:58 +080065
pingping-lina2cbfad2013-03-07 08:39:21 +080066 @Post
67 public String store(String fmJson) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070068 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
69 get(IBgpRouteService.class.getCanonicalName());
70
Jonathan Harte4c98692013-10-18 17:40:03 -070071 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
72 String strSequence = (String) getRequestAttributes().get("sequence");
Jonathan Hart61ba9372013-05-19 20:10:29 -070073 String routerId = (String) getRequestAttributes().get("routerid");
pingping-lina2cbfad2013-03-07 08:39:21 +080074 String prefix = (String) getRequestAttributes().get("prefix");
75 String mask = (String) getRequestAttributes().get("mask");
76 String nexthop = (String) getRequestAttributes().get("nexthop");
77 String capability = (String) getRequestAttributes().get("capability");
Jonathan Harte4c98692013-10-18 17:40:03 -070078
79 log.debug("sysuptime: {}", strSysuptime);
80 log.debug("sequence: {}", strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -070081
pingping-line2a09ca2013-03-23 09:33:58 +080082 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -070083
pingping-lina2cbfad2013-03-07 08:39:21 +080084 if (capability == null) {
85 // this is a prefix add
Jonathan Hart61ba9372013-05-19 20:10:29 -070086 Prefix p;
Jonathan Harte4c98692013-10-18 17:40:03 -070087 long sysUpTime, sequenceNum;
Jonathan Hart61ba9372013-05-19 20:10:29 -070088 try {
89 p = new Prefix(prefix, Integer.valueOf(mask));
Jonathan Harte4c98692013-10-18 17:40:03 -070090 sysUpTime = Long.parseLong(strSysuptime);
91 sequenceNum = Long.parseLong(strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -070092 } catch (NumberFormatException e) {
93 reply = "[POST: mask format is wrong]";
94 log.info(reply);
95 return reply + "\n";
Jonathan Hart32e18222013-08-07 22:05:42 +120096 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070097 reply = "[POST: prefix format is wrong]";
98 log.info(reply);
99 return reply + "\n";
100 }
101
Jonathan Harte4c98692013-10-18 17:40:03 -0700102 RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime, sequenceNum);
pingping-lina2cbfad2013-03-07 08:39:21 +0800103
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200104 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
105
pingping-lina2cbfad2013-03-07 08:39:21 +0800106 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
107 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800108 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700109 else if(capability.equals("1")) {
110 reply = "[POST-capability: " + capability + "]\n";
111 log.info(reply);
112 // to store the number in the top node of the Ptree
113 }
114 else {
115 reply = "[POST-capability: " + capability + "]\n";
116 log.info(reply);
117 // to store the number in the top node of the Ptree
118 }
119
pingping-lina2cbfad2013-03-07 08:39:21 +0800120 return reply + "\n";
121 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700122
pingping-lina2cbfad2013-03-07 08:39:21 +0800123 @Delete
124 public String delete(String fmJson) {
pingping-line2a09ca2013-03-23 09:33:58 +0800125 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
Jonathan Hart61ba9372013-05-19 20:10:29 -0700126 get(IBgpRouteService.class.getCanonicalName());
pingping-lina2cbfad2013-03-07 08:39:21 +0800127
Jonathan Harte4c98692013-10-18 17:40:03 -0700128 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
129 String strSequence = (String) getRequestAttributes().get("sequence");
pingping-lina2cbfad2013-03-07 08:39:21 +0800130 String routerId = (String) getRequestAttributes().get("routerid");
131 String prefix = (String) getRequestAttributes().get("prefix");
132 String mask = (String) getRequestAttributes().get("mask");
133 String nextHop = (String) getRequestAttributes().get("nexthop");
134 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700135
Jonathan Harte4c98692013-10-18 17:40:03 -0700136 log.debug("sysuptime: {}", strSysuptime);
137 log.debug("sequence: {}", strSequence);
138
pingping-line2a09ca2013-03-23 09:33:58 +0800139 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700140
pingping-lina2cbfad2013-03-07 08:39:21 +0800141 if (capability == null) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700142 // this is a prefix delete
143 Prefix p;
Jonathan Harte4c98692013-10-18 17:40:03 -0700144 long sysUpTime, sequenceNum;
Jonathan Hart61ba9372013-05-19 20:10:29 -0700145 try {
146 p = new Prefix(prefix, Integer.valueOf(mask));
Jonathan Harte4c98692013-10-18 17:40:03 -0700147 sysUpTime = Long.parseLong(strSysuptime);
148 sequenceNum = Long.parseLong(strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -0700149 } catch (NumberFormatException e) {
150 reply = "[DELE: mask format is wrong]";
151 log.info(reply);
152 return reply + "\n";
Jonathan Hart32e18222013-08-07 22:05:42 +1200153 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700154 reply = "[DELE: prefix format is wrong]";
155 log.info(reply);
156 return reply + "\n";
157 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200158
Jonathan Harte4c98692013-10-18 17:40:03 -0700159 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200160
161 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
162
Jonathan Hart61ba9372013-05-19 20:10:29 -0700163 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
164 }
165 else {
pingping-line2a09ca2013-03-23 09:33:58 +0800166 // clear the local rib: Ptree
167 bgpRoute.clearPtree();
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200168 reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700169
pingping-line2a09ca2013-03-23 09:33:58 +0800170 // to store the number in the top node of the Ptree
Jonathan Hart61ba9372013-05-19 20:10:29 -0700171 }
172
pingping-line2a09ca2013-03-23 09:33:58 +0800173 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800174 return reply + "\n";
175 }
176}