blob: d5abb5aa2c24eb7cfde4fe978d6dab4a1a732935 [file] [log] [blame]
pingping-lina2cbfad2013-03-07 08:39:21 +08001package net.floodlightcontroller.bgproute;
2
3import org.restlet.resource.Get;
4import org.restlet.resource.Post;
5import org.restlet.resource.Delete;
6import org.restlet.resource.ServerResource;
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9import net.floodlightcontroller.restclient.RestClient;
10
11public class BgpRouteResource extends ServerResource {
12
13 protected static Logger log = LoggerFactory
14 .getLogger(BgpRouteResource.class);
15
16 private String addrToString(byte [] addr) {
17 String str = "";
18
19 for (int i = 0; i < 4; i++) {
20 int val = (addr[i] & 0xff);
21 str += val;
22 if (i != 3)
23 str += ".";
24 }
25
26 return str;
27 }
28
29 @SuppressWarnings("unused")
30 @Get
31 public String get(String fmJson) {
32 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) {
38 Prefix p = new Prefix(dest, 32);
39 if (p == null) {
40 return "[GET]: dest address format is wrong";
41 }
42 byte [] nexthop = bgpRoute.lookupRib(p.getAddress()).nextHop.getAddress();
43 if (nexthop != null) {
44 output += "{\"result\": \"" + addrToString(nexthop) + "\"}\n";
45 } else {
46 output += "{\"result\": \"Nexthop does not exist\"}\n";
47 }
48 } else {
49 Ptree ptree = bgpRoute.getPtree();
50 output += "{\n \"rib\": [\n";
51 boolean printed = false;
52 for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
53 if (node.rib == null) {
54 continue;
55 }
56 if (printed == true) {
57 output += ",\n";
58 }
59 output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
60 output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
61 printed = true;
62 }
63 //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
64 output += "\n ]\n}\n";
65 }
66
67 return output;
68 }
69 @Post
70 public String store(String fmJson) {
71 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
72 get(IBgpRouteService.class.getCanonicalName());
73
74 Ptree ptree = bgpRoute.getPtree();
75
76 String router_id = (String) getRequestAttributes().get("routerid");
77 String prefix = (String) getRequestAttributes().get("prefix");
78 String mask = (String) getRequestAttributes().get("mask");
79 String nexthop = (String) getRequestAttributes().get("nexthop");
80 String capability = (String) getRequestAttributes().get("capability");
81 String reply = null;
82
83 if (capability == null) {
84 // this is a prefix add
85 Prefix p = new Prefix(prefix, Integer.valueOf(mask));
86 PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
87 Rib rib = new Rib(router_id, nexthop, p.masklen);
88
89 if (node.rib != null) {
90 node.rib = null;
91 ptree.delReference(node);
92 }
93 node.rib = rib;
94
95 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
96 log.info(reply);
97
98 RestClient.get("http://localhost:5000/bgp_update");
99 }
100
101 return reply + "\n";
102 }
103
104 @Delete
105 public String delete(String fmJson) {
106 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
107 get(IBgpRouteService.class.getCanonicalName());
108
109 Ptree ptree = bgpRoute.getPtree();
110
111 String routerId = (String) getRequestAttributes().get("routerid");
112 String prefix = (String) getRequestAttributes().get("prefix");
113 String mask = (String) getRequestAttributes().get("mask");
114 String nextHop = (String) getRequestAttributes().get("nexthop");
115 String capability = (String) getRequestAttributes().get("capability");
116 String reply = null;
117
118 if (capability == null) {
119 // this is a prefix delete
120 Prefix p = new Prefix(prefix, Integer.valueOf(mask));
121 PtreeNode node = ptree.lookup(p.getAddress(), p.masklen);
122 Rib r = new Rib(routerId, nextHop, p.masklen);
123
124 if (node != null && node.rib != null) {
125 if (r.equals(node.rib)) {
126 node.rib = null;
127 ptree.delReference(node);
128 }
129 }
130
131 reply = "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
132 log.info(reply);
133
134 RestClient.get("http://localhost:5000/bgp_update");
135 }
136
137 return reply + "\n";
138 }
139}