BGPManager module sync up
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / thrift / client / BgpRouter.java
index cf0d1b509e852e14759a8e090690cfeb624b6a35..3311f0350ed45d700f3426b46f370772198ceee4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -15,10 +15,7 @@ import org.apache.thrift.TException;
 import org.apache.thrift.transport.TSocket;
 import org.apache.thrift.transport.TTransport;
 import org.apache.thrift.transport.TTransportException;
-import org.opendaylight.netvirt.bgpmanager.thrift.gen.BgpConfigurator;
-import org.opendaylight.netvirt.bgpmanager.thrift.gen.Routes;
-import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
-import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
+import org.opendaylight.netvirt.bgpmanager.thrift.gen.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -26,15 +23,47 @@ public class BgpRouter {
     private static TTransport transport;
     private static TProtocol protocol;
     private static BgpConfigurator.Client bgpClient=null;
+    boolean isConnected = false;
     private static final Logger LOGGER = LoggerFactory.getLogger(BgpRouter.class);
+    public int startBGPresult = Integer.MIN_VALUE;
+    public String bgpHost = null;
+    public int bgpHostPort = 0;
+    private long startTS = 0;
+    private long connectTS = 0;
+    private long lastConnectedTS = 0;
+
+    public long getLastConnectedTS() {
+        return lastConnectedTS;
+    }
+
+    public void setLastConnectedTS(long lastConnectedTS) {
+        this.lastConnectedTS = lastConnectedTS;
+    }
+
+    public long getConnectTS() {
+        return connectTS;
+    }
+
+    public void setConnectTS(long connectTS) {
+        this.connectTS = connectTS;
+    }
+
+    public long getStartTS() {
+        return startTS;
+    }
+
+    public void setStartTS(long startTS) {
+        this.startTS = startTS;
+    }
+
 
     private enum Optype {
         START, STOP, NBR, VRF, PFX, SRC, MHOP, LOG, AF, GR
-    }
+    };
 
     private final static int GET_RTS_INIT = 0;
     private final static int GET_RTS_NEXT = 1;
