#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import string
from nokia.symptomreport.plugins.lib.PluginLibrary import PluginLibrary


def current_node_is_management_node():
    return os.getenv("NODE_BASE_TYPE") == "MANAGEMENT"

class ShowAllPlugin(PluginLibrary):
    def execute_command_and_get_output(self, cmd):
        output = "===command: %s ====================\n"%cmd
        self.write_to_file(output)
        output = os.popen(cmd).read()
        self.write_to_file(output)
        return output
    
    def getListofAPsFromOutput(self):
        apidList = []
        summaryOutput = os.popen('fsclish -c "show flexizone access-point summary"').read()
        for line in summaryOutput.splitlines():
            apLine = string.split(line)
            if len(apLine) > 2:
                if apLine[0].isdigit():
                    apidList.append([apLine[0],apLine[2]])
        return apidList

    def createShowApCellInfoCmds(self):
        commandList = []
        showAPRouteCmds = []
        for apId in self.getListofAPsFromOutput():
            commandList.append('fsclish -c "show flexizone access-point ' + apId[0] + ' cell"')
            showAPRouteCmds.append('echo "show-route ' + apId[1] + '"|fpdebug')
        commandList.extend(showAPRouteCmds)
        return commandList

    def getListofPeersFromOutput(self):
        peerList = set()
        tunnelOutput = os.popen('trutil -c "gtpu show tunnel"').read()
        for line in tunnelOutput.splitlines():
            tunnelLine = string.split(line)
            if len(tunnelLine) > 0:
                osaid = tunnelLine[0]
                try:
                    int(osaid, 16)
                except ValueError:
                    continue
                if len(tunnelLine) > 6:
                    peerList.add(tunnelLine[6])
        return peerList

    def createShowRouteCmds(self):
        showRouteCommandList = []
        for peerId in self.getListofPeersFromOutput():
            showRouteCommandList.append('echo "show-route ' + peerId + '" |fpdebug')
        return showRouteCommandList


    def run(self):
        self.start()
        self.write_to_file("===Start to collect show related information.====================\n")
        if current_node_is_management_node():
            fCPUCommandList = [
            'fsclish -c "show ipsec vpn-remote-access"', 
            'fsclish -c "show security cert default cmp"',
            'fsclish -c "show security cert default ca-cert all"',
            'fsclish -c "show security cert default ee-cert"',
            'fsclish -c "show security cert default cert-chain ee-cert"',
            'fsclish -c "show ipsec rule all"',
            'fsclish -c "show ipsec vpn all"',
            'fsclish -c "show networking vlan"',
            'fsclish -c "show networking interface"',
            'fsclish -c "show networking address"',
            'fsclish -c "show networking address iface sbzplane"',
            'fsclish -c "show networking address iface nbcplane"',
            'fsclish -c "show networking address iface nbmplane"',
            'fsclish -c "show networking aclrule owner /FCPU-0"',
            'fsclish -c "show networking aclrule owner /FZBU-0"',
            'fsclish -c "show networking interface owner /FCPU-0"',
            'fsclish -c "show networking interface owner /FZBU-0"',
            'fsclish -c "show networking vlan owner /FCPU-0"',
            'fsclish -c "show networking vlan owner /FZBU-0"',
            'fsclish -c "show networking address owner /FCPU-0"',
            'fsclish -c "show networking address owner /FZBU-0"',
            'fsclish -c "show networking aclrule owner /FCPU-0"',
            'fsclish -c "show networking aclrule owner /FZBU-0"',
            'fsclish -c "show has state managed-object /*/*/*"',
            'fsclish -c "show alarm active-summary"',
            'fsclish -c "show ipsec template ikev1 all"',
            'fsclish -c "show ipsec template ikev2 all"',
            'fsclish -c "show routing node FCPU-0 route"',
            'fsclish -c "show routing node FZBU-0 route"',
            'fsclish -c "show routing instance default node FCPU-0 static-route config"',
            'fsclish -c "show routing instance default node FZBU-0 static-route config"',
            'fsclish -c "show fzc-ipsec nb bypass cmp"',
            'fsclish -c "show fzc-ipsec nb bypass cplane"',
            'fsclish -c "show fzc-ipsec nb bypass mplane"',
            'fsclish -c "show fzc-ipsec nb protect cplane"',
            'fsclish -c "show fzc-ipsec nb protect mplane"',
            'fsclish -c "show fzc-ipsec nb protect uplane"',
            'fsclish -c "show fzc-ipsec sb bypass allzone"',
            'fsclish -c "show fzc-ipsec sb protect allzone"',
            'fsclish -c "show flexizone access-point summary"',
            'fsclish -c "show flexizone cell-mapping"',
            'fsclish -c "show flexizone access-point cmp-status"',
            'fsclish -c "show flexizone access-point port"',
            'fsclish -c "show flexizone access-point service-account credentials"',
            'fsclish -c "show flexizone access-point summary"',
            'fsclish -c "show flexizone cell"',
            'fsclish -c "show flexizone cell-mapping"',
            'fsclish -c "show flexizone dhcp"',
            'fsclish -c "show flexizone mgmt"',
            'fsclish -c "show flexizone port debug"',
            'fsclish -c "show flexizone port ssh timers"',
            'fsclish -c "show flexizone shared-backhaul enterprise"',
            'fsclish -c "show flexizone shared-backhaul state"',
            'fsclish -c "show flexizone summary"',
            'fsclish -c "show flexizone z1link-statistics summary"',
            'fsclish -c "show networking aclrule"',
            'getinitarg',
            'ifconfig -a',
            'echo "dump-ip-stats"|fpdebug',
            'echo "dump-spd all"|fpdebug',
            'echo "dump-sad all"|fpdebug',
            'ip addr',
            'cat /opt/nokia/etc/dpdk_conf/cfzc_dpdk.yaml',
            "ip x s | perl -lne \'s/((enc|auth-trunc) .*)0x\S+/$1--truncated--/; print\'",
            "ip -s -s -s x s | perl -lne 's/((enc|auth-trunc) .*)0x\S+/$1--truncated--/; print'",
            "setkey -D | perl -lne 's/([EA]:\s+\S+\s+)(\S.*)$/$1 --truncated--/; print'",
            "echo dump-sad all | fpdebug| perl -lne 's/((key auth|key ecn):)(.*)$/$1 --truncated--/; print'",
            "echo ipsec4-sad all | fpdebug| perl -lne 's/((key auth|key ecn):)(.*)$/$1 --truncated--/; print'",
            "ip a s",
            "ip r s",
            "ip x p",
            "ip -s -s -s x p",
            "ip -s link",
            "ssh fzbu-0 'curl http://169.254.169.254/latest/user-data'",
            "ssh fcpu-0 'curl http://169.254.169.254/latest/user-data'",
            "ssh fzbu-0 'curl http://169.254.169.254/latest/meta-data'",
            "ssh fcpu-0 'curl http://169.254.169.254/latest/meta-data'"
            ]

            for command in fCPUCommandList:
                self.execute_command_and_get_output(command)
            cellCommandList = self.createShowApCellInfoCmds()
            for command in cellCommandList:
                self.execute_command_and_get_output(command)
            
        else:
            fZBUCommandList = ['route -n',
            'ip a',
            'ifconfig',
            'echo "dump-ip-stats"|fpdebug',
            'echo "dump-stats"|fpdebug',
            'echo "dump-spd all"|fpdebug',
            'echo "dump-sad all"|fpdebug',
            'cat /etc/ipsec.conf',
            'cat /etc/strongswan.conf',
            'getinitarg',
            'ifconfig -a',
            'ip addr',
            'trutil -c "gtpu show tunnel"',
            'trutil -c "statistics show summary"',
            'trutil -c "gtpu show ip"',
            'trutil -c "gtpu show supervision"',
            'trutil -c "gtpu show sup-list"',
            "ip x s | perl -lne \'s/((enc|auth-trunc) .*)0x\S+/$1--truncated--/; print\'",
            "ip -s -s -s x s | perl -lne 's/((enc|auth-trunc) .*)0x\S+/$1--truncated--/; print'",
            "setkey -D | perl -lne 's/([EA]:\s+\S+\s+)(\S.*)$/$1 --truncated--/; print'",
            "echo dump-sad all | fpdebug| perl -lne 's/((key auth|key ecn):)(.*)$/$1 --truncated--/; print'",
            "echo ipsec4-sad all | fpdebug| perl -lne 's/((key auth|key ecn):)(.*)$/$1 --truncated--/; print'",
            "ip a s",
            "ip r s",
            "ip x p",
            "ip -s -s -s x p",
            "ip -s link"
            ]
            for command in fZBUCommandList:
                self.execute_command_and_get_output(command)
            showPeerCommandList = self.createShowRouteCmds()
            self.write_to_file("===Collecting show-route commands.==================================\n")
            
            for command in showPeerCommandList:
                self.write_to_file("===Running " + command + "============================================\n")
                self.execute_command_and_get_output(command)
        self.write_to_file(        "===Collecting show related information finished.====================\n")
        self.stop()

def main():
    plugin = ShowAllPlugin()
    plugin.run()

if __name__ == "__main__":
    main()
