Cleanup requirements & tox config, update pylint
[nfvbench.git] / client / nfvbench_client.py
1 #!/usr/bin/env python
2 # Copyright 2017 Cisco Systems, Inc.  All rights reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15 #
16
17 #
18 # This is an example of python application controling a nfvbench server
19 # using the nfvbench client API.
20 # The nfvbench server must run in background using the --server option.
21 # Since HTML pages are not required, the path to pass to --server can be any directory on the host.
22 #
23 import argparse
24 import json
25 import sys
26
27 from client import NfvbenchClient
28
29
30 #
31 # At the CLI, the user can either:
32 # - pass an nfvbench configuration as a string (-c <config>)
33 # - pass an nfvbench configuration as a file name containing the
34 #   configuration (-f <config_file_path>)
35 # - or pass a test config (-e <config>) that will be echoed back by the server as is
36 #
37 def main():
38     parser = argparse.ArgumentParser()
39
40     parser.add_argument('-f', '--file', dest='file',
41                         action='store',
42                         help='NFVbench config file to execute (json format)',
43                         metavar='<config_file_path>')
44     parser.add_argument('-c', '--config', dest='config',
45                         action='store',
46                         help='NFVbench config to execute (json format)',
47                         metavar='<config>')
48     parser.add_argument('-e', '--echo', dest='echo',
49                         action='store',
50                         help='NFVbench config to echo (json format)',
51                         metavar='<config>')
52     parser.add_argument('-t', '--timeout', dest='timeout',
53                         default=900,
54                         action='store',
55                         help='time (seconds) to wait for NFVbench result',
56                         metavar='<config>')
57     parser.add_argument('url', help='nfvbench server url (e.g. http://10.0.0.1:5000)')
58     opts = parser.parse_args()
59
60     if not opts.file and not opts.config and not opts.echo:
61         print('at least one of -f or -c or -e required')
62         sys.exit(-1)
63
64     nfvbench = NfvbenchClient(opts.url)
65     # convert JSON into a dict
66     try:
67         timeout = int(opts.timeout)
68         if opts.file:
69             with open(opts.file) as fd:
70                 config = json.loads(fd.read())
71                 result = nfvbench.run_config(config, timeout=timeout)
72         elif opts.config:
73             config = json.loads(opts.config)
74             result = nfvbench.run_config(config, timeout=timeout)
75         elif opts.echo:
76             config = json.loads(opts.echo)
77             result = nfvbench.echo_config(config, timeout=timeout)
78         print('Result:', result)
79     except ValueError as ex:
80         print('Input configuration is invalid: ' + str(ex))
81         print()
82
83
84 if __name__ == "__main__":
85     main()