updated SDN chaining commands
[osm/vim-emu.git] / src / emuvim / cli / monitor.py
1 """
2 son-emu network CLI
3 (c) 2016 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5
6 import argparse
7 import pprint
8 from tabulate import tabulate
9 import zerorpc
10 import time
11
12
13 pp = pprint.PrettyPrinter(indent=4)
14
15 class ZeroRpcClient(object):
16
17 def __init__(self):
18 self.c = zerorpc.Client()
19 # TODO connect to DCNetwork API
20 #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
21 self.c.connect("tcp://127.0.0.1:5151")
22 self.cmds = {}
23
24 def execute_command(self, args):
25 if getattr(self, args["command"]) is not None:
26 # call the local method with the same name as the command arg
27 getattr(self, args["command"])(args)
28 else:
29 print "Command not implemented."
30
31 def get_rate(self, args):
32 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
33 vnf_interface = self._parse_vnf_interface(args.get("vnf_name"))
34 self.c.monitor_setup_rate_measurement(
35 vnf_name,
36 vnf_interface,
37 args.get("direction"),
38 args.get("metric"))
39 while True:
40 r = self.c.monitor_get_rate(
41 vnf_name,
42 vnf_interface,
43 args.get("direction"),
44 args.get("metric"))
45 pp.pprint(r)
46 time.sleep(1)
47
48 def _parse_vnf_name(self, vnf_name_str):
49 vnf_name = vnf_name_str.split(':')[0]
50 return vnf_name
51
52 def _parse_vnf_interface(self, vnf_name_str):
53 try:
54 vnf_interface = vnf_name_str.split(':')[1]
55 except:
56 vnf_interface = None
57
58 return vnf_interface
59
60 parser = argparse.ArgumentParser(description='son-emu network')
61 parser.add_argument(
62 "command",
63 help="Action to be executed: get_rate")
64 parser.add_argument(
65 "--vnf_name", "-vnf", dest="vnf_name",
66 help="vnf name to be monitored")
67 parser.add_argument(
68 "--direction", "-d", dest="direction",
69 help="rx (ingress rate) or tx (egress rate)")
70 parser.add_argument(
71 "--metric", "-m", dest="metric",
72 help="bytes (byte rate), packets (packet rate)")
73
74 def main(argv):
75 print "This is the son-emu monitor CLI."
76 print "Arguments: %s" % str(argv)
77 args = vars(parser.parse_args(argv))
78 c = ZeroRpcClient()
79 c.execute_command(args)