blob: 2e14757fe4b59200ac8cc48397c6f2f0ab7bd493 [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 {
Jonathan Hart61ba9372013-05-19 20:10:29 -070015
Ray Milkey269ffb92014-04-03 14:43:30 -070016 protected final static Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
Jonathan Hart61ba9372013-05-19 20:10:29 -070017
Ray Milkey269ffb92014-04-03 14:43:30 -070018 @Get
19 public String get(String fmJson) {
20 String dest = (String) getRequestAttributes().get("dest");
21 String output = "";
22 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
23 get(IBgpRouteService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070024
Ray Milkey269ffb92014-04-03 14:43:30 -070025 if (dest != null) {
26 //TODO Needs to be changed to use the new RestClient.get().
Jonathan Hart61ba9372013-05-19 20:10:29 -070027
Ray Milkey269ffb92014-04-03 14:43:30 -070028 // the dest here refers to router-id
29 //bgpdRestIp includes port number, such as 1.1.1.1:8080
30 String BGPdRestIp = bgpRoute.getBGPdRestIp();
31 String url = "http://" + BGPdRestIp + "/wm/bgp/" + dest;
Jonathan Hart61ba9372013-05-19 20:10:29 -070032
Ray Milkey269ffb92014-04-03 14:43:30 -070033 //Doesn't actually do anything with the response
34 RestClient.get(url);
pingping-line2a09ca2013-03-23 09:33:58 +080035
Ray Milkey269ffb92014-04-03 14:43:30 -070036 output = "Get rib from bgpd finished!\n";
37 return output;
38 } else {
39 IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree();
40 output += "{\n \"rib\": [\n";
41 boolean printed = false;
Jonathan Hart61ba9372013-05-19 20:10:29 -070042
Ray Milkey269ffb92014-04-03 14:43:30 -070043 synchronized (ptree) {
44 Iterator<IPatriciaTrie.Entry<RibEntry>> it = ptree.iterator();
45 while (it.hasNext()) {
46 IPatriciaTrie.Entry<RibEntry> entry = it.next();
Jonathan Hart61ba9372013-05-19 20:10:29 -070047
Ray Milkey269ffb92014-04-03 14:43:30 -070048 if (printed == true) {
49 output += ",\n";
50 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070051
Ray Milkey269ffb92014-04-03 14:43:30 -070052 output += " {\"prefix\": \"" + entry.getPrefix() + "\", ";
53 output += "\"nexthop\": \"" + entry.getValue().getNextHop().getHostAddress() + "\"}";
pingping-lina2cbfad2013-03-07 08:39:21 +080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 printed = true;
56 }
57 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070058
Ray Milkey269ffb92014-04-03 14:43:30 -070059 output += "\n ]\n}\n";
60 }
Jonathan Hart61ba9372013-05-19 20:10:29 -070061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 return output;
63 }
pingping-lina2cbfad2013-03-07 08:39:21 +080064
Ray Milkey269ffb92014-04-03 14:43:30 -070065 @Post
66 public String store(String fmJson) {
67 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
68 get(IBgpRouteService.class.getCanonicalName());
Jonathan Hart61ba9372013-05-19 20:10:29 -070069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
71 String strSequence = (String) getRequestAttributes().get("sequence");
72 String routerId = (String) getRequestAttributes().get("routerid");
73 String prefix = (String) getRequestAttributes().get("prefix");
74 String mask = (String) getRequestAttributes().get("mask");
75 String nexthop = (String) getRequestAttributes().get("nexthop");
76 String capability = (String) getRequestAttributes().get("capability");
Jonathan Hart61ba9372013-05-19 20:10:29 -070077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 log.debug("sysuptime: {}", strSysuptime);
79 log.debug("sequence: {}", strSequence);
Jonathan Hart61ba9372013-05-19 20:10:29 -070080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 String reply = "";
82
83 if (capability == null) {
84 // this is a prefix add
85 Prefix p;
86 long sysUpTime, sequenceNum;
87 try {
88 p = new Prefix(prefix, Integer.valueOf(mask));
89 sysUpTime = Long.parseLong(strSysuptime);
90 sequenceNum = Long.parseLong(strSequence);
91 } catch (NumberFormatException e) {
92 reply = "[POST: mask format is wrong]";
93 log.info(reply);
94 return reply + "\n";
95 } catch (IllegalArgumentException e1) {
96 reply = "[POST: prefix format is wrong]";
97 log.info(reply);
98 return reply + "\n";
99 }
100
101 RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime, sequenceNum);
102
103 bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
104
105 reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
106 log.info(reply);
107 } else if (capability.equals("1")) {
108 reply = "[POST-capability: " + capability + "]\n";
109 log.info(reply);
110 // to store the number in the top node of the Ptree
111 } else {
112 reply = "[POST-capability: " + capability + "]\n";
113 log.info(reply);
114 // to store the number in the top node of the Ptree
115 }
116
117 return reply + "\n";
118 }
119
120 @Delete
121 public String delete(String fmJson) {
122 IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
123 get(IBgpRouteService.class.getCanonicalName());
124
125 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
126 String strSequence = (String) getRequestAttributes().get("sequence");
127 String routerId = (String) getRequestAttributes().get("routerid");
128 String prefix = (String) getRequestAttributes().get("prefix");
129 String mask = (String) getRequestAttributes().get("mask");
130 String nextHop = (String) getRequestAttributes().get("nexthop");
131 String capability = (String) getRequestAttributes().get("capability");
132
133 log.debug("sysuptime: {}", strSysuptime);
134 log.debug("sequence: {}", strSequence);
135
136 String reply = "";
137
138 if (capability == null) {
139 // this is a prefix delete
140 Prefix p;
141 long sysUpTime, sequenceNum;
142 try {
143 p = new Prefix(prefix, Integer.valueOf(mask));
144 sysUpTime = Long.parseLong(strSysuptime);
145 sequenceNum = Long.parseLong(strSequence);
146 } catch (NumberFormatException e) {
147 reply = "[DELE: mask format is wrong]";
148 log.info(reply);
149 return reply + "\n";
150 } catch (IllegalArgumentException e1) {
151 reply = "[DELE: prefix format is wrong]";
152 log.info(reply);
153 return reply + "\n";
154 }
155
156 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
157
158 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
159
160 reply = reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
161 } else {
162 // clear the local rib: Ptree
163 bgpRoute.clearPtree();
164 reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n";
165
166 // to store the number in the top node of the Ptree
167 }
168
169 log.info(reply);
170 return reply + "\n";
171 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800172}