12e74e404SFilip Tehlar# Script for checking LISP counters
22e74e404SFilip Tehlar
32e74e404SFilip Tehlarhelp_string = """
42e74e404SFilip Tehlar Params:
52e74e404SFilip Tehlar   vat_exec - VAT executable
62e74e404SFilip Tehlar   vpp_prefix - shared vpp memory prefix
72e74e404SFilip Tehlar   vat_path - VAT template file
82e74e404SFilip Tehlar   vni
92e74e404SFilip Tehlar   seid
102e74e404SFilip Tehlar   deid
112e74e404SFilip Tehlar   loc_rloc
122e74e404SFilip Tehlar   rmt_rloc
132e74e404SFilip Tehlar   pkt_count
142e74e404SFilip Tehlar   bytes
152e74e404SFilip Tehlar"""
162e74e404SFilip Tehlar
172e74e404SFilip Tehlarimport sys
182e74e404SFilip Tehlarimport subprocess
192e74e404SFilip Tehlarimport json
202e74e404SFilip Tehlar
212e74e404SFilip Tehlardef get_stat_entry(json, vni, seid, deid, loc_rloc, rmt_rloc):
222e74e404SFilip Tehlar  if len (json) == 0:
232e74e404SFilip Tehlar    return None
242e74e404SFilip Tehlar
252e74e404SFilip Tehlar  for obj in json:
262e74e404SFilip Tehlar    if obj['vni'] == int(vni) and\
272e74e404SFilip Tehlar            obj['seid'] == seid and\
282e74e404SFilip Tehlar            obj['deid'] == deid and\
292e74e404SFilip Tehlar            obj['lloc'] == loc_rloc and\
302e74e404SFilip Tehlar            obj['rloc'] == rmt_rloc:
312e74e404SFilip Tehlar      return obj
322e74e404SFilip Tehlar
332e74e404SFilip Tehlar  return None
342e74e404SFilip Tehlar
352e74e404SFilip Tehlar
362e74e404SFilip Tehlardef check_counters(vat_exec, vat_path, vpp_prefix, vni, seid, deid, loc_rloc,
372e74e404SFilip Tehlar        rmt_rloc, pkt_count, total_bytes):
382e74e404SFilip Tehlar  vat_file = vat_path + '/' + 'dump_stats.tpl'
392e74e404SFilip Tehlar  out = subprocess.Popen([vat_exec, "chroot", "prefix", vpp_prefix, "json", "script",
402e74e404SFilip Tehlar      "in", vat_file], stdout=subprocess.PIPE).communicate()[0]
412e74e404SFilip Tehlar
422e74e404SFilip Tehlar  o = json.loads(out)
432e74e404SFilip Tehlar  stat_entry = get_stat_entry(o, vni, seid, deid, loc_rloc, rmt_rloc)
442e74e404SFilip Tehlar
452e74e404SFilip Tehlar  if stat_entry is None:
462e74e404SFilip Tehlar    return False
472e74e404SFilip Tehlar
482e74e404SFilip Tehlar  if stat_entry['pkt_count'] != int(pkt_count):
492e74e404SFilip Tehlar    return False
502e74e404SFilip Tehlar  if stat_entry['bytes'] != int(total_bytes):
512e74e404SFilip Tehlar    return False
522e74e404SFilip Tehlar  return True
532e74e404SFilip Tehlar
542e74e404SFilip Tehlar
552e74e404SFilip Tehlarif __name__ == "__main__":
562e74e404SFilip Tehlar  if len(sys.argv) < 10:
572e74e404SFilip Tehlar    raise Exception('expected 10 parameters: ' + help_string)
582e74e404SFilip Tehlar
592e74e404SFilip Tehlar  if check_counters(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],
602e74e404SFilip Tehlar          sys.argv[5],
612e74e404SFilip Tehlar          sys.argv[6],
622e74e404SFilip Tehlar          sys.argv[7],
632e74e404SFilip Tehlar          sys.argv[8],
642e74e404SFilip Tehlar          sys.argv[9],
652e74e404SFilip Tehlar          sys.argv[10]):
662e74e404SFilip Tehlar    sys.exit(0)
672e74e404SFilip Tehlar  else:
682e74e404SFilip Tehlar    sys.exit(1)
69