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