xref: /openbmc/linux/tools/net/ynl/cli.py (revision e2ad626f)
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    args = parser.parse_args()
31
32    if args.no_schema:
33        args.schema = ''
34
35    attrs = {}
36    if args.json_text:
37        attrs = json.loads(args.json_text)
38
39    ynl = YnlFamily(args.spec, args.schema)
40
41    if args.ntf:
42        ynl.ntf_subscribe(args.ntf)
43
44    if args.sleep:
45        time.sleep(args.sleep)
46
47    if args.do:
48        reply = ynl.do(args.do, attrs, args.flags)
49        pprint.PrettyPrinter().pprint(reply)
50    if args.dump:
51        reply = ynl.dump(args.dump, attrs)
52        pprint.PrettyPrinter().pprint(reply)
53
54    if args.ntf:
55        ynl.check_ntf()
56        pprint.PrettyPrinter().pprint(ynl.async_msg_queue)
57
58
59if __name__ == "__main__":
60    main()
61