Fix for BgpManager Sonar issues.
[netvirt.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.info("BGP Thrift Server starting.");
35                 startBgpThriftServer();
36         }
37         
38         public void stop() {
39                 LOGGER.info("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             LOGGER.debug("Server stopping");
72
73             if (serverTransport != null) {
74                 serverTransport.close();
75             }
76             
77             server.stop();
78         } catch (Exception e) {
79             LOGGER.error("Error while stopping the server - {} {}", getClass().getName(), e.getMessage());
80         }
81         }
82         
83         private class ThriftRunnable implements Runnable {
84                 @Override
85                 public void run() {
86
87                 try {
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);
92                                 return;
93                         }
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.
97                          */
98                 server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport)
99                         .maxWorkerThreads(maxWorkerThreads).minWorkerThreads(minWorkerThreads)
100                         .processor(new BgpUpdater.Processor<BgpUpdateHandler>(notificationHandler)));
101                         server.serve();
102                 }               
103         }
104
105 }