hwTozteNew_outputWithGroup0212_threeLayerOutWithXDPIProtocols

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/1/22 11:22
# @Author  : hu.zhezhong
# @Site    : 
# @File    : hwTozteNew_outputWithGroup0128.py
# @Software: PyCharm Community Edition


import os
import  xlwt
import operator

userProfileList = []
L7InfoList = []
L7InfoDict = {}
L7InfoGrpList = []
L7InfoGrpDict = {}
L7RuleList = []
L7RuleDict = {}
RuleList = []
RuleDict = {}
CategoryGroupList = []
RuleBindList = []
RuleBindDict = {}
ProtocolGroupDict = {}
ProtocolGroupList = []
FilterGroupDict = {}
FilterDict = {}
FilterList = []

class Utils(object):
    cmdStr = ['user-profile', 'rule-binding', 'rule', 'l7-rule', 'l7-info-group', 'l7-info', 'filter', 'filter-group', 'protocol-group']
    @staticmethod
    def noNeedProcLine(line):
        #line = line.strip()
        charList = line.split()
        if len(charList) == 0:
            return True
        char = charList[0]
        for cmdChar in Utils.cmdStr:
            if cmp(char.lower(), cmdChar) == 0:
                return False
        return True

    @staticmethod
    def specialProtocol(protocol):
        if protocol != 'http' and protocol != 'ftp' and protocol != 'rtsp' and protocol != 'connection-wap1.x' and protocol != 'connectionless-wap1.x' and protocol != 'dns':
            return True
        return False


class UserProfile(object):
    def __init__(self):
        self._name_ = ''
    def infoPrint(self):
        print("UserProfile info name: %s" % (self._name_))

class Rule(object):
    def __init__(self):
        self._name_ = ''
        self._categoryGroup_ = ''
        self._l7Rule_ = ''
        self._priority_ = 0
        self._filterGroup_ = ''

    def hasL7Rule(self):
        if cmp(self._l7Rule_, '') == 0:
            return False
        else:
            return True

    def hasFilterGroup(self):
        if cmp(self._filterGroup_) == 0:
            return False
        else:
            return True

class RuleBind(object):
    def __init__(self):
        self._name_ =''
        self._priority_ = 0
        self._userProfile_ = ''
    def infoPrint(self):
        print("RuleBind info name: %s, priority: %s, userProfile: %s" % (self._name_, self._priority_, self._userProfile_))




class L7Rule(object):
    def __init__(self):
        self._name_ = ''
        self._protocol_ = ''
        self._protocolGroup_ = ''
        self._l7InfoGroup_ = ''
        self._priority_ = 0
        self._categoryGroup_ = ''

    def hasCategoryGroup(self):
        if cmp(self._categoryGroup_, '') == 0:
            return False
        else:
            return True

    def hasProtocol(self):
        if cmp(self._protocol_, '') == 0:
            return False
        else:
            return True

    def hasProtocolGroup(self):
        if cmp(self._protocolGroup_, '') == 0:
            return False
        else:
            return True


class L7InfoGroup(object):
    def __init__(self):
        self._name_ = ''
        self._l7info_ = ''


class L7Info(object):
    def __init__(self):
        self._name_ = ''
        self._url_ = ''
        self._l7CategoryGroup_ = ''


class FilterGroup(object):
    def __init__(self):
        self._name_ = ''
        self._filterName_ = ''


class Filter(object):
    def __init__(self):
        self._name_ = ''
        self._protocol_ = ''


class ProtocolGroup(object):
    def __init__(self):
        self._name_ = ''
        self._protocol_ = ''

    def hasProtocol(self):
        if cmp(self._protocol_, '') == 0:
            return False
        else:
            return True





