blob: 690ec4dc58bf03326f40c023d8aa994dec2f2e2d [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;
6import net.onrc.onos.apps.sdnip.IPatriciaTree;
7import net.onrc.onos.apps.sdnip.Prefix;
8import net.onrc.onos.apps.sdnip.RestClient;
9import net.onrc.onos.apps.sdnip.RibEntry;
10import net.onrc.onos.apps.sdnip.RibUpdate;
11import net.onrc.onos.apps.sdnip.RibUpdate.Operation;
Jonathan Hart8b9349e2013-07-26 15:55:28 +120012
Jonathan Hart61ba9372013-05-19 20:10:29 -070013import org.restlet.resource.Delete;
pingping-lina2cbfad2013-03-07 08:39:21 +080014import org.restlet.resource.Get;
15import org.restlet.resource.Post;
pingping-lina2cbfad2013-03-07 08:39:21 +080016import org.restlet.resource.ServerResource;
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
pingping-lina2cbfad2013-03-07 08:39:21 +080019
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070020/**
21 * REST resource that handles REST calls from BGPd. This is the interface BGPd
22 * uses to push RIB entries (routes) to SDN-IP.
23 */
Jonathan Hart8f6dc092014-04-18 15:56:43 -070024public class IncomingRequestResource extends ServerResource {
25 private static final Logger log = LoggerFactory.getLogger(IncomingRequestResource.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070026
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070027 /**
28 * Gets the contents of SDN-IP's route table.
29 *
30 * @return the contents of SDN-IP's route table formatted as JSON
31 */
Ray Milkey269ffb92014-04-03 14:43:30 -070032 @Get
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070033 public String handleGetMethod() {
Ray Milkey269ffb92014-04-03 14:43:30 -070034 String dest = (String) getRequestAttributes().get("dest");
Jonathan Hart738980f2014-04-04 10:11:15 -070035 StringBuilder output = new StringBuilder(80);
Jonathan Hart8f6dc092014-04-18 15:56:43 -070036 ISdnIpService sdnIp = (ISdnIpService) getContext()
Jonathan Hart738980f2014-04-04 10:11:15 -070037 .getAttributes().
Jonathan Hart8f6dc092014-04-18 15:56:43 -070038 get(ISdnIpService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070039
Jonathan Hart738980f2014-04-04 10:11:15 -070040 if (dest == null) {
Jonathan Hart8f6dc092014-04-18 15:56:43 -070041 IPatriciaTree<RibEntry> ptree = sdnIp.getPtree();
Jonathan Hart738980f2014-04-04 10:11:15 -070042 output.append("{\n \"rib\": [\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070043 boolean printed = false;
Jonathan Hart61ba9372013-05-19 20:10:29 -070044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 synchronized (ptree) {
Jonathan Hart5e54f2e2014-04-17 13:43:40 -070046 Iterator<IPatriciaTree.Entry<RibEntry>> it = ptree.iterator();
Ray Milkey269ffb92014-04-03 14:43:30 -070047 while (it.hasNext()) {
Jonathan Hart5e54f2e2014-04-17 13:43:40 -070048 IPatriciaTree.Entry<RibEntry> entry = it.next();
Jonathan Hart61ba9372013-05-19 20:10:29 -070049
Jonathan Hart738980f2014-04-04 10:11:15 -070050 if (printed) {
51 output.append(",\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070052 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070053
Jonathan Hart738980f2014-04-04 10:11:15 -070054 output.append(" {\"prefix\": \"");
55 output.append(entry.getPrefix());
56 output.append("\", \"nexthop\": \"");
57 output.append(entry.getValue().getNextHop().getHostAddress());
58 output.append("\"}");
pingping-lina2cbfad2013-03-07 08:39:21 +080059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 printed = true;
61 }
62 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070063
Jonathan Hart738980f2014-04-04 10:11:15 -070064 output.append("\n ]\n}\n");
65 } else {
66 // TODO Needs to be changed to use the new RestClient.get().
67
68 // the dest here refers to router-id
69 // bgpdRestIp includes port number, such as 1.1.1.1:8080
Jonathan Hart8f6dc092014-04-18 15:56:43 -070070 String bgpdRestIp = sdnIp.getBgpdRestIp();
Jonathan Hart738980f2014-04-04 10:11:15 -070071 String url = "http://" + bgpdRestIp + "/wm/bgp/" + dest;
72
73 // Doesn't actually do anything with the response
74 RestClient.get(url);
75
76 output.append("Get rib from bgpd finished!\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070077 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070078
Jonathan Hart738980f2014-04-04 10:11:15 -070079 return output.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -070080 }
pingping-lina2cbfad2013-03-07 08:39:21 +080081
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070082 /**
83 * Handler to receive a new RIB entry. The details of the RIB entry are
84 * given as part of the URL.
85 *
86 * @return a String describing the result of the action
87 */
Ray Milkey269ffb92014-04-03 14:43:30 -070088 @Post
Jonathan Hart4e7c22e2014-04-09 10:59:34 -070089 public String handlePostMethod() {
Jonathan Hart8f6dc092014-04-18 15:56:43 -070090 ISdnIpService sdnIp = (ISdnIpService) getContext()
Jonathan Hart738980f2014-04-04 10:11:15 -070091 .getAttributes().
Jonathan Hart8f6dc092014-04-18 15:56:43 -070092 get(ISdnIpService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
95 String strSequence = (String) getRequestAttributes().get("sequence");
96 String routerId = (String) getRequestAttributes().get("routerid");
97 String prefix = (String) getRequestAttributes().get("prefix");
98 String mask = (String) getRequestAttributes().get("mask");
99 String nexthop = (String) getRequestAttributes().get("nexthop");
100 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -0700101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 log.debug("sysuptime: {}", strSysuptime);
103 log.debug("sequence: {}", strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -0700104
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 String reply = "";
106
107 if (capability == null) {
108 // this is a prefix add
109 Prefix p;
110 long sysUpTime, sequenceNum;
111 try {
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -0700112 p = new Prefix(prefix, Integer.parseInt(mask));
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 sysUpTime = Long.parseLong(strSysuptime);
114 sequenceNum = Long.parseLong(strSequence);
115 } catch (NumberFormatException e) {
116 reply = "[POST: mask format is wrong]";
117 log.info(reply);
118 return reply + "\n";
119 } catch (IllegalArgumentException e1) {
120 reply = "[POST: prefix format is wrong]";
121 log.info(reply);
122 return reply + "\n";
123 }
124
Jonathan Hart738980f2014-04-04 10:11:15 -0700125 RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime,
126 sequenceNum);
Ray Milkey269ffb92014-04-03 14:43:30 -0700127
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700128 sdnIp.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
Ray Milkey269ffb92014-04-03 14:43:30 -0700129
130 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
131 log.info(reply);
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 } else {
133 reply = "[POST-capability: " + capability + "]\n";
134 log.info(reply);
135 // to store the number in the top node of the Ptree
136 }
Jonathan Hartec9ee2e2014-04-08 22:45:44 -0700137 /*else if ("1".equals(capability)) {
138 reply = "[POST-capability: " + capability + "]\n";
139 log.info(reply);
140 // to store the number in the top node of the Ptree
141 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700142
143 return reply + "\n";
144 }
145
Jonathan Hart4e7c22e2014-04-09 10:59:34 -0700146 /**
147 * Handler to remove a RIB entry from ONOS's route table. The details of
148 * the route to remove are passed as part of the URL.
149 *
150 * @return a String describing the result of the action
151 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 @Delete
Jonathan Hart4e7c22e2014-04-09 10:59:34 -0700153 public String handleDeleteMethod() {
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700154 ISdnIpService sdnIp = (ISdnIpService) getContext()
Jonathan Hart738980f2014-04-04 10:11:15 -0700155 .getAttributes().
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700156 get(ISdnIpService.class.getCanonicalName());
Ray Milkey269ffb92014-04-03 14:43:30 -0700157
158 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
159 String strSequence = (String) getRequestAttributes().get("sequence");
160 String routerId = (String) getRequestAttributes().get("routerid");
161 String prefix = (String) getRequestAttributes().get("prefix");
162 String mask = (String) getRequestAttributes().get("mask");
163 String nextHop = (String) getRequestAttributes().get("nexthop");
164 String capability = (String) getRequestAttributes().get("capability");
165
166 log.debug("sysuptime: {}", strSysuptime);
167 log.debug("sequence: {}", strSequence);
168
Jonathan Hart938a0152014-04-07 18:27:31 -0700169 //String reply = "";
170 StringBuilder replyStringBuilder = new StringBuilder(80);
Ray Milkey269ffb92014-04-03 14:43:30 -0700171
172 if (capability == null) {
173 // this is a prefix delete
174 Prefix p;
175 long sysUpTime, sequenceNum;
176 try {
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -0700177 p = new Prefix(prefix, Integer.parseInt(mask));
Ray Milkey269ffb92014-04-03 14:43:30 -0700178 sysUpTime = Long.parseLong(strSysuptime);
179 sequenceNum = Long.parseLong(strSequence);
180 } catch (NumberFormatException e) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700181 String reply = "[DELE: mask format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 log.info(reply);
183 return reply + "\n";
184 } catch (IllegalArgumentException e1) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700185 String reply = "[DELE: prefix format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700186 log.info(reply);
187 return reply + "\n";
188 }
189
190 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
191
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700192 sdnIp.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
Ray Milkey269ffb92014-04-03 14:43:30 -0700193
Jonathan Hart938a0152014-04-07 18:27:31 -0700194 replyStringBuilder.append("[DELE: ")
195 .append(prefix)
196 .append('/')
197 .append(mask)
198 .append(':')
199 .append(nextHop)
200 .append(']');
Ray Milkey269ffb92014-04-03 14:43:30 -0700201 } else {
202 // clear the local rib: Ptree
Jonathan Hart8f6dc092014-04-18 15:56:43 -0700203 sdnIp.clearPtree();
Jonathan Hart938a0152014-04-07 18:27:31 -0700204 replyStringBuilder.append("[DELE-capability: ")
205 .append(capability)
206 .append("; The local RibEntry is cleared!]\n");
Ray Milkey269ffb92014-04-03 14:43:30 -0700207
208 // to store the number in the top node of the Ptree
209 }
210
Jonathan Hart938a0152014-04-07 18:27:31 -0700211 log.info(replyStringBuilder.toString());
212 replyStringBuilder.append('\n');
213 return replyStringBuilder.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800215}