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.server.implementation;
11 import org.apache.thrift.server.THsHaServer;
12 import org.apache.thrift.server.TServer;
13 import org.apache.thrift.server.TSimpleServer;
14 import org.apache.thrift.server.TThreadPoolServer;
15 import org.apache.thrift.transport.*;
16 import org.opendaylight.bgpmanager.BgpManager;
17 import org.opendaylight.bgpmanager.FibDSWriter;
18 import org.opendaylight.bgpmanager.thrift.common.Constants;
19 import org.opendaylight.bgpmanager.thrift.gen.BgpUpdater;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class BgpThriftService {
25 private static final Logger LOGGER = LoggerFactory.getLogger(BgpThriftService.class);
28 private int maxWorkerThreads;
29 private int minWorkerThreads;
30 private TServerTransport serverTransport;
31 private TServer server;
32 private BgpUpdateHandler notificationHandler;
33 private BgpManager bgpManager;
35 public BgpThriftService(BgpManager bgpMgr, FibDSWriter dsWriter) {
37 notificationHandler = new BgpUpdateHandler(bgpManager, dsWriter);
42 LOGGER.debug("BGP Thrift Server starting...");
43 startBgpThriftServer();
47 LOGGER.debug("BGP Thrift Server stopping...");
48 stopBgpThriftServer();
52 * Destroy method called up after the bundle has been stopped
54 public void destroy() {
55 LOGGER.debug("BGP Thrift Server destroy ");
59 * Loading the parameters required for a connection
62 private void loadParameters() {
63 port = Integer.getInteger(Constants.PROP_BGP_THRIFT_PORT, Constants.BGP_SERVICE_PORT);
64 maxWorkerThreads = Integer.getInteger(Constants.PROP_MAX_WORKER_THREADS,
65 Constants.DEFAULT_MAX_WORKER_THREADS);
66 minWorkerThreads = Integer.getInteger(Constants.PROP_MIN_WORKER_THREADS,
67 Constants.DEFAULT_MIN_WORKER_THREADS);
72 public void startBgpThriftServer() {
74 new Thread(new ThriftRunnable()).start();
77 public void stopBgpThriftServer() {
79 if (serverTransport != null) {
80 serverTransport.close();
84 LOGGER.info("BGP Thrift Server stopped");
85 } catch (Exception e) {
86 LOGGER.error("Error while stopping the server - {} {}", getClass().getName(), e.getMessage());
90 private class ThriftRunnable implements Runnable {
95 serverTransport = new TServerSocket(port);
96 LOGGER.info("BGP Thrift Server started on port {} ", port);
97 } catch (TTransportException e) {
98 LOGGER.error("Transport Exception while starting bgp thrift server", e);
101 /* This may need to change. Right now, its as good as a blocking server for each client (client would be
102 single - qbgp). We may want to change the server to TSimpleServer which queues up the notifications and
103 let worker threads process notifications based on rd.
105 server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport)
106 .maxWorkerThreads(maxWorkerThreads).minWorkerThreads(minWorkerThreads)
107 .processor(new BgpUpdater.Processor<BgpUpdateHandler>(notificationHandler)));