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 java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeoutException;
15 import org.apache.thrift.protocol.TBinaryProtocol;
16 import org.apache.thrift.protocol.TProtocol;
17 import org.apache.thrift.server.ServerContext;
18 import org.apache.thrift.server.TServer;
19 import org.apache.thrift.server.TServerEventHandler;
20 import org.apache.thrift.server.TThreadedSelectorServer;
21 import org.apache.thrift.transport.TFramedTransport;
22 import org.apache.thrift.transport.TNonblockingServerSocket;
23 import org.apache.thrift.transport.TNonblockingServerTransport;
24 import org.apache.thrift.transport.TTransport;
25 import org.apache.thrift.transport.TTransportException;
26 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
27 import org.opendaylight.netvirt.bgpmanager.FibDSWriter;
28 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
29 import org.opendaylight.netvirt.bgpmanager.thrift.gen.BgpUpdater;
30 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
31 import org.opendaylight.netvirt.bgpmanager.thrift.gen.protocol_type;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class BgpThriftService {
38 IBgpManager bgpManager;
39 FibDSWriter fibDSWriter;
42 // to store copy fo FIB-VRF tables on QBGP restart.
43 public List<VrfTables> staleVrfTables;
45 private static final Logger LOG = LoggerFactory.getLogger(BgpThriftService.class);
47 public BgpThriftService(int ourPort, IBgpManager bm, FibDSWriter fibDSWriter) {
48 this.ourPort = ourPort;
50 this.fibDSWriter = fibDSWriter;
53 public static class ThriftClientContext implements ServerContext {
56 public ThriftClientContext(TProtocol in) {
60 public TProtocol getIn() {
65 public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
67 ThriftClientContext oldThriftClientContext;
74 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
76 TNonblockingServerTransport trans = new TNonblockingServerSocket(ourPort);
77 TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(trans);
78 args.transportFactory(new TFramedTransport.Factory());
79 args.protocolFactory(new TBinaryProtocol.Factory());
80 args.processor(processor);
81 args.selectorThreads(1);
82 args.workerThreads(1);
83 server = new TThreadedSelectorServer(args);
84 server.setServerEventHandler(new TServerEventHandler() {
86 public void preServe() {
87 LOG.info("Bgp thrift server pre serve event");
91 public ServerContext createContext(TProtocol input, TProtocol output) {
92 LOG.info("Bgp thrift server create context event");
94 if (oldThriftClientContext != null) {
95 LOG.info("Bgp thrift server closing old context");
96 oldThriftClientContext.getIn().getTransport().close();
98 LOG.info("Bgp thrift server old context is null nothing to close");
100 oldThriftClientContext = new ThriftClientContext(input);
101 return oldThriftClientContext;
106 public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
107 LOG.info("Bgp thrift server delete context event");
108 if (oldThriftClientContext == serverContext) {
109 LOG.info("Bgp thrift server cleanup old context");
110 oldThriftClientContext = null;
112 LOG.info("Bgp thrift server cleanup context");
117 public void processContext(ServerContext serverContext, TTransport inputTransport,
118 TTransport outputTransport) {
119 LOG.trace("Bgp thrift server process context event");
123 } catch (TTransportException e) {
124 LOG.error("Exception in BGP Updater server" + e);
128 @SuppressWarnings("checkstyle:IllegalCatch")
129 public void onUpdatePushRoute(protocol_type protocolType,
142 LOG.debug("Update on push route : rd {} prefix {} plen {}", rd, prefix, plen);
144 // l2label is ignored even in case of RT5. only l3label considered
145 BgpConfigurationManager.onUpdatePushRoute(
159 } catch (Throwable e) {
160 LOG.error("failed to handle update route ", e);
164 public void onUpdateWithdrawRoute(protocol_type protocolType,
176 LOG.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
177 BgpConfigurationManager.onUpdateWithdrawRoute(
184 } catch (InterruptedException e1) {
185 LOG.error("Interrupted exception for withdraw route", e1);
186 } catch (ExecutionException e2) {
187 LOG.error("Execution exception for withdraw route", e2);
188 } catch (TimeoutException e3) {
189 LOG.error("Timeout exception for withdraw route", e3);
193 public void onStartConfigResyncNotification() {
194 LOG.info("BGP (re)started");
195 bgpManager.setQbgprestartTS(System.currentTimeMillis());
196 bgpManager.bgpRestarted();
199 public void onNotificationSendEvent(String prefix, byte errCode,
201 bgpManager.sendNotificationEvent(prefix, (int) errCode, (int) errSubcode);
207 public void start() {
208 thread = new Thread(new BgpUpdateServer());