-    private final static int CONNECTION_TIMEOUT = 2000;
+    private final static int CONNECTION_TIMEOUT = 60000;
 
 
     private class BgpOp {
@@ -55,6 +84,7 @@ public class BgpRouter {
 
     public synchronized void disconnect() {
         bgpClient = null;
+        isConnected = false;
         if (transport != null) {
             transport.close();
         }
@@ -63,15 +93,21 @@ public class BgpRouter {
     public synchronized boolean connect(String bgpHost, int bgpPort) {
         String msgPiece = "BGP config server at "+bgpHost+":"+bgpPort;
 
+        this.bgpHost = bgpHost;
+        this.bgpHostPort = bgpPort;
+
         disconnect();
+        setConnectTS(System.currentTimeMillis());
         try {
             TSocket ts = new TSocket(bgpHost, bgpPort, CONNECTION_TIMEOUT);
-            transport = ts; 
+            transport = ts;
             transport.open();
             ts.setTimeout(0);
+            isConnected = true;
+            setLastConnectedTS(System.currentTimeMillis());
         } catch (TTransportException tte) {
-            LOGGER.error("Failed connecting to "+msgPiece+
-                         "; Exception: "+tte);
+            LOGGER.error("Failed connecting to " + msgPiece + "; Exception: " + tte);
+            isConnected = false;
             return false;
         }
         protocol = new TBinaryProtocol(transport);
@@ -80,6 +116,10 @@ public class BgpRouter {
         return true;
     }
 
+    public boolean isBgpConnected() {
+        return isConnected;
+    }
+
     private BgpRouter() {
         bop = new BgpOp();
     }
@@ -90,8 +130,7 @@ public class BgpRouter {
         return (br == null ? br = new BgpRouter() : br);
     }
 
-    private void dispatch(BgpOp op)
-        throws TException, BgpRouterException {
+    private void dispatch(BgpOp op) throws TException, BgpRouterException {
         int result = 1;
 
         if (bgpClient == null) {
@@ -100,55 +139,57 @@ public class BgpRouter {
 
         switch (op.type) {
             case START:
+                setStartTS(System.currentTimeMillis());
                 result = bgpClient.startBgp(op.ints[0], op.strs[0],
-                    op.ignore, op.ignore, op.ignore, op.ints[1], op.add);
+                        op.ignore, op.ignore, op.ignore, op.ints[1], op.add);
+                startBGPresult = result;
                 break;
             case STOP:
                 result = bgpClient.stopBgp(op.ints[0]);
                 break;
             case NBR:
-                result = bop.add ? 
-                           bgpClient.createPeer(op.strs[0], op.ints[0]) 
-                           : bgpClient.deletePeer(op.strs[0]); 
+                result = bop.add ?
+                        bgpClient.createPeer(op.strs[0], op.ints[0])
+                        : bgpClient.deletePeer(op.strs[0]);
                 break;
             case VRF:
-                result = bop.add ? 
-                           bgpClient.addVrf(op.strs[0], op.irts, op.erts)
-                           : bgpClient.delVrf(op.strs[0]);
+                result = bop.add ?
+                        bgpClient.addVrf(op.strs[0], op.irts, op.erts)
+                        : bgpClient.delVrf(op.strs[0]);
                 break;
             case PFX:
                 // order of args is different in addPrefix(), hence the
                 // seeming out-of-order-ness of string indices
-                result = bop.add ? 
-                           bgpClient.pushRoute(op.strs[1], op.strs[2], 
-                                                op.strs[0], op.ints[0])
-                      : bgpClient.withdrawRoute(op.strs[1], op.strs[0]);
+                result = bop.add ?
+                        bgpClient.pushRoute(op.strs[1], op.strs[2],
+                                op.strs[0], op.ints[0])
+                        : bgpClient.withdrawRoute(op.strs[1], op.strs[0]);
                 break;
             case LOG:
                 result = bgpClient.setLogConfig(op.strs[0], op.strs[1]);
                 break;
-            case MHOP: 
-                result = bop.add ? 
-                      bgpClient.setEbgpMultihop(op.strs[0], op.ints[0])
-                      : bgpClient.unsetEbgpMultihop(op.strs[0]);
+            case MHOP:
+                result = bop.add ?
+                        bgpClient.setEbgpMultihop(op.strs[0], op.ints[0])
+                        : bgpClient.unsetEbgpMultihop(op.strs[0]);
                 break;
-            case SRC: 
+            case SRC:
                 result = bop.add ?
-                      bgpClient.setUpdateSource(op.strs[0], op.strs[1])
-                      : bgpClient.unsetUpdateSource(op.strs[0]);
+                        bgpClient.setUpdateSource(op.strs[0], op.strs[1])
+                        : bgpClient.unsetUpdateSource(op.strs[0]);
                 break;
             default: break;
-            case AF: 
+            case AF:
                 af_afi afi = af_afi.findByValue(op.ints[0]);
                 af_safi safi = af_safi.findByValue(op.ints[1]);
                 result = bop.add ?
-                  bgpClient.enableAddressFamily(op.strs[0], afi, safi)
-                  : bgpClient.disableAddressFamily(op.strs[0], afi, safi);
+                        bgpClient.enableAddressFamily(op.strs[0], afi, safi)
+                        : bgpClient.disableAddressFamily(op.strs[0], afi, safi);
                 break;
-            case GR: 
+            case GR:
                 result = bop.add ?
-                           bgpClient.enableGracefulRestart(op.ints[0])
-                           : bgpClient.disableGracefulRestart();
+                        bgpClient.enableGracefulRestart(op.ints[0])
+                        : bgpClient.disableGracefulRestart();
                 break;
         }
         if (result != 0) {
@@ -156,28 +197,26 @@ public class BgpRouter {
         }
     }
 
-    public synchronized void startBgp(int asNum, String rtrId, int stalepathTime,
-                         boolean announceFbit)
-        throws TException, BgpRouterException {
+    public synchronized void startBgp(int asNum, String rtrId, int stalepathTime, boolean announceFbit)
+            throws TException, BgpRouterException {
         bop.type = Optype.START;
         bop.add = announceFbit;
         bop.ints[0] = asNum;
         bop.ints[1] = stalepathTime;
         bop.strs[0] = rtrId;
-        LOGGER.debug("Starting BGP with as number {} and router ID {} ", asNum, rtrId);
+        LOGGER.debug("Starting BGP with as number {} and router ID {} StalePathTime: {}", asNum, rtrId, stalepathTime);
         dispatch(bop);
     }
 
     public synchronized void stopBgp(int asNum)
-        throws TException, BgpRouterException {
+            throws TException, BgpRouterException {
         bop.type = Optype.STOP;
         bop.ints[0] = asNum;
         LOGGER.debug("Stopping BGP with as number {}", asNum);
         dispatch(bop);
     }
 
-    public synchronized void addNeighbor(String nbrIp, int nbrAsNum)
-        throws TException, BgpRouterException {
+    public synchronized void addNeighbor(String nbrIp, int nbrAsNum) throws TException, BgpRouterException {
         bop.type = Optype.NBR;
         bop.add = true;
         bop.strs[0] = nbrIp;
@@ -186,8 +225,7 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public synchronized void delNeighbor(String nbrIp)
-        throws TException, BgpRouterException {
+    public synchronized void delNeighbor(String nbrIp) throws TException, BgpRouterException {
         bop.type = Optype.NBR;
         bop.add = false;
         bop.strs[0] = nbrIp;
@@ -196,7 +234,7 @@ public class BgpRouter {
     }
 
     public synchronized void addVrf(String rd, List<String> irts, List<String> erts)
-        throws TException, BgpRouterException {
+            throws TException, BgpRouterException {
         bop.type = Optype.VRF;
         bop.add = true;
         bop.strs[0] = rd;
@@ -206,8 +244,7 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public synchronized void delVrf(String rd)
-        throws TException, BgpRouterException {
+    public synchronized void delVrf(String rd) throws TException, BgpRouterException {
         bop.type = Optype.VRF;
         bop.add = false;
         bop.strs[0] = rd;
@@ -216,10 +253,10 @@ public class BgpRouter {
     }
 
     // bit of a mess-up: the order of arguments is different in
-    // the Thrift RPC: prefix-nexthop-rd-label. 
+    // the Thrift RPC: prefix-nexthop-rd-label.
 
     public synchronized void addPrefix(String rd, String prefix, String nexthop, int label)
-        throws TException, BgpRouterException {
+            throws TException, BgpRouterException {
         bop.type = Optype.PFX;
         bop.add = true;
         bop.strs[0] = rd;
@@ -230,8 +267,7 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public synchronized void delPrefix(String rd, String prefix)
-        throws TException, BgpRouterException {
+    public synchronized void delPrefix(String rd, String prefix) throws TException, BgpRouterException {
         bop.type = Optype.PFX;
         bop.add = false;
         bop.strs[0] = rd;
@@ -240,8 +276,7 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public int initRibSync(BgpSyncHandle handle)
-        throws TException, BgpRouterException {
+    public int initRibSync(BgpSyncHandle handle) throws TException, BgpRouterException {
         if (bgpClient == null) {
             throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
         }
@@ -253,8 +288,7 @@ public class BgpRouter {
         return 0;
     }
 
-    public int endRibSync(BgpSyncHandle handle)
-        throws TException, BgpRouterException {
+    public int endRibSync(BgpSyncHandle handle) throws TException, BgpRouterException {
         if (bgpClient == null) {
             throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
         }
@@ -274,8 +308,7 @@ public class BgpRouter {
         return 0;
     }
 
-    public Routes doRibSync(BgpSyncHandle handle)
-        throws TException, BgpRouterException {
+    public Routes doRibSync(BgpSyncHandle handle) throws TException, BgpRouterException {
         if (bgpClient == null) {
             throw new BgpRouterException(BgpRouterException.BGP_ERR_NOT_INITED);
         }
@@ -286,13 +319,13 @@ public class BgpRouter {
             return r;
         }
         int op = (state == BgpSyncHandle.INITED) ?
-            GET_RTS_INIT : GET_RTS_NEXT;
+                GET_RTS_INIT : GET_RTS_NEXT;
         handle.setState(BgpSyncHandle.ITERATING);
         int winSize = handle.getMaxCount()*handle.getRouteSize();
         Routes outRoutes = bgpClient.getRoutes(op, winSize);
-       if (outRoutes.errcode != 0) {
-           return outRoutes;
-       }
+        if (outRoutes.errcode != 0) {
+            return outRoutes;
+        }
         handle.setMore(outRoutes.more);
         if (outRoutes.more == 0) {
             handle.setState(BgpSyncHandle.DONE);
@@ -300,8 +333,7 @@ public class BgpRouter {
         return outRoutes;
     }
 
-    public synchronized void setLogging(String fileName, String debugLevel)
-            throws TException, BgpRouterException {
+    public synchronized void setLogging(String fileName, String debugLevel) throws TException, BgpRouterException {
         bop.type = Optype.LOG;
         bop.strs[0] = fileName;
         bop.strs[1] = debugLevel;
@@ -309,19 +341,17 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public synchronized void addEbgpMultihop(String nbrIp, int nhops)
-        throws TException, BgpRouterException {
+    public synchronized void addEbgpMultihop(String nbrIp, int nhops) throws TException, BgpRouterException {
         bop.type = Optype.MHOP;
         bop.add = true;
         bop.strs[0] = nbrIp;
         bop.ints[0] = nhops;
         LOGGER.debug("ebgp-multihop set for peer {}, num hops = {}",
-                      nbrIp, nhops);
+                nbrIp, nhops);
         dispatch(bop);
     }
 
-    public synchronized void delEbgpMultihop(String nbrIp)
-        throws TException, BgpRouterException {
+    public synchronized void delEbgpMultihop(String nbrIp) throws TException, BgpRouterException {
         bop.type = Optype.MHOP;
         bop.add = false;
         bop.strs[0] = nbrIp;
@@ -329,19 +359,17 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public synchronized void addUpdateSource(String nbrIp, String srcIp)
-        throws TException, BgpRouterException {
+    public synchronized void addUpdateSource(String nbrIp, String srcIp) throws TException, BgpRouterException {
         bop.type = Optype.SRC;
         bop.add = true;
         bop.strs[0] = nbrIp;
         bop.strs[1] = srcIp;
-        LOGGER.debug("update-source added for peer {}, src-ip = {}", 
-                      nbrIp, srcIp);
+        LOGGER.debug("update-source added for peer {}, src-ip = {}",
+                nbrIp, srcIp);
         dispatch(bop);
     }
 
-    public synchronized void delUpdateSource(String nbrIp)
-        throws TException, BgpRouterException {
+    public synchronized void delUpdateSource(String nbrIp) throws TException, BgpRouterException {
         bop.type = Optype.SRC;
         bop.add = false;
         bop.strs[0] = nbrIp;
@@ -349,47 +377,43 @@ public class BgpRouter {
         dispatch(bop);
     }
 
-    public synchronized void addAddressFamily(String nbrIp, 
-                                  af_afi afi, af_safi safi)
-        throws TException, BgpRouterException {
+    public synchronized void addAddressFamily(String nbrIp, af_afi afi, af_safi safi)
+            throws TException, BgpRouterException {
         bop.type = Optype.AF;
         bop.add = true;
         bop.strs[0] = nbrIp;
         bop.ints[0] = afi.getValue();
         bop.ints[1] = safi.getValue();
         LOGGER.debug("addr family added for peer {}, afi = {}, safi = {}",
-                     nbrIp, bop.ints[0], bop.ints[1]);
+                nbrIp, bop.ints[0], bop.ints[1]);
         dispatch(bop);
     }
 
-    public synchronized void delAddressFamily(String nbrIp,
-                                  af_afi afi, af_safi safi)
-        throws TException, BgpRouterException {
+    public synchronized void delAddressFamily(String nbrIp, af_afi afi, af_safi safi)
+            throws TException, BgpRouterException {
         bop.type = Optype.AF;
         bop.add = false;
         bop.strs[0] = nbrIp;
         bop.ints[0] = afi.getValue();
         bop.ints[1] = safi.getValue();
         LOGGER.debug("addr family deleted for peer {}, afi = {}, safi = {}",
-                     nbrIp, bop.ints[0], bop.ints[1]);
+                nbrIp, bop.ints[0], bop.ints[1]);
         dispatch(bop);
     }
 
-    public synchronized void addGracefulRestart(int stalepathTime)
-        throws TException, BgpRouterException {
+    public synchronized void addGracefulRestart(int stalepathTime) throws TException, BgpRouterException {
         bop.type = Optype.GR;
         bop.add = true;
         bop.ints[0] = stalepathTime;
         LOGGER.debug("graceful restart added, stale-path-time = {}",
-                      stalepathTime);
+                stalepathTime);
         dispatch(bop);
     }
 
-    public synchronized void delGracefulRestart()
-        throws TException, BgpRouterException {
+    public synchronized void delGracefulRestart() throws TException, BgpRouterException {
         bop.type = Optype.GR;
         bop.add = false;
         LOGGER.debug("graceful restart deleted");
         dispatch(bop);
     }
-}
+}
\ No newline at end of file