# This script is sourced by the clagd startup script

#
#   Clean up any residue from any previous clagd instances. 
#
do_cleanup()
{
    # Remove any files left over in /var/run
    echo Cleanup is executing.
    /bin/rm -f /var/run/clagd.*

    # On all bonds change the ad_actor_system to the default value and the  
    # lacp fallback to not active
    # All clag bond slaves are shutdown
    clagRestartScript="/lib/clagctl-utils/clagctlRestartConfig"
    if [ -x "$clagRestartScript" ]
    then
        clagBondList=$(python  $clagRestartScript  --clagBondDump >&1)
        for bond in $clagBondList
        do
            if [ -r /sys/class/net/${bond}/bonding/mode ] && 
               /bin/grep -Fxq "802.3ad 4" /sys/class/net/${bond}/bonding/mode
            then
                if [ -w /sys/class/net/${bond}/bonding/ad_actor_system ] 
                then
                    init_state=$(( $(cat /sys/class/net/${bond}/flags) & 1 ))
                    [ ${init_state} -eq 1 ] && /bin/ip link set ${bond} down
                    bond_mac=$(cat /sys/class/net/${bond}/address)
                    echo "${bond_mac}" > /sys/class/net/${bond}/bonding/ad_actor_system
                    [ ${init_state} -eq 1 ] && /bin/ip link set ${bond} up
                fi
            fi
        done < /sys/class/net/bonding_masters
    fi

    clagBondCleanupScript="/lib/clagctl-utils/clagBondCleanup"
    if [ -x "$clagBondCleanupScript" ]
    then
        "$clagBondCleanupScript"
    fi

    # Check if the VxLANs were configured.
    doVxLans=0
    # In case of scale configs, during bring-up switchd will be busy in processing
    # the configs and there are chances of this script getting blocked or even timed-out
    # over accessing local_ip sfs node. This is true even in case of non-VxLAN testbeds
    # which can be avoided by adding an extra check for VxLAN Devices.
    vxlanDevices=$(/bin/ip -d -o link show | grep " vxlan id ")

    if [ -n "${vxlanDevices}" ] && [ -r /cumulus/switchd/config/vxlan/local_ip ]
    then
        # Get the anycast IP and local IP, if configured
        anycastIp=$(cat /cumulus/switchd/config/vxlan/local_ip | /usr/bin/cut -d \  -f 1)
        localIp=$(cat /cumulus/switchd/config/vxlan/local_ip | /usr/bin/cut -d \  -f 2)
        if [ -n "${anycastIp}" ] && [ "${anycastIp}" != "0.0.0.0" ] && 
           [ -n "${localIp}" ] && [ "${localIp}" != "0.0.0.0" ]
        then
            # Remove the anycast IP from the loopback device
            doVxLans=1
            /bin/ip addr del dev lo ${anycastIp}/32 scope global 2> /dev/null

            # Get a list of VxLAN devices and set them all protodown
            /bin/ip -d -o link show | grep " vxlan id " | /usr/bin/cut -d \  -f 2 | /usr/bin/tr -d : | while read vxlan
            do
                /bin/ip link set dev ${vxlan} protodown on
            done
        fi
    fi

    # Remove peerlink and enable learning on the peerlink interfaces
    tmpdir=$(/bin/mktemp -dt "$(basename ${0}).XXXXXXXXXX") && \
    /usr/bin/touch ${tmpdir}/brbatch && \
    /sbin/bridge -o -d link show | grep "peerlink on" | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /bin/echo "link set dev $intf peerlink off" >> ${tmpdir}/brbatch
    done && \
    /sbin/bridge -force -batch ${tmpdir}/brbatch &&
    /bin/rm -rf ${tmpdir} ||
    /sbin/bridge -o -d link show | grep "peerlink on" | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /sbin/bridge link set dev $intf peerlink off
    done

    # Remove duallink on any interface which have it set
    tmpdir=$(/bin/mktemp -dt "$(basename ${0}).XXXXXXXXXX") && \
    /usr/bin/touch ${tmpdir}/brbatch && \
    /sbin/bridge -o -d link show | grep "duallink on" | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /bin/echo "link set dev $intf duallink off" >> ${tmpdir}/brbatch
    done && \
    /sbin/bridge -force -batch ${tmpdir}/brbatch &&
    /bin/rm -rf ${tmpdir} ||
    /sbin/bridge -o -d link show | grep "duallink on" | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /sbin/bridge link set dev $intf duallink off
    done

    # Remove no_flush on any interface which have it set
    tmpdir=$(/bin/mktemp -dt "$(basename ${0}).XXXXXXXXXX") && \
    /usr/bin/touch ${tmpdir}/brbatch && \
    /sbin/bridge -o -d link show | grep "no_flush on" | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /bin/echo "link set dev $intf no_flush off" >> ${tmpdir}/brbatch
    done && \
    /sbin/bridge -force -batch ${tmpdir}/brbatch &&
    /bin/rm -rf ${tmpdir} ||
    /sbin/bridge -o -d link show | grep "no_flush on" | /usr/bin/cut -d \  -f 2 | while read intf
    do
        /sbin/bridge link set dev $intf no_flush off
    done

    # Change the local IP address of VxLAN devices back to the original value
    if [ ${doVxLans} -eq 1 ]
    then
        /bin/ip -d -o link show | grep " vxlan id " | /usr/bin/cut -d \  -f 2 | /usr/bin/tr -d : | while read vxlan
        do
            /bin/ip link set dev ${vxlan} type vxlan local ${localIp}
        done
        echo -n "0.0.0.0 0.0.0.0                " > /cumulus/switchd/config/vxlan/local_ip
    fi

    echo Cleanup is finished
}

do_cleanup
