1aae5e3f4SFilip Tehlar# Script for checking whether a mapping exists in the vpp's map-cache
2aae5e3f4SFilip Tehlar#
3aae5e3f4SFilip Tehlar# Params:
4aae5e3f4SFilip Tehlar#   vat_exec - VAT executable
5aae5e3f4SFilip Tehlar#   vpp_prefix - shared vpp memory prefix
6aae5e3f4SFilip Tehlar#   mapping - mapping to verify
7aae5e3f4SFilip Tehlar
8aae5e3f4SFilip Tehlarimport sys
9aae5e3f4SFilip Tehlarimport subprocess
10aae5e3f4SFilip Tehlarimport json
11aae5e3f4SFilip Tehlar
12aae5e3f4SFilip Tehlardef has_mapping(json, mapping):
13aae5e3f4SFilip Tehlar  if len (json) == 0:
14aae5e3f4SFilip Tehlar    return False
15aae5e3f4SFilip Tehlar
16aae5e3f4SFilip Tehlar  for obj in json:
17aae5e3f4SFilip Tehlar    if obj['eid'] == mapping:
18aae5e3f4SFilip Tehlar      return True;
19aae5e3f4SFilip Tehlar
20aae5e3f4SFilip Tehlar  return False
21aae5e3f4SFilip Tehlar
22aae5e3f4SFilip Tehlardef verify_mapping(vat_exec, prefix, mapping, vat_path):
23aae5e3f4SFilip Tehlar  vat_file = vat_path + '/' + 'dump_remote_mappings.tpl'
24aae5e3f4SFilip Tehlar  out = subprocess.Popen([vat_exec, "chroot", "prefix", prefix, "json", "script",
25aae5e3f4SFilip Tehlar      "in", vat_file], stdout=subprocess.PIPE).communicate()[0]
26aae5e3f4SFilip Tehlar
27aae5e3f4SFilip Tehlar  o = json.loads(out)
28aae5e3f4SFilip Tehlar  return has_mapping(o, mapping)
29aae5e3f4SFilip Tehlar
30aae5e3f4SFilip Tehlarif __name__ == "__main__":
31aae5e3f4SFilip Tehlar  if len(sys.argv) < 4:
322e74e404SFilip Tehlar    raise Exception('expected 4 parameters: VAT executable, shared prefix '
33aae5e3f4SFilip Tehlar        + ' name, mapping expected, path to vat templates!')
34aae5e3f4SFilip Tehlar
35aae5e3f4SFilip Tehlar  if verify_mapping(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]):
36aae5e3f4SFilip Tehlar    sys.exit(0)
37aae5e3f4SFilip Tehlar  else:
38aae5e3f4SFilip Tehlar    sys.exit(1)
39