blob: 7580d511d7644cf7f211cfa4a15b21068e3fdfa7 [file] [log] [blame]
Jonathan Hart382623d2014-04-03 09:48:11 -07001package net.onrc.onos.apps.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 Hart382623d2014-04-03 09:48:11 -07005import net.onrc.onos.apps.bgproute.RibUpdate.Operation;
Jonathan Hart8b9349e2013-07-26 15:55:28 +12006
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
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070014/**
15 * REST resource that handles REST calls from BGPd. This is the interface BGPd
16 * uses to push RIB entries (routes) to SDN-IP.
17 */
pingping-lina2cbfad2013-03-07 08:39:21 +080018public class BgpRouteResource extends ServerResource {
Ray Milkeyec838942014-04-09 11:28:43 -070019 private static final Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070020
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070021 /**
22 * Gets the contents of SDN-IP's route table.
23 *
24 * @return the contents of SDN-IP's route table formatted as JSON
25 */
Ray Milkey269ffb92014-04-03 14:43:30 -070026 @Get
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070027 public String handleGetMethod() {
Ray Milkey269ffb92014-04-03 14:43:30 -070028 String dest = (String) getRequestAttributes().get("dest");
Jonathan Hart738980f2014-04-04 10:11:15 -070029 StringBuilder output = new StringBuilder(80);
30 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
31 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -070032 get(IBgpRouteService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070033
Jonathan Hart738980f2014-04-04 10:11:15 -070034 if (dest == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree();
Jonathan Hart738980f2014-04-04 10:11:15 -070036 output.append("{\n \"rib\": [\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070037 boolean printed = false;
Jonathan Hart61ba9372013-05-19 20:10:29 -070038
Ray Milkey269ffb92014-04-03 14:43:30 -070039 synchronized (ptree) {
40 Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator();
41 while (it.hasNext()) {
42 IPatriciaTrie.Entry<RibEntry> entry = it.next();
Jonathan Hart61ba9372013-05-19 20:10:29 -070043
Jonathan Hart738980f2014-04-04 10:11:15 -070044 if (printed) {
45 output.append(",\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070046 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070047
Jonathan Hart738980f2014-04-04 10:11:15 -070048 output.append(" {\"prefix\": \"");
49 output.append(entry.getPrefix());
50 output.append("\", \"nexthop\": \"");
51 output.append(entry.getValue().getNextHop().getHostAddress());
52 output.append("\"}");
pingping-lina2cbfad2013-03-07 08:39:21 +080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 printed = true;
55 }
56 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070057
Jonathan Hart738980f2014-04-04 10:11:15 -070058 output.append("\n ]\n}\n");
59 } else {
60 // TODO Needs to be changed to use the new RestClient.get().
61
62 // the dest here refers to router-id
63 // bgpdRestIp includes port number, such as 1.1.1.1:8080
64 String bgpdRestIp = bgpRoute.getBGPdRestIp();
65 String url = "http://" + bgpdRestIp + "/wm/bgp/" + dest;
66
67 // Doesn't actually do anything with the response
68 RestClient.get(url);
69
70 output.append("Get rib from bgpd finished!\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070071 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070072
Jonathan Hart738980f2014-04-04 10:11:15 -070073 return output.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -070074 }
pingping-lina2cbfad2013-03-07 08:39:21 +080075
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070076 /**
77 * Handler to receive a new RIB entry. The details of the RIB entry are
78 * given as part of the URL.
79 *
80 * @return a String describing the result of the action
81 */
Ray Milkey269ffb92014-04-03 14:43:30 -070082 @Post
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070083 public String handlePostMethod() {
Jonathan Hart738980f2014-04-04 10:11:15 -070084 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
85 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -070086 get(IBgpRouteService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070087
Ray Milkey269ffb92014-04-03 14:43:30 -070088 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
89 String strSequence = (String) getRequestAttributes().get("sequence");
90 String routerId = (String) getRequestAttributes().get("routerid");
91 String prefix = (String) getRequestAttributes().get("prefix");
92 String mask = (String) getRequestAttributes().get("mask");
93 String nexthop = (String) getRequestAttributes().get("nexthop");
94 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -070095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 log.debug("sysuptime: {}", strSysuptime);
97 log.debug("sequence: {}", strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -070098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 String reply = "";
100
101 if (capability == null) {
102 // this is a prefix add
103 Prefix p;
104 long sysUpTime, sequenceNum;
105 try {
106 p = new Prefix(prefix, Integer.valueOf(mask));
107 sysUpTime = Long.parseLong(strSysuptime);
108 sequenceNum = Long.parseLong(strSequence);
109 } catch (NumberFormatException e) {
110 reply = "[POST: mask format is wrong]";
111 log.info(reply);
112 return reply + "\n";
113 } catch (IllegalArgumentException e1) {
114 reply = "[POST: prefix format is wrong]";
115 log.info(reply);
116 return reply + "\n";
117 }
118
Jonathan Hart738980f2014-04-04 10:11:15 -0700119 RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime,
120 sequenceNum);
Ray Milkey269ffb92014-04-03 14:43:30 -0700121
122 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
123
124 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
125 log.info(reply);
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 } else {
127 reply = "[POST-capability: " + capability + "]\n";
128 log.info(reply);
129 // to store the number in the top node of the Ptree
130 }
Jonathan Hartec9ee2e2014-04-08 22:45:44 -0700131 /*else if ("1".equals(capability)) {
132 reply = "[POST-capability: " + capability + "]\n";
133 log.info(reply);
134 // to store the number in the top node of the Ptree
135 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700136
137 return reply + "\n";
138 }
139
Jonathan Hart4e7c22e2014-04-09 10:59:34 -0700140 /**
141 * Handler to remove a RIB entry from ONOS's route table. The details of
142 * the route to remove are passed as part of the URL.
143 *
144 * @return a String describing the result of the action
145 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 @Delete
Jonathan Hart4e7c22e2014-04-09 10:59:34 -0700147 public String handleDeleteMethod() {
Jonathan Hart738980f2014-04-04 10:11:15 -0700148 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
149 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 get(IBgpRouteService.class.getCanonicalName());
151
152 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
153 String strSequence = (String) getRequestAttributes().get("sequence");
154 String routerId = (String) getRequestAttributes().get("routerid");
155 String prefix = (String) getRequestAttributes().get("prefix");
156 String mask = (String) getRequestAttributes().get("mask");
157 String nextHop = (String) getRequestAttributes().get("nexthop");
158 String capability = (String) getRequestAttributes().get("capability");
159
160 log.debug("sysuptime: {}", strSysuptime);
161 log.debug("sequence: {}", strSequence);
162
Jonathan Hart938a0152014-04-07 18:27:31 -0700163 //String reply = "";
164 StringBuilder replyStringBuilder = new StringBuilder(80);
Ray Milkey269ffb92014-04-03 14:43:30 -0700165
166 if (capability == null) {
167 // this is a prefix delete
168 Prefix p;
169 long sysUpTime, sequenceNum;
170 try {
171 p = new Prefix(prefix, Integer.valueOf(mask));
172 sysUpTime = Long.parseLong(strSysuptime);
173 sequenceNum = Long.parseLong(strSequence);
174 } catch (NumberFormatException e) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700175 String reply = "[DELE: mask format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 log.info(reply);
177 return reply + "\n";
178 } catch (IllegalArgumentException e1) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700179 String reply = "[DELE: prefix format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700180 log.info(reply);
181 return reply + "\n";
182 }
183
184 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
185
186 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
187
Jonathan Hart938a0152014-04-07 18:27:31 -0700188 replyStringBuilder.append("[DELE: ")
189 .append(prefix)
190 .append('/')
191 .append(mask)
192 .append(':')
193 .append(nextHop)
194 .append(']');
Ray Milkey269ffb92014-04-03 14:43:30 -0700195 } else {
196 // clear the local rib: Ptree
197 bgpRoute.clearPtree();
Jonathan Hart938a0152014-04-07 18:27:31 -0700198 replyStringBuilder.append("[DELE-capability: ")
199 .append(capability)
200 .append("; The local RibEntry is cleared!]\n");
Ray Milkey269ffb92014-04-03 14:43:30 -0700201
202 // to store the number in the top node of the Ptree
203 }
204
Jonathan Hart938a0152014-04-07 18:27:31 -0700205 log.info(replyStringBuilder.toString());
206 replyStringBuilder.append('\n');
207 return replyStringBuilder.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700208 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800209}