BGPManager module sync up
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / thrift / server / 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.netvirt.bgpmanager.thrift.server;
10
11 import java.util.*;
12
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;
24
25 public class BgpThriftService {
26     int ourPort;
27     BgpManager bgpManager;
28     FibDSWriter fibDSWriter;
29     TServer server;
30
31     // to store copy fo FIB-VRF tables on QBGP restart.
32     public List<VrfTables> stale_vrfTables;
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(BgpThriftService.class);
35
36     public BgpThriftService(int ourPort, BgpManager bm) {
37         this.ourPort = ourPort;
38         bgpManager = bm;
39         fibDSWriter = bm.getFibWriter();
40     }
41
42     public static class ThriftClientContext implements  ServerContext {
43         TProtocol in;
44         public ThriftClientContext(TProtocol in) {
45             this.in = in;
46         }
47         public TProtocol getIn() {
48             return in;
49         }
50     }
51     public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
52
53         ThriftClientContext oldThriftClientContext;
54
55         public void BgpUpdateServer() {
56         }
57
58         public void run() {
59             try {
60                 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
61
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() {
71                     @Override
72                     public void preServe() {
73                         LOGGER.error("Bgp thrift server pre serve event");
74                     }
75
76                     @Override
77                     public ServerContext createContext(TProtocol input, TProtocol output) {
78                         LOGGER.error("Bgp thrift server create context event");
79                         synchronized (this) {
80                             try {
81                                 if (oldThriftClientContext != null) {
82                                     LOGGER.error("Bgp thrift server closing old context");
83                                     oldThriftClientContext.getIn().getTransport().close();
84                                 } else {
85                                     LOGGER.error("Bgp thrift server old context is null nothing to close");
86                                 }
87                                 oldThriftClientContext = null;
88                             } catch (Throwable ignore) {
89                             }
90                             oldThriftClientContext = new ThriftClientContext(input);
91                             return oldThriftClientContext;
92                         }
93                     }
94
95                     @Override
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;
101                         } else {
102                             LOGGER.error("Bgp thrift server cleanup context");
103                         }
104                     }
105
106                     @Override
107                     public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport) {
108                         LOGGER.trace("Bgp thrift server process context event");
109                     }
110                 });
111                 server.serve();
112             } catch (Exception e) {
113                 LOGGER.error("Exception in BGP Updater server" + e);
114             }
115         }
116
117         public void onUpdatePushRoute(String rd, String prefix, int plen, String nexthop, int label) {
118             try {
119                 BgpConfigurationManager.onUpdatePushRoute(rd, prefix, plen, nexthop, label);
120             } catch (Throwable e) {
121                 LOGGER.error("failed to handle update route ", e);
122             }
123         }
124
125         public void onUpdateWithdrawRoute(String rd, String prefix, int plen) {
126             LOGGER.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
127             try {
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);
133             }
134         }
135
136         public void onStartConfigResyncNotification() {
137             LOGGER.error("BGP (re)started");
138             bgpManager.setqBGPrestartTS(System.currentTimeMillis());
139             try {
140                 bgpManager.bgpRestarted();
141             } catch (Throwable e) {
142                 LOGGER.error("failed to handle onStartConfigResyncNotification " ,e);
143             }
144         }
145
146         public void onNotificationSendEvent(String prefix, byte errCode,
147                                                            byte errSubcode) {
148             int code = errCode;
149             int subCode = errSubcode;
150             bgpManager.sendNotificationEvent(prefix, code, subCode);
151         }
152     }
153
154     Thread thread;
155
156     public void start() {
157         thread = new Thread(new BgpUpdateServer());
158         thread.start();
159     }
160
161     public void stop() {
162         server.stop();
163         thread.stop();
164     }
165
166