f1951ba7426f2292b5373fc46ba2ba242da54379
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / 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.bgpmanager.thrift.server;
10
11 import java.util.*;
12
13 import org.apache.thrift.server.TServer;
14 import org.apache.thrift.server.TServer.Args;
15 import org.apache.thrift.server.TSimpleServer;
16 import org.apache.thrift.transport.TServerSocket;
17 import org.apache.thrift.transport.TServerTransport;
18 import org.opendaylight.bgpmanager.BgpManager;
19 import org.opendaylight.bgpmanager.BgpConfigurationManager;
20 import org.opendaylight.bgpmanager.FibDSWriter;
21 import org.opendaylight.bgpmanager.thrift.gen.BgpUpdater;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class BgpThriftService {
27     int ourPort;
28     BgpManager bgpManager;
29     FibDSWriter fibDSWriter;
30     TServer server;
31     // to store copy fo FIB-VRF tables on QBGP restart.
32     public List<VrfTables> stale_vrfTables;
33
34     private static final Logger LOGGER =
35         LoggerFactory.getLogger(BgpThriftService.class);
36      
37     public BgpThriftService(int ourPort, BgpManager bm) {
38         this.ourPort = ourPort;
39         bgpManager = bm;
40         fibDSWriter = bm.getFibWriter();
41     }
42  
43     public class BgpUpdateServer implements Runnable, BgpUpdater.Iface {
44
45         public void BgpUpdateServer() {
46         }
47
48         public void run() {
49             try {
50                 BgpUpdater.Processor processor = new BgpUpdater.Processor(this);
51                 TServerTransport transport = new TServerSocket(ourPort);
52                 server = new TSimpleServer(new Args(transport).processor(processor));
53                 server.serve();
54             } catch (Exception e) {
55                 LOGGER.error("Exception in BGP Updater server"+e);
56             }
57         }
58
59         public void onUpdatePushRoute(String rd, String prefix, int plen,
60                                                  String nexthop, int label) {
61             try {
62                 BgpConfigurationManager.onUpdatePushRoute(rd, prefix, plen, nexthop, label);
63             } catch (Throwable e) {
64                 LOGGER.error("failed to handle update route " ,e);
65             }
66         }
67
68         public void onUpdateWithdrawRoute(String rd, String prefix, int plen) {
69             LOGGER.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
70             try {
71                 fibDSWriter.removeFibEntryFromDS(rd, prefix + "/" + plen);
72             } catch (Throwable e) {
73                 LOGGER.error("failed to handle withdraw route " ,e);
74             }
75         }
76
77         public void onStartConfigResyncNotification() {
78             LOGGER.info("BGP (re)started");
79             try {
80                 bgpManager.bgpRestarted();
81             } catch (Throwable e) {
82                 LOGGER.error("failed to handle onStartConfigResyncNotification " ,e);
83             }
84         }
85
86         public void onNotificationSendEvent(String prefix, byte errCode,
87                                                            byte errSubcode) {
88             int code = errCode;
89             int subCode = errSubcode;
90             bgpManager.sendNotificationEvent(prefix, errCode, errSubcode);
91         }
92
93     }
94
95     Thread thread;
96
97     public void start() {
98         thread = new Thread(new BgpUpdateServer());
99         thread.start();
100     }
101
102     public void stop() {
103         server.stop();
104         thread.stop();
105     }
106
107