UpdServicePortCmd.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import argparse
  2. from ciscoconfparse import CiscoConfParse
  3. def get_value(line, key, onu=False):
  4. """
  5. Obtain value from line
  6. :return: Returns value
  7. """
  8. data_compare = line.lower().split()
  9. try:
  10. index = data_compare.index(key)
  11. except ValueError:
  12. index = -1
  13. if index >= 0 and onu:
  14. return data_compare[index + 2].strip()
  15. elif index >= 0:
  16. return data_compare[index + 1].strip()
  17. else:
  18. return ""
  19. parser = argparse.ArgumentParser(description='Arguments')
  20. parser.add_argument('-cf', '--config_file', type=str, help='Config file', required=True)
  21. args = parser.parse_args()
  22. file_name = args.config_file
  23. conf_file_parse = CiscoConfParse(args.config_file)
  24. sql = []
  25. types = ['voip', 'data']
  26. bbs_config = conf_file_parse.find_objects("bbs-config")
  27. for lines in bbs_config:
  28. for line in lines.children:
  29. number = get_value(line.text, 'service-port')
  30. if number <> '':
  31. vlan = get_value(line.text, 'vlan')
  32. gpon = get_value(line.text, 'gpon')
  33. gpon_split = gpon.split('/')
  34. link = gpon_split[-1]
  35. ont = get_value(line.text, 'ont')
  36. gemport = get_value(line.text, 'gemport')
  37. type = 'voip'
  38. if gemport <> '' and (int(gemport) == 0 or int(gemport) == 1):
  39. type = types[int(gemport)]
  40. gpon_ = gpon_split[0] + '/' + gpon_split[1];
  41. parse = conf_file_parse.find_objects("gpon-%(gpon_)s" % locals())
  42. for child in parse:
  43. for line in child.children:
  44. if "ont add %(link)s %(ont)s " % locals() in line.text:
  45. sn_auth = get_value(line.text, 'sn-auth')
  46. sql.append(""" ((SELECT o.id FROM olt AS o JOIN onu ON o.id=onu.olt_id \
  47. WHERE onu.pon_serial_number=%(sn_auth)s),(SELECT id FROM onu \
  48. WHERE pon_serial_number=%(sn_auth)s),\
  49. %(number)s,%(gemport)s,%(vlan)s)\n""" % locals())
  50. if len(sql) > 0:
  51. print("""TRUNCATE service_port; INSERT INTO service_port (`olt_id`, `onu_id`, `number`, `gemport`, `vlan`)
  52. VALUES """ + ','.join(sql) + ";")
  53. else:
  54. print "NO SE PUDO PROCESAR EL ARCHIVO"