921c9cf96217643c340dd9b0f6826e5c4d2632fd
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / thrift / server / implementation / BgpThriftService.java
1 package org.opendaylight.bgpmanager.thrift.server.implementation;
2
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;
14
15 public class BgpThriftService {
16         
17         private static final Logger LOGGER = LoggerFactory.getLogger(BgpThriftService.class);
18         
19         private int port;
20         private int maxWorkerThreads;
21         private int minWorkerThreads;
22     private TServerTransport serverTransport;
23     private TServer server;
24         private BgpUpdateHandler notificationHandler;
25         private BgpManager bgpManager;
26     
27     public BgpThriftService(BgpManager bgpMgr, FibDSWriter dsWriter) {
28         bgpManager = bgpMgr;
29                 notificationHandler = new BgpUpdateHandler(bgpManager, dsWriter);
30     }
31
32
33         public void start() {
34                 LOGGER.debug("BGP Thrift Server starting...");
35                 startBgpThriftServer();
36         }
37         
38         public void stop() {
39                 LOGGER.debug("BGP Thrift Server stopping...");
40                 stopBgpThriftServer();
41         }
42
43         /**
44          * Destroy method called up after the bundle has been stopped
45          */
46         public void destroy() {
47                 LOGGER.debug("BGP Thrift Server destroy ");
48         }
49
50     /**
51      * Loading the parameters required for a connection
52      * 
53      */
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);
60
61         }
62
63
64         public void startBgpThriftServer() {
65                 loadParameters();
66                 new Thread(new ThriftRunnable()).start();
67         }
68
69         public void stopBgpThriftServer() {
70                 try {
71             if (serverTransport != null) {
72                 serverTransport.close();
73             }
74             
75             server.stop();
76                         LOGGER.info("BGP Thrift Server stopped");
77         } catch (Exception e) {
78             LOGGER.error("Error while stopping the server - {} {}", getClass().getName(), e.getMessage());
79         }
80         }
81         
82         private class ThriftRunnable implements Runnable {
83                 @Override
84                 public void run() {
85
86                 try {
87                                 serverTransport = new TServerSocket(port);
88                                 LOGGER.info("BGP Thrift Server started on port {} ", port);
89                         } catch (TTransportException e) {
90                                 LOGGER.error("Transport Exception while starting bgp thrift server", e);
91                                 return;
92                         }
93                         /* This may need to change. Right now, its as good as a blocking server for each client (client would be
94                         single - qbgp). We may want to change the server to TSimpleServer which queues up the notifications and
95                         let worker threads process notifications based on rd.
96                          */
97                 server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport)
98                         .maxWorkerThreads(maxWorkerThreads).minWorkerThreads(minWorkerThreads)
99                         .processor(new BgpUpdater.Processor<BgpUpdateHandler>(notificationHandler)));
100                         server.serve();
101                 }               
102         }
103
104 }