2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.bgpmanager.thrift.client.implementation;
11 import org.apache.thrift.TException;
12 import org.apache.thrift.transport.TTransport;
13 import org.apache.thrift.transport.TSocket;
14 import org.apache.thrift.protocol.TBinaryProtocol;
15 import org.apache.thrift.protocol.TProtocol;
18 import org.opendaylight.bgpmanager.thrift.client.globals.Route;
19 import org.opendaylight.bgpmanager.thrift.common.Constants;
20 import org.opendaylight.bgpmanager.thrift.gen.*;
21 import org.opendaylight.bgpmanager.thrift.exceptions.BgpRouterException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public class BgpRouter {
27 private TTransport transport;
28 private TProtocol protocol;
29 private static BgpConfigurator.Client bgpClient=null;
30 private static final Logger LOGGER = LoggerFactory.getLogger(BgpRouter.class);
33 private final static int ADD_NBR = 1;
34 private final static int DEL_NBR = 2;
35 private final static int ADD_VRF = 3;
36 private final static int DEL_VRF = 4;
37 private final static int ADD_PFX = 5;
38 private final static int DEL_PFX = 6;
39 private final static int START_BGP = 7;
41 private final static int GET_RTS_INIT = 0;
42 private final static int GET_RTS_NEXT = 1;
50 public List<String> irts;
51 public List<String> erts;
57 public static final int ignore = 0;
61 static BgpOp bop = null;
63 private String bgpHost;
70 public void setBgpServer(String host, int port) {
75 public void connect(String bgpHost, int bgpPort)
76 throws TException, BgpRouterException {
77 this.bgpHost = bgpHost;
78 this.bgpPort = bgpPort;
81 LOGGER.debug("Connecting to BGP Server " + bgpHost + " on port " + bgpPort);
83 } catch (Exception e) {
84 LOGGER.error("Failed connecting to BGP server ");
89 public void disconnect() {
90 if(transport != null) {
96 throws TException, BgpRouterException {
97 if(transport != null) {
100 transport = new TSocket(bgpHost, bgpPort);
101 ((TSocket)transport).setTimeout(Constants.CL_SKT_TIMEO_MS);
103 protocol = new TBinaryProtocol(transport);
104 bgpClient = new BgpConfigurator.Client(protocol);
110 private void dispatch(BgpOp op)
111 throws TException, BgpRouterException {
114 if (bgpClient == null) {
115 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
120 result = bgpClient.startBgpServer(op.asNum, op.rtrId,
121 op.ignore, op.ignore, op.ignore);
124 result = bgpClient.createPeer(op.nbrIp, op.nbrAsNum);
127 result = bgpClient.deletePeer(op.nbrIp);
130 result = bgpClient.addVrf(op.rd, op.irts, op.erts);
133 result = bgpClient.delVrf(op.rd);
136 result = bgpClient.pushRoute(op.pfx, op.nh, op.rd, op.lbl);
139 result = bgpClient.withdrawRoute(op.pfx, op.rd);
144 throw new BgpRouterException(result);
148 public void startBgp(int asNum, String rtrId)
149 throws TException, BgpRouterException {
150 bop.type = START_BGP;
153 LOGGER.debug("Starting BGP Server with as number {} and router ID {} ", asNum, rtrId);
157 public synchronized void addNeighbor(String nbrIp, int nbrAsNum)
158 throws TException, BgpRouterException {
161 bop.nbrAsNum = nbrAsNum;
162 LOGGER.debug("Adding BGP Neighbor {} with as number {} ", nbrIp, nbrAsNum);
166 public synchronized void delNeighbor(String nbrIp)
167 throws TException, BgpRouterException {
170 LOGGER.debug("Deleting BGP Neighbor {} ", nbrIp);
174 public synchronized void addVrf(String rd, List<String> irts, List<String> erts)
175 throws TException, BgpRouterException {
180 LOGGER.debug("Adding BGP VRF rd: {} ", rd);
184 public synchronized void delVrf(String rd)
185 throws TException, BgpRouterException {
188 LOGGER.debug("Deleting BGP VRF rd: {} " + rd);
192 public synchronized void addPrefix(String rd, String prefix, String nexthop, int label)
193 throws TException, BgpRouterException {
199 LOGGER.debug("Adding BGP route - rd:{} prefix:{} nexthop:{} label:{} ", rd ,prefix, nexthop, label);
203 public synchronized void delPrefix(String rd, String prefix)
204 throws TException, BgpRouterException {
208 LOGGER.debug("Deleting BGP route - rd:{} prefix:{} ", rd, prefix);
212 public int initRibSync(BgpSyncHandle handle)
213 throws TException, BgpRouterException {
214 if (bgpClient == null) {
215 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
217 if (handle.getState() == BgpSyncHandle.ITERATING) {
218 return BgpRouterException.BGP_ERR_IN_ITER;
220 handle.setState(BgpSyncHandle.INITED);
225 public int endRibSync(BgpSyncHandle handle)
226 throws TException, BgpRouterException {
227 if (bgpClient == null) {
228 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
230 int state = handle.getState();
232 case BgpSyncHandle.INITED:
233 case BgpSyncHandle.ITERATING:
234 handle.setState(BgpSyncHandle.ABORTED);
236 case BgpSyncHandle.DONE:
238 case BgpSyncHandle.NEVER_DONE:
239 return BgpRouterException.BGP_ERR_NOT_ITER;
246 public Routes doRibSync(BgpSyncHandle handle)
247 throws TException, BgpRouterException {
248 if (bgpClient == null) {
249 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
251 int state = handle.getState();
252 if (state != BgpSyncHandle.INITED && state != BgpSyncHandle.ITERATING) {
253 Routes r = new Routes();
254 r.setErrcode(BgpRouterException.BGP_ERR_NOT_ITER);
257 int op = (state == BgpSyncHandle.INITED) ?
258 GET_RTS_INIT : GET_RTS_NEXT;
259 handle.setState(BgpSyncHandle.ITERATING);
260 int winSize = handle.getMaxCount()*handle.getRouteSize();
261 Routes outRoutes = bgpClient.getRoutes(op, winSize);
262 if (outRoutes.errcode != 0) {
265 handle.setMore(outRoutes.more);
266 if (outRoutes.more == 0) {
267 handle.setState(BgpSyncHandle.DONE);
272 //We would support this only when we support controller restarts
273 public void doRouteSync()
274 throws TException, BgpRouterException {
275 BgpSyncHandle bsh = BgpSyncHandle.getInstance();
278 LOGGER.debug("Starting BGP Route sync.. ");
280 while (bsh.getState() != bsh.DONE) {
281 Routes r = doRibSync(bsh);
282 if(r.getErrcode() == BgpRouterException.BGP_ERR_INACTIVE) {
283 //BGP server is inactive; log and return
284 LOGGER.error("BGP Server is inactive. Failed BGP Route sync");
287 Iterator<Update> iter = r.getUpdatesIterator();
288 while (iter.hasNext()) {
289 Update u = iter.next();
290 Route route = new Route(u.rd, u.prefix, u.prefixlen, u.nexthop, u.label);
293 if(bgpRouteCb != null) {
294 bgpRouteCb.setRoute(route);
299 LOGGER.debug("Completed BGP Route sync.");
300 } catch (Exception e) {