1 package org.opendaylight.bgpmanager.thrift.client.implementation;
3 import org.apache.thrift.TException;
4 import org.apache.thrift.transport.TTransport;
5 import org.apache.thrift.transport.TSocket;
6 import org.apache.thrift.protocol.TBinaryProtocol;
7 import org.apache.thrift.protocol.TProtocol;
10 import org.opendaylight.bgpmanager.thrift.client.globals.Route;
11 import org.opendaylight.bgpmanager.thrift.common.Constants;
12 import org.opendaylight.bgpmanager.thrift.gen.*;
13 import org.opendaylight.bgpmanager.thrift.exceptions.BgpRouterException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 public class BgpRouter {
19 private TTransport transport;
21 private TProtocol protocol;
22 private static BgpConfigurator.Client bgpClient=null;
23 private static final Logger logger = LoggerFactory.getLogger(BgpRouter.class);
26 private final static int ADD_NBR = 1;
27 private final static int DEL_NBR = 2;
28 private final static int ADD_VRF = 3;
29 private final static int DEL_VRF = 4;
30 private final static int ADD_PFX = 5;
31 private final static int DEL_PFX = 6;
32 private final static int START_BGP = 7;
34 //public final static int BGP_ERR_INITED = 101;
35 //public final static int BGP_ERR_NOT_INITED = 102;
38 private final static int GET_RTS_INIT = 0;
39 private final static int GET_RTS_NEXT = 1;
47 public List<String> irts;
48 public List<String> erts;
54 public static final int ignore = 0;
58 static BgpOp bop = null;
60 private String bgpHost;
67 public void setBgpServer(String host, int port) {
72 public void connect(String bgpHost, int bgpPort)
73 throws TException, BgpRouterException {
74 this.bgpHost = bgpHost;
75 this.bgpPort = bgpPort;
78 logger.info("Connecting to BGP Server " + bgpHost + " on port " + bgpPort);
80 } catch (Exception e) {
81 logger.error("Failed connecting to BGP server ");
86 public void disconnect() {
92 throws TException, BgpRouterException {
95 transport = new TSocket(bgpHost, bgpPort);
96 ((TSocket)transport).setTimeout(Constants.CL_SKT_TIMEO_MS);
98 protocol = new TBinaryProtocol(transport);
99 bgpClient = new BgpConfigurator.Client(protocol);
104 private void dispatch(BgpOp op)
105 throws TException, BgpRouterException {
108 if (bgpClient == null)
109 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
113 result = bgpClient.startBgpServer(op.asNum, op.rtrId,
114 op.ignore, op.ignore, op.ignore);
117 result = bgpClient.createPeer(op.nbrIp, op.nbrAsNum);
120 result = bgpClient.deletePeer(op.nbrIp);
123 result = bgpClient.addVrf(op.rd, op.irts, op.erts);
126 result = bgpClient.delVrf(op.rd);
129 result = bgpClient.pushRoute(op.pfx, op.nh, op.rd, op.lbl);
132 result = bgpClient.withdrawRoute(op.pfx, op.rd);
136 if (result != 0) throw new BgpRouterException(result);
139 public void startBgp(int asNum, String rtrId)
140 throws TException, BgpRouterException {
141 bop.type = START_BGP;
144 logger.info("Starting BGP Server with as number " + asNum + " and router ID " + rtrId);
148 public synchronized void addNeighbor(String nbrIp, int nbrAsNum)
149 throws TException, BgpRouterException {
152 bop.nbrAsNum = nbrAsNum;
153 logger.info("Adding BGP Neighbor " + nbrIp + " with as number " + nbrAsNum);
157 public synchronized void delNeighbor(String nbrIp)
158 throws TException, BgpRouterException {
161 logger.info("Deleting BGP Neighbor " + nbrIp);
165 public synchronized void addVrf(String rd, List<String> irts, List<String> erts)
166 throws TException, BgpRouterException {
171 logger.info("Adding BGP VRF rd: " + rd);
175 public synchronized void delVrf(String rd)
176 throws TException, BgpRouterException {
179 logger.info("Deleting BGP VRF rd: " + rd);
183 public synchronized void addPrefix(String rd, String prefix, String nexthop, int label)
184 throws TException, BgpRouterException {
190 logger.info("Adding BGP route - rd:" + rd + " prefix:" + prefix + " nexthop:" + nexthop + " label:" + label);
194 public synchronized void delPrefix(String rd, String prefix)
195 throws TException, BgpRouterException {
199 logger.info("Deleting BGP route - rd:" + rd + " prefix:" + prefix);
203 public int initRibSync(BgpSyncHandle handle)
204 throws TException, BgpRouterException {
205 if (bgpClient == null)
206 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
207 if (handle.getState() == BgpSyncHandle.ITERATING)
208 return BgpRouterException.BGP_ERR_IN_ITER;
209 handle.setState(BgpSyncHandle.INITED);
214 public int endRibSync(BgpSyncHandle handle)
215 throws TException, BgpRouterException {
216 if (bgpClient == null)
217 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
218 int state = handle.getState();
220 case BgpSyncHandle.INITED:
221 case BgpSyncHandle.ITERATING:
222 handle.setState(BgpSyncHandle.ABORTED);
224 case BgpSyncHandle.DONE:
226 case BgpSyncHandle.NEVER_DONE:
227 return BgpRouterException.BGP_ERR_NOT_ITER;
234 public Routes doRibSync(BgpSyncHandle handle)
235 throws TException, BgpRouterException {
236 if (bgpClient == null)
237 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
238 int state = handle.getState();
239 if (state != BgpSyncHandle.INITED && state != BgpSyncHandle.ITERATING) {
240 Routes r = new Routes();
241 r.setErrcode(BgpRouterException.BGP_ERR_NOT_ITER);
244 int op = (state == BgpSyncHandle.INITED) ?
245 GET_RTS_INIT : GET_RTS_NEXT;
246 handle.setState(BgpSyncHandle.ITERATING);
247 int winSize = handle.getMaxCount()*handle.getRouteSize();
248 Routes outRoutes = bgpClient.getRoutes(op, winSize);
249 if (outRoutes.errcode != 0)
251 handle.setMore(outRoutes.more);
252 if (outRoutes.more == 0)
253 handle.setState(BgpSyncHandle.DONE);
257 //We would support this only when we support controller restarts
258 public void doRouteSync()
259 throws TException, BgpRouterException {
260 BgpSyncHandle bsh = BgpSyncHandle.getInstance();
263 logger.info("Starting BGP Route sync.. ");
265 while (bsh.getState() != bsh.DONE) {
266 Routes r = doRibSync(bsh);
267 if(r.getErrcode() == BgpRouterException.BGP_ERR_INACTIVE) {
268 //BGP server is inactive; log and return
269 logger.error("BGP Server is inactive. Failed BGP Route sync");
272 Iterator<Update> iter = r.getUpdatesIterator();
273 while (iter.hasNext()) {
274 Update u = iter.next();
275 Route route = new Route(u.rd, u.prefix, u.prefixlen, u.nexthop, u.label);
278 if(bgpRouteCb != null) {
279 bgpRouteCb.setRoute(route);
284 logger.info("Completed BGP Route sync.");
285 } catch (Exception e) {
291 public List<Route> getRoutes()
292 throws TException, BgpRouterException {
294 BgpSyncHandle bsh = BgpSyncHandle.getInstance();
295 List<Route> allRoutes = new ArrayList<Route>();
299 while (bsh.getState() != bsh.DONE) {
300 Routes r = doRibSync(bsh);
301 Iterator<Update> iter = r.getUpdatesIterator();
302 while (iter.hasNext()) {
303 Update u = iter.next();
304 Route route = new Route(u.rd, u.prefix, u.prefixlen, u.nexthop, u.label);
306 allRoutes.add(route);
310 } catch (Exception e) {