class Line(object):
    def __init__(self,file):
        self._file_ = file


    def userProfileExtract(self, charList):
        global currentUserProfileName
        length = len(charList)
        index = 0
        newUserProfile = UserProfile()
        while index < length:
            if cmp(charList[index], 'user-profile') == 0:
                index += 1
                newUserProfile._name_ = charList[index]
                currentUserProfileName = charList[index]
            index += 1
        userProfileList.append(newUserProfile)
        # print userProfileList[0]._name_

    def ruleBindExtract(self, charList):
        length = len(charList)
        index = 0
        newRuleBind = RuleBind()
        newRuleBind._userProfile_ = currentUserProfileName   # 不懂
        while index < length:
            if cmp(charList[index], 'rule-binding') == 0:
                index = index + 1
                newRuleBind._name_ = charList[index]
            elif cmp(charList[index], 'priority') == 0:
                index = index + 1
                newRuleBind._priority_ = int(charList[index])
            index = index + 1
        # newRuleBind.infoPrint()
        RuleBindList.append(newRuleBind)
        # print RuleBindList[0]._name_

    def ruleExtract(self, charList):
        length = len(charList)
        index = 0
        newRule = Rule()
        while index < length:
            if cmp(charList[index], 'rule') == 0:
                index = index + 1
                newRule._name_ = charList[index]
            elif cmp(charList[index], 'filter-group') == 0:
                index = index + 1
                newRule._filterGroup_ = charList[index]
            elif cmp(charList[index], 'service-category-group') == 0:
                index = index + 1
                newRule._categoryGroup_ = charList[index]
            elif cmp(charList[index], 'extension-action') == 0:
                index = index + 1
                newRule._exAction_ = charList[index]
            elif cmp(charList[index], 'l7-rule') == 0:
                index = index + 1
                newRule._l7Rule_ = charList[index]
            elif cmp(charList[index], 'priority') == 0:
                index = index + 1
                newRule._priority_ = int(charList[index])
            index = index + 1
        # newRule.infoPrint()
        if not RuleDict.has_key(newRule._name_):
            RuleDict[newRule._name_] = newRule
        RuleList.append(newRule)

    def l7RuleExtract(self, charList):
        length = len(charList)
        index = 0
        newL7Rule = L7Rule()
        while index < length:
            if cmp(charList[index], 'l7-rule') == 0:
                index = index + 1
                newL7Rule._name_ = charList[index]
            elif cmp(charList[index], 'protocol') == 0:
                index = index + 1
                newL7Rule._protocol_ = charList[index]
            elif cmp(charList[index], 'l7-info-group') == 0:
                index = index + 1
                newL7Rule._l7InfoGroup_ = charList[index]
            elif cmp(charList[index], 'priority') == 0:
                index = index + 1
                newL7Rule._priority_ = int(charList[index])
            elif cmp(charList[index], 'signaling-category-group') == 0:
                index = index + 1
                newL7Rule._sigCateGrp_ = charList[index]
            elif cmp(charList[index], 'signaling-associate') == 0:
                index = index + 1
                newL7Rule._sigAssoc_ = charList[index]
            elif cmp(charList[index], 'non-specific') == 0:
                newL7Rule._nonSpecific_ = True
            elif cmp(charList[index], 'category-group') == 0:
                index = index + 1
                newL7Rule._categoryGroup_ = charList[index]
            elif cmp(charList[index], 'protocol-group') == 0:
                index = index + 1
                newL7Rule._protocolGroup_ = charList[index]
            index = index + 1
        #newL7Rule.infoPrint()
        if not L7RuleDict.has_key(newL7Rule._name_):
            L7RuleDict[newL7Rule._name_] = []
            L7RuleDict[newL7Rule._name_].append(newL7Rule)
        else:
            L7RuleDict[newL7Rule._name_].append(newL7Rule)
        L7RuleList.append(newL7Rule)

    def l7InfoGroupExtract(self, charList):
        length = len(charList)
        index = 0
        newL7InfoGrp = L7InfoGroup()
        while index < length:
            if cmp(charList[index], 'l7-info-group') == 0:
                index = index + 1
                newL7InfoGrp._name_ = charList[index]
            elif cmp(charList[index], 'l7-info') == 0:
                index = index + 1
                newL7InfoGrp._l7info_ = charList[index]
            elif cmp(charList[index], 'sequence') == 0:
                index = index + 1
                newL7InfoGrp._priority_ = int(charList[index])
            index = index + 1
        # newL7InfoGrp.infoPrint()
        if not L7InfoGrpDict.has_key(newL7InfoGrp._name_):
            L7InfoGrpDict[newL7InfoGrp._name_] = []
            L7InfoGrpDict[newL7InfoGrp._name_].append(newL7InfoGrp)
        else:
            L7InfoGrpDict[newL7InfoGrp._name_].append(newL7InfoGrp)
        L7InfoGrpList.append(newL7InfoGrp)

    def l7InfoExtract(self, charList):
        length = len(charList)
        index = 0
        newL7Info = L7Info()
        while index < length:
            if cmp(charList[index], 'l7-info') == 0:
                index = index + 1
                newL7Info._name_ = charList[index]
            elif cmp(charList[index], 'url') == 0 or cmp(charList[index], 'host') == 0:
                index = index + 1
                newL7Info._url_ = charList[index]
            elif cmp(charList[index], 'method') == 0:
                index = index + 1
                newL7Info._method_ = charList[index]
            elif cmp(charList[index], 'media-type') == 0:
                index = index + 1
                newL7Info._mediaType_ = charList[index]
            elif cmp(charList[index], 'l7-category-group') == 0:
                index = index + 1
                newL7Info._l7CategoryGroup_ = charList[index]
            index = index + 1
        if not L7InfoDict.has_key(newL7Info._name_):
            L7InfoDict[newL7Info._name_] = newL7Info
        # newL7Info.infoPrint()
        L7InfoList.append(newL7Info)

    def filterGroupExtract(self, charList):
        length = len(charList)
        index = 0
        newFilterGroup = FilterGroup()
        while index < length:
            if cmp(charList[index], 'filter-group') == 0:
                index += 1
                newFilterGroup._name_ = charList[index]
            elif cmp(charList[index], 'filter') == 0:
                index += 1
                if index >= length:                         #filter-group没有filter情况(filter-group fg_tethering tethering-detect filter)
                    return
                newFilterGroup._filter_ = charList[index]
            index +=1                                      #如果charlist[index]不等于'filter-group'和'filter',则index不更新,进入死循环
        if not FilterGroupDict.has_key(newFilterGroup._name_):
            FilterGroupDict[newFilterGroup._name_] = []
            FilterGroupDict[newFilterGroup._name_].append(newFilterGroup)
        else:
            FilterGroupDict[newFilterGroup._name_].append(newFilterGroup)


    def filterExtract(self, charList):
        length = len(charList)
        index = 0
        newFilter = Filter()
        while index < length:
            if cmp(charList[index], 'filter') == 0:
                index += 1
                newFilter._name_ = charList[index]
            elif cmp(charList[index], 'l34-protocol') == 0:
                index += 1
                newFilter._protocol_ = charList[index]
            index += 1
        if not FilterDict.has_key(newFilter._name_):
            FilterDict[newFilter._name_] = newFilter
        FilterList.append(newFilter)



    def protocolGroupExtract(self, charList):
        length = len(charList)
        index = 0
        newProtocolGroup = ProtocolGroup()
        while index < length:
            if cmp(charList[index], 'protocol-group') == 0:
                index += 1
                newProtocolGroup._name_ = charList[index]
            elif cmp(charList[index], 'protocol') == 0:
                index += 1
                newProtocolGroup._protocol_ = charList[index]
            index += 1
        if not ProtocolGroupDict.has_key(newProtocolGroup._name_):
            ProtocolGroupDict[newProtocolGroup._name_] = []
            ProtocolGroupDict[newProtocolGroup._name_].append(newProtocolGroup._protocol_)
        else:
            ProtocolGroupDict[newProtocolGroup._name_].append(newProtocolGroup._protocol_)
        ProtocolGroupList.append(ProtocolGroup)
        # print 1
        # print ProtocolGroupDict
        # print ProtocolGroupList


    def lineProc(self,line):
        charList = line.split()
        keyChar = charList[0]
        if cmp(keyChar, 'user-profile') == 0:
            self.userProfileExtract(charList)
        elif cmp(keyChar, 'rule-binding') == 0:
            self.ruleBindExtract(charList)
        elif cmp(keyChar, 'rule') == 0:
            self.ruleExtract(charList)
        elif cmp(keyChar, 'l7-rule') == 0:
            self.l7RuleExtract(charList)
        elif cmp(keyChar, 'l7-info-group') == 0:
            self.l7InfoGroupExtract(charList)
        elif cmp(keyChar, 'l7-info') == 0:
            self.l7InfoExtract(charList)
        elif cmp(keyChar, 'filter-group') == 0:
            self.filterGroupExtract(charList)
        elif cmp(keyChar, 'filter') == 0:
            self.filterExtract(charList)
        elif cmp(keyChar, 'protocol-group') == 0:
            self.protocolGroupExtract(charList)

    def proc(self):
        line = self._file_.readline()
        while line:
            line = line.strip()
            if Utils.noNeedProcLine(line):
                line = self._file_.readline()
                continue
            self.lineProc(line)
            line = self._file_.readline()

