123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import argparse
- from ciscoconfparse import CiscoConfParse
- def get_value(line, key, onu=False):
- """
- Obtain value from line
- :return: Returns value
- """
- data_compare = line.lower().split()
-
- try:
- index = data_compare.index(key)
- except ValueError:
- index = -1
-
- if index >= 0 and onu:
- return data_compare[index + 2].strip()
- elif index >= 0:
- return data_compare[index + 1].strip()
- else:
- return ""
-
- parser = argparse.ArgumentParser(description='Arguments')
- parser.add_argument('-cf', '--config_file', type=str, help='Config file', required=True)
- args = parser.parse_args()
- file_name = args.config_file
- conf_file_parse = CiscoConfParse(args.config_file)
- sql = []
- types = ['voip', 'data']
- bbs_config = conf_file_parse.find_objects("bbs-config")
- for lines in bbs_config:
- for line in lines.children:
- number = get_value(line.text, 'service-port')
- if number <> '':
- vlan = get_value(line.text, 'vlan')
- gpon = get_value(line.text, 'gpon')
- gpon_split = gpon.split('/')
- link = gpon_split[-1]
- ont = get_value(line.text, 'ont')
- gemport = get_value(line.text, 'gemport')
- type = 'voip'
- if gemport <> '' and (int(gemport) == 0 or int(gemport) == 1):
- type = types[int(gemport)]
- gpon_ = gpon_split[0] + '/' + gpon_split[1];
- parse = conf_file_parse.find_objects("gpon-%(gpon_)s" % locals())
- for child in parse:
- for line in child.children:
- if "ont add %(link)s %(ont)s " % locals() in line.text:
- sn_auth = get_value(line.text, 'sn-auth')
- sql.append(""" ((SELECT o.id FROM olt AS o JOIN onu ON o.id=onu.olt_id \
- WHERE onu.pon_serial_number=%(sn_auth)s),(SELECT id FROM onu \
- WHERE pon_serial_number=%(sn_auth)s),\
- %(number)s,%(gemport)s,%(vlan)s,\'%(type)s\')\n""" % locals())
- sql = """REPLACE INTO service_port (`olt_id`, `onu_id`, `number`, `gemport`, `vlan`, `type`)
- VALUES """ + ','.join(sql) + ";"
- print sql
|