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