summaryrefslogtreecommitdiffstats
path: root/tests/data_plane/vpp_lite_topo/scripts/generate_config.py
blob: 87204a6aa182126e529f2b042f597aa21c93c0b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python

"""
generate_config.py - Generate specific configuration file for VPP from
                     generic config file

Usage:
  ./generate_config.py <directory> <output-file-type>

where <directory> is a system directory containing generic config file(s)
    (suffixed with *.config)
    <output-file-type> is one of 'vat' or 'cli'

This script looks for *.config files in provided directory and for each
generates a specific configuration file based on output file type in form
'<filename>.cli' or '<filename>.vat' respectively.
"""

import sys
import glob
import cmd_mappings


def generate_config(file_name, mode):
  """
  param file_name:
  param mode: one of 'vat' or 'cli'
  """
  s = ''
  f = open(file_name, 'r')
  line_num = 0

  for line in f:
    line_num += 1
    line = line.strip()
    if line == '' or line[0] == '#':
      continue

    kw = line[: line.index(' ')]
    args = line[ line.index(' ') + 1:]

    if kw not in cmd_mappings.mappings:
      raise Exception('Conversion error at {}:{}:\n > {}\nKeyword not found:'
              ' {}'.format(file_name, line_num, line, kw))

    mapping = cmd_mappings.mappings[kw]
    try:
      s = s + mapping.generate(mode, args) + '\n'
    except Exception as e:
      raise Exception('Conversion error at {}:{}:\n > {}'
              .format(file_name, line_num, line))

  return s


def main():
  if len(sys.argv) != 3:
    print('Error: expected 2 arguments!')
    sys.exit(1)

  dir_name = sys.argv[1]
  config_type = sys.argv[2]

  if config_type != 'vat' and config_type != 'cli':
    print('Error: expected second parameter one of "vat" or "cli"!')
    sys.exit(1)

  for f in glob.glob(dir_name + "/*.config"):
    config = generate_config(f, config_type)

    output_fname = f.replace('.config', '.' + config_type)
    print('\n* Generated config from {}:'.format(f))
    print(config)
    print ('* Saving to {}'.format(output_fname))

    fout = open(output_fname, 'w')
    fout.write(config);


main()