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;
20 private TProtocol protocol;
21 private static BgpConfigurator.Client bgpClient=null;
22 private static final Logger LOGGER = LoggerFactory.getLogger(BgpRouter.class);
25 private final static int ADD_NBR = 1;
26 private final static int DEL_NBR = 2;
27 private final static int ADD_VRF = 3;
28 private final static int DEL_VRF = 4;
29 private final static int ADD_PFX = 5;
30 private final static int DEL_PFX = 6;
31 private final static int START_BGP = 7;
33 private final static int GET_RTS_INIT = 0;
34 private final static int GET_RTS_NEXT = 1;
42 public List<String> irts;
43 public List<String> erts;
49 public static final int ignore = 0;
53 static BgpOp bop = null;
55 private String bgpHost;
62 public void setBgpServer(String host, int port) {
67 public void connect(String bgpHost, int bgpPort)
68 throws TException, BgpRouterException {
69 this.bgpHost = bgpHost;
70 this.bgpPort = bgpPort;
73 LOGGER.info("Connecting to BGP Server " + bgpHost + " on port " + bgpPort);
75 } catch (Exception e) {
76 LOGGER.error("Failed connecting to BGP server ");
81 public void disconnect() {
82 if(transport != null) {
88 throws TException, BgpRouterException {
89 if(transport != null) {
92 transport = new TSocket(bgpHost, bgpPort);
93 ((TSocket)transport).setTimeout(Constants.CL_SKT_TIMEO_MS);
95 protocol = new TBinaryProtocol(transport);
96 bgpClient = new BgpConfigurator.Client(protocol);
102 private void dispatch(BgpOp op)
103 throws TException, BgpRouterException {
106 if (bgpClient == null) {
107 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
112 result = bgpClient.startBgpServer(op.asNum, op.rtrId,
113 op.ignore, op.ignore, op.ignore);
116 result = bgpClient.createPeer(op.nbrIp, op.nbrAsNum);
119 result = bgpClient.deletePeer(op.nbrIp);
122 result = bgpClient.addVrf(op.rd, op.irts, op.erts);
125 result = bgpClient.delVrf(op.rd);
128 result = bgpClient.pushRoute(op.pfx, op.nh, op.rd, op.lbl);
131 result = bgpClient.withdrawRoute(op.pfx, op.rd);
136 throw new BgpRouterException(result);
140 public void startBgp(int asNum, String rtrId)
141 throws TException, BgpRouterException {
142 bop.type = START_BGP;
145 LOGGER.info("Starting BGP Server with as number " + asNum + " and router ID " + rtrId);
149 public synchronized void addNeighbor(String nbrIp, int nbrAsNum)
150 throws TException, BgpRouterException {
153 bop.nbrAsNum = nbrAsNum;
154 LOGGER.info("Adding BGP Neighbor " + nbrIp + " with as number " + nbrAsNum);
158 public synchronized void delNeighbor(String nbrIp)
159 throws TException, BgpRouterException {
162 LOGGER.info("Deleting BGP Neighbor " + nbrIp);
166 public synchronized void addVrf(String rd, List<String> irts, List<String> erts)
167 throws TException, BgpRouterException {
172 LOGGER.info("Adding BGP VRF rd: " + rd);
176 public synchronized void delVrf(String rd)
177 throws TException, BgpRouterException {
180 LOGGER.info("Deleting BGP VRF rd: " + rd);
184 public synchronized void addPrefix(String rd, String prefix, String nexthop, int label)
185 throws TException, BgpRouterException {
191 LOGGER.info("Adding BGP route - rd:" + rd + " prefix:" + prefix + " nexthop:" + nexthop + " label:" + label);
195 public synchronized void delPrefix(String rd, String prefix)
196 throws TException, BgpRouterException {
200 LOGGER.info("Deleting BGP route - rd:" + rd + " prefix:" + prefix);
204 public int initRibSync(BgpSyncHandle handle)
205 throws TException, BgpRouterException {
206 if (bgpClient == null) {
207 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
209 if (handle.getState() == BgpSyncHandle.ITERATING) {
210 return BgpRouterException.BGP_ERR_IN_ITER;
212 handle.setState(BgpSyncHandle.INITED);
217 public int endRibSync(BgpSyncHandle handle)
218 throws TException, BgpRouterException {
219 if (bgpClient == null) {
220 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
222 int state = handle.getState();
224 case BgpSyncHandle.INITED:
225 case BgpSyncHandle.ITERATING:
226 handle.setState(BgpSyncHandle.ABORTED);
228 case BgpSyncHandle.DONE:
230 case BgpSyncHandle.NEVER_DONE:
231 return BgpRouterException.BGP_ERR_NOT_ITER;
238 public Routes doRibSync(BgpSyncHandle handle)
239 throws TException, BgpRouterException {
240 if (bgpClient == null) {
241 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
243 int state = handle.getState();
244 if (state != BgpSyncHandle.INITED && state != BgpSyncHandle.ITERATING) {
245 Routes r = new Routes();
246 r.setErrcode(BgpRouterException.BGP_ERR_NOT_ITER);
249 int op = (state == BgpSyncHandle.INITED) ?
250 GET_RTS_INIT : GET_RTS_NEXT;
251 handle.setState(BgpSyncHandle.ITERATING);
252 int winSize = handle.getMaxCount()*handle.getRouteSize();
253 Routes outRoutes = bgpClient.getRoutes(op, winSize);
254 if (outRoutes.errcode != 0) {
257 handle.setMore(outRoutes.more);
258 if (outRoutes.more == 0) {
259 handle.setState(BgpSyncHandle.DONE);
264 //We would support this only when we support controller restarts
265 public void doRouteSync()
266 throws TException, BgpRouterException {
267 BgpSyncHandle bsh = BgpSyncHandle.getInstance();
270 LOGGER.info("Starting BGP Route sync.. ");
272 while (bsh.getState() != bsh.DONE) {
273 Routes r = doRibSync(bsh);
274 if(r.getErrcode() == BgpRouterException.BGP_ERR_INACTIVE) {
275 //BGP server is inactive; log and return
276 LOGGER.error("BGP Server is inactive. Failed BGP Route sync");
279 Iterator<Update> iter = r.getUpdatesIterator();
280 while (iter.hasNext()) {
281 Update u = iter.next();
282 Route route = new Route(u.rd, u.prefix, u.prefixlen, u.nexthop, u.label);
285 if(bgpRouteCb != null) {
286 bgpRouteCb.setRoute(route);
291 LOGGER.info("Completed BGP Route sync.");
292 } catch (Exception e) {