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.netvirt.bgpmanager.thrift.server;
11 import com.google.common.util.concurrent.ThreadFactoryBuilder;
12 import java.net.InetSocketAddress;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.ThreadFactory;
17 import org.apache.thrift.protocol.TBinaryProtocol;
18 import org.apache.thrift.protocol.TProtocol;
19 import org.apache.thrift.server.ServerContext;
20 import org.apache.thrift.server.TServer;
21 import org.apache.thrift.server.TServerEventHandler;
22 import org.apache.thrift.server.TThreadedSelectorServer;
23 import org.apache.thrift.transport.TFramedTransport;
24 import org.apache.thrift.transport.TNonblockingServerSocket;
25 import org.apache.thrift.transport.TNonblockingServerTransport;
26 import org.apache.thrift.transport.TTransport;
27 import org.apache.thrift.transport.TTransportException;
28 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
29 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
30 import org.opendaylight.netvirt.bgpmanager.thrift.gen.BgpUpdater;
31 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
32 import org.opendaylight.netvirt.bgpmanager.thrift.gen.protocol_type;
33 import org.opendaylight.yangtools.yang.common.Uint32;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class BgpThriftService {
38 private static final Logger LOG = LoggerFactory.getLogger(BgpThriftService.class);
40 private final InetSocketAddress bgpThriftServerSocketAddr;
41 private final IBgpManager bgpManager;
42 private final BgpConfigurationManager bgpConfigManager;
43 private volatile TServer server;
44 private static ThreadFactory thriftServerThreadFactory = new ThreadFactoryBuilder()
45 .setNameFormat("bgp-thrift-server-%d").build();
46 private static ExecutorService threadPool = Executors.newFixedThreadPool(1, thriftServerThreadFactory);
47 private volatile Future ft;
49 public BgpThriftService(InetSocketAddress bgpThriftServerSocketAddr, IBgpManager bm,
50 BgpConfigurationManager bgpConfigManager) {
51 this.bgpThriftServerSocketAddr = bgpThriftServerSocketAddr;
53 this.bgpConfigManager = bgpConfigManager;
56 public static class ThriftClientContext implements ServerContext {
59 public ThriftClientContext(TProtocol in) {
63 public TProtocol getIn() {
68 public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
70 ThriftClientContext oldThriftClientContext;
78 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
80 TNonblockingServerTransport trans = new TNonblockingServerSocket(bgpThriftServerSocketAddr);
81 TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(trans);
82 args.transportFactory(new TFramedTransport.Factory());
83 args.protocolFactory(new TBinaryProtocol.Factory());
84 args.processor(processor);
85 args.selectorThreads(1);
86 args.workerThreads(1);
87 ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("bgp-receiver-%d").build();
88 ExecutorService service = Executors.newFixedThreadPool(1, tf);
89 args.executorService(service);
90 server = new TThreadedSelectorServer(args);
91 server.setServerEventHandler(new TServerEventHandler() {
93 public void preServe() {
94 LOG.info("Bgp thrift server pre serve event");
98 public ServerContext createContext(TProtocol input, TProtocol output) {
99 LOG.info("Bgp thrift server create context event");
100 synchronized (this) {
101 if (oldThriftClientContext != null) {
102 LOG.info("Bgp thrift server closing old context");
103 oldThriftClientContext.getIn().getTransport().close();
105 LOG.info("Bgp thrift server old context is null nothing to close");
107 oldThriftClientContext = new ThriftClientContext(input);
108 return oldThriftClientContext;
113 public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
114 synchronized (this) {
115 LOG.info("Bgp thrift server delete context event");
116 if (oldThriftClientContext == serverContext) {
117 LOG.info("Bgp thrift server cleanup old context");
118 oldThriftClientContext = null;
120 LOG.info("Bgp thrift server cleanup context");
126 public void processContext(ServerContext serverContext, TTransport inputTransport,
127 TTransport outputTransport) {
128 LOG.trace("Bgp thrift server process context event");
132 } catch (TTransportException e) {
133 LOG.error("Exception in BGP Updater server", e);
138 @SuppressWarnings("checkstyle:IllegalCatch")
139 public void onUpdatePushRoute(protocol_type protocolType,
151 LOG.debug("Update on push route : rd {} prefix {} plen {}", rd, prefix, plen);
153 // l2label is ignored even in case of RT5. only l3label considered
154 bgpConfigManager.onUpdatePushRoute(
161 Uint32.valueOf(l3label),
162 Uint32.valueOf(l2label),
168 public void onUpdateWithdrawRoute(protocol_type protocolType,
179 LOG.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
180 bgpConfigManager.onUpdateWithdrawRoute(
190 public void peerDown(String ipAddress, long asNumber) {
191 LOG.info("PeerDown {} : {} From BGP", ipAddress, asNumber);
192 bgpConfigManager.peerDown(ipAddress, asNumber);
196 public void peerUp(String ipAddress, long asNumber) {
197 LOG.info("PeerUp {} : {} from BGP", ipAddress, asNumber);
198 bgpConfigManager.peerUp(ipAddress, asNumber);
202 public void onStartConfigResyncNotification() {
203 LOG.info("BGP (re)started");
204 bgpManager.setQbgprestartTS(System.currentTimeMillis());
205 bgpManager.bgpRestarted();
209 public void onNotificationSendEvent(String prefix, byte errCode,
211 bgpManager.sendNotificationEvent((int) errCode, (int) errSubcode);
215 public synchronized void start() {
216 if (isBgpThriftServiceStarted()) {
219 ft = threadPool.submit(new BgpUpdateServer());
222 public boolean isBgpThriftServiceStarted() {
223 return ((ft == null) ? false : true);
226 public synchronized void stop() {
227 if (server != null) {
231 if (ft != null && !ft.isDone()) {