1 package org.opendaylight.bgpmanager.thrift.server.implementation;
3 import org.apache.thrift.server.THsHaServer;
4 import org.apache.thrift.server.TServer;
5 import org.apache.thrift.server.TSimpleServer;
6 import org.apache.thrift.server.TThreadPoolServer;
7 import org.apache.thrift.transport.*;
8 import org.opendaylight.bgpmanager.BgpManager;
9 import org.opendaylight.bgpmanager.FibDSWriter;
10 import org.opendaylight.bgpmanager.thrift.common.Constants;
11 import org.opendaylight.bgpmanager.thrift.gen.BgpUpdater;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 public class BgpThriftService {
17 private static final Logger LOGGER = LoggerFactory.getLogger(BgpThriftService.class);
20 private int maxWorkerThreads;
21 private int minWorkerThreads;
22 private TServerTransport serverTransport;
23 private TServer server;
24 private BgpUpdateHandler notificationHandler;
25 private BgpManager bgpManager;
27 public BgpThriftService(BgpManager bgpMgr, FibDSWriter dsWriter) {
29 notificationHandler = new BgpUpdateHandler(bgpManager, dsWriter);
34 LOGGER.info("BGP Thrift Server starting.");
35 startBgpThriftServer();
39 LOGGER.info("BGP Thrift Server stopping.");
40 stopBgpThriftServer();
44 * Destroy method called up after the bundle has been stopped
46 public void destroy() {
47 LOGGER.debug("BGP Thrift Server destroy ");
51 * Loading the parameters required for a connection
54 private void loadParameters() {
55 port = Integer.getInteger(Constants.PROP_BGP_THRIFT_PORT, Constants.BGP_SERVICE_PORT);
56 maxWorkerThreads = Integer.getInteger(Constants.PROP_MAX_WORKER_THREADS,
57 Constants.DEFAULT_MAX_WORKER_THREADS);
58 minWorkerThreads = Integer.getInteger(Constants.PROP_MIN_WORKER_THREADS,
59 Constants.DEFAULT_MIN_WORKER_THREADS);
64 public void startBgpThriftServer() {
66 new Thread(new ThriftRunnable()).start();
69 public void stopBgpThriftServer() {
71 LOGGER.debug("Server stopping");
73 if (serverTransport != null) {
74 serverTransport.close();
78 } catch (Exception e) {
79 LOGGER.error("Error while stopping the server - {} {}", getClass().getName(), e.getMessage());
83 private class ThriftRunnable implements Runnable {
88 serverTransport = new TServerSocket(port);
89 LOGGER.info("Server Socket on Port {} ", port);
90 } catch (TTransportException e) {
91 LOGGER.error("Transport Exception while starting bgp thrift server", e);
94 /* This may need to change. Right now, its as good as a blocking server for each client (client would be
95 single - qbgp). We may want to change the server to TSimpleServer which queues up the notifications and
96 let worker threads process notifications based on rd.
98 server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport)
99 .maxWorkerThreads(maxWorkerThreads).minWorkerThreads(minWorkerThreads)
100 .processor(new BgpUpdater.Processor<BgpUpdateHandler>(notificationHandler)));