Timer and log enhancements in BGP
[netvirt.git] / bgpmanager / impl / src / main / java / org / opendaylight / netvirt / bgpmanager / thrift / client / BgpRouter.java
index 59a0be9402081e3438307b9b10c66c641c19f783..6aafaa9b6fc99127cf928ad8f17366a2e7b09d99 100644 (file)
@@ -8,17 +8,21 @@
 
 package org.opendaylight.netvirt.bgpmanager.thrift.client;
 
+import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.HISTORY_LIMIT;
+import static org.opendaylight.netvirt.bgpmanager.oam.BgpConstants.HISTORY_THRESHOLD;
+
 import com.google.common.annotations.VisibleForTesting;
 import java.net.ConnectException;
+import java.util.Arrays;
 import java.util.List;
 import java.util.function.BooleanSupplier;
 import java.util.function.Supplier;
-import javax.annotation.Nullable;
 import org.apache.thrift.TException;
 import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.transport.TSocket;
 import org.apache.thrift.transport.TTransport;
 import org.apache.thrift.transport.TTransportException;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netvirt.bgpmanager.BgpConfigurationManager;
 import org.opendaylight.netvirt.bgpmanager.RetryOnException;
 import org.opendaylight.netvirt.bgpmanager.thrift.gen.BfdConfigData;
@@ -29,6 +33,8 @@ import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
 import org.opendaylight.netvirt.bgpmanager.thrift.gen.encap_type;
 import org.opendaylight.netvirt.bgpmanager.thrift.gen.layer_type;
 import org.opendaylight.netvirt.bgpmanager.thrift.gen.protocol_type;
+import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionHistory;
+import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.LayerType;
 import org.slf4j.Logger;
@@ -64,6 +70,8 @@ public final class BgpRouter {
         String macAddress;
         int l2label;
         int l3label;
+        int holdTime;
+        int kaTime;
         encap_type thriftEncapType;
         String routermac;
         public af_afi afi;
@@ -74,8 +82,65 @@ public final class BgpRouter {
             strs = new String[3];
             ints = new int[3];
         }
+
+        BgpOp(BgpOp bgpOp) {
+            strs = new String[3];
+            ints = new int[3];
+            this.strs = bgpOp.strs;
+            this.ints = bgpOp.ints;
+            this.afi = bgpOp.afi;
+            this.safi = bgpOp.safi;
+            this.type = bgpOp.type;
+            this.add = bgpOp.add;
+            this.multiHop = bgpOp.multiHop;
+            this.asNumber = bgpOp.asNumber;
+            this.holdTime = bgpOp.holdTime;
+            this.kaTime = bgpOp.kaTime;
+            this.thriftProtocolType = bgpOp.thriftProtocolType;
+            this.thriftLayerType = bgpOp.thriftLayerType;
+            this.ethernetTag = bgpOp.ethernetTag;
+            this.esi = bgpOp.esi;
+            this.macAddress = bgpOp.macAddress;
+            this.l2label = bgpOp.l2label;
+            this.l3label = bgpOp.l3label;
+            this.routermac = bgpOp.routermac;
+            this.thriftEncapType = bgpOp.thriftEncapType;
+            this.delayEOR = bgpOp.delayEOR;
+
+        }
+
+        @Override
+        public String toString() {
+            //TODO pretty print
+            return "BgpOp{"
+                    + "type=" + type
+                    + ", add=" + add
+                    + ",multihop=" + multiHop
+                    + ", strs=" + Arrays.toString(strs)
+                    + ", ints=" + Arrays.toString(ints)
+                    + ", irts=" + irts
+                    + ", erts=" + erts
+                    + ", asNumber=" + asNumber
+                    + ", Hold Time=" + holdTime
+                    + ", KA Time=" + kaTime
+                    + ", thriftLayerType=" + thriftLayerType
+                    + ", thriftProtocolType=" + thriftProtocolType
+                    + ", ethernetTag=" + ethernetTag
+                    + ", esi='" + esi + '\''
+                    + ", macAddress='" + macAddress + '\''
+                    + ", l2label=" + l2label
+                    + ", l3label=" + l3label
+                    + ", thriftEncapType=" + thriftEncapType
+                    + ", routermac='" + routermac + '\''
+                    + ", afi=" + afi
+                    + ", delayEOR=" + delayEOR
+                    + ", safi=" + safi
+                    + '}' + '\n';
+        }
     }
 
