c27da59f2909b2df13e869b37be50f83f954bff9
[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_src_interface = self._parse_vnf_interface(args.get("source"))
33 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
34 vnf_dst_interface = self._parse_vnf_interface(args.get("destination"))
35 r = self.c.network_action_start(
36 #args.get("datacenter"),
37 vnf_src_name,
38 vnf_dst_name,
39 vnf_src_interface,
40 vnf_dst_interface)
41 pp.pprint(r)
42
43 def remove(self, args):
44 vnf_src_name = self._parse_vnf_name(args.get("source"))
45 vnf_src_interface = self._parse_vnf_interface(args.get("source"))
46 vnf_dst_name = self._parse_vnf_name(args.get("destination"))
47 vnf_dst_interface = self._parse_vnf_interface(args.get("destination"))
48 r = self.c.network_action_stop(
49 #args.get("datacenter"),
50 vnf_src_name,
51 vnf_dst_name,
52 vnf_src_interface,
53 vnf_dst_interface)
54 pp.pprint(r)
55
56 def _parse_vnf_name(self, vnf_name_str):
57 vnf_name = vnf_name_str.split(':')[0]
58 return vnf_name
59
60 def _parse_vnf_interface(self, vnf_name_str):
61 try:
62 vnf_interface = vnf_name_str.split(':')[1]
63 except:
64 vnf_interface = None
65
66 return vnf_interface
67
68
69 parser = argparse.ArgumentParser(description='son-emu network')
70 parser.add_argument(
71 "command",
72 help="Action to be executed: add|remove")
73 parser.add_argument(
74 "--datacenter", "-d", dest="datacenter",
75 help="Data center to in which the network action should be initiated")
76 parser.add_argument(
77 "--source", "-src", dest="source",
78 help="vnf name of the source of the chain")
79 parser.add_argument(
80 "--destination", "-dst", dest="destination",
81 help="vnf name of the destination of the chain")
82
83 def main(argv):
84 args = vars(parser.parse_args(argv))
85 c = ZeroRpcClient()
86 c.execute_command(args)