blob: 2ac341487928449108d8904ec6efcfa2cbb38c58 [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 Hart738980f2014-04-04 10:11:15 -070015 private final static 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);
Jonathan Hart738980f2014-04-04 10:11:15 -0700111 } else if ("1".equals(capability)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 reply = "[POST-capability: " + capability + "]\n";
113 log.info(reply);
114 // to store the number in the top node of the Ptree
115 } else {
116 reply = "[POST-capability: " + capability + "]\n";
117 log.info(reply);
118 // to store the number in the top node of the Ptree
119 }
120
121 return reply + "\n";
122 }
123
124 @Delete
125 public String delete(String fmJson) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700126 IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
127 .getAttributes().
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 get(IBgpRouteService.class.getCanonicalName());
129
130 String strSysuptime = (String) getRequestAttributes().get("sysuptime");
131 String strSequence = (String) getRequestAttributes().get("sequence");
132 String routerId = (String) getRequestAttributes().get("routerid");
133 String prefix = (String) getRequestAttributes().get("prefix");
134 String mask = (String) getRequestAttributes().get("mask");
135 String nextHop = (String) getRequestAttributes().get("nexthop");
136 String capability = (String) getRequestAttributes().get("capability");
137
138 log.debug("sysuptime: {}", strSysuptime);
139 log.debug("sequence: {}", strSequence);
140
141 String reply = "";
142
143 if (capability == null) {
144 // this is a prefix delete
145 Prefix p;
146 long sysUpTime, sequenceNum;
147 try {
148 p = new Prefix(prefix, Integer.valueOf(mask));
149 sysUpTime = Long.parseLong(strSysuptime);
150 sequenceNum = Long.parseLong(strSequence);
151 } catch (NumberFormatException e) {
152 reply = "[DELE: mask format is wrong]";
153 log.info(reply);
154 return reply + "\n";
155 } catch (IllegalArgumentException e1) {
156 reply = "[DELE: prefix format is wrong]";
157 log.info(reply);
158 return reply + "\n";
159 }
160
161 RibEntry r = new RibEntry(routerId, nextHop, sysUpTime, sequenceNum);
162
163 bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
164
Jonathan Hart738980f2014-04-04 10:11:15 -0700165 reply = reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop
166 + "]";
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 } else {
168 // clear the local rib: Ptree
169 bgpRoute.clearPtree();
Jonathan Hart738980f2014-04-04 10:11:15 -0700170 reply = "[DELE-capability: " + capability
171 + "; The local RibEntry is cleared!]\n";
Ray Milkey269ffb92014-04-03 14:43:30 -0700172
173 // to store the number in the top node of the Ptree
174 }
175
176 log.info(reply);
177 return reply + "\n";
178 }
pingping-lina2cbfad2013-03-07 08:39:21 +0800179}