'Clean up junk which might be left over from old runs;
do fast stuff before slow dp and link removal!'

@classmethod
def cleanup(cls):

    info('***    Removing    excess    controllers/ofprotocols/ofdatapaths/pings/noxes\n')
    zombies = 'controller    ofprotocol    ofdatapath    ping    nox_corelt-nox_core    ovs-openflowd    ovs-controllerovs-testcontroller    udpbwtest    mnexec    ivs    ryu-manager'
    sh((('killall    ' + zombies) + '    2>    /dev/null'))
    time.sleep(1)
    sh((('killall    -9    ' + zombies) + '    2>    /dev/null'))
    sh('pkill    -9    -f    "sudo    mnexec"')
    info('***    Removing    junk    from    /tmp\n')
    sh('rm    -f    /tmp/vconn*    /tmp/vlogs*    /tmp/*.out    /tmp/*.log')
    info('***    Removing    old    X11    tunnels\n')
    cleanUpScreens()
    info('***    Removing    excess    kernel    datapaths\n')
    dps = sh("ps    ax    |    egrep    -o    'dp[0-9]+'    |    sed    's/dp/nl:/'").splitlines()
    for dp in dps:
        if dp:
            sh(('dpctl    deldp    ' + dp))
    info('***        Removing    OVS    datapaths\n')
    dps = sh('ovs-vsctl    --timeout=1    list-br').strip().splitlines()
    if dps:
        sh(('ovs-vsctl    ' + '    --    '.join((('--if-exists    del-br    ' + dp) for dp in dps if dp))))
    dps = sh('ovs-vsctl    --timeout=1    list-br').strip().splitlines()
    for dp in dps:
        sh(('ovs-vsctl    del-br    ' + dp))
    info('***    Removing    all    links    of    the    pattern    foo-ethX\n')
    links = sh("ip    link    show    |    egrep    -o    '([-_.[:alnum:]]+-eth[[:digit:]]+)'").splitlines()
    n = 1000
    for i in range(0, len(links), n):
        cmd = ';'.join((('ip    link    del    %s' % link) for link in links[i:(i + n)]))
        sh(('(    %s    )    2>    /dev/null' % cmd))
    if ('tap9' in sh('ip    link    show')):
        info("***    Removing    tap9    -    assuming    it's    from    cluster    edition\n")
        sh('ip    link    del    tap9')
    info('***    Killing    stale    mininet    node    processes\n')
    killprocs('mininet:')
    info('***    Shutting    down    stale    tunnels\n')
    killprocs('Tunnel=Ethernet')
    killprocs('.ssh/mn')
    sh('rm    -f    ~/.ssh/mn/*')
    for callback in cls.callbacks:
        callback()
    info('***    Cleanup    complete.\n')
