Merge pull request #131 from stevenvanrossem/master
[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 Steven Van Rossem <steven.vanrossem@intec.ugent.be>
5 """
6
7 import argparse
8 import pprint
9 from tabulate import tabulate
10 import zerorpc
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 add(self, args):
32 vnf_src_name = self._parse_vnf_name(args.get("source"))
33 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
34
35 params = self._create_dict(
36 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
37 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
38 weight=args.get("weight"),
39 match=args.get("match"),
40 bidirectional=args.get("bidirectional"),
41 cookie=args.get("cookie"))
42
43 # note zerorpc does not support named arguments
44 r = self.c.network_action_start(
45 #args.get("datacenter"),
46 vnf_src_name,
47 vnf_dst_name,
48 params)
49 pp.pprint(r)
50
51 def remove(self, args):
52 vnf_src_name = self._parse_vnf_name(args.get("source"))
53 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
54
55 params = self._create_dict(
56 vnf_src_interface=self._parse_vnf_interface(args.get("source")),
57 vnf_dst_interface=self._parse_vnf_interface(args.get("destination")),
58 weight=args.get("weight"),
59 match=args.get("match"),
60 bidirectional=args.get("bidirectional"),
61 cookie=args.get("cookie"))
62
63 r = self.c.network_action_stop(
64 #args.get("datacenter"),
65 vnf_src_name,
66 vnf_dst_name,
67 params)
68 pp.pprint(r)
69
70 def _parse_vnf_name(self, vnf_name_str):
71 vnf_name = vnf_name_str.split(':')[0]
72 return vnf_name
73
74 def _parse_vnf_interface(self, vnf_name_str):
75 try:
76 vnf_interface = vnf_name_str.split(':')[1]
77 except:
78 vnf_interface = None
79
80 return vnf_interface
81
82 def _create_dict(self, **kwargs):
83 return kwargs
84
85 parser = argparse.ArgumentParser(description='son-emu network')
86 parser.add_argument(
87 "command",
88 choices=['add', 'remove'],
89 help="Action to be executed.")
90 parser.add_argument(
91 "--datacenter", "-d", dest="datacenter",
92 help="Data center to in which the network action should be initiated")
93 parser.add_argument(
94 "--source", "-src", dest="source",
95 help="vnf name of the source of the chain")
96 parser.add_argument(
97 "--destination", "-dst", dest="destination",
98 help="vnf name of the destination of the chain")
99 parser.add_argument(
100 "--weight", "-w", dest="weight",
101 help="weight metric to calculate the path")
102 parser.add_argument(
103 "--match", "-m", dest="match",
104 help="string holding extra matches for the flow entries")
105 parser.add_argument(
106 "--bidirectional", "-b", dest="bidirectional",
107 action='store_true',
108 help="add/remove the flow entries from src to dst and back")
109 parser.add_argument(
110 "--cookie", "-c", dest="cookie",
111 help="cookie for this flow, as easy to use identifier (eg. per tenant/service)")
112
113 def main(argv):
114 args = vars(parser.parse_args(argv))
115 c = ZeroRpcClient()
116 c.execute_command(args)