blob: 9093118423f729ab9b0f5e9260222e0b9e1e365d [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
14public class BgpRouteResource extends ServerResource {
Ray Milkeyec838942014-04-09 11:28:43 -070015 private static final Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070016
Ray Milkey269ffb92014-04-03 14:43:30 -070017 @Get
18 public String get(String fmJson) {
19 String dest = (String) getRequestAttributes().get("dest");
Jonathan Hart738980f2014-04-04 10:11:15 -070020 StringBuilder output = new StringBuilder(80);
21 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
22 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -070023 get(IBgpRouteService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070024
Jonathan Hart738980f2014-04-04 10:11:15 -070025 if (dest == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -070026 IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree();
Jonathan Hart738980f2014-04-04 10:11:15 -070027 output.append("{\n \"rib\": [\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070028 boolean printed = false;
Jonathan Hart61ba9372013-05-19 20:10:29 -070029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 synchronized (ptree) {
31 Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator();
32 while (it.hasNext()) {
33 IPatriciaTrie.Entry<RibEntry> entry = it.next();
Jonathan Hart61ba9372013-05-19 20:10:29 -070034
Jonathan Hart738980f2014-04-04 10:11:15 -070035 if (printed) {
36 output.append(",\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070037 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070038
Jonathan Hart738980f2014-04-04 10:11:15 -070039 output.append(" {\"prefix\": \"");
40 output.append(entry.getPrefix());
41 output.append("\", \"nexthop\": \"");
42 output.append(entry.getValue().getNextHop().getHostAddress());
43 output.append("\"}");
pingping-lina2cbfad2013-03-07 08:39:21 +080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 printed = true;
46 }
47 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070048
Jonathan Hart738980f2014-04-04 10:11:15 -070049 output.append("\n ]\n}\n");
50 } else {
51 // TODO Needs to be changed to use the new RestClient.get().
52
53 // the dest here refers to router-id
54 // bgpdRestIp includes port number, such as 1.1.1.1:8080
55 String bgpdRestIp = bgpRoute.getBGPdRestIp();
56 String url = "http://" + bgpdRestIp + "/wm/bgp/" + dest;
57
58 // Doesn't actually do anything with the response
59 RestClient.get(url);
60
61 output.append("Get rib from bgpd finished!\n");
Ray Milkey269ffb92014-04-03 14:43:30 -070062 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070063
Jonathan Hart738980f2014-04-04 10:11:15 -070064 return output.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -070065 }
pingping-lina2cbfad2013-03-07 08:39:21 +080066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 @Post
68 public String store(String fmJson) {
Jonathan Hart738980f2014-04-04 10:11:15 -070069 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
70 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -070071 get(IBgpRouteService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070072
Ray Milkey269ffb92014-04-03 14:43:30 -070073 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
74 String strSequence = (String) getRequestAttributes().get("sequence");
75 String routerId = (String) getRequestAttributes().get("routerid");
76 String prefix = (String) getRequestAttributes().get("prefix");
77 String mask = (String) getRequestAttributes().get("mask");
78 String nexthop = (String) getRequestAttributes().get("nexthop");
79 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -070080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 log.debug("sysuptime: {}", strSysuptime);
82 log.debug("sequence: {}", strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -070083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 String reply = "";
85
86 if (capability == null) {
87 // this is a prefix add
88 Prefix p;
89 long sysUpTime, sequenceNum;
90 try {
91 p = new Prefix(prefix, Integer.valueOf(mask));
92 sysUpTime = Long.parseLong(strSysuptime);
93 sequenceNum = Long.parseLong(strSequence);
94 } catch (NumberFormatException e) {
95 reply = "[POST: mask format is wrong]";
96 log.info(reply);
97 return reply + "\n";
98 } catch (IllegalArgumentException e1) {
99 reply = "[POST: prefix format is wrong]";
100 log.info(reply);
101 return reply + "\n";
102 }
103
Jonathan Hart738980f2014-04-04 10:11:15 -0700104 RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime,
105 sequenceNum);
Ray Milkey269ffb92014-04-03 14:43:30 -0700106
107 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
108
109 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
110 log.info(reply);
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 } else {
112 reply = "[POST-capability: " + capability + "]\n";
113 log.info(reply);
114 // to store the number in the top node of the Ptree
115 }
Jonathan Hartec9ee2e2014-04-08 22:45:44 -0700116 /*else if ("1".equals(capability)) {
117 reply = "[POST-capability: " + capability + "]\n";
118 log.info(reply);
119 // to store the number in the top node of the Ptree
120 }*/
Ray Milkey269ffb92014-04-03 14:43:30 -0700121
122 return reply + "\n";
123 }
124
125 @Delete
126 public String delete(String fmJson) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700127 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
128 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 get(IBgpRouteService.class.getCanonicalName());
130
131 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
132 String strSequence = (String) getRequestAttributes().get("sequence");
133 String routerId = (String) getRequestAttributes().get("routerid");
134 String prefix = (String) getRequestAttributes().get("prefix");
135 String mask = (String) getRequestAttributes().get("mask");
136 String nextHop = (String) getRequestAttributes().get("nexthop");
137 String capability = (String) getRequestAttributes().get("capability");
138
139 log.debug("sysuptime: {}", strSysuptime);
140 log.debug("sequence: {}", strSequence);
141
Jonathan Hart938a0152014-04-07 18:27:31 -0700142 //String reply = "";
143 StringBuilder replyStringBuilder = new StringBuilder(80);
Ray Milkey269ffb92014-04-03 14:43:30 -0700144
145 if (capability == null) {
146 // this is a prefix delete
147 Prefix p;
148 long sysUpTime, sequenceNum;
149 try {
150 p = new Prefix(prefix, Integer.valueOf(mask));
151 sysUpTime = Long.parseLong(strSysuptime);
152 sequenceNum = Long.parseLong(strSequence);
153 } catch (NumberFormatException e) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700154 String reply = "[DELE: mask format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 log.info(reply);
156 return reply + "\n";
157 } catch (IllegalArgumentException e1) {
Jonathan Hart938a0152014-04-07 18:27:31 -0700158 String reply = "[DELE: prefix format is wrong]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700159 log.info(reply);
160 return reply + "\n";
161 }
162
163 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
164
165 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
166
Jonathan Hart938a0152014-04-07 18:27:31 -0700167 replyStringBuilder.append("[DELE: ")
168 .append(prefix)
169 .append('/')
170 .append(mask)
171 .append(':')
172 .append(nextHop)
173 .append(']');
Ray Milkey269ffb92014-04-03 14:43:30 -0700174 } else {
175 // clear the local rib: Ptree
176 bgpRoute.clearPtree();
Jonathan Hart938a0152014-04-07 18:27:31 -0700177 replyStringBuilder.append("[DELE-capability: ")
178 .append(capability)
179 .append("; The local RibEntry is cleared!]\n");
Ray Milkey269ffb92014-04-03 14:43:30 -0700180
181 // to store the number in the top node of the Ptree
182 }
183
Jonathan Hart938a0152014-04-07 18:27:31 -0700184 log.info(replyStringBuilder.toString());
185 replyStringBuilder.append('\n');
186 return replyStringBuilder.toString();
Ray Milkey269ffb92014-04-03 14:43:30 -0700187 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800188}