#!/usr/bin/env bash

# run 2x2x2 topology and bring down bunch of links looking for POD level auto deaggregation

# Copyright (c) 2016-, Juniper Networks, Inc.
# All rights reserved.

set -e

ulimit -n 4096

`which python` --version || {
    echo "install python ..."
    exit 1
}

OF=/tmp/rift.out.$$

echo running as $$ writing out to $OF

echo " break the link from 202 to 2002 which should lead to disaggregation on 201"
echo " break then the link from 201 to core 2 which should lead to disaggregation of 2002 to 101 and 102"
echo
echo "                 +--------+          +--------+                       "
echo "                 |        |          |        |          ^ N          "
echo "                 |core   1|          |core  2 |          |            "
echo "                 ++-+--+-++          ++-X--+-++        <-*-> E/W      "
echo "                  | |  | |            | X  | |           |            "
echo "                  | |  | |            | X  | |         S v            "
echo "                  ^ ^  ^ ^            | X  | |                        "
echo "                  | |  | |            | X  | |                        "
echo "   +--------------+ |  +-----------+  | X  | +---------------+        "
echo "   |                |    |         |  | X  |                 |        "
echo "   |    +-----------------------------+ |  |                 ^        "
echo "   |    |           |    |         |    |  |                 |        "
echo "   |    |           |    +-----------------------------+     |        "
echo "   v    v           |              |    |  |           |     |        "
echo "   |    |           +-+    +---------------+           |     |        "
echo "   |    |             |    |       |    |              |     |        "
echo " +-+----++          +-+----++     ++----+-+           ++-----++       "
echo " |       |          |       |     |       |           |       |       "
echo " |agg 101+          +102    |     |201    |           |202    |       "
echo " +-+---+-+          ++----+-+     +-+---+-+           ++---+--+       "
echo "   |   |             |    |         |   |              |   |          "
echo "   |   +--------->-----+  |         |   +----------------+ X          "
echo "   |                 | |  |         |                  | | X <=== !CUT! "
echo "   |   +---<---------+ |  v         |   +--------------+ | X          "
echo "   v   |               |  |         |   |                | |          "
echo " +-+---+-+          +--+--+-+     +-+---+-+          +---+-+-+        "
echo " |1.1/24 |          |1.2/24 |     |2.1/24 |          |2.2/24 |        "
echo " |1001   |          |1002   |     |2001   |          |2002   |        "
echo " +-+-----+          +-+---+-+     +--+--+-+          +-+-----+        "
echo

# -vvv -Nedge_1002 -Cspf
../bin/rift-environ  -vv -c topology -u -DD ../topology/two_by_two_by_two_few_prefixes.yaml > $OF 2>&1 << EOF
sleep F00
bfd down node "agg_202" interface "if_202_2002-"
bfd down node "edge_2002" interface "if_2002_202-"
bfd down node "agg_201" interface "if_201_2-"
bfd down node "core_2" interface "if_2_201-"
sleep 800
quit

EOF

# derive all the downtimes

echo
LFT=`grep 'stopping interface' $OF`

echo "$LFT"
echo

echo "--- last southbound TIE, RIB transaction timestamps, routing tables for key nodes"
echo

for N in core_1 agg_101 core_2 agg_201 agg_202 edge_2001 edge_2002
do
    echo

    LRT=`python lib/curlies.py "RIB stats for $N" < $OF | python lib/curlies.py " *last_transaction:" | egrep "(secs|msecs)"`
    echo "$N last RIB transaction: " $LRT

    LRR=`python lib/curlies.py "RIB stats for $N" < $OF | python lib/curlies.py " *IPV4: AFRoutingTableStatistics" | egrep "currentprefixes"`
    echo "$N RIB routes: " $LRR

    LST=`python lib/curlies.py "LSDB Stats for $N" < $OF | python lib/curlies.py " *last_southbound"`

    [ -z "$LST" ] || {
        LST=`echo $LST | egrep "(secs|msecs)"`
        echo "$N last southbound prefix/deaggregation: " $LST
    }

    DAP=`python lib/curlies.py "SPO stats for $N" < $OF | egrep " *current_deaggregated_prefixes:"`
    echo "$N deaggregated prefixes: " $DAP

    RT=`python lib/curlies.py "RIB routing table for $N" \< \> < $OF`
    echo "$N v4 RIB: $RT"
    NH=`python lib/curlies.py "RIB nexthops for $N" \< \> < $OF`
    echo "$N NHOPS: $NH"

done

unlink $OF
