xref: /linux/tools/net/ynl/cli.py (revision e7d759f31ca295d589f7420719c311870bb3166f)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3
4import argparse
5import json
6import pprint
7import time
8
9from lib import YnlFamily, Netlink
10
11
12def main():
13    parser = argparse.ArgumentParser(description='YNL CLI sample')
14    parser.add_argument('--spec', dest='spec', type=str, required=True)
15    parser.add_argument('--schema', dest='schema', type=str)
16    parser.add_argument('--no-schema', action='store_true')
17    parser.add_argument('--json', dest='json_text', type=str)
18    parser.add_argument('--do', dest='do', type=str)
19    parser.add_argument('--dump', dest='dump', type=str)
20    parser.add_argument('--sleep', dest='sleep', type=int)
21    parser.add_argument('--subscribe', dest='ntf', type=str)
22    parser.add_argument('--replace', dest='flags', action='append_const',
23                        const=Netlink.NLM_F_REPLACE)
24    parser.add_argument('--excl', dest='flags', action='append_const',
25                        const=Netlink.NLM_F_EXCL)
26    parser.add_argument('--create', dest='flags', action='append_const',
27                        const=Netlink.NLM_F_CREATE)
28    parser.add_argument('--append', dest='flags', action='append_const',
29                        const=Netlink.NLM_F_APPEND)
30    parser.add_argument('--process-unknown', action=argparse.BooleanOptionalAction)
31    args = parser.parse_args()
32
33    if args.no_schema:
34        args.schema = ''
35
36    attrs = {}
37    if args.json_text:
38        attrs = json.loads(args.json_text)
39
40    ynl = YnlFamily(args.spec, args.schema, args.process_unknown)
41
42    if args.ntf:
43        ynl.ntf_subscribe(args.ntf)
44
45    if args.sleep:
46        time.sleep(args.sleep)
47
48    if args.do:
49        reply = ynl.do(args.do, attrs, args.flags)
50        pprint.PrettyPrinter().pprint(reply)
51    if args.dump:
52        reply = ynl.dump(args.dump, attrs)
53        pprint.PrettyPrinter().pprint(reply)
54
55    if args.ntf:
56        ynl.check_ntf()
57        pprint.PrettyPrinter().pprint(ynl.async_msg_queue)
58
59
60if __name__ == "__main__":
61    main()
62