Replace LOGGER by LOG
[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.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeoutException;
14
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;
35
36 public class BgpThriftService {
37     int ourPort;
38     IBgpManager bgpManager;
39     FibDSWriter fibDSWriter;
40     TServer server;
41
42     // to store copy fo FIB-VRF tables on QBGP restart.
43     public List<VrfTables> staleVrfTables;
44
45     private static final Logger LOG = LoggerFactory.getLogger(BgpThriftService.class);
46
47     public BgpThriftService(int ourPort, IBgpManager bm, FibDSWriter fibDSWriter) {
48         this.ourPort = ourPort;
49         bgpManager = bm;
50         this.fibDSWriter = fibDSWriter;
51     }
52
53     public static class ThriftClientContext implements ServerContext {
54         TProtocol in;
55
56         public ThriftClientContext(TProtocol in) {
57             this.in = in;
58         }
59
60         public TProtocol getIn() {
61             return in;
62         }
63     }
64
65     public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
66
67         ThriftClientContext oldThriftClientContext;
68
69         BgpUpdateServer() {
70         }
71
72         public void run() {
73             try {
74                 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
75
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() {
85                     @Override
86                     public void preServe() {
87                         LOG.info("Bgp thrift server pre serve event");
88                     }
89
90                     @Override
91                     public ServerContext createContext(TProtocol input, TProtocol output) {
92                         LOG.info("Bgp thrift server create context event");
93                         synchronized (this) {
94                             if (oldThriftClientContext != null) {
95                                 LOG.info("Bgp thrift server closing old context");
96                                 oldThriftClientContext.getIn().getTransport().close();
97                             } else {
98                                 LOG.info("Bgp thrift server old context is null nothing to close");
99                             }
100                             oldThriftClientContext = new ThriftClientContext(input);
101                             return oldThriftClientContext;
102                         }
103                     }
104
105                     @Override
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;
111                         } else {
112                             LOG.info("Bgp thrift server cleanup context");
113                         }
114                     }
115
116                     @Override
117                     public void processContext(ServerContext serverContext, TTransport inputTransport,
118                             TTransport outputTransport) {
119                         LOG.trace("Bgp thrift server process context event");
120                     }
121                 });
122                 server.serve();
123             } catch (TTransportException e) {
124                 LOG.error("Exception in BGP Updater server" + e);
125             }
126         }
127
128         @SuppressWarnings("checkstyle:IllegalCatch")
129         public void onUpdatePushRoute(protocol_type protocolType,
130                                       String rd,
131                                       String prefix,
132                                       int plen,
133                                       String nexthop,
134                                       int ethtag,
135                                       String esi,
136                                       String macaddress,
137                                       int l3label,
138                                       int l2label,
139                                       String routermac,
140                                       af_afi afi) {
141             try {
142                 LOG.debug("Update on push route : rd {} prefix {} plen {}", rd, prefix, plen);
143
144                 // l2label is ignored even in case of RT5. only l3label considered
145                 BgpConfigurationManager.onUpdatePushRoute(
146                         protocolType,
147                         rd,
148                         prefix,
149                         plen,
150                         nexthop,
151                         ethtag,
152                         esi,
153                         macaddress,
154                         l3label,
155                         l2label,
156                         routermac,
157                         afi);
158
159             } catch (Throwable e) {
160                 LOG.error("failed to handle update route ", e);
161             }
162         }
163
164         public void onUpdateWithdrawRoute(protocol_type protocolType,
165                                           String rd,
166                                           String prefix,
167                                           int plen,
168                                           String nexthop,
169                                           int ethtag,
170                                           String esi,
171                                           String macaddress,
172                                           int l3label,
173                                           int l2label,
174                                           af_afi afi) {
175             try {
176                 LOG.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
177                 BgpConfigurationManager.onUpdateWithdrawRoute(
178                         protocolType,
179                         rd,
180                         prefix,
181                         plen,
182                         nexthop,
183                         macaddress);
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);
190             }
191         }
192
193         public void onStartConfigResyncNotification() {
194             LOG.info("BGP (re)started");
195             bgpManager.setQbgprestartTS(System.currentTimeMillis());
196             bgpManager.bgpRestarted();
197         }
198
199         public void onNotificationSendEvent(String prefix, byte errCode,
200                 byte errSubcode) {
201             bgpManager.sendNotificationEvent(prefix, (int) errCode, (int) errSubcode);
202         }
203     }
204
205     Thread thread;
206
207     public void start() {
208         thread = new Thread(new BgpUpdateServer());
209         thread.start();
210     }
211
212     public void stop() {
213         server.stop();
214         thread.stop();
215     }
216 }