Merge "Support for CreatingTerminatingService RPC"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / thrift / server / implementation / BgpThriftService.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.bgpmanager.thrift.server.implementation;
10
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;
22
23 public class BgpThriftService {
24
25         private static final Logger LOGGER = LoggerFactory.getLogger(BgpThriftService.class);
26
27         private int port;
28         private int maxWorkerThreads;
29         private int minWorkerThreads;
30     private TServerTransport serverTransport;
31     private TServer server;
32         private BgpUpdateHandler notificationHandler;
33         private BgpManager bgpManager;
34
35     public BgpThriftService(BgpManager bgpMgr, FibDSWriter dsWriter) {
36         bgpManager = bgpMgr;
37                 notificationHandler = new BgpUpdateHandler(bgpManager, dsWriter);
38     }
39
40
41         public void start() {
42                 LOGGER.debug("BGP Thrift Server starting...");
43                 startBgpThriftServer();
44         }
45
46         public void stop() {
47                 LOGGER.debug("BGP Thrift Server stopping...");
48                 stopBgpThriftServer();
49         }
50
51         /**
52          * Destroy method called up after the bundle has been stopped
53          */
54         public void destroy() {
55                 LOGGER.debug("BGP Thrift Server destroy ");
56         }
57
58     /**
59      * Loading the parameters required for a connection
60      *
61      */
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);
68
69         }
70
71
72         public void startBgpThriftServer() {
73                 loadParameters();
74                 new Thread(new ThriftRunnable()).start();
75         }
76
77         public void stopBgpThriftServer() {
78                 try {
79             if (serverTransport != null) {
80                 serverTransport.close();
81             }
82
83             server.stop();
84                         LOGGER.info("BGP Thrift Server stopped");
85         } catch (Exception e) {
86             LOGGER.error("Error while stopping the server - {} {}", getClass().getName(), e.getMessage());
87         }
88         }
89
90         private class ThriftRunnable implements Runnable {
91                 @Override
92                 public void run() {
93
94                 try {
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);
99                                 return;
100                         }
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.
104                          */
105                 server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport)
106                         .maxWorkerThreads(maxWorkerThreads).minWorkerThreads(minWorkerThreads)
107                         .processor(new BgpUpdater.Processor<BgpUpdateHandler>(notificationHandler)));
108                         server.serve();
109                 }
110         }
111
112 }