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;
13 import org.apache.thrift.protocol.TBinaryProtocol;
14 import org.apache.thrift.protocol.TProtocol;
15 import org.apache.thrift.server.*;
16 import org.apache.thrift.transport.*;
17 import org.opendaylight.netvirt.bgpmanager.BgpManager;
18 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
19 import org.opendaylight.netvirt.bgpmanager.FibDSWriter;
20 import org.opendaylight.netvirt.bgpmanager.thrift.gen.BgpUpdater;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class BgpThriftService {
27 BgpManager bgpManager;
28 FibDSWriter fibDSWriter;
31 // to store copy fo FIB-VRF tables on QBGP restart.
32 public List<VrfTables> stale_vrfTables;
34 private static final Logger LOGGER = LoggerFactory.getLogger(BgpThriftService.class);
36 public BgpThriftService(int ourPort, BgpManager bm) {
37 this.ourPort = ourPort;
39 fibDSWriter = bm.getFibWriter();
42 public static class ThriftClientContext implements ServerContext {
44 public ThriftClientContext(TProtocol in) {
47 public TProtocol getIn() {
51 public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
53 ThriftClientContext oldThriftClientContext;
55 public void BgpUpdateServer() {
60 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
62 TNonblockingServerTransport trans = new TNonblockingServerSocket(ourPort);
63 TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(trans);
64 args.transportFactory(new TFramedTransport.Factory());
65 args.protocolFactory(new TBinaryProtocol.Factory());
66 args.processor(processor);
67 args.selectorThreads(1);
68 args.workerThreads(1);
69 server = new TThreadedSelectorServer(args);
70 server.setServerEventHandler(new TServerEventHandler() {
72 public void preServe() {
73 LOGGER.error("Bgp thrift server pre serve event");
77 public ServerContext createContext(TProtocol input, TProtocol output) {
78 LOGGER.error("Bgp thrift server create context event");
81 if (oldThriftClientContext != null) {
82 LOGGER.error("Bgp thrift server closing old context");
83 oldThriftClientContext.getIn().getTransport().close();
85 LOGGER.error("Bgp thrift server old context is null nothing to close");
87 oldThriftClientContext = null;
88 } catch (Throwable ignore) {
90 oldThriftClientContext = new ThriftClientContext(input);
91 return oldThriftClientContext;
96 public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
97 LOGGER.error("Bgp thrift server delete context event");
98 if (oldThriftClientContext == serverContext) {
99 LOGGER.error("Bgp thrift server cleanup old context");
100 oldThriftClientContext = null;
102 LOGGER.error("Bgp thrift server cleanup context");
107 public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport) {
108 LOGGER.trace("Bgp thrift server process context event");
112 } catch (Exception e) {
113 LOGGER.error("Exception in BGP Updater server" + e);
117 public void onUpdatePushRoute(String rd, String prefix, int plen, String nexthop, int label) {
119 BgpConfigurationManager.onUpdatePushRoute(rd, prefix, plen, nexthop, label);
120 } catch (Throwable e) {
121 LOGGER.error("failed to handle update route ", e);
125 public void onUpdateWithdrawRoute(String rd, String prefix, int plen) {
126 LOGGER.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
128 LOGGER.info("REMOVE: Removing Fib entry rd {} prefix {}", rd, prefix);
129 fibDSWriter.removeFibEntryFromDS(rd, prefix + "/" + plen);
130 LOGGER.info("REMOVE: Removed Fib entry rd {} prefix {}", rd, prefix);
131 } catch (Throwable e) {
132 LOGGER.error("failed to handle withdraw route " ,e);
136 public void onStartConfigResyncNotification() {
137 LOGGER.error("BGP (re)started");
138 bgpManager.setqBGPrestartTS(System.currentTimeMillis());
140 bgpManager.bgpRestarted();
141 } catch (Throwable e) {
142 LOGGER.error("failed to handle onStartConfigResyncNotification " ,e);
146 public void onNotificationSendEvent(String prefix, byte errCode,
149 int subCode = errSubcode;
150 bgpManager.sendNotificationEvent(prefix, code, subCode);
156 public void start() {
157 thread = new Thread(new BgpUpdateServer());