blob: 8403f713798495691017a4f140a3beeba566c6c0 [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 Hart61ba9372013-05-19 20:10:29 -070071 String routerId = (String) getRequestAttributes().get("routerid");
pingping-lina2cbfad2013-03-07 08:39:21 +080072 String prefix = (String) getRequestAttributes().get("prefix");
73 String mask = (String) getRequestAttributes().get("mask");
74 String nexthop = (String) getRequestAttributes().get("nexthop");
75 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -070076
pingping-line2a09ca2013-03-23 09:33:58 +080077 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -070078
pingping-lina2cbfad2013-03-07 08:39:21 +080079 if (capability == null) {
80 // this is a prefix add
Jonathan Hart61ba9372013-05-19 20:10:29 -070081 Prefix p;
82 try {
83 p = new Prefix(prefix, Integer.valueOf(mask));
84 } catch (NumberFormatException e) {
85 reply = "[POST: mask format is wrong]";
86 log.info(reply);
87 return reply + "\n";
Jonathan Hart32e18222013-08-07 22:05:42 +120088 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -070089 reply = "[POST: prefix format is wrong]";
90 log.info(reply);
91 return reply + "\n";
92 }
93
Jonathan Hartb39a67d2013-08-10 23:59:50 +120094 RibEntry rib = new RibEntry(routerId, nexthop);
pingping-lina2cbfad2013-03-07 08:39:21 +080095
Jonathan Hart8b9349e2013-07-26 15:55:28 +120096 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
97
pingping-lina2cbfad2013-03-07 08:39:21 +080098 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
99 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800100 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700101 else if(capability.equals("1")) {
102 reply = "[POST-capability: " + capability + "]\n";
103 log.info(reply);
104 // to store the number in the top node of the Ptree
105 }
106 else {
107 reply = "[POST-capability: " + capability + "]\n";
108 log.info(reply);
109 // to store the number in the top node of the Ptree
110 }
111
pingping-lina2cbfad2013-03-07 08:39:21 +0800112 return reply + "\n";
113 }
Jonathan Hart61ba9372013-05-19 20:10:29 -0700114
pingping-lina2cbfad2013-03-07 08:39:21 +0800115 @Delete
116 public String delete(String fmJson) {
pingping-line2a09ca2013-03-23 09:33:58 +0800117 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
Jonathan Hart61ba9372013-05-19 20:10:29 -0700118 get(IBgpRouteService.class.getCanonicalName());
pingping-lina2cbfad2013-03-07 08:39:21 +0800119
pingping-lina2cbfad2013-03-07 08:39:21 +0800120 String routerId = (String) getRequestAttributes().get("routerid");
121 String prefix = (String) getRequestAttributes().get("prefix");
122 String mask = (String) getRequestAttributes().get("mask");
123 String nextHop = (String) getRequestAttributes().get("nexthop");
124 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700125
pingping-line2a09ca2013-03-23 09:33:58 +0800126 String reply = "";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700127
pingping-lina2cbfad2013-03-07 08:39:21 +0800128 if (capability == null) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700129 // this is a prefix delete
130 Prefix p;
131 try {
132 p = new Prefix(prefix, Integer.valueOf(mask));
133 } catch (NumberFormatException e) {
134 reply = "[DELE: mask format is wrong]";
135 log.info(reply);
136 return reply + "\n";
Jonathan Hart32e18222013-08-07 22:05:42 +1200137 } catch (IllegalArgumentException e1) {
Jonathan Hart61ba9372013-05-19 20:10:29 -0700138 reply = "[DELE: prefix format is wrong]";
139 log.info(reply);
140 return reply + "\n";
141 }
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200142
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200143 RibEntry r = new RibEntry(routerId, nextHop);
Jonathan Hart8b9349e2013-07-26 15:55:28 +1200144
145 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
146
Jonathan Hart61ba9372013-05-19 20:10:29 -0700147 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
148 }
149 else {
pingping-line2a09ca2013-03-23 09:33:58 +0800150 // clear the local rib: Ptree
151 bgpRoute.clearPtree();
Jonathan Hartb39a67d2013-08-10 23:59:50 +1200152 reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n";
Jonathan Hart61ba9372013-05-19 20:10:29 -0700153
pingping-line2a09ca2013-03-23 09:33:58 +0800154 // to store the number in the top node of the Ptree
Jonathan Hart61ba9372013-05-19 20:10:29 -0700155 }
156
pingping-line2a09ca2013-03-23 09:33:58 +0800157 log.info(reply);
pingping-lina2cbfad2013-03-07 08:39:21 +0800158 return reply + "\n";
159 }
160}