+
+
     private final BgpOp bop = new BgpOp();
     private final Supplier<Bgp> bgpConfigSupplier;
     private final BooleanSupplier isBGPEntityOwner;
@@ -86,16 +151,19 @@ public final class BgpRouter {
     private volatile long startTS;
     private volatile long connectTS;
     private volatile long lastConnectedTS;
+    private final TransactionHistory transactionHistory;
     private volatile boolean configServerUpdated = false;
 
-    private BgpRouter(Supplier<Bgp> bgpConfigSupplier, BooleanSupplier isBGPEntityOwner) {
+    private BgpRouter(Supplier<Bgp> bgpConfigSupplier, BooleanSupplier isBGPEntityOwner,
+                      TransactionHistory transactionHistory) {
         this.bgpConfigSupplier = bgpConfigSupplier;
         this.isBGPEntityOwner = isBGPEntityOwner;
+        this.transactionHistory = transactionHistory;
     }
 
     // private ctor FOR UNIT TESTS ONLY
     private BgpRouter(BgpConfigurator.Client bgpClient) {
-        this(() -> null, () -> false);
+        this(() -> null, () -> false, new TransactionHistory(HISTORY_LIMIT, HISTORY_THRESHOLD));
         this.bgpClient = bgpClient;
     }
 
@@ -105,8 +173,9 @@ public final class BgpRouter {
         return new BgpRouter(bgpClient);
     }
 
-    public static BgpRouter newInstance(Supplier<Bgp> bgpConfigSupplier, BooleanSupplier isEntityBGPOwner) {
-        return new BgpRouter(bgpConfigSupplier, isEntityBGPOwner);
+    public static BgpRouter newInstance(Supplier<Bgp> bgpConfigSupplier, BooleanSupplier isEntityBGPOwner,
+                                        TransactionHistory transactionHistory) {
+        return new BgpRouter(bgpConfigSupplier, isEntityBGPOwner,transactionHistory);
     }
 
     public TTransport getTransport() {
@@ -212,9 +281,15 @@ public final class BgpRouter {
         return isConnected;
     }
 
+    private TransactionType getTransactionType(BgpOp op) {
+        return op.add ? TransactionType.ADD : TransactionType.DELETE;
+    }
+
     private void dispatch(BgpOp op) throws TException, BgpRouterException {
         try {
             dispatchInternal(op);
+            transactionHistory.addToHistory(getTransactionType(op), new BgpOp(op));
+            LOG.trace("History size is {}", transactionHistory.getElements().size());
         } catch (TTransportException tte) {
             LOG.error("dispatch command to qthriftd failed, command: {}, exception:", op.toString(), tte);
             reConnect(tte);
@@ -261,7 +336,7 @@ public final class BgpRouter {
                 setStartTS(System.currentTimeMillis());
                 LOG.debug("startBgp thrift call for AsId {}", op.asNumber);
                 result = bgpClient.startBgp(op.asNumber, op.strs[0],
-                        BgpOp.IGNORE, BgpOp.IGNORE, BgpOp.IGNORE, op.ints[0], op.add);
+                        BgpOp.IGNORE, op.holdTime, op.kaTime, op.ints[0], op.add);
                 LOG.debug("Result of startBgp thrift call for AsId {} : {}", op.asNumber, result);
                 break;
             case STOP:
@@ -375,11 +450,14 @@ public final class BgpRouter {
         }
     }
 
-    public synchronized void startBgp(long asNum, String rtrId, int stalepathTime, boolean announceFbit)
+    public synchronized void startBgp(long asNum, String rtrId, int bgpKaTime, int bgpHoldTime,
+                                      int stalepathTime, boolean announceFbit)
             throws TException, BgpRouterException {
         bop.type = Optype.START;
         bop.add = announceFbit;
         bop.asNumber = asNum;
+        bop.kaTime = bgpKaTime;
+        bop.holdTime = bgpHoldTime;
         bop.ints[0] = stalepathTime;
         bop.strs[0] = rtrId;
         LOG.debug("Starting BGP with as number {} and router ID {} StalePathTime: {}", asNum, rtrId, stalepathTime);