blob: 7f175f0b0ce6d3b34e6302e726576f2f0acbcaf6 [file] [log] [blame]
Jonathan Hart8f6dc092014-04-18 15:56:43 -07001package net.onrc.onos.apps.sdnip.web;
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 Hart8f6dc092014-04-18 15:56:43 -07005import net.onrc.onos.apps.sdnip.ISdnIpService;
Jonathan Hart8f6dc092014-04-18 15:56:43 -07006import net.onrc.onos.apps.sdnip.Prefix;
7import net.onrc.onos.apps.sdnip.RestClient;
8import net.onrc.onos.apps.sdnip.RibEntry;
9import net.onrc.onos.apps.sdnip.RibUpdate;
10import net.onrc.onos.apps.sdnip.RibUpdate.Operation;
Jonathan Hart8b9349e2013-07-26 15:55:28 +120011
Jonathan Hart61ba9372013-05-19 20:10:29 -070012import org.restlet.resource.Delete;
pingping-lina2cbfad2013-03-07 08:39:21 +080013import org.restlet.resource.Get;
14import org.restlet.resource.Post;
pingping-lina2cbfad2013-03-07 08:39:21 +080015import org.restlet.resource.ServerResource;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
pingping-lina2cbfad2013-03-07 08:39:21 +080018
Jonathan Hartf6978ce2014-06-23 11:20:04 -070019import com.googlecode.concurrenttrees.common.KeyValuePair;
20import com.googlecode.concurrenttrees.radix.RadixTree;
21
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070022/**
23 * REST resource that handles REST calls from BGPd. This is the interface BGPd
24 * uses to push RIB entries (routes) to SDN-IP.
25 */
Jonathan Hart8f6dc092014-04-18 15:56:43 -070026public class IncomingRequestResource extends ServerResource {
27 private static final Logger log = LoggerFactory.getLogger(IncomingRequestResource.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070028
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070029 /**
30 * Gets the contents of SDN-IP's route table.
31 *
32 * @return the contents of SDN-IP's route table formatted as JSON
33 */
Ray Milkey269ffb92014-04-03 14:43:30 -070034 @Get
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070035 public String handleGetMethod() {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 String dest = (String) getRequestAttributes().get("dest");
Jonathan Hart738980f2014-04-04 10:11:15 -070037 StringBuilder output = new StringBuilder(80);
Jonathan Hart8f6dc092014-04-18 15:56:43 -070038 ISdnIpService sdnIp = (ISdnIpService) getContext()
Jonathan Hart738980f2014-04-04 10:11:15 -070039 .getAttributes().
Jonathan Hart8f6dc092014-04-18 15:56:43 -070040 get(ISdnIpService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070041
Jonathan Hart738980f2014-04-04 10:11:15 -070042 if (dest == null) {
Jonathan Hartf6978ce2014-06-23 11:20:04 -070043 RadixTree<RibEntry> radixTree = sdnIp.getPtree();
Jonathan Hart738980f2014-04-04 10:11:15 -070044 output.append("{\n \"rib\": [\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070045 boolean printed = false;
Jonathan Hart61ba9372013-05-19 20:10:29 -070046
Jonathan Hartf6978ce2014-06-23 11:20:04 -070047 Iterator<KeyValuePair<RibEntry>> it =
48 radixTree.getKeyValuePairsForKeysStartingWith("").iterator();
49 while (it.hasNext()) {
50 KeyValuePair<RibEntry> entry = it.next();
Jonathan Hart61ba9372013-05-19 20:10:29 -070051
Jonathan Hartf6978ce2014-06-23 11:20:04 -070052 if (printed) {
53 output.append(",\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070054 }
Jonathan Hartf6978ce2014-06-23 11:20:04 -070055
56 output.append(" {\"prefix\": \"");
Jonathan Hart850dddc2014-08-28 09:54:10 -070057 output.append(Prefix.fromBinaryString(entry.getKey().toString()));
Jonathan Hartf6978ce2014-06-23 11:20:04 -070058 output.append("\", \"nexthop\": \"");
59 output.append(entry.getValue().getNextHop().getHostAddress());
60 output.append("\"}");
61
62 printed = true;
Ray Milkey269ffb92014-04-03 14:43:30 -070063 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070064
Jonathan Hart738980f2014-04-04 10:11:15 -070065 output.append("\n ]\n}\n");
66 } else {
67 // TODO Needs to be changed to use the new RestClient.get().
68
69 // the dest here refers to router-id
70 // bgpdRestIp includes port number, such as 1.1.1.1:8080
Jonathan Hart8f6dc092014-04-18 15:56:43 -070071 String bgpdRestIp = sdnIp.getBgpdRestIp();
Jonathan Hart738980f2014-04-04 10:11:15 -070072 String url = "http://" + bgpdRestIp + "/wm/bgp/" + dest;
73
74 // Doesn't actually do anything with the response
75 RestClient.get(url);
76
77 output.append("Get rib from bgpd finished!\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070078 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070079
Jonathan Hart738980f2014-04-04 10:11:15 -070080 return output.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -070081 }
pingping-lina2cbfad2013-03-07 08:39:21 +080082
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070083 /**
84 * Handler to receive a new RIB entry. The details of the RIB entry are
85 * given as part of the URL.
86 *
87 * @return a String describing the result of the action
88 */
Ray Milkey269ffb92014-04-03 14:43:30 -070089 @Post
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070090 public String handlePostMethod() {
Jonathan Hart8f6dc092014-04-18 15:56:43 -070091 ISdnIpService sdnIp = (ISdnIpService) getContext()
Jonathan Hart738980f2014-04-04 10:11:15 -070092 .getAttributes().
Jonathan Hart8f6dc092014-04-18 15:56:43 -070093 get(ISdnIpService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
96 String strSequence = (String) getRequestAttributes().get("sequence");
97 String routerId = (String) getRequestAttributes().get("routerid");
98 String prefix = (String) getRequestAttributes().get("prefix");
99 String mask = (String) getRequestAttributes().get("mask");
100 String nexthop = (String) getRequestAttributes().get("nexthop");
101 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 log.debug("sysuptime: {}", strSysuptime);
104 log.debug("sequence: {}", strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -0700105
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 String reply = "";
107
108 if (capability == null) {
109 // this is a prefix add
110 Prefix p;
111 long sysUpTime, sequenceNum;
112 try {
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -0700113 p = new Prefix(prefix, Integer.parseInt(mask));
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 sysUpTime = Long.parseLong(strSysuptime);
115 sequenceNum = Long.parseLong(strSequence);
116 } catch (NumberFormatException e) {
117 reply = "[POST: mask format is wrong]";
118 log.info(reply);
119 return reply + "\n";
120 } catch (IllegalArgumentException e1) {
121 reply = "[POST: prefix format is wrong]";
122 log.info(reply);
123 return reply + "\n";
124 }
125
Jonathan Hart738980f2014-04-04 10:11:15 -0700126 RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime,
127 sequenceNum);
Ray Milkey269ffb92014-04-03 14:43:30 -0700128
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700129 sdnIp.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
Ray Milkey269ffb92014-04-03 14:43:30 -0700130
131 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
132 log.info(reply);
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 } else {
134 reply = "[POST-capability: " + capability + "]\n";
135 log.info(reply);
136 // to store the number in the top node of the Ptree
137 }
Jonathan Hartec9ee2e2014-04-08 22:45:44 -0700138 /*else if ("1".equals(capability)) {
139 reply = "[POST-capability: " + capability + "]\n";
140 log.info(reply);
141 // to store the number in the top node of the Ptree
142 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700143
144 return reply + "\n";
145 }
146
Jonathan Hart4e7c22e2014-04-09 10:59:34 -0700147 /**
148 * Handler to remove a RIB entry from ONOS's route table. The details of
149 * the route to remove are passed as part of the URL.
150 *
151 * @return a String describing the result of the action
152 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700153 @Delete
Jonathan Hart4e7c22e2014-04-09 10:59:34 -0700154 public String handleDeleteMethod() {
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700155 ISdnIpService sdnIp = (ISdnIpService) getContext()
Jonathan Hart738980f2014-04-04 10:11:15 -0700156 .getAttributes().
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700157 get(ISdnIpService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -0700158
159 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
160 String strSequence = (String) getRequestAttributes().get("sequence");
161 String routerId = (String) getRequestAttributes().get("routerid");
162 String prefix = (String) getRequestAttributes().get("prefix");
163 String mask = (String) getRequestAttributes().get("mask");
164 String nextHop = (String) getRequestAttributes().get("nexthop");
165 String capability = (String) getRequestAttributes().get("capability");
166
167 log.debug("sysuptime: {}", strSysuptime);
168 log.debug("sequence: {}", strSequence);
169
Jonathan Hart938a0152014-04-07 18:27:31 -0700170 //String reply = "";
171 StringBuilder replyStringBuilder = new StringBuilder(80);
Ray Milkey269ffb92014-04-03 14:43:30 -0700172
173 if (capability == null) {
174 // this is a prefix delete
175 Prefix p;
176 long sysUpTime, sequenceNum;
177 try {
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -0700178 p = new Prefix(prefix, Integer.parseInt(mask));
Ray Milkey269ffb92014-04-03 14:43:30 -0700179 sysUpTime = Long.parseLong(strSysuptime);
180 sequenceNum = Long.parseLong(strSequence);
181 } catch (NumberFormatException e) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700182 String reply = "[DELE: mask format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700183 log.info(reply);
184 return reply + "\n";
185 } catch (IllegalArgumentException e1) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700186 String reply = "[DELE: prefix format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 log.info(reply);
188 return reply + "\n";
189 }
190
191 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
192
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700193 sdnIp.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
Ray Milkey269ffb92014-04-03 14:43:30 -0700194
Jonathan Hart938a0152014-04-07 18:27:31 -0700195 replyStringBuilder.append("[DELE: ")
196 .append(prefix)
197 .append('/')
198 .append(mask)
199 .append(':')
200 .append(nextHop)
201 .append(']');
Ray Milkey269ffb92014-04-03 14:43:30 -0700202 } else {
203 // clear the local rib: Ptree
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700204 sdnIp.clearPtree();
Jonathan Hart938a0152014-04-07 18:27:31 -0700205 replyStringBuilder.append("[DELE-capability: ")
206 .append(capability)
207 .append("; The local RibEntry is cleared!]\n");
Ray Milkey269ffb92014-04-03 14:43:30 -0700208
209 // to store the number in the top node of the Ptree
210 }
211
Jonathan Hart938a0152014-04-07 18:27:31 -0700212 log.info(replyStringBuilder.toString());
213 replyStringBuilder.append('\n');
214 return replyStringBuilder.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800216}