blob: 28d962167538b92443b8e09e95ce47af97e989d3 [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;
pingping-line2a09ca2013-03-23 09:33:58 +080010import java.io.UnsupportedEncodingException;
11import java.nio.ByteBuffer;
pingping-lina2cbfad2013-03-07 08:39:21 +080012
13public class BgpRouteResource extends ServerResource {
14
15 protected static Logger log = LoggerFactory
16 .getLogger(BgpRouteResource.class);
17
18 private String addrToString(byte [] addr) {
19 String str = "";
20
21 for (int i = 0; i < 4; i++) {
22 int val = (addr[i] & 0xff);
23 str += val;
24 if (i != 3)
25 str += ".";
26 }
27
28 return str;
29 }
30
31 @SuppressWarnings("unused")
pingping-line2a09ca2013-03-23 09:33:58 +080032 @Get
33 public String get(String fmJson) {
34 String linpp=fmJson;
35 String dest = (String) getRequestAttributes().get("dest");
36 String output = "";
37 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
38 get(IBgpRouteService.class.getCanonicalName());
39
40 if (dest != null) {
41 Prefix p = new Prefix(dest, 32);
42 if (p == null) {
43 return "[GET]: dest address format is wrong";
44 }
45
46 // the dest here refers to router-id
47 //BGPdRestIp includes port number, such as 1.1.1.1:8080
48 String BGPdRestIp = bgpRoute.getBGPdRestIp();
49 String url="http://"+BGPdRestIp+"/wm/bgp/"+dest;
50
51
52
53 RestClient.get(url);
54 output="Get rib from bgpd finished!\n";
55 return output;
56
pingping-lina2cbfad2013-03-07 08:39:21 +080057 } else {
pingping-line2a09ca2013-03-23 09:33:58 +080058 Ptree ptree = bgpRoute.getPtree();
59 output += "{\n \"rib\": [\n";
60 boolean printed = false;
61 for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
62 if (node.rib == null) {
63 continue;
64 }
65 if (printed == true) {
66 output += ",\n";
67 }
68 output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
69 output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
70 printed = true;
pingping-lina2cbfad2013-03-07 08:39:21 +080071 }
pingping-line2a09ca2013-03-23 09:33:58 +080072 //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
73 output += "\n ]\n}\n";
74
pingping-lina2cbfad2013-03-07 08:39:21 +080075 }
pingping-line2a09ca2013-03-23 09:33:58 +080076 return output;
pingping-lina2cbfad2013-03-07 08:39:21 +080077 }
pingping-line2a09ca2013-03-23 09:33:58 +080078
79 public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException
80 {
81 return ByteBuffer.wrap(value.getBytes("UTF-8"));
82 }
83
84public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException
85 {
86 byte[] bytes = new byte[buffer.remaining()];
87 buffer.get(bytes);
88 return new String(bytes, "UTF-8");
89
90 }
91
92
pingping-lina2cbfad2013-03-07 08:39:21 +080093 @Post
94 public String store(String fmJson) {
95 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
96 get(IBgpRouteService.class.getCanonicalName());
pingping-line2a09ca2013-03-23 09:33:58 +080097
98 Ptree ptree = bgpRoute.getPtree();
99
pingping-lina2cbfad2013-03-07 08:39:21 +0800100 String router_id = (String) getRequestAttributes().get("routerid");
101 String prefix = (String) getRequestAttributes().get("prefix");
102 String mask = (String) getRequestAttributes().get("mask");
103 String nexthop = (String) getRequestAttributes().get("nexthop");
104 String capability = (String) getRequestAttributes().get("capability");
pingping-line2a09ca2013-03-23 09:33:58 +0800105
106
107 String reply = "";
pingping-lina2cbfad2013-03-07 08:39:21 +0800108
109 if (capability == null) {
pingping-line2a09ca2013-03-23 09:33:58 +0800110
pingping-lina2cbfad2013-03-07 08:39:21 +0800111 // this is a prefix add
112 Prefix p = new Prefix(prefix, Integer.valueOf(mask));
113 PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
114 Rib rib = new Rib(router_id, nexthop, p.masklen);
115
116 if (node.rib != null) {
117 node.rib = null;
118 ptree.delReference(node);
119 }
120 node.rib = rib;
121
122 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
123 log.info(reply);
pingping-line2a09ca2013-03-23 09:33:58 +0800124
pingping-lina2cbfad2013-03-07 08:39:21 +0800125
pingping-line2a09ca2013-03-23 09:33:58 +0800126 }else if(capability.equals("1")){
127 reply = "[POST-capability: " + capability + "]\n";
128 log.info(reply);
129 // to store the number in the top node of the Ptree
130
131 }else{
132 reply = "[POST-capability: " + capability + "]\n";
133 log.info(reply);
134 // to store the number in the top node of the Ptree
135
pingping-lina2cbfad2013-03-07 08:39:21 +0800136 }
137
pingping-line2a09ca2013-03-23 09:33:58 +0800138
pingping-lina2cbfad2013-03-07 08:39:21 +0800139 return reply + "\n";
pingping-line2a09ca2013-03-23 09:33:58 +0800140
141
pingping-lina2cbfad2013-03-07 08:39:21 +0800142 }
143
144 @Delete
145 public String delete(String fmJson) {
pingping-line2a09ca2013-03-23 09:33:58 +0800146 IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
pingping-lina2cbfad2013-03-07 08:39:21 +0800147 get(IBgpRouteService.class.getCanonicalName());
148
pingping-line2a09ca2013-03-23 09:33:58 +0800149 Ptree ptree = bgpRoute.getPtree();
150
pingping-lina2cbfad2013-03-07 08:39:21 +0800151 String routerId = (String) getRequestAttributes().get("routerid");
152 String prefix = (String) getRequestAttributes().get("prefix");
153 String mask = (String) getRequestAttributes().get("mask");
154 String nextHop = (String) getRequestAttributes().get("nexthop");
155 String capability = (String) getRequestAttributes().get("capability");
pingping-line2a09ca2013-03-23 09:33:58 +0800156
157 String reply = "";
pingping-lina2cbfad2013-03-07 08:39:21 +0800158
159 if (capability == null) {
pingping-line2a09ca2013-03-23 09:33:58 +0800160 // this is a prefix delete
161 Prefix p = new Prefix(prefix, Integer.valueOf(mask));
162
163 PtreeNode node = ptree.lookup(p.getAddress(), p.masklen);
164
165 Rib r = new Rib(routerId, nextHop, p.masklen);
166
167 if (node != null && node.rib != null) {
168
169 if (r.equals(node.rib)) {
170
171 node.rib = null;
172 ptree.delReference(node);
173 }
174 }
175
176
177 reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
178
179 }else {
pingping-lina2cbfad2013-03-07 08:39:21 +0800180
pingping-line2a09ca2013-03-23 09:33:58 +0800181 // clear the local rib: Ptree
182 bgpRoute.clearPtree();
183 reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n";
pingping-lina2cbfad2013-03-07 08:39:21 +0800184
pingping-line2a09ca2013-03-23 09:33:58 +0800185
186 // to store the number in the top node of the Ptree
187
188 }
189 log.info(reply);
190
pingping-lina2cbfad2013-03-07 08:39:21 +0800191 return reply + "\n";
192 }
193}