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;
12 import org.apache.thrift.protocol.TBinaryProtocol;
13 import org.apache.thrift.protocol.TProtocol;
14 import org.apache.thrift.TException;
15 import org.apache.thrift.transport.TSocket;
16 import org.apache.thrift.transport.TTransport;
17 import org.apache.thrift.transport.TTransportException;
18 import org.opendaylight.bgpmanager.thrift.gen.*;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class BgpRouter {
23 private static TTransport transport;
24 private static TProtocol protocol;
25 private static BgpConfigurator.Client bgpClient=null;
26 private static final Logger LOGGER = LoggerFactory.getLogger(BgpRouter.class);
29 START, STOP, NBR, VRF, PFX, SRC, MHOP, LOG, AF, GR
32 private final static int GET_RTS_INIT = 0;
33 private final static int GET_RTS_NEXT = 1;
34 private final static int CONNECTION_TIMEOUT = 2000;
42 public List<String> irts;
43 public List<String> erts;
44 public static final int ignore = 0;
51 private static BgpOp bop;
53 public synchronized void disconnect() {
55 if (transport != null) {
60 public synchronized boolean connect(String bgpHost, int bgpPort) {
61 String msgPiece = "BGP config server at "+bgpHost+":"+bgpPort;
65 TSocket ts = new TSocket(bgpHost, bgpPort, CONNECTION_TIMEOUT);
69 } catch (TTransportException tte) {
70 LOGGER.error("Failed connecting to "+msgPiece+
74 protocol = new TBinaryProtocol(transport);
75 bgpClient = new BgpConfigurator.Client(protocol);
76 LOGGER.info("Connected to "+msgPiece);
84 private static BgpRouter br = null;
86 public static synchronized BgpRouter getInstance() {
87 return (br == null ? br = new BgpRouter() : br);
90 private void dispatch(BgpOp op)
91 throws TException, BgpRouterException {
94 if (bgpClient == null) {
95 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
100 result = bgpClient.startBgp(op.ints[0], op.strs[0],
101 op.ignore, op.ignore, op.ignore, op.ints[1], op.add);
104 result = bgpClient.stopBgp(op.ints[0]);
108 bgpClient.createPeer(op.strs[0], op.ints[0])
109 : bgpClient.deletePeer(op.strs[0]);
113 bgpClient.addVrf(op.strs[0], op.irts, op.erts)
114 : bgpClient.delVrf(op.strs[0]);
117 // order of args is different in addPrefix(), hence the
118 // seeming out-of-order-ness of string indices
120 bgpClient.pushRoute(op.strs[1], op.strs[2],
121 op.strs[0], op.ints[0])
122 : bgpClient.withdrawRoute(op.strs[1], op.strs[0]);
125 result = bgpClient.setLogConfig(op.strs[0], op.strs[1]);
129 bgpClient.setEbgpMultihop(op.strs[0], op.ints[0])
130 : bgpClient.unsetEbgpMultihop(op.strs[0]);
134 bgpClient.setUpdateSource(op.strs[0], op.strs[1])
135 : bgpClient.unsetUpdateSource(op.strs[0]);
139 af_afi afi = af_afi.findByValue(op.ints[0]);
140 af_safi safi = af_safi.findByValue(op.ints[1]);
142 bgpClient.enableAddressFamily(op.strs[0], afi, safi)
143 : bgpClient.disableAddressFamily(op.strs[0], afi, safi);
147 bgpClient.enableGracefulRestart(op.ints[0])
148 : bgpClient.disableGracefulRestart();
152 throw new BgpRouterException(result);
156 public synchronized void startBgp(int asNum, String rtrId, int stalepathTime,
157 boolean announceFbit)
158 throws TException, BgpRouterException {
159 bop.type = Optype.START;
160 bop.add = announceFbit;
162 bop.ints[1] = stalepathTime;
164 LOGGER.debug("Starting BGP with as number {} and router ID {} ", asNum, rtrId);
168 public synchronized void stopBgp(int asNum)
169 throws TException, BgpRouterException {
170 bop.type = Optype.STOP;
172 LOGGER.debug("Stopping BGP with as number {}", asNum);
176 public synchronized void addNeighbor(String nbrIp, int nbrAsNum)
177 throws TException, BgpRouterException {
178 bop.type = Optype.NBR;
181 bop.ints[0] = nbrAsNum;
182 LOGGER.debug("Adding BGP Neighbor {} with as number {} ", nbrIp, nbrAsNum);
186 public synchronized void delNeighbor(String nbrIp)
187 throws TException, BgpRouterException {
188 bop.type = Optype.NBR;
191 LOGGER.debug("Deleting BGP Neighbor {} ", nbrIp);
195 public synchronized void addVrf(String rd, List<String> irts, List<String> erts)
196 throws TException, BgpRouterException {
197 bop.type = Optype.VRF;
202 LOGGER.debug("Adding BGP VRF rd: {} ", rd);
206 public synchronized void delVrf(String rd)
207 throws TException, BgpRouterException {
208 bop.type = Optype.VRF;
211 LOGGER.debug("Deleting BGP VRF rd: {} " + rd);
215 // bit of a mess-up: the order of arguments is different in
216 // the Thrift RPC: prefix-nexthop-rd-label.
218 public synchronized void addPrefix(String rd, String prefix, String nexthop, int label)
219 throws TException, BgpRouterException {
220 bop.type = Optype.PFX;
223 bop.strs[1] = prefix;
224 bop.strs[2] = nexthop;
226 LOGGER.debug("Adding BGP route - rd:{} prefix:{} nexthop:{} label:{} ", rd ,prefix, nexthop, label);
230 public synchronized void delPrefix(String rd, String prefix)
231 throws TException, BgpRouterException {
232 bop.type = Optype.PFX;
235 bop.strs[1] = prefix;
236 LOGGER.debug("Deleting BGP route - rd:{} prefix:{} ", rd, prefix);
240 public int initRibSync(BgpSyncHandle handle)
241 throws TException, BgpRouterException {
242 if (bgpClient == null) {
243 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
245 if (handle.getState() == BgpSyncHandle.ITERATING) {
246 return BgpRouterException.BGP_ERR_IN_ITER;
248 handle.setState(BgpSyncHandle.INITED);
253 public int endRibSync(BgpSyncHandle handle)
254 throws TException, BgpRouterException {
255 if (bgpClient == null) {
256 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
258 int state = handle.getState();
260 case BgpSyncHandle.INITED:
261 case BgpSyncHandle.ITERATING:
262 handle.setState(BgpSyncHandle.ABORTED);
264 case BgpSyncHandle.DONE:
266 case BgpSyncHandle.NEVER_DONE:
267 return BgpRouterException.BGP_ERR_NOT_ITER;
274 public Routes doRibSync(BgpSyncHandle handle)
275 throws TException, BgpRouterException {
276 if (bgpClient == null) {
277 throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
279 int state = handle.getState();
280 if (state != BgpSyncHandle.INITED && state != BgpSyncHandle.ITERATING) {
281 Routes r = new Routes();
282 r.setErrcode(BgpRouterException.BGP_ERR_NOT_ITER);
285 int op = (state == BgpSyncHandle.INITED) ?
286 GET_RTS_INIT : GET_RTS_NEXT;
287 handle.setState(BgpSyncHandle.ITERATING);
288 int winSize = handle.getMaxCount()*handle.getRouteSize();
289 Routes outRoutes = bgpClient.getRoutes(op, winSize);
290 if (outRoutes.errcode != 0) {
293 handle.setMore(outRoutes.more);
294 if (outRoutes.more == 0) {
295 handle.setState(BgpSyncHandle.DONE);
300 public synchronized void setLogging(String fileName, String debugLevel)
301 throws TException, BgpRouterException {
302 bop.type = Optype.LOG;
303 bop.strs[0] = fileName;
304 bop.strs[1] = debugLevel;
305 LOGGER.debug("Setting Log file to BGP VRF rd: {} ", fileName, debugLevel);
309 public synchronized void addEbgpMultihop(String nbrIp, int nhops)
310 throws TException, BgpRouterException {
311 bop.type = Optype.MHOP;
315 LOGGER.debug("ebgp-multihop set for peer {}, num hops = {}",
320 public synchronized void delEbgpMultihop(String nbrIp)
321 throws TException, BgpRouterException {
322 bop.type = Optype.MHOP;
325 LOGGER.debug("ebgp-multihop deleted for peer {}", nbrIp);
329 public synchronized void addUpdateSource(String nbrIp, String srcIp)
330 throws TException, BgpRouterException {
331 bop.type = Optype.SRC;
335 LOGGER.debug("update-source added for peer {}, src-ip = {}",
340 public synchronized void delUpdateSource(String nbrIp)
341 throws TException, BgpRouterException {
342 bop.type = Optype.SRC;
345 LOGGER.debug("update-source deleted for peer {}", nbrIp);
349 public synchronized void addAddressFamily(String nbrIp,
350 af_afi afi, af_safi safi)
351 throws TException, BgpRouterException {
352 bop.type = Optype.AF;
355 bop.ints[0] = afi.getValue();
356 bop.ints[1] = safi.getValue();
357 LOGGER.debug("addr family added for peer {}, afi = {}, safi = {}",
358 nbrIp, bop.ints[0], bop.ints[1]);
362 public synchronized void delAddressFamily(String nbrIp,
363 af_afi afi, af_safi safi)
364 throws TException, BgpRouterException {
365 bop.type = Optype.AF;
368 bop.ints[0] = afi.getValue();
369 bop.ints[1] = safi.getValue();
370 LOGGER.debug("addr family deleted for peer {}, afi = {}, safi = {}",
371 nbrIp, bop.ints[0], bop.ints[1]);
375 public synchronized void addGracefulRestart(int stalepathTime)
376 throws TException, BgpRouterException {
377 bop.type = Optype.GR;
379 bop.ints[0] = stalepathTime;
380 LOGGER.debug("graceful restart added, stale-path-time = {}",
385 public synchronized void delGracefulRestart()
386 throws TException, BgpRouterException {
387 bop.type = Optype.GR;
389 LOGGER.debug("graceful restart deleted");