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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class BgpThriftService {
37 private static final Logger LOG = LoggerFactory.getLogger(BgpThriftService.class);
39 private final InetSocketAddress bgpThriftServerSocketAddr;
40 private final IBgpManager bgpManager;
41 private final BgpConfigurationManager bgpConfigManager;
42 private volatile TServer server;
43 private static ThreadFactory thriftServerThreadFactory = new ThreadFactoryBuilder()
44 .setNameFormat("bgp-thrift-server-%d").build();
45 private static ExecutorService threadPool = Executors.newFixedThreadPool(1, thriftServerThreadFactory);
46 private volatile Future ft;
48 public BgpThriftService(InetSocketAddress bgpThriftServerSocketAddr, IBgpManager bm,
49 BgpConfigurationManager bgpConfigManager) {
50 this.bgpThriftServerSocketAddr = bgpThriftServerSocketAddr;
52 this.bgpConfigManager = bgpConfigManager;
55 public static class ThriftClientContext implements ServerContext {
58 public ThriftClientContext(TProtocol in) {
62 public TProtocol getIn() {
67 public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
69 ThriftClientContext oldThriftClientContext;
77 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
79 TNonblockingServerTransport trans = new TNonblockingServerSocket(bgpThriftServerSocketAddr);
80 TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(trans);
81 args.transportFactory(new TFramedTransport.Factory());
82 args.protocolFactory(new TBinaryProtocol.Factory());
83 args.processor(processor);
84 args.selectorThreads(1);
85 args.workerThreads(1);
86 server = new TThreadedSelectorServer(args);
87 server.setServerEventHandler(new TServerEventHandler() {
89 public void preServe() {
90 LOG.info("Bgp thrift server pre serve event");
94 public ServerContext createContext(TProtocol input, TProtocol output) {
95 LOG.info("Bgp thrift server create context event");
97 if (oldThriftClientContext != null) {
98 LOG.info("Bgp thrift server closing old context");
99 oldThriftClientContext.getIn().getTransport().close();
101 LOG.info("Bgp thrift server old context is null nothing to close");
103 oldThriftClientContext = new ThriftClientContext(input);
104 return oldThriftClientContext;
109 public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
110 LOG.info("Bgp thrift server delete context event");
111 if (oldThriftClientContext == serverContext) {
112 LOG.info("Bgp thrift server cleanup old context");
113 oldThriftClientContext = null;
115 LOG.info("Bgp thrift server cleanup context");
120 public void processContext(ServerContext serverContext, TTransport inputTransport,
121 TTransport outputTransport) {
122 LOG.trace("Bgp thrift server process context event");
126 } catch (TTransportException e) {
127 LOG.error("Exception in BGP Updater server", e);
132 @SuppressWarnings("checkstyle:IllegalCatch")
133 public void onUpdatePushRoute(protocol_type protocolType,
145 LOG.debug("Update on push route : rd {} prefix {} plen {}", rd, prefix, plen);
147 // l2label is ignored even in case of RT5. only l3label considered
148 bgpConfigManager.onUpdatePushRoute(
162 public void onUpdateWithdrawRoute(protocol_type protocolType,
173 LOG.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
174 bgpConfigManager.onUpdateWithdrawRoute(
184 public void onStartConfigResyncNotification() {
185 LOG.info("BGP (re)started");
186 bgpManager.setQbgprestartTS(System.currentTimeMillis());
187 bgpManager.bgpRestarted();
191 public void onNotificationSendEvent(String prefix, byte errCode,
193 bgpManager.sendNotificationEvent((int) errCode, (int) errSubcode);
197 public synchronized void start() {
198 ft = threadPool.submit(new BgpUpdateServer());
201 public boolean isBgpThriftServiceStarted() {
202 return ((ft == null) ? false : true);
205 public synchronized void stop() {
206 if (server != null) {
210 if (ft != null && !ft.isDone()) {