516a7525b64fbfaa73d32bd40b49df283940489b
[osm/vim-emu.git] / src / emuvim / cli / network.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
11
12 pp = pprint.PrettyPrinter(indent=4)
13
14 class ZeroRpcClient(object):
15
16 def __init__(self):
17 self.c = zerorpc.Client()
18 # TODO connect to DCNetwork API
19 #self.c.connect("tcp://127.0.0.1:4242") # TODO hard coded for now. we'll change this later
20 self.c.connect("tcp://127.0.0.1:5151")
21 self.cmds = {}
22
23 def execute_command(self, args):
24 if getattr(self, args["command"]) is not None:
25 # call the local method with the same name as the command arg
26 getattr(self, args["command"])(args)
27 else:
28 print "Command not implemented."
29
30 def add(self, args):
31 vnf_src_name = self._parse_vnf_name(args.get("source"))
32 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
33
34 params = self._create_dict(
35 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
36 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
37 weight=args.get("weight"),
38 match=args.get("match"),
39 bidirectional=args.get("bidirectional"),
40 cookie=args.get("cookie"))
41
42 # note zerorpc does not support named arguments
43 r = self.c.network_action_start(
44 #args.get("datacenter"),
45 vnf_src_name,
46 vnf_dst_name,
47 params)
48 pp.pprint(r)
49
50 def remove(self, args):
51 vnf_src_name = self._parse_vnf_name(args.get("source"))
52 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
53
54 params = self._create_dict(
55 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
56 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
57 weight=args.get("weight"),
58 match=args.get("match"),
59 bidirectional=args.get("bidirectional"),
60 cookie=args.get("cookie"))
61
62 r = self.c.network_action_stop(
63 #args.get("datacenter"),
64 vnf_src_name,
65 vnf_dst_name,
66 params)
67 pp.pprint(r)
68
69 def _parse_vnf_name(self, vnf_name_str):
70 vnf_name = vnf_name_str.split(':')[0]
71 return vnf_name
72
73 def _parse_vnf_interface(self, vnf_name_str):
74 try:
75 vnf_interface = vnf_name_str.split(':')[1]
76 except:
77 vnf_interface = None
78
79 return vnf_interface
80
81 def _create_dict(self, **kwargs):
82 return kwargs
83
84 parser = argparse.ArgumentParser(description='son-emu network')
85 parser.add_argument(
86 "command",
87 choices=['add', 'remove'],
88 help="Action to be executed.")
89 parser.add_argument(
90 "--datacenter", "-d", dest="datacenter",
91 help="Data center to in which the network action should be initiated")
92 parser.add_argument(
93 "--source", "-src", dest="source",
94 help="vnf name of the source of the chain")
95 parser.add_argument(
96 "--destination", "-dst", dest="destination",
97 help="vnf name of the destination of the chain")
98 parser.add_argument(
99 "--weight", "-w", dest="weight",
100 help="weight metric to calculate the path")
101 parser.add_argument(
102 "--match", "-m", dest="match",
103 help="string holding extra matches for the flow entries")
104 parser.add_argument(
105 "--bidirectional", "-b", dest="bidirectional",
106 action='store_true',
107 help="add/remove the flow entries from src to dst and back")
108 parser.add_argument(
109 "--cookie", "-c", dest="cookie",
110 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
111
112 def main(argv):
113 args = vars(parser.parse_args(argv))
114 c = ZeroRpcClient()
115 c.execute_command(args)