def writTOxls(input_file):

    wb = xlwt.Workbook()
    ws = wb.add_sheet('hwDPI', cell_overwrite_ok=True)
    title = ['user-profile', 'rule', 'category-group', 'priority', 'rulebaseid', 'filter-group', 'l7-info-group', '17-rule', 'protocol']
    for i in range(len(title)):
        ws.write(0, i, title[i])
    raw1 = 1
    raw2 = 1
    rulebaseid = 10000
    categorygroupdict = {}

    for rulebind in RuleBindList:
        rulename = rulebind._name_
        up = rulebind._userProfile_
        # print up
        rule = RuleDict[rulebind._name_]
        l7rule = []
        l7InfoGrp = []
        outList = []
        if L7RuleDict.has_key(rule._l7Rule_):
            l7rule = L7RuleDict[rule._l7Rule_]
        if rulebind._priority_ != 0:
            priority = rulebind._priority_
        elif rule._priority_ != 0:
            priority = rule._priority_
        elif l7rule._priority_ != 0:
            priority = l7rule._priority_


        for l7rul in l7rule:
            # 输出category-group在L7-rule层的
            if l7rul.hasProtocol():
                protocols = ([l7rul._protocol_] if Utils.specialProtocol(l7rul._protocol_) else '')
            elif l7rul.hasProtocolGroup():
                if ProtocolGroupDict.has_key(l7rul._protocolGroup_) and ProtocolGroupDict[l7rul._protocolGroup_] != ['']:
                    protocols = ProtocolGroupDict[l7rul._protocolGroup_]
                else:
                    protocols = [l7rul._protocolGroup_]
            if l7rul._categoryGroup_ != '':
                cateGrp = l7rul._categoryGroup_
                if not categorygroupdict.has_key(cateGrp):
                    rulebaseid += 1
                    categorygroupdict.update({cateGrp: rulebaseid})
                    newoutList = [up, rulename, cateGrp, priority, categorygroupdict[cateGrp], '', '', '', protocols]
                    if newoutList in outList:
                        continue
                    else:
                        ws.write(raw1, 0, up)
                        ws.write(raw1, 1, rulename)
                        ws.write(raw1, 2, cateGrp)
                        ws.write(raw1, 3, priority)
                        ws.write(raw1, 4, categorygroupdict[cateGrp])
                        ws.write(raw1, 7, l7rul._name_)
                        ws.write(raw1, 8, [(protocols[i] + (',' if i<len(protocols)-1 else '')) for i in range(len(protocols))])
                        raw1 += 1
                        outList.append(newoutList)
            #输出category-group在L7-info层的
            if L7InfoGrpDict.has_key(l7rul._l7InfoGroup_):
                l7InfoGrp = L7InfoGrpDict[l7rul._l7InfoGroup_]
                for l7infogroup in l7InfoGrp:
                    if L7InfoDict.has_key(l7infogroup._l7info_):
                        l7info = L7InfoDict[l7infogroup._l7info_]
                        cateGrp = l7info._l7CategoryGroup_
                        if not categorygroupdict.has_key(cateGrp):
                            rulebaseid += 1
                            categorygroupdict.update({cateGrp: rulebaseid})
                        # print up
                        newoutList = [up, rulename, cateGrp, priority, categorygroupdict[cateGrp], '', '', '', protocols]
                        # print outList
                        if newoutList in outList:
                            continue
                        else:
                            ws.write(raw1, 0, up)
                            ws.write(raw1, 1, rulename)
                            ws.write(raw1, 2, cateGrp)
                            ws.write(raw1, 3, priority)
                            ws.write(raw1, 4, categorygroupdict[cateGrp])
                            ws.write(raw1, 6, l7infogroup._name_)
                            ws.write(raw1, 8, [(protocols[i] + (',' if i < len(protocols) - 1 else '')) for i in range(len(protocols))])
                            raw1 += 1
                            outList.append(newoutList)

        #输出category-group在rule层的
        l34_protocolList = []
        l34_protocols = ''
        if FilterGroupDict.has_key(rule._filterGroup_):
            filterGroupList = FilterGroupDict[rule._filterGroup_]
            for filterGroup in filterGroupList:
                if FilterDict.has_key(filterGroup._filter_):
                    l34_protocols = FilterDict[filterGroup._filter_]._protocol_
                l34_protocolList.append(l34_protocols)


        if rule._categoryGroup_ != '':
            cateGrp = rule._categoryGroup_
        if not categorygroupdict.has_key(cateGrp):
            rulebaseid += 1
            categorygroupdict.update({cateGrp: rulebaseid})
        newoutList = [up, rulename, cateGrp, priority, categorygroupdict[cateGrp], '', '', '', l34_protocolList]
        if newoutList in outList:
            continue
        else:
            ws.write(raw1, 0, up)
            ws.write(raw1, 1, rulename)
            ws.write(raw1, 2, cateGrp)
            ws.write(raw1, 3, priority)
            ws.write(raw1, 4, categorygroupdict[cateGrp])
            ws.write(raw1, 5, rule._filterGroup_)
            # ws.write(raw1, 8, [(l34_protocolList[i] + (',' if i < len(l34_protocolList) - 1 else '')) for i in range(len(l34_protocolList))])
            raw1 += 1
            outList.append(newoutList)


    wb.save(os.getcwd() + r'\ml-hwDPI.xls')


def main():
    cfgFilePath = os.getcwd()
    files = os.listdir(cfgFilePath)
    findCfgFile = False
    for file in files:
        if cmp(os.path.splitext(file)[len(os.path.splitext(file))-1],'.cfg') == 0 or cmp(os.path.splitext(file)[len(os.path.splitext(file))-1],'.log') == 0:
            cfgfile = file
            findCfgFile = True
            break
    if not findCfgFile:
        print('no cfg file under this dir, please copy cfg file to this dir!')
        exit(1)

    input_file = os.path.join(os.getcwd(), cfgfile)
    line = Line(open (input_file,'r'))
    line.proc()
    writTOxls(input_file)
    exit(1)
if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/u014686859/article/details